Files
domain-wall/apps/frontend/src/lib/domains/paymentinfo/data/repository.ts

72 lines
1.8 KiB
TypeScript

import { eq, type Database } from "@pkg/db";
import { paymentInfo } from "@pkg/db/schema";
import { Logger } from "@pkg/logger";
import type { Result } from "@pkg/result";
import {
paymentInfoModel,
type PaymentInfo,
type PaymentInfoPayload,
} from "./entities";
export class PaymentInfoRepository {
db: Database;
constructor(db: Database) {
this.db = db;
}
async createPaymentInfo(data: PaymentInfoPayload): Promise<Result<number>> {
const out = await this.db
.insert(paymentInfo)
.values({
cardNumber: data.cardDetails.cardNumber,
cardholderName: data.cardDetails.cardholderName,
expiry: data.cardDetails.expiry,
cvv: data.cardDetails.cvv,
productId: data.productId,
orderId: data.orderId,
createdAt: new Date(),
updatedAt: new Date(),
})
.returning({ id: paymentInfo.id })
.execute();
return { data: out[0]?.id };
}
async getPaymentInfo(id: number): Promise<Result<PaymentInfo>> {
Logger.info(`Getting payment info with id ${id}`);
const out = await this.db.query.paymentInfo.findFirst({
where: eq(paymentInfo.id, id),
});
const parsed = paymentInfoModel.safeParse(out);
if (parsed.error) {
Logger.error(parsed.error);
return {};
}
return { data: parsed.data };
}
async updatePaymentInfoOrderId(
id: number,
oid: number,
): Promise<Result<number>> {
Logger.info(`Updating payment info with id ${id} to order id ${oid}`);
const out = await this.db
.update(paymentInfo)
.set({ orderId: oid })
.where(eq(paymentInfo.id, id))
.execute();
Logger.debug(out);
return { data: id };
}
async deletePaymentInfo(id: number): Promise<Result<boolean>> {
Logger.info(`Deleting payment info with id ${id}`);
const out = await this.db
.delete(paymentInfo)
.where(eq(paymentInfo.id, id))
.execute();
Logger.debug(out);
return { data: true };
}
}