refactor!: user code gen

This commit is contained in:
Methapon2001 2024-08-09 09:51:36 +07:00
parent e0b3ed6383
commit c434b085bc

View file

@ -28,12 +28,19 @@ async function userBranchCodeGen(branch: Branch, user: User[]) {
const typ = usr.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 } },
@ -42,7 +49,7 @@ async function userBranchCodeGen(branch: Branch, user: User[]) {
await tx.user.update({
where: { id: usr.id },
data: {
code: `${last.key.slice(7)}${last.value.toString().padStart(4, "0")}`,
code: mapTypeNo + `${last.value}`.padStart(6, "9"),
},
});
}