stashing code
This commit is contained in:
59
apps/frontend/src/lib/domains/paymentinfo/data/repository.ts
Normal file
59
apps/frontend/src/lib/domains/paymentinfo/data/repository.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { eq, type Database } from "@pkg/db";
|
||||
import {
|
||||
paymentDetailsModel,
|
||||
type PaymentDetails,
|
||||
type PaymentDetailsPayload,
|
||||
} from "./entities";
|
||||
import type { Result } from "@pkg/result";
|
||||
import { paymentDetails } from "@pkg/db/schema";
|
||||
import { Logger } from "@pkg/logger";
|
||||
|
||||
export class PaymentInfoRepository {
|
||||
db: Database;
|
||||
constructor(db: Database) {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
async createPaymentInfo(
|
||||
data: PaymentDetailsPayload,
|
||||
): Promise<Result<number>> {
|
||||
const out = await this.db
|
||||
.insert(paymentDetails)
|
||||
.values({
|
||||
cardNumber: data.cardDetails.cardNumber,
|
||||
cardholderName: data.cardDetails.cardholderName,
|
||||
expiry: data.cardDetails.expiry,
|
||||
cvv: data.cardDetails.cvv,
|
||||
flightTicketInfoId: data.flightTicketInfoId,
|
||||
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.returning({ id: paymentDetails.id })
|
||||
.execute();
|
||||
return { data: out[0]?.id };
|
||||
}
|
||||
|
||||
async getPaymentInfo(id: number): Promise<Result<PaymentDetails>> {
|
||||
Logger.info(`Getting payment info with id ${id}`);
|
||||
const out = await this.db.query.paymentDetails.findFirst({
|
||||
where: eq(paymentDetails.id, id),
|
||||
});
|
||||
const parsed = paymentDetailsModel.safeParse(out);
|
||||
if (parsed.error) {
|
||||
Logger.error(parsed.error);
|
||||
return {};
|
||||
}
|
||||
return { data: parsed.data };
|
||||
}
|
||||
|
||||
async deletePaymentInfo(id: number): Promise<Result<boolean>> {
|
||||
Logger.info(`Deleting payment info with id ${id}`);
|
||||
const out = await this.db
|
||||
.delete(paymentDetails)
|
||||
.where(eq(paymentDetails.id, id))
|
||||
.execute();
|
||||
Logger.debug(out);
|
||||
return { data: true };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user