added error handling for upsert customer

This commit is contained in:
John Yeo
2025-01-29 15:38:24 +00:00
parent 058643588f
commit 6172eea5cb
2 changed files with 48 additions and 39 deletions

View File

@@ -1,3 +1,4 @@
# Run current file
# npx tsx scripts/alex.ts
npx tsx scripts/alex_test.ts
filename=$1
npx tsx $filename

View File

@@ -221,51 +221,59 @@ cusRouter.get("/:customer_id/events", async (req: any, res: any) => {
});
cusRouter.put("", async (req: any, res: any) => {
const { id, name, email, fingerprint, next_reset_at } = req.body;
try {
const { id, name, email, fingerprint, next_reset_at } = req.body;
if (!id && !email) {
throw new RecaseError({
message: "Customer ID or email is required",
code: ErrCode.InvalidCustomer,
statusCode: StatusCodes.BAD_REQUEST,
});
}
if (!id && !email) {
throw new RecaseError({
message: "Customer ID or email is required",
code: ErrCode.InvalidCustomer,
statusCode: StatusCodes.BAD_REQUEST,
});
}
let existing = await CusService.getByIdOrEmail({
sb: req.sb,
id,
email,
orgId: req.orgId,
env: req.env,
});
let newCustomer: Customer;
if (existing) {
newCustomer = await CusService.update({
sb: req.sb,
internalCusId: existing.internal_id,
update: { id, name, email, fingerprint },
});
} else {
newCustomer = await createNewCustomer({
let existing = await CusService.getByIdOrEmail({
sb: req.sb,
id,
email,
orgId: req.orgId,
env: req.env,
customer: {
id,
name,
email,
fingerprint,
},
nextResetAt: next_reset_at,
});
}
res.status(200).json({
customer: CustomerResponseSchema.parse(newCustomer),
success: true,
action: existing ? "update" : "create",
});
console.log("existing", existing);
console.log("Request header:", req.headers.authorization);
console.log("Org ID:", req.orgId);
let newCustomer: Customer;
if (existing) {
newCustomer = await CusService.update({
sb: req.sb,
internalCusId: existing.internal_id,
update: { id, name, email, fingerprint },
});
} else {
newCustomer = await createNewCustomer({
sb: req.sb,
orgId: req.orgId,
env: req.env,
customer: {
id,
name,
email,
fingerprint,
},
nextResetAt: next_reset_at,
});
}
res.status(200).json({
customer: CustomerResponseSchema.parse(newCustomer),
success: true,
action: existing ? "update" : "create",
});
} catch (error) {
handleRequestError({ error, res, action: "update customer" });
}
});
cusRouter.post("/:customer_id/balances", async (req: any, res: any) => {