feat: use retry function

This commit is contained in:
Methapon Metanipat 2024-10-22 17:59:31 +07:00
parent 6235186ff9
commit dfd927eeb4

View file

@ -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 },
),
);
}