fix(approvals): gate automated tasks by category and claim approval atomically
This commit is contained in:
1 parent
ee90284129
commit
28be46e14b
2 files changed
+108
-58
No files matched your search
@@ -144,70 +144,66 @@ export class ApprovalsController {
|
||||
throw new BadRequestException("Decision must be either APPROVED or REJECTED");
|
||||
}
|
||||
|
||||
const item = await (this.prisma as any).approvalRequest.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
if (!item) {
|
||||
throw new NotFoundException(`Approval request ${id} not found`);
|
||||
}
|
||||
return (this.prisma as any).$transaction(async (tx: any) => {
|
||||
const item = await tx.approvalRequest.findUnique({ where: { id } });
|
||||
if (!item) throw new NotFoundException(`Approval request ${id} not found`);
|
||||
|
||||
if (dto.decision === "REJECTED") {
|
||||
// 5. رد توسط مدیرعامل: تسک و درخواست کلاً از سیستم پاکسازی کامل میشوند
|
||||
await (this.prisma as any).departmentTask.deleteMany({
|
||||
where: { approvalId: id },
|
||||
});
|
||||
await (this.prisma as any).approvalRequest.delete({
|
||||
where: { id },
|
||||
if (dto.decision === "REJECTED") {
|
||||
if (item.status !== "PENDING") {
|
||||
throw new BadRequestException("Only pending requests can be rejected");
|
||||
}
|
||||
await tx.departmentTask.deleteMany({ where: { approvalId: id } });
|
||||
await tx.approvalRequest.delete({ where: { id, status: "PENDING" } });
|
||||
return { success: true, message: "درخواست با موفقیت رد و از سیستم پاکسازی شد." };
|
||||
}
|
||||
|
||||
// Only explicitly digital site work may enter the automated task queue.
|
||||
// Supplier strategy and every unknown category remain outside orchestration.
|
||||
const automatedCategories = new Set(["SEO", "SEO_DIGITAL", "CEO_EXPEDITE"]);
|
||||
if (item.category !== "B2B_SUPPLIER_STRATEGY" && !automatedCategories.has(item.category)) {
|
||||
throw new BadRequestException(`Approval category ${item.category} is not routed to automated tasks`);
|
||||
}
|
||||
if (item.status !== "PENDING") {
|
||||
return { success: true, message: "درخواست پیشتر تایید شده است.", data: item };
|
||||
}
|
||||
|
||||
// The conditional write locks the approval row. A racing decision loses
|
||||
// the PENDING predicate and must not create a second task or reset one.
|
||||
const claimed = await tx.approvalRequest.updateMany({
|
||||
where: { id, status: "PENDING" },
|
||||
data: { status: "APPROVED", ceoNotes: dto.ceoNotes || null, executedAt: new Date() },
|
||||
});
|
||||
if (claimed.count !== 1) {
|
||||
const current = await tx.approvalRequest.findUnique({ where: { id } });
|
||||
if (!current) throw new NotFoundException(`Approval request ${id} not found`);
|
||||
return { success: true, message: "درخواست پیشتر بررسی شده است.", data: current };
|
||||
}
|
||||
const updated = await tx.approvalRequest.findUnique({ where: { id } });
|
||||
if (automatedCategories.has(item.category)) {
|
||||
const existingTask = await tx.departmentTask.findFirst({ where: { approvalId: id } });
|
||||
if (!existingTask) {
|
||||
await tx.departmentTask.create({
|
||||
data: {
|
||||
code: "TSK-" + item.id,
|
||||
department: item.department,
|
||||
title: item.title,
|
||||
description: dto.ceoNotes ? item.title + "\n\n[یادداشت مدیرعامل]: " + dto.ceoNotes : item.title,
|
||||
status: "TODO",
|
||||
priority: "HIGH",
|
||||
assignee: "AI_AGENT",
|
||||
approvalId: item.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
message: "درخواست با موفقیت رد و از سیستم پاکسازی شد.",
|
||||
message: automatedCategories.has(item.category)
|
||||
? "درخواست تایید شد و مأموریت به صف اقدام (TODO) دپارتمان منتقل گردید."
|
||||
: "طرح تامینکننده تایید شد؛ تسک خودکار ایجاد نشد.",
|
||||
data: updated,
|
||||
};
|
||||
}
|
||||
|
||||
// 1. تایید مدیرعامل: وضعیت درخواست APPROVED و وضعیت تسک در صف اقدام به TODO تغییر مییابد
|
||||
const updated = await (this.prisma as any).approvalRequest.update({
|
||||
where: { id },
|
||||
data: {
|
||||
status: "APPROVED",
|
||||
ceoNotes: dto.ceoNotes || null,
|
||||
executedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
// جستجو یا ایجاد تسک متناظر با استاتوس TODO
|
||||
const existingTask = await (this.prisma as any).departmentTask.findFirst({
|
||||
where: { approvalId: id },
|
||||
});
|
||||
|
||||
if (existingTask) {
|
||||
await (this.prisma as any).departmentTask.update({
|
||||
where: { id: existingTask.id },
|
||||
data: {
|
||||
status: "TODO",
|
||||
description: dto.ceoNotes ? (existingTask.description || "") + "\n\n[یادداشت مدیرعامل]: " + dto.ceoNotes : existingTask.description,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await (this.prisma as any).departmentTask.create({
|
||||
data: {
|
||||
code: "TSK-" + Date.now().toString().slice(-6),
|
||||
department: item.department,
|
||||
title: item.title,
|
||||
description: dto.ceoNotes ? (item.title + "\n\n[یادداشت مدیرعامل]: " + dto.ceoNotes) : item.title,
|
||||
status: "TODO",
|
||||
priority: "HIGH",
|
||||
assignee: "AI_AGENT",
|
||||
approvalId: item.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "درخواست تایید شد و مأموریت به صف اقدام (TODO) دپارتمان منتقل گردید.",
|
||||
data: updated,
|
||||
};
|
||||
}
|
||||
|
||||
@Delete(":id")
|
||||
|
||||
Reference in new issue
Block a user