This repository was archived by the owner on Mar 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 555
/
Copy pathreservation.ts
44 lines (37 loc) · 1.53 KB
/
reservation.ts
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
import ReservationModel, { Reservation } from "./reservation.schema";
import { ReservationStatus } from "./reservation-status";
export async function saveReservation(reservation: Partial<Reservation>): Promise<Reservation> {
return ReservationModel.create(reservation);
}
export async function updateReservationStatus(id: string, status: ReservationStatus): Promise<Reservation | null> {
const record = await ReservationModel.findOne({ _id: id });
if (record) {
if (
(status === ReservationStatus.Active && record.status !== ReservationStatus.Pending) ||
(status === ReservationStatus.Cancelled && record.status !== ReservationStatus.Pending)
) {
throw new Error(`Invalid reservation status transition: ${record.status} -> ${status}`);
}
record.status = status;
return await record.save();
}
return null;
}
export async function findReservationById(id: string): Promise<Reservation | null> {
return await ReservationModel.findOne({ _id: id });
}
export async function findReservationsByUserId(userId: string, offset: number, limit: number): Promise<Reservation[]> {
return await ReservationModel.find({ userId }).skip(offset).limit(limit).sort({ _id: -1 });
}
export async function findReservationsByListingIdAndDateRange(
listingId: string,
from: string,
to: string,
): Promise<Reservation[]> {
return await ReservationModel.find({
listingId,
status: { $in: ["pending", "active"] },
from: { $lt: new Date(to).toISOString() },
to: { $gt: new Date(from).toISOString() },
});
}