feat: notify request, task update
This commit is contained in:
parent
afb4a83efa
commit
17b92b4012
2 changed files with 137 additions and 105 deletions
|
|
@ -1136,41 +1136,53 @@ export class QuotationActionController extends Controller {
|
|||
},
|
||||
update: { value: { increment: quotation.worker.length } },
|
||||
});
|
||||
await tx.quotation.update({
|
||||
where: { id: quotationId, isDebitNote: false },
|
||||
data: {
|
||||
quotationStatus: QuotationStatus.PaymentSuccess, // NOTE: change back if already complete or canceled
|
||||
worker: {
|
||||
createMany: {
|
||||
data: rearrange
|
||||
.filter((lhs) => !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId))
|
||||
.map((v, i) => ({
|
||||
no: quotation._count.worker + i + 1,
|
||||
employeeId: v.workerId,
|
||||
})),
|
||||
await tx.quotation
|
||||
.update({
|
||||
include: { requestData: true },
|
||||
where: { id: quotationId, isDebitNote: false },
|
||||
data: {
|
||||
quotationStatus: QuotationStatus.PaymentSuccess, // NOTE: change back if already complete or canceled
|
||||
worker: {
|
||||
createMany: {
|
||||
data: rearrange
|
||||
.filter((lhs) => !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId))
|
||||
.map((v, i) => ({
|
||||
no: quotation._count.worker + i + 1,
|
||||
employeeId: v.workerId,
|
||||
})),
|
||||
},
|
||||
},
|
||||
requestData:
|
||||
quotation.quotationStatus === "PaymentInProcess" ||
|
||||
quotation.quotationStatus === "PaymentSuccess"
|
||||
? {
|
||||
create: rearrange
|
||||
.filter(
|
||||
(lhs) =>
|
||||
!quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId) &&
|
||||
lhs.productServiceId.length > 0,
|
||||
)
|
||||
.map((v, i) => ({
|
||||
code: `TR${year}${month}${(lastRequest.value - quotation._count.worker + i + 1).toString().padStart(6, "0")}`,
|
||||
employeeId: v.workerId,
|
||||
requestWork: {
|
||||
create: v.productServiceId.map((v) => ({ productServiceId: v })),
|
||||
},
|
||||
})),
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
requestData:
|
||||
quotation.quotationStatus === "PaymentInProcess" ||
|
||||
quotation.quotationStatus === "PaymentSuccess"
|
||||
? {
|
||||
create: rearrange
|
||||
.filter(
|
||||
(lhs) =>
|
||||
!quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId) &&
|
||||
lhs.productServiceId.length > 0,
|
||||
)
|
||||
.map((v, i) => ({
|
||||
code: `TR${year}${month}${(lastRequest.value - quotation._count.worker + i + 1).toString().padStart(6, "0")}`,
|
||||
employeeId: v.workerId,
|
||||
requestWork: {
|
||||
create: v.productServiceId.map((v) => ({ productServiceId: v })),
|
||||
},
|
||||
})),
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
});
|
||||
})
|
||||
.then(async (ret) => {
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
title: "New Request",
|
||||
detail: "New request: " + ret.requestData.map((v) => v.code).join(", "),
|
||||
registeredBranchId: ret.registeredBranchId,
|
||||
groupReceiver: { create: { name: "document_checker" } },
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -433,88 +433,101 @@ export class TaskController extends Controller {
|
|||
);
|
||||
}
|
||||
|
||||
return await prisma.$transaction(async (tx) => {
|
||||
await Promise.all(
|
||||
record.taskList
|
||||
.filter(
|
||||
(lhs) =>
|
||||
!body.taskList.find(
|
||||
(rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step,
|
||||
),
|
||||
)
|
||||
.map((v) =>
|
||||
tx.task.update({
|
||||
where: { id: v.id },
|
||||
data: {
|
||||
requestWorkStep: { update: { workStatus: "Ready" } },
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
return await prisma
|
||||
.$transaction(async (tx) => {
|
||||
await Promise.all(
|
||||
record.taskList
|
||||
.filter(
|
||||
(lhs) =>
|
||||
!body.taskList.find(
|
||||
(rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step,
|
||||
),
|
||||
)
|
||||
.map((v) =>
|
||||
tx.task.update({
|
||||
where: { id: v.id },
|
||||
data: {
|
||||
requestWorkStep: { update: { workStatus: "Ready" } },
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
await tx.requestWorkStepStatus.updateMany({
|
||||
where: {
|
||||
OR: body.taskList,
|
||||
workStatus: RequestWorkStatus.Ready,
|
||||
},
|
||||
data: { workStatus: RequestWorkStatus.InProgress },
|
||||
});
|
||||
|
||||
const work = await tx.requestWorkStepStatus.findMany({
|
||||
include: {
|
||||
requestWork: {
|
||||
include: {
|
||||
request: {
|
||||
include: { quotation: true },
|
||||
},
|
||||
},
|
||||
await tx.requestWorkStepStatus.updateMany({
|
||||
where: {
|
||||
OR: body.taskList,
|
||||
workStatus: RequestWorkStatus.Ready,
|
||||
},
|
||||
},
|
||||
where: { OR: body.taskList },
|
||||
});
|
||||
data: { workStatus: RequestWorkStatus.InProgress },
|
||||
});
|
||||
|
||||
return await tx.taskOrder.update({
|
||||
where: { id: taskOrderId },
|
||||
include: {
|
||||
taskList: {
|
||||
include: {
|
||||
requestWorkStep: {
|
||||
include: {
|
||||
requestWork: true,
|
||||
const work = await tx.requestWorkStepStatus.findMany({
|
||||
include: {
|
||||
requestWork: {
|
||||
include: {
|
||||
request: {
|
||||
include: { quotation: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
institution: true,
|
||||
registeredBranch: true,
|
||||
createdBy: true,
|
||||
},
|
||||
data: {
|
||||
...body,
|
||||
urgent: work.some((v) => v.requestWork.request.quotation.urgent),
|
||||
taskList: {
|
||||
deleteMany: record?.taskList
|
||||
.filter(
|
||||
(lhs) =>
|
||||
!body.taskList.find(
|
||||
(rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step,
|
||||
),
|
||||
)
|
||||
.map((v) => ({ id: v.id })),
|
||||
createMany: {
|
||||
data: body.taskList.filter(
|
||||
(lhs) =>
|
||||
!record?.taskList.find(
|
||||
(rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step,
|
||||
),
|
||||
),
|
||||
skipDuplicates: true,
|
||||
where: { OR: body.taskList },
|
||||
});
|
||||
|
||||
return await tx.taskOrder.update({
|
||||
where: { id: taskOrderId },
|
||||
include: {
|
||||
taskList: {
|
||||
include: {
|
||||
requestWorkStep: {
|
||||
include: {
|
||||
requestWork: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
institution: true,
|
||||
registeredBranch: true,
|
||||
createdBy: true,
|
||||
},
|
||||
taskProduct: { deleteMany: {}, create: body.taskProduct },
|
||||
},
|
||||
data: {
|
||||
...body,
|
||||
urgent: work.some((v) => v.requestWork.request.quotation.urgent),
|
||||
taskList: {
|
||||
deleteMany: record?.taskList
|
||||
.filter(
|
||||
(lhs) =>
|
||||
!body.taskList.find(
|
||||
(rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step,
|
||||
),
|
||||
)
|
||||
.map((v) => ({ id: v.id })),
|
||||
createMany: {
|
||||
data: body.taskList.filter(
|
||||
(lhs) =>
|
||||
!record?.taskList.find(
|
||||
(rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step,
|
||||
),
|
||||
),
|
||||
skipDuplicates: true,
|
||||
},
|
||||
},
|
||||
taskProduct: { deleteMany: {}, create: body.taskProduct },
|
||||
},
|
||||
});
|
||||
})
|
||||
.then(async (ret) => {
|
||||
if (body.taskOrderStatus && record.taskOrderStatus !== body.taskOrderStatus) {
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
title: "Task Submitted",
|
||||
detail: "Task submitted in order: " + record.code,
|
||||
receiverId: record.createdByUserId,
|
||||
},
|
||||
});
|
||||
}
|
||||
return ret;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Delete("{taskOrderId}")
|
||||
|
|
@ -651,6 +664,13 @@ export class TaskActionController extends Controller {
|
|||
},
|
||||
data: { userTaskStatus: UserTaskStatus.Submit, submittedAt: new Date() },
|
||||
}),
|
||||
prisma.notification.create({
|
||||
data: {
|
||||
title: "Task Submitted",
|
||||
detail: "Task submitted in order: " + record.code,
|
||||
receiverId: record.createdByUserId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue