diff --git a/src/controllers/branch-user-controller.ts b/src/controllers/branch-user-controller.ts index c96e5a2..19ffcd9 100644 --- a/src/controllers/branch-user-controller.ts +++ b/src/controllers/branch-user-controller.ts @@ -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) =>