fix: auto gen user code

This commit is contained in:
Methapon2001 2024-04-11 17:08:22 +07:00
parent 30acdfdd32
commit 849a7d0078

View file

@ -126,16 +126,20 @@ export class BranchUserController extends Controller {
for (const g of Object.values(UserType)) {
if (group[g].length === 0) continue;
const last = await prisma.user.findFirst({
const last = await prisma.branchUser.findFirst({
orderBy: { createdAt: "desc" },
include: { user: true },
where: {
userType: g,
code: { startsWith: `${branch.code.slice(2, 5).padEnd(3, "0")}` },
branchId,
user: {
userType: g,
code: { startsWith: `${branch.code.slice(4).padEnd(3, "0")}` },
},
},
});
const code = (idx: number) =>
`${branch.code.slice(4).padEnd(3, "0")}${g !== "USER" ? g.charAt(0) : ""}${(+(last?.code?.slice(-4) || 0) + idx + 1).toString().padStart(4, "0")}`;
`${branch.code.slice(4).padEnd(3, "0")}${g !== "USER" ? g.charAt(0) : ""}${(+(last?.user.code?.slice(-4) || 0) + idx + 1).toString().padStart(4, "0")}`;
await prisma.$transaction(
group[g].map((v, i) =>