전체 코드, 구조 검토 - P0 코드 수정 진행
This commit is contained in:
+64
-50
@@ -121,48 +121,69 @@ export async function approveUpgradeRequest(
|
||||
requestedAccountType: number;
|
||||
}> {
|
||||
return db.$transaction(async (tx) => {
|
||||
const request = await getActionTarget(tx, maestroUpgradeID);
|
||||
// Atomically claim the request — prevents concurrent double-approval
|
||||
const claimed = await tx.maestro_upgrade.updateMany({
|
||||
where: { MaestroUpgradeID: maestroUpgradeID, Status: REQUEST_STATUSES.REQUESTED },
|
||||
data: { Status: REQUEST_STATUSES.APPLIED },
|
||||
});
|
||||
if (claimed.count === 0) {
|
||||
const exists = await tx.maestro_upgrade.count({
|
||||
where: { MaestroUpgradeID: maestroUpgradeID },
|
||||
});
|
||||
throw new ApiError(
|
||||
exists ? "이미 처리된 업그레이드 신청입니다." : "업그레이드 신청을 찾을 수 없습니다.",
|
||||
exists ? 409 : 404
|
||||
);
|
||||
}
|
||||
|
||||
const request = await tx.maestro_upgrade.findUnique({
|
||||
where: { MaestroUpgradeID: maestroUpgradeID },
|
||||
select: {
|
||||
MaestroID: true,
|
||||
RequestedAccountType: true,
|
||||
RegisteredActivateStatus: true,
|
||||
RegisteredAccountType: true,
|
||||
maestro: { select: { Name: true, Email: true } },
|
||||
},
|
||||
});
|
||||
|
||||
const availableActivateDateTime = calculateUpgradeAvailableDate();
|
||||
|
||||
await tx.maestro.update({
|
||||
where: { MaestroID: request.MaestroID },
|
||||
where: { MaestroID: request!.MaestroID },
|
||||
data: {
|
||||
AccountType: request.RequestedAccountType,
|
||||
AccountType: request!.RequestedAccountType,
|
||||
ActivateStatus: ACTIVATE_STATUSES.ACTIVE,
|
||||
AvailableActivateDateTime: availableActivateDateTime,
|
||||
},
|
||||
});
|
||||
await tx.maestro_upgrade.updateMany({
|
||||
where: {
|
||||
MaestroID: request.MaestroID,
|
||||
MaestroID: request!.MaestroID,
|
||||
Status: REQUEST_STATUSES.REQUESTED,
|
||||
},
|
||||
data: { Status: REQUEST_STATUSES.CLOSED },
|
||||
});
|
||||
await tx.maestro_upgrade.update({
|
||||
where: { MaestroUpgradeID: request.MaestroUpgradeID },
|
||||
data: { Status: REQUEST_STATUSES.APPLIED },
|
||||
});
|
||||
await tx.maestro_log.create({
|
||||
data: {
|
||||
Type: "upgrade_maestro",
|
||||
MaestroID: request.MaestroID,
|
||||
MaestroID: request!.MaestroID,
|
||||
LogDateTime: new Date(),
|
||||
Remark: buildUpgradeLogRemark(
|
||||
request.RegisteredActivateStatus,
|
||||
request.RegisteredAccountType,
|
||||
request.RequestedAccountType
|
||||
request!.RegisteredActivateStatus,
|
||||
request!.RegisteredAccountType,
|
||||
request!.RequestedAccountType
|
||||
),
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
availableActivateDateTime: availableActivateDateTime.toISOString(),
|
||||
maestroName: request.maestro.Name.trim(),
|
||||
maestroEmail: request.maestro.Email.trim(),
|
||||
registeredActivateStatus: request.RegisteredActivateStatus,
|
||||
registeredAccountType: request.RegisteredAccountType,
|
||||
requestedAccountType: request.RequestedAccountType,
|
||||
maestroName: request!.maestro.Name.trim(),
|
||||
maestroEmail: request!.maestro.Email.trim(),
|
||||
registeredActivateStatus: request!.RegisteredActivateStatus,
|
||||
registeredAccountType: request!.RegisteredAccountType,
|
||||
requestedAccountType: request!.RequestedAccountType,
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -215,21 +236,39 @@ export async function rejectUpgradeRequest(
|
||||
maestroUpgradeID: number
|
||||
): Promise<void> {
|
||||
await db.$transaction(async (tx) => {
|
||||
const request = await getActionTarget(tx, maestroUpgradeID);
|
||||
|
||||
await tx.maestro_upgrade.update({
|
||||
where: { MaestroUpgradeID: request.MaestroUpgradeID },
|
||||
const claimed = await tx.maestro_upgrade.updateMany({
|
||||
where: { MaestroUpgradeID: maestroUpgradeID, Status: REQUEST_STATUSES.REQUESTED },
|
||||
data: { Status: REQUEST_STATUSES.CLOSED },
|
||||
});
|
||||
if (claimed.count === 0) {
|
||||
const exists = await tx.maestro_upgrade.count({
|
||||
where: { MaestroUpgradeID: maestroUpgradeID },
|
||||
});
|
||||
throw new ApiError(
|
||||
exists ? "이미 처리된 업그레이드 신청입니다." : "업그레이드 신청을 찾을 수 없습니다.",
|
||||
exists ? 409 : 404
|
||||
);
|
||||
}
|
||||
|
||||
const request = await tx.maestro_upgrade.findUnique({
|
||||
where: { MaestroUpgradeID: maestroUpgradeID },
|
||||
select: {
|
||||
MaestroID: true,
|
||||
RegisteredActivateStatus: true,
|
||||
RegisteredAccountType: true,
|
||||
RequestedAccountType: true,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.maestro_log.create({
|
||||
data: {
|
||||
Type: "reject_upgrade_maestro",
|
||||
MaestroID: request.MaestroID,
|
||||
MaestroID: request!.MaestroID,
|
||||
LogDateTime: new Date(),
|
||||
Remark: buildUpgradeLogRemark(
|
||||
request.RegisteredActivateStatus,
|
||||
request.RegisteredAccountType,
|
||||
request.RequestedAccountType
|
||||
request!.RegisteredActivateStatus,
|
||||
request!.RegisteredAccountType,
|
||||
request!.RequestedAccountType
|
||||
),
|
||||
},
|
||||
});
|
||||
@@ -244,31 +283,6 @@ export function parseUpgradeRequestSearchParams(
|
||||
);
|
||||
}
|
||||
|
||||
async function getActionTarget(
|
||||
tx: Prisma.TransactionClient,
|
||||
maestroUpgradeID: number
|
||||
) {
|
||||
const request = await tx.maestro_upgrade.findUnique({
|
||||
where: { MaestroUpgradeID: maestroUpgradeID },
|
||||
include: { maestro: { select: { Name: true, Email: true } } },
|
||||
});
|
||||
|
||||
if (!request) {
|
||||
throw new ApiError(
|
||||
"업그레이드 신청을 찾을 수 없습니다.",
|
||||
404
|
||||
);
|
||||
}
|
||||
|
||||
if (request.Status !== REQUEST_STATUSES.REQUESTED) {
|
||||
throw new ApiError(
|
||||
"이미 처리된 업그레이드 신청입니다.",
|
||||
409
|
||||
);
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
function buildUpgradeRequestWhere(
|
||||
params: UpgradeRequestSearchParams
|
||||
|
||||
Reference in New Issue
Block a user