feat: support bulk action on put request work step (#15)
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 8s
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 8s
* add updateRequestWorkDataStepStatus * change updateRequestWorkDataStepStatus * change position updateRequestWorkDataStepStatus
This commit is contained in:
parent
3b59aa1e14
commit
d40bd5eb8c
1 changed files with 211 additions and 0 deletions
|
|
@ -287,6 +287,217 @@ export class RequestDataActionController extends Controller {
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Put("request-work/step-status/{step}")
|
||||||
|
@Security("keycloak")
|
||||||
|
async updateRequestWorkDataStepStatus(
|
||||||
|
@Path() requestDataId: string,
|
||||||
|
@Path() step: number,
|
||||||
|
@Body()
|
||||||
|
payload: {
|
||||||
|
workStatus?: RequestWorkStatus;
|
||||||
|
requestWorkId: string;
|
||||||
|
attributes?: Record<string, any>;
|
||||||
|
customerDuty?: boolean | null;
|
||||||
|
customerDutyCost?: number | null;
|
||||||
|
companyDuty?: boolean | null;
|
||||||
|
companyDutyCost?: number | null;
|
||||||
|
individualDuty?: boolean | null;
|
||||||
|
individualDutyCost?: number | null;
|
||||||
|
responsibleUserLocal?: boolean | null;
|
||||||
|
responsibleUserId?: string | null;
|
||||||
|
}[],
|
||||||
|
) {
|
||||||
|
payload.forEach((item) => {
|
||||||
|
if (!item.responsibleUserId) item.responsibleUserId = undefined;
|
||||||
|
});
|
||||||
|
return await prisma.$transaction(async (tx) => {
|
||||||
|
const workStepCondition = await tx.requestData.findFirst({
|
||||||
|
where: {
|
||||||
|
id: requestDataId,
|
||||||
|
},
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!workStepCondition) {
|
||||||
|
throw new Error("RequestWork not found requestDataId");
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await Promise.all(
|
||||||
|
payload.map(async (item) => {
|
||||||
|
return await tx.requestWorkStepStatus.upsert({
|
||||||
|
include: {
|
||||||
|
requestWork: {
|
||||||
|
include: {
|
||||||
|
request: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
step_requestWorkId: {
|
||||||
|
step: step,
|
||||||
|
requestWorkId: item.requestWorkId,
|
||||||
|
},
|
||||||
|
requestWork: {
|
||||||
|
request: { id: requestDataId },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
...item,
|
||||||
|
step: step,
|
||||||
|
requestWorkId: item.requestWorkId,
|
||||||
|
},
|
||||||
|
update: item,
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
data.some((item) => {
|
||||||
|
return (
|
||||||
|
item.workStatus === "Ready" && item.requestWork.request.requestDataStatus === "Pending"
|
||||||
|
);
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
await tx.requestData.updateMany({
|
||||||
|
where: {
|
||||||
|
id: requestDataId,
|
||||||
|
requestDataStatus: "Pending",
|
||||||
|
},
|
||||||
|
data: { requestDataStatus: "Ready" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
data.some((item) => {
|
||||||
|
return (
|
||||||
|
item.workStatus === "InProgress" ||
|
||||||
|
item.workStatus === "Waiting" ||
|
||||||
|
item.workStatus === "Validate" ||
|
||||||
|
item.workStatus === "Completed" ||
|
||||||
|
item.workStatus === "Ended"
|
||||||
|
);
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
await tx.requestData.update({
|
||||||
|
where: {
|
||||||
|
id: requestDataId,
|
||||||
|
},
|
||||||
|
data: { requestDataStatus: "InProgress" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
data.some((item) => {
|
||||||
|
return item.workStatus === "Canceled";
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
const dataId = data.map((itemId) => itemId.requestWork.id);
|
||||||
|
await tx.task.updateMany({
|
||||||
|
where: {
|
||||||
|
taskStatus: { notIn: [TaskStatus.Complete, TaskStatus.Redo] },
|
||||||
|
requestWorkStep: {
|
||||||
|
step: step,
|
||||||
|
requestWorkId: { in: dataId },
|
||||||
|
workStatus: { notIn: [RequestWorkStatus.Completed, RequestWorkStatus.Ended] },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
data: { taskStatus: TaskStatus.Canceled },
|
||||||
|
});
|
||||||
|
await Promise.all([
|
||||||
|
tx.quotation.updateMany({
|
||||||
|
where: {
|
||||||
|
requestData: {
|
||||||
|
every: { requestDataStatus: RequestDataStatus.Canceled },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
data: { quotationStatus: QuotationStatus.Canceled, urgent: false },
|
||||||
|
}),
|
||||||
|
tx.taskOrder.updateMany({
|
||||||
|
where: {
|
||||||
|
taskList: {
|
||||||
|
every: { taskStatus: TaskStatus.Canceled },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
data: { taskOrderStatus: TaskStatus.Canceled },
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestList = await tx.requestData.findMany({
|
||||||
|
include: {
|
||||||
|
requestWork: {
|
||||||
|
include: {
|
||||||
|
productService: {
|
||||||
|
include: {
|
||||||
|
product: true,
|
||||||
|
service: true,
|
||||||
|
work: {
|
||||||
|
include: { productOnWork: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
stepStatus: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
requestWork: {
|
||||||
|
some: {
|
||||||
|
requestDataId: requestDataId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
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 ||
|
||||||
|
v.workStatus === RequestWorkStatus.Ended ||
|
||||||
|
v.workStatus === RequestWorkStatus.Canceled,
|
||||||
|
).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 },
|
||||||
|
});
|
||||||
|
await tx.quotation.updateMany({
|
||||||
|
where: {
|
||||||
|
quotationStatus: {
|
||||||
|
notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete],
|
||||||
|
},
|
||||||
|
requestData: {
|
||||||
|
every: {
|
||||||
|
requestDataStatus: {
|
||||||
|
in: [RequestDataStatus.Canceled, RequestDataStatus.Completed],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false },
|
||||||
|
});
|
||||||
|
// dataRecord.push(record);
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route("/api/v1/request-work")
|
@Route("/api/v1/request-work")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue