From f7e4d415496ceb8c201de152612dadb7457c4d4a Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 8 Aug 2024 09:14:26 +0700 Subject: [PATCH] feat: change code gen of user --- src/controllers/user-controller.ts | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/controllers/user-controller.ts b/src/controllers/user-controller.ts index a1bf870..e2b8e14 100644 --- a/src/controllers/user-controller.ts +++ b/src/controllers/user-controller.ts @@ -124,12 +124,19 @@ async function userBranchCodeGen(user: User, branch: Branch) { async (tx) => { const typ = user.userType; + const mapTypeNo = { + USER: 1, + MESSENGER: 2, + DELEGATE: 3, + AGENCY: 4, + }[typ]; + const last = await tx.runningNo.upsert({ where: { - key: `BR_USR_${branch.code.slice(4).padEnd(3, "0")}${typ !== "USER" ? typ.charAt(0).toLocaleUpperCase() : ""}`, + key: `BR_USR_${branch.code}_${mapTypeNo}`, }, create: { - key: `BR_USR_${branch.code.slice(4).padEnd(3, "0")}${typ !== "USER" ? typ.charAt(0).toLocaleUpperCase() : ""}`, + key: `BR_USR_${branch.code}_${mapTypeNo}`, value: 1, }, update: { value: { increment: 1 } }, @@ -138,7 +145,7 @@ async function userBranchCodeGen(user: User, branch: Branch) { return await tx.user.update({ where: { id: user.id }, data: { - code: `${last.key.slice(7)}${last.value.toString().padStart(4, "0")}`, + code: mapTypeNo + `${last.value}`.padStart(6, "9"), }, }); }, @@ -301,7 +308,6 @@ export class UserController extends Controller { !["system", "head_of_admin", "admin"].some((v) => req.user.roles?.includes(v)) && branch?.some((v) => !v.user.find((v) => v.userId === req.user.sub)) ) { - console.log(req.user.roles); throw new HttpError( HttpStatus.FORBIDDEN, "You do not have permission to perform this action.", @@ -506,18 +512,6 @@ export class UserController extends Controller { const { provinceId, districtId, subDistrictId, branchId, ...rest } = body; - const lastUserOfType = - body.userType && - body.userType !== user.userType && - user.code && - (await prisma.user.findFirst({ - orderBy: { createdAt: "desc" }, - where: { - userType: body.userType, - code: { startsWith: `${user.code?.slice(0, 3)}` }, - }, - })); - const record = await prisma.user.update({ include: { province: true, @@ -530,10 +524,6 @@ export class UserController extends Controller { ...rest, statusOrder: +(rest.status === "INACTIVE"), userRole, - code: - (lastUserOfType && - `${user.code?.slice(0, 3)}${body.userType !== "USER" ? body.userType?.charAt(0) : ""}${(+(lastUserOfType?.code?.slice(-4) || 0) + 1).toString().padStart(4, "0")}`) || - undefined, province: { connect: provinceId ? { id: provinceId } : undefined, disconnect: provinceId === null || undefined,