feat: change code gen of user
This commit is contained in:
parent
c92ae50f82
commit
f7e4d41549
1 changed files with 10 additions and 20 deletions
|
|
@ -124,12 +124,19 @@ async function userBranchCodeGen(user: User, branch: Branch) {
|
||||||
async (tx) => {
|
async (tx) => {
|
||||||
const typ = user.userType;
|
const typ = user.userType;
|
||||||
|
|
||||||
|
const mapTypeNo = {
|
||||||
|
USER: 1,
|
||||||
|
MESSENGER: 2,
|
||||||
|
DELEGATE: 3,
|
||||||
|
AGENCY: 4,
|
||||||
|
}[typ];
|
||||||
|
|
||||||
const last = await tx.runningNo.upsert({
|
const last = await tx.runningNo.upsert({
|
||||||
where: {
|
where: {
|
||||||
key: `BR_USR_${branch.code.slice(4).padEnd(3, "0")}${typ !== "USER" ? typ.charAt(0).toLocaleUpperCase() : ""}`,
|
key: `BR_USR_${branch.code}_${mapTypeNo}`,
|
||||||
},
|
},
|
||||||
create: {
|
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,
|
value: 1,
|
||||||
},
|
},
|
||||||
update: { value: { increment: 1 } },
|
update: { value: { increment: 1 } },
|
||||||
|
|
@ -138,7 +145,7 @@ async function userBranchCodeGen(user: User, branch: Branch) {
|
||||||
return await tx.user.update({
|
return await tx.user.update({
|
||||||
where: { id: user.id },
|
where: { id: user.id },
|
||||||
data: {
|
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)) &&
|
!["system", "head_of_admin", "admin"].some((v) => req.user.roles?.includes(v)) &&
|
||||||
branch?.some((v) => !v.user.find((v) => v.userId === req.user.sub))
|
branch?.some((v) => !v.user.find((v) => v.userId === req.user.sub))
|
||||||
) {
|
) {
|
||||||
console.log(req.user.roles);
|
|
||||||
throw new HttpError(
|
throw new HttpError(
|
||||||
HttpStatus.FORBIDDEN,
|
HttpStatus.FORBIDDEN,
|
||||||
"You do not have permission to perform this action.",
|
"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 { 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({
|
const record = await prisma.user.update({
|
||||||
include: {
|
include: {
|
||||||
province: true,
|
province: true,
|
||||||
|
|
@ -530,10 +524,6 @@ export class UserController extends Controller {
|
||||||
...rest,
|
...rest,
|
||||||
statusOrder: +(rest.status === "INACTIVE"),
|
statusOrder: +(rest.status === "INACTIVE"),
|
||||||
userRole,
|
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: {
|
province: {
|
||||||
connect: provinceId ? { id: provinceId } : undefined,
|
connect: provinceId ? { id: provinceId } : undefined,
|
||||||
disconnect: provinceId === null || undefined,
|
disconnect: provinceId === null || undefined,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue