diff --git a/src/controllers/02-user-controller.ts b/src/controllers/02-user-controller.ts index 387ff0e..68b1503 100644 --- a/src/controllers/02-user-controller.ts +++ b/src/controllers/02-user-controller.ts @@ -46,6 +46,7 @@ import { } from "../services/permission"; import { connectOrDisconnect, connectOrNot, whereAddressQuery } from "../utils/relation"; import { isUsedError, notFoundError, relationError } from "../utils/error"; +import { retry } from "../utils/func"; if (!process.env.MINIO_BUCKET) { throw Error("Require MinIO bucket."); @@ -174,36 +175,38 @@ const permissionCond = createPermCondition(globalAllow); const permissionCheck = createPermCheck(globalAllow); async function userBranchCodeGen(user: User, branch: Branch) { - return await prisma.$transaction( - async (tx) => { - const typ = user.userType; + return await retry(() => + prisma.$transaction( + async (tx) => { + const typ = user.userType; - const mapTypeNo = { - USER: 1, - MESSENGER: 2, - DELEGATE: 3, - AGENCY: 4, - }[typ]; + const mapTypeNo = { + USER: 1, + MESSENGER: 2, + DELEGATE: 3, + AGENCY: 4, + }[typ]; - const last = await tx.runningNo.upsert({ - where: { - key: `BR_USR_${branch.code}_${mapTypeNo}`, - }, - create: { - key: `BR_USR_${branch.code}_${mapTypeNo}`, - value: 1, - }, - update: { value: { increment: 1 } }, - }); + const last = await tx.runningNo.upsert({ + where: { + key: `BR_USR_${branch.code}_${mapTypeNo}`, + }, + create: { + key: `BR_USR_${branch.code}_${mapTypeNo}`, + value: 1, + }, + update: { value: { increment: 1 } }, + }); - return await tx.user.update({ - where: { id: user.id }, - data: { - code: mapTypeNo + `${last.value}`.padStart(6, "0"), - }, - }); - }, - { isolationLevel: Prisma.TransactionIsolationLevel.Serializable }, + return await tx.user.update({ + where: { id: user.id }, + data: { + code: mapTypeNo + `${last.value}`.padStart(6, "0"), + }, + }); + }, + { isolationLevel: Prisma.TransactionIsolationLevel.Serializable }, + ), ); }