diff --git a/src/controllers/07-task-controller.ts b/src/controllers/07-task-controller.ts index 2c3f500..ecab4c3 100644 --- a/src/controllers/07-task-controller.ts +++ b/src/controllers/07-task-controller.ts @@ -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 }, + }); + }); } }