refactor: more legacy code replaced more on the model and a bit on the admin side
This commit is contained in:
@@ -14,7 +14,6 @@ export const customerInfoModel = z.object({
|
||||
zipCode: z.string().min(1).max(21),
|
||||
address: z.string().min(1),
|
||||
address2: z.string().optional().nullable(),
|
||||
orderId: z.number().optional().nullable(),
|
||||
createdAt: z.coerce.string().optional(),
|
||||
updatedAt: z.coerce.string().optional(),
|
||||
});
|
||||
|
||||
@@ -3,10 +3,10 @@ import { customerInfo } from "@pkg/db/schema";
|
||||
import { getError, Logger } from "@pkg/logger";
|
||||
import { ERROR_CODES, type Result } from "@pkg/result";
|
||||
import {
|
||||
customerInfoModel,
|
||||
type CreateCustomerInfoPayload,
|
||||
type CustomerInfoModel,
|
||||
type UpdateCustomerInfoPayload,
|
||||
customerInfoModel,
|
||||
type CreateCustomerInfoPayload,
|
||||
type CustomerInfoModel,
|
||||
type UpdateCustomerInfoPayload,
|
||||
} from "./data";
|
||||
|
||||
export class CustomerInfoRepository {
|
||||
@@ -97,41 +97,6 @@ export class CustomerInfoRepository {
|
||||
}
|
||||
}
|
||||
|
||||
async getCustomerInfoByOrderId(
|
||||
orderId: number,
|
||||
): Promise<Result<CustomerInfoModel[]>> {
|
||||
try {
|
||||
const results = await this.db.query.customerInfo.findMany({
|
||||
where: eq(customerInfo.orderId, orderId),
|
||||
});
|
||||
|
||||
const out = [] as CustomerInfoModel[];
|
||||
for (const result of results) {
|
||||
const parsed = customerInfoModel.safeParse(result);
|
||||
if (!parsed.success) {
|
||||
Logger.error("Failed to parse customer info", result);
|
||||
continue;
|
||||
}
|
||||
out.push(parsed.data);
|
||||
}
|
||||
return { data: out };
|
||||
} catch (e) {
|
||||
return {
|
||||
error: getError(
|
||||
{
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "Failed to fetch customer information",
|
||||
detail:
|
||||
"An error occurred while retrieving customer information for the order",
|
||||
userHint: "Please try again",
|
||||
actionable: false,
|
||||
},
|
||||
e,
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async createCustomerInfo(
|
||||
payload: CreateCustomerInfoPayload,
|
||||
): Promise<Result<number>> {
|
||||
@@ -151,7 +116,6 @@ export class CustomerInfoRepository {
|
||||
zipCode: payload.zipCode,
|
||||
address: payload.address,
|
||||
address2: payload.address2 || null,
|
||||
orderId: payload.orderId || null,
|
||||
})
|
||||
.returning({ id: customerInfo.id })
|
||||
.execute();
|
||||
@@ -231,7 +195,6 @@ export class CustomerInfoRepository {
|
||||
if (payload.address !== undefined) updateValues.address = payload.address;
|
||||
if (payload.address2 !== undefined)
|
||||
updateValues.address2 = payload.address2;
|
||||
if (payload.orderId !== undefined) updateValues.orderId = payload.orderId;
|
||||
updateValues.updatedAt = new Date();
|
||||
|
||||
await this.db
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { db } from "@pkg/db";
|
||||
import type {
|
||||
CreateCustomerInfoPayload,
|
||||
UpdateCustomerInfoPayload,
|
||||
CreateCustomerInfoPayload,
|
||||
UpdateCustomerInfoPayload,
|
||||
} from "./data";
|
||||
import { CustomerInfoRepository } from "./repository";
|
||||
|
||||
@@ -20,10 +20,6 @@ export class CustomerInfoUseCases {
|
||||
return this.repo.getCustomerInfoById(id);
|
||||
}
|
||||
|
||||
async getCustomerInfoByOrderId(orderId: number) {
|
||||
return this.repo.getCustomerInfoByOrderId(orderId);
|
||||
}
|
||||
|
||||
async createCustomerInfo(payload: CreateCustomerInfoPayload) {
|
||||
return this.repo.createCustomerInfo(payload);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user