diff --git a/packages/api-legacy/models/reservation-request.ts b/packages/api-legacy/models/reservation-request.ts index e52f55d8..84db0af9 100644 --- a/packages/api-legacy/models/reservation-request.ts +++ b/packages/api-legacy/models/reservation-request.ts @@ -1,7 +1,7 @@ export interface ReservationRequest { - userId: string; - listingId: string; - from: string; - to: string; - guests: number; -} + userId: string; + listingId: string; + from: string; + to: string; + guests: number; +}; \ No newline at end of file diff --git a/packages/api-legacy/models/reservation-status.ts b/packages/api-legacy/models/reservation-status.ts new file mode 100644 index 00000000..b83f78f5 --- /dev/null +++ b/packages/api-legacy/models/reservation-status.ts @@ -0,0 +1,6 @@ +export enum ReservationStatus { + Pending = "pending", + Active = "active", + Cancelled = "cancelled" , + Archived = "archived" +}; \ No newline at end of file diff --git a/packages/api-legacy/models/reservation.ts b/packages/api-legacy/models/reservation.ts index cd8809a9..7daff4de 100644 --- a/packages/api-legacy/models/reservation.ts +++ b/packages/api-legacy/models/reservation.ts @@ -1,18 +1,17 @@ import ReservationModel, { Reservation } from "./reservation.schema"; +import { ReservationStatus } from "./reservation-status"; export async function saveReservation(reservation: Partial): Promise { return ReservationModel.create(reservation); } -export async function updateReservationStatus( - id: string, - status: "pending" | "active" | "cancelled" | "archived", -): Promise { +export async function updateReservationStatus(id: string, status: ReservationStatus): Promise { const record = await ReservationModel.findOne({ _id: id }); if (record) { if ( - (status === "active" && record.status !== "pending") || - (status === "cancelled" && record.status !== "pending") + (status === ReservationStatus.Active && record.status !== ReservationStatus.Pending) || + (status === ReservationStatus.Cancelled && record.status !== ReservationStatus.Pending) + ) { throw new Error(`Invalid reservation status transition: ${record.status} -> ${status}`); } diff --git a/packages/api-legacy/patch-reservation-by-id/index.ts b/packages/api-legacy/patch-reservation-by-id/index.ts index 67252aac..1248bbde 100644 --- a/packages/api-legacy/patch-reservation-by-id/index.ts +++ b/packages/api-legacy/patch-reservation-by-id/index.ts @@ -1,6 +1,7 @@ import { AzureFunction, Context, HttpRequest } from "@azure/functions"; import { initializeDatabaseConfiguration } from "../config"; import { updateReservationStatus } from "../models/reservation"; +import { ReservationStatus } from "../models/reservation-status"; const patchReservationById: AzureFunction = async function (context: Context, req: HttpRequest): Promise { await initializeDatabaseConfiguration(); @@ -16,7 +17,7 @@ const patchReservationById: AzureFunction = async function (context: Context, re }, }; return; - } else if (status !== "active" && status !== "cancelled") { + } else if (status !== ReservationStatus.Active && status !== ReservationStatus.Cancelled) { context.res = { status: 400, body: { diff --git a/packages/api-legacy/post-checkout/index.ts b/packages/api-legacy/post-checkout/index.ts index 9eb74999..9b2bb07e 100644 --- a/packages/api-legacy/post-checkout/index.ts +++ b/packages/api-legacy/post-checkout/index.ts @@ -2,12 +2,13 @@ import { AzureFunction, Context, HttpRequest } from "@azure/functions"; import { getConfig, initializeDatabaseConfiguration } from "../config"; import { getListingById } from "../models/listing"; import { Listing } from "../models/listing.schema"; +import { ReservationRequest } from "../models/reservation-request"; +import { ReservationStatus } from "../models/reservation-status"; import { findReservationsByListingIdAndDateRange, saveReservation, updateReservationStatus, } from "../models/reservation"; -import { ReservationRequest } from "../models/reservation-request"; const postCheckout: AzureFunction = async function (context: Context, req: HttpRequest): Promise { await initializeDatabaseConfiguration(); @@ -157,7 +158,7 @@ const postCheckout: AzureFunction = async function (context: Context, req: HttpR } catch (error: unknown) { const err = error as Error; context.log.error(`Error creating stripe checkout session: ${err.message}`); - await updateReservationStatus(reservationRecord.id, "cancelled"); + await updateReservationStatus(reservationRecord.id, ReservationStatus.Cancelled); throw error; } } catch (error: unknown) { diff --git a/packages/api-legacy/post-payment/index.ts b/packages/api-legacy/post-payment/index.ts index 39a83140..ca40dd91 100644 --- a/packages/api-legacy/post-payment/index.ts +++ b/packages/api-legacy/post-payment/index.ts @@ -3,6 +3,7 @@ import { initializeDatabaseConfiguration } from "../config"; import { findUserById } from "../models/user"; import { savePayment } from "../models/payment"; import { updateReservationStatus } from "../models/reservation"; +import { ReservationStatus } from "../models/reservation-status"; const postPayment: AzureFunction = async function (context: Context, req: HttpRequest): Promise { await initializeDatabaseConfiguration(); @@ -40,7 +41,7 @@ const postPayment: AzureFunction = async function (context: Context, req: HttpRe return; } - const reservationRecord = await updateReservationStatus(payment.reservationId, "active"); + const reservationRecord = await updateReservationStatus(payment.reservationId, ReservationStatus.Active); if (!reservationRecord) { context.log.error(`Error payment received for unknown reservation id: ${payment.reservationId}`); context.res = { diff --git a/packages/api/src/functions/checkout.ts b/packages/api/src/functions/checkout.ts index b7d96145..6f889b86 100644 --- a/packages/api/src/functions/checkout.ts +++ b/packages/api/src/functions/checkout.ts @@ -8,6 +8,7 @@ import { updateReservationStatus, } from "../models/reservation"; import { Listing } from "../models/listing.schema"; +import { ReservationStatus } from "../models/reservation-status"; // POST: Checkout export async function postCheckout(request: HttpRequest, context: InvocationContext): Promise { @@ -114,7 +115,7 @@ export async function postCheckout(request: HttpRequest, context: InvocationCont guests, from, to, - status: "pending" as const, + status: ReservationStatus.Pending as const, createdAt: now, }; @@ -157,7 +158,7 @@ export async function postCheckout(request: HttpRequest, context: InvocationCont } catch (error: unknown) { const err = error as Error; context.error(`Error creating checkout session: ${err.message}`); - await updateReservationStatus(reservationRecord.id, "cancelled"); + await updateReservationStatus(reservationRecord.id, ReservationStatus.Cancelled); throw error; } } catch (error: unknown) { diff --git a/packages/api/src/functions/payments.ts b/packages/api/src/functions/payments.ts index bf2950c4..218e3d1c 100644 --- a/packages/api/src/functions/payments.ts +++ b/packages/api/src/functions/payments.ts @@ -4,6 +4,7 @@ import { findPaymentById, findPaymentsByUserId, savePayment, isValidPayment } fr import { findUserById } from "../models/user"; import { updateReservationStatus } from "../models/reservation"; import { Payment } from "../models/payment.schema"; +import { ReservationStatus } from "../models/reservation-status"; // GET: Get Payment by Id export async function getPaymentById(request: HttpRequest, context: InvocationContext): Promise { @@ -127,7 +128,7 @@ export async function postPayment(request: HttpRequest, context: InvocationConte }; } - const reservationRecord = await updateReservationStatus(payment.reservationId, "active"); + const reservationRecord = await updateReservationStatus(payment.reservationId, ReservationStatus.Active); if (!reservationRecord) { context.error(`Error payment received for unknown reservation id: ${payment.reservationId}`); return { diff --git a/packages/api/src/functions/reservations.ts b/packages/api/src/functions/reservations.ts index c99498ae..0d03285a 100644 --- a/packages/api/src/functions/reservations.ts +++ b/packages/api/src/functions/reservations.ts @@ -2,6 +2,7 @@ import { HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functio import { initializeDatabaseConfiguration } from "../config"; import { findReservationById, findReservationsByUserId, updateReservationStatus } from "../models/reservation"; import { Reservation } from "../models/reservation.schema"; +import { ReservationStatus } from "../models/reservation-status"; // GET: Get Reservation by Id export async function getReservationById(request: HttpRequest, context: InvocationContext): Promise { @@ -115,7 +116,7 @@ export async function patchReservationById( error: "Reservation status is missing", }, }; - } else if (status !== "active" && status !== "cancelled") { + } else if (status !== ReservationStatus.Active && status !== ReservationStatus.Cancelled) { return { status: 400, jsonBody: { diff --git a/packages/api/src/models/listing.schema.ts b/packages/api/src/models/listing.schema.ts index ea5848db..5e1ad9c0 100644 --- a/packages/api/src/models/listing.schema.ts +++ b/packages/api/src/models/listing.schema.ts @@ -20,7 +20,7 @@ export interface Listing { address: string; fees: string; createdAt: string; -} +}; const ListingSchema = new Schema({ id: { diff --git a/packages/api/src/models/listing.ts b/packages/api/src/models/listing.ts index ead5ab88..f80b80aa 100644 --- a/packages/api/src/models/listing.ts +++ b/packages/api/src/models/listing.ts @@ -1,5 +1,5 @@ import { pgQuery } from "../config/pgclient"; -import { Listing } from "./listing.schema"; +import { Listing } from "../models/listing.schema"; export async function getListings({ offset, diff --git a/packages/api/src/models/payment.schema.ts b/packages/api/src/models/payment.schema.ts index d8e9ab10..a8050369 100644 --- a/packages/api/src/models/payment.schema.ts +++ b/packages/api/src/models/payment.schema.ts @@ -9,7 +9,7 @@ export interface Payment { amount: number; currency: string; createdAt: Date; -} +}; const PaymentSchema = new Schema({ userId: { diff --git a/packages/api/src/models/reservation-request.ts b/packages/api/src/models/reservation-request.ts index e52f55d8..cc1ed907 100644 --- a/packages/api/src/models/reservation-request.ts +++ b/packages/api/src/models/reservation-request.ts @@ -1,7 +1,7 @@ export interface ReservationRequest { - userId: string; - listingId: string; - from: string; - to: string; - guests: number; -} + userId: string; + listingId: string; + from: string; + to: string; + guests: number; +}; diff --git a/packages/api/src/models/reservation-status.ts b/packages/api/src/models/reservation-status.ts new file mode 100644 index 00000000..b83f78f5 --- /dev/null +++ b/packages/api/src/models/reservation-status.ts @@ -0,0 +1,6 @@ +export enum ReservationStatus { + Pending = "pending", + Active = "active", + Cancelled = "cancelled" , + Archived = "archived" +}; \ No newline at end of file diff --git a/packages/api/src/models/reservation.schema.ts b/packages/api/src/models/reservation.schema.ts index 1a2c8388..f547e80b 100644 --- a/packages/api/src/models/reservation.schema.ts +++ b/packages/api/src/models/reservation.schema.ts @@ -8,9 +8,9 @@ export interface Reservation { guests: number; from: Date; to: Date; - status: "pending" | "active" | "cancelled" | "archived"; + status: ReservationStatus; createdAt: Date; -} +}; const ReservationSchema = new Schema({ userId: { diff --git a/packages/api/src/models/reservation.ts b/packages/api/src/models/reservation.ts index 46f31dc4..359c359c 100644 --- a/packages/api/src/models/reservation.ts +++ b/packages/api/src/models/reservation.ts @@ -1,5 +1,6 @@ import mongoose from "mongoose"; import ReservationModel, { Reservation } from "./reservation.schema"; +import { ReservationStatus } from "./reservation-status"; export async function saveReservation(reservation: Partial): Promise { return ReservationModel.create(reservation); @@ -7,7 +8,7 @@ export async function saveReservation(reservation: Partial): Promis export async function updateReservationStatus( id: string, - status: "pending" | "active" | "cancelled" | "archived", + status: ReservationStatus, ): Promise { const reservation = await ReservationModel.findById(id); @@ -16,8 +17,8 @@ export async function updateReservationStatus( } if ( - (status === "active" && reservation.status !== "pending") || - (status === "cancelled" && reservation.status !== "pending") + (status === ReservationStatus.Active && reservation.status !== ReservationStatus.Pending) || + (status === ReservationStatus.Cancelled && reservation.status !== ReservationStatus.Pending) ) { throw new Error(`Invalid reservation status transition: ${reservation.status} -> ${status}`); } diff --git a/packages/api/src/models/user.schema.ts b/packages/api/src/models/user.schema.ts index b5dd1f22..e740d797 100644 --- a/packages/api/src/models/user.schema.ts +++ b/packages/api/src/models/user.schema.ts @@ -1,4 +1,4 @@ -import { ObjectId } from "mongodb"; +import { ObjectId } from "mongoose"; import { model, Schema } from "mongoose"; export interface User { @@ -19,7 +19,7 @@ export interface User { lastLogin: number; }; createdAt: Date; -} +}; const UserSchema = new Schema({ id: {