-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment.js
48 lines (43 loc) · 1.78 KB
/
payment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { getDB } from "$lib/db";
import { formatDate, formatTime, toDateTime } from "$lib/helper";
export class Payment {
constructor({ payment_id = null, booking_id, datetime = null, amount, method, payment_status }) {
this.id = payment_id;
this.booking_id = booking_id;
this.amount = amount;
this.datetime = datetime;
this.method = method;
this.payment_status = payment_status;
}
async save() {
let date = new Date();
this.datetime = toDateTime(formatDate(date), formatTime(date));
let db = await getDB();
let result = await db.execute(
`INSERT INTO payment (booking_id, datetime, amount, method, payment_status) VALUES (?, ?, ?, ?, ?)`,
[this.booking_id, this.datetime, this.amount, this.method, this.payment_status]
);
this.id = result.lastInsertId;
return result.rowsAffected === 1;
}
static async create({ booking_id, amount, method, payment_status }) {
let payment = new Payment({ booking_id, amount, method, payment_status });
let success = await payment.save();
return success ? payment : null;
}
}
export async function getPaymentMethods() {
let db = await getDB();
let result = await db.select(`SHOW COLUMNS FROM payment WHERE Field = 'method'`);
return result[0].Type.match(/'([^']+)'/g).map((v) => v.slice(1, -1));
}
/**
* Get payment by booking_id
* @param {number} booking_id - Payment ID
* @returns {Promise<Payment|null>} - Payment object or null if not found
*/
export async function getPaymentByBookingId(booking_id) {
let db = await getDB();
let result = await db.select(`SELECT * FROM payment WHERE booking_id = ?`, [booking_id]);
return result.length === 1 ? new Payment(result[0]) : null;
}