From ac58fe47e942e949410aa9265bd898d40985b429 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 10 Apr 2024 12:23:59 +0700 Subject: [PATCH] feat: auto generate customer code --- src/controllers/customer-controller.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/controllers/customer-controller.ts b/src/controllers/customer-controller.ts index 51eb4e4..c2a8b52 100644 --- a/src/controllers/customer-controller.ts +++ b/src/controllers/customer-controller.ts @@ -96,10 +96,17 @@ export class CustomerController extends Controller { @Post() async create(@Request() req: RequestWithUser, @Body() body: CustomerCreate) { + const last = await prisma.customer.findFirst({ + orderBy: { createdAt: "desc" }, + where: { customerType: body.customerType }, + }); + + const code = `${body.customerType}${(+(last?.code.slice(-6) || 0) + 1).toString().padStart(6, "0")}`; + const record = await prisma.customer.create({ data: { ...body, - code: "CUSTOMER001", + code, createdBy: req.user.name, updateBy: req.user.name, },