feat: change code gen of user

This commit is contained in:
Methapon2001 2024-08-08 09:14:26 +07:00
parent c92ae50f82
commit f7e4d41549

View file

@ -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,