Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor and add new types in connection with payments scenario #456

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/api-legacy/models/reservation-request.ts
Original file line number Diff line number Diff line change
@@ -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;
};
6 changes: 6 additions & 0 deletions packages/api-legacy/models/reservation-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum ReservationStatus {
Pending = "pending",
Active = "active",
Cancelled = "cancelled" ,
Archived = "archived"
};
11 changes: 5 additions & 6 deletions packages/api-legacy/models/reservation.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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: "pending" | "active" | "cancelled" | "archived",
): Promise<Reservation | null> {
export async function updateReservationStatus(id: string, status: ReservationStatus): Promise<Reservation | null> {
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}`);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/api-legacy/patch-reservation-by-id/index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
await initializeDatabaseConfiguration();
Expand All @@ -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: {
Expand Down
5 changes: 3 additions & 2 deletions packages/api-legacy/post-checkout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
await initializeDatabaseConfiguration();
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion packages/api-legacy/post-payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
await initializeDatabaseConfiguration();
Expand Down Expand Up @@ -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 = {
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/functions/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpResponseInit> {
Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/functions/payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpResponseInit> {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/functions/reservations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpResponseInit> {
Expand Down Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/models/listing.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface Listing {
address: string;
fees: string;
createdAt: string;
}
};

const ListingSchema = new Schema<Listing>({
id: {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/models/listing.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/models/payment.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Payment {
amount: number;
currency: string;
createdAt: Date;
}
};

const PaymentSchema = new Schema<Payment>({
userId: {
Expand Down
12 changes: 6 additions & 6 deletions packages/api/src/models/reservation-request.ts
Original file line number Diff line number Diff line change
@@ -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;
};
6 changes: 6 additions & 0 deletions packages/api/src/models/reservation-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum ReservationStatus {
Pending = "pending",
Active = "active",
Cancelled = "cancelled" ,
Archived = "archived"
};
4 changes: 2 additions & 2 deletions packages/api/src/models/reservation.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Reservation>({
userId: {
Expand Down
7 changes: 4 additions & 3 deletions packages/api/src/models/reservation.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import mongoose from "mongoose";
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: "pending" | "active" | "cancelled" | "archived",
status: ReservationStatus,
): Promise<Reservation | null> {
const reservation = await ReservationModel.findById(id);

Expand All @@ -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}`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/models/user.schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ObjectId } from "mongodb";
import { ObjectId } from "mongoose";
import { model, Schema } from "mongoose";

export interface User {
Expand All @@ -19,7 +19,7 @@ export interface User {
lastLogin: number;
};
createdAt: Date;
}
};

const UserSchema = new Schema<User>({
id: {
Expand Down