feat: notify quotation status change
This commit is contained in:
parent
071262a85a
commit
2697b4f6e0
4 changed files with 192 additions and 103 deletions
|
|
@ -177,55 +177,66 @@ export class QuotationPayment extends Controller {
|
|||
},
|
||||
});
|
||||
|
||||
await tx.quotation.update({
|
||||
where: { id: quotation.id },
|
||||
data: {
|
||||
quotationStatus:
|
||||
(paymentSum._sum.amount || 0) >= quotation.finalPrice
|
||||
? "PaymentSuccess"
|
||||
: "PaymentInProcess",
|
||||
requestData: await (async () => {
|
||||
if (
|
||||
body.paymentStatus === "PaymentSuccess" &&
|
||||
(paymentSum._sum.amount || 0) - payment.amount <= 0
|
||||
) {
|
||||
const lastRequest = await tx.runningNo.upsert({
|
||||
where: {
|
||||
key: `REQUEST_${year}${month}`,
|
||||
},
|
||||
create: {
|
||||
key: `REQUEST_${year}${month}`,
|
||||
value: quotation.worker.length,
|
||||
},
|
||||
update: { value: { increment: quotation.worker.length } },
|
||||
});
|
||||
return {
|
||||
create: quotation.worker.flatMap((v, i) => {
|
||||
const productEmployee = quotation.productServiceList.flatMap((item) =>
|
||||
item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1
|
||||
? { productServiceId: item.id }
|
||||
: [],
|
||||
);
|
||||
await tx.quotation
|
||||
.update({
|
||||
where: { id: quotation.id },
|
||||
data: {
|
||||
quotationStatus:
|
||||
(paymentSum._sum.amount || 0) >= quotation.finalPrice
|
||||
? "PaymentSuccess"
|
||||
: "PaymentInProcess",
|
||||
requestData: await (async () => {
|
||||
if (
|
||||
body.paymentStatus === "PaymentSuccess" &&
|
||||
(paymentSum._sum.amount || 0) - payment.amount <= 0
|
||||
) {
|
||||
const lastRequest = await tx.runningNo.upsert({
|
||||
where: {
|
||||
key: `REQUEST_${year}${month}`,
|
||||
},
|
||||
create: {
|
||||
key: `REQUEST_${year}${month}`,
|
||||
value: quotation.worker.length,
|
||||
},
|
||||
update: { value: { increment: quotation.worker.length } },
|
||||
});
|
||||
return {
|
||||
create: quotation.worker.flatMap((v, i) => {
|
||||
const productEmployee = quotation.productServiceList.flatMap((item) =>
|
||||
item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1
|
||||
? { productServiceId: item.id }
|
||||
: [],
|
||||
);
|
||||
|
||||
if (productEmployee.length <= 0) return [];
|
||||
if (productEmployee.length <= 0) return [];
|
||||
|
||||
return {
|
||||
code: `TR${year}${month}${(lastRequest.value - quotation.worker.length + i + 1).toString().padStart(6, "0")}`,
|
||||
employeeId: v.employeeId,
|
||||
requestWork: {
|
||||
create: quotation.productServiceList.flatMap((item) =>
|
||||
item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1
|
||||
? { productServiceId: item.id }
|
||||
: [],
|
||||
),
|
||||
},
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
})(),
|
||||
},
|
||||
});
|
||||
return {
|
||||
code: `TR${year}${month}${(lastRequest.value - quotation.worker.length + i + 1).toString().padStart(6, "0")}`,
|
||||
employeeId: v.employeeId,
|
||||
requestWork: {
|
||||
create: quotation.productServiceList.flatMap((item) =>
|
||||
item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1
|
||||
? { productServiceId: item.id }
|
||||
: [],
|
||||
),
|
||||
},
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
})(),
|
||||
},
|
||||
})
|
||||
.then(async (res) => {
|
||||
if (quotation.quotationStatus !== res.quotationStatus)
|
||||
await tx.notification.create({
|
||||
data: {
|
||||
title: "Quotation Status Updated",
|
||||
detail: res.code + res.quotationStatus,
|
||||
receiverId: res.createdByUserId,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return payment;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -454,7 +454,7 @@ export class QuotationController extends Controller {
|
|||
|
||||
const { productServiceList: _productServiceList, worker: _worker, ...rest } = body;
|
||||
|
||||
return await prisma.$transaction(async (tx) => {
|
||||
const ret = await prisma.$transaction(async (tx) => {
|
||||
const nonExistEmployee = body.worker.filter((v) => typeof v !== "string");
|
||||
const lastEmployee = await tx.runningNo.upsert({
|
||||
where: {
|
||||
|
|
@ -639,6 +639,17 @@ export class QuotationController extends Controller {
|
|||
},
|
||||
});
|
||||
});
|
||||
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
title: "New Quotation",
|
||||
detail: "New quotation: " + ret.code,
|
||||
registeredBranchId: ret.registeredBranchId,
|
||||
groupReceiver: { create: [{ name: "accountant" }, { name: "head_of_accountant" }] },
|
||||
},
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Put("{quotationId}")
|
||||
|
|
|
|||
|
|
@ -268,14 +268,24 @@ export class RequestDataActionController extends Controller {
|
|||
}),
|
||||
]);
|
||||
await Promise.all([
|
||||
tx.quotation.updateMany({
|
||||
where: {
|
||||
requestData: {
|
||||
every: { requestDataStatus: RequestDataStatus.Canceled },
|
||||
tx.quotation
|
||||
.updateManyAndReturn({
|
||||
where: {
|
||||
requestData: {
|
||||
every: { requestDataStatus: RequestDataStatus.Canceled },
|
||||
},
|
||||
},
|
||||
},
|
||||
data: { quotationStatus: QuotationStatus.Canceled, urgent: false },
|
||||
}),
|
||||
data: { quotationStatus: QuotationStatus.Canceled, urgent: false },
|
||||
})
|
||||
.then(async (res) => {
|
||||
await tx.notification.createMany({
|
||||
data: res.map((v) => ({
|
||||
title: "Quotation Status Updated",
|
||||
detail: v.code + "Canceled",
|
||||
receiverId: v.createdByUserId,
|
||||
})),
|
||||
});
|
||||
}),
|
||||
tx.taskOrder.updateMany({
|
||||
where: {
|
||||
taskList: {
|
||||
|
|
@ -405,14 +415,25 @@ export class RequestDataActionController extends Controller {
|
|||
data: { taskStatus: TaskStatus.Canceled },
|
||||
});
|
||||
await Promise.all([
|
||||
tx.quotation.updateMany({
|
||||
where: {
|
||||
requestData: {
|
||||
every: { requestDataStatus: RequestDataStatus.Canceled },
|
||||
tx.quotation
|
||||
.updateManyAndReturn({
|
||||
where: {
|
||||
quotationStatus: { not: QuotationStatus.Canceled },
|
||||
requestData: {
|
||||
every: { requestDataStatus: RequestDataStatus.Canceled },
|
||||
},
|
||||
},
|
||||
},
|
||||
data: { quotationStatus: QuotationStatus.Canceled, urgent: false },
|
||||
}),
|
||||
data: { quotationStatus: QuotationStatus.Canceled, urgent: false },
|
||||
})
|
||||
.then(async (res) => {
|
||||
await tx.notification.createMany({
|
||||
data: res.map((v) => ({
|
||||
title: "Quotation Status Updated",
|
||||
detail: v.code + "Canceled",
|
||||
receiverId: v.createdByUserId,
|
||||
})),
|
||||
});
|
||||
}),
|
||||
tx.taskOrder.updateMany({
|
||||
where: {
|
||||
taskList: {
|
||||
|
|
@ -479,21 +500,31 @@ export class RequestDataActionController extends Controller {
|
|||
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],
|
||||
await tx.quotation
|
||||
.updateManyAndReturn({
|
||||
where: {
|
||||
quotationStatus: {
|
||||
notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete],
|
||||
},
|
||||
requestData: {
|
||||
every: {
|
||||
requestDataStatus: {
|
||||
in: [RequestDataStatus.Canceled, RequestDataStatus.Completed],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false },
|
||||
});
|
||||
data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false },
|
||||
})
|
||||
.then(async (res) => {
|
||||
await tx.notification.createMany({
|
||||
data: res.map((v) => ({
|
||||
title: "Quotation Status Updated",
|
||||
detail: v.code + "Completed",
|
||||
receiverId: v.createdByUserId,
|
||||
})),
|
||||
});
|
||||
});
|
||||
// dataRecord.push(record);
|
||||
return data;
|
||||
});
|
||||
|
|
@ -812,14 +843,25 @@ export class RequestListController extends Controller {
|
|||
data: { taskStatus: TaskStatus.Canceled },
|
||||
});
|
||||
await Promise.all([
|
||||
tx.quotation.updateMany({
|
||||
where: {
|
||||
requestData: {
|
||||
every: { requestDataStatus: RequestDataStatus.Canceled },
|
||||
tx.quotation
|
||||
.updateManyAndReturn({
|
||||
where: {
|
||||
quotationStatus: { not: QuotationStatus.Canceled },
|
||||
requestData: {
|
||||
every: { requestDataStatus: RequestDataStatus.Canceled },
|
||||
},
|
||||
},
|
||||
},
|
||||
data: { quotationStatus: QuotationStatus.Canceled, urgent: false },
|
||||
}),
|
||||
data: { quotationStatus: QuotationStatus.Canceled, urgent: false },
|
||||
})
|
||||
.then(async (res) => {
|
||||
await tx.notification.createMany({
|
||||
data: res.map((v) => ({
|
||||
title: "Quotation Status Updated",
|
||||
detail: v.code + "Canceled",
|
||||
receiverId: v.createdByUserId,
|
||||
})),
|
||||
});
|
||||
}),
|
||||
tx.taskOrder.updateMany({
|
||||
where: {
|
||||
taskList: {
|
||||
|
|
@ -887,19 +929,32 @@ export class RequestListController extends Controller {
|
|||
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] },
|
||||
await tx.quotation
|
||||
.updateManyAndReturn({
|
||||
where: {
|
||||
quotationStatus: {
|
||||
notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete],
|
||||
},
|
||||
requestData: {
|
||||
every: {
|
||||
requestDataStatus: {
|
||||
in: [RequestDataStatus.Canceled, RequestDataStatus.Completed],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false },
|
||||
});
|
||||
data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false },
|
||||
})
|
||||
.then(async (res) => {
|
||||
await tx.notification.createMany({
|
||||
data: res.map((v) => ({
|
||||
title: "Quotation Status Updated",
|
||||
detail: v.code + "Completed",
|
||||
receiverId: v.createdByUserId,
|
||||
})),
|
||||
});
|
||||
});
|
||||
|
||||
return record;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -785,19 +785,31 @@ export class TaskActionController extends Controller {
|
|||
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] },
|
||||
await tx.quotation
|
||||
.updateManyAndReturn({
|
||||
where: {
|
||||
quotationStatus: {
|
||||
notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete],
|
||||
},
|
||||
requestData: {
|
||||
every: {
|
||||
requestDataStatus: {
|
||||
in: [RequestDataStatus.Canceled, RequestDataStatus.Completed],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false },
|
||||
});
|
||||
data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false },
|
||||
})
|
||||
.then(async (res) => {
|
||||
await tx.notification.createMany({
|
||||
data: res.map((v) => ({
|
||||
title: "Quotation Status Updated",
|
||||
detail: v.code + "Completed",
|
||||
receiverId: v.createdByUserId,
|
||||
})),
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue