feat: check if request data is complete

This commit is contained in:
Methapon2001 2024-12-20 18:04:34 +07:00
parent ad551bdf10
commit 37a9dd2677

View file

@ -525,6 +525,65 @@ export class TaskActionController extends Controller {
data: { taskStatus: TaskStatus.Complete },
}),
]);
await prisma.$transaction(async (tx) => {
const requestList = await tx.requestData.findMany({
include: {
requestWork: {
include: {
productService: {
include: {
product: true,
service: true,
work: {
include: { productOnWork: true },
},
},
},
stepStatus: true,
},
},
},
where: {
requestWork: {
some: {
stepStatus: {
some: {
task: { some: { taskOrderId } },
},
},
},
},
},
});
const completed: string[] = [];
requestList.forEach((item) => {
const completeCheck = item.requestWork.every((work) => {
const stepCount =
work.productService.work?.productOnWork.find(
(v) => v.productId === work.productService.productId,
)?.stepCount || 0;
const completeCount = work.stepStatus.filter(
(v) => v.workStatus === RequestWorkStatus.Completed,
).length;
// NOTE: step found then check if complete count equals step count
if (stepCount === completeCount && completeCount > 0) return true;
// NOTE: likely no step found and completed at least one
if (stepCount === 0 && completeCount > 0) return true;
});
if (completeCheck) completed.push(item.id);
});
await tx.requestData.updateMany({
where: { id: { in: completed } },
data: { requestDataStatus: RequestDataStatus.Completed },
});
});
}
}