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({
|
await tx.quotation
|
||||||
where: { id: quotation.id },
|
.update({
|
||||||
data: {
|
where: { id: quotation.id },
|
||||||
quotationStatus:
|
data: {
|
||||||
(paymentSum._sum.amount || 0) >= quotation.finalPrice
|
quotationStatus:
|
||||||
? "PaymentSuccess"
|
(paymentSum._sum.amount || 0) >= quotation.finalPrice
|
||||||
: "PaymentInProcess",
|
? "PaymentSuccess"
|
||||||
requestData: await (async () => {
|
: "PaymentInProcess",
|
||||||
if (
|
requestData: await (async () => {
|
||||||
body.paymentStatus === "PaymentSuccess" &&
|
if (
|
||||||
(paymentSum._sum.amount || 0) - payment.amount <= 0
|
body.paymentStatus === "PaymentSuccess" &&
|
||||||
) {
|
(paymentSum._sum.amount || 0) - payment.amount <= 0
|
||||||
const lastRequest = await tx.runningNo.upsert({
|
) {
|
||||||
where: {
|
const lastRequest = await tx.runningNo.upsert({
|
||||||
key: `REQUEST_${year}${month}`,
|
where: {
|
||||||
},
|
key: `REQUEST_${year}${month}`,
|
||||||
create: {
|
},
|
||||||
key: `REQUEST_${year}${month}`,
|
create: {
|
||||||
value: quotation.worker.length,
|
key: `REQUEST_${year}${month}`,
|
||||||
},
|
value: quotation.worker.length,
|
||||||
update: { value: { increment: quotation.worker.length } },
|
},
|
||||||
});
|
update: { value: { increment: quotation.worker.length } },
|
||||||
return {
|
});
|
||||||
create: quotation.worker.flatMap((v, i) => {
|
return {
|
||||||
const productEmployee = quotation.productServiceList.flatMap((item) =>
|
create: quotation.worker.flatMap((v, i) => {
|
||||||
item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1
|
const productEmployee = quotation.productServiceList.flatMap((item) =>
|
||||||
? { productServiceId: item.id }
|
item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1
|
||||||
: [],
|
? { productServiceId: item.id }
|
||||||
);
|
: [],
|
||||||
|
);
|
||||||
|
|
||||||
if (productEmployee.length <= 0) return [];
|
if (productEmployee.length <= 0) return [];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code: `TR${year}${month}${(lastRequest.value - quotation.worker.length + i + 1).toString().padStart(6, "0")}`,
|
code: `TR${year}${month}${(lastRequest.value - quotation.worker.length + i + 1).toString().padStart(6, "0")}`,
|
||||||
employeeId: v.employeeId,
|
employeeId: v.employeeId,
|
||||||
requestWork: {
|
requestWork: {
|
||||||
create: quotation.productServiceList.flatMap((item) =>
|
create: quotation.productServiceList.flatMap((item) =>
|
||||||
item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1
|
item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1
|
||||||
? { productServiceId: item.id }
|
? { 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;
|
return payment;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -454,7 +454,7 @@ export class QuotationController extends Controller {
|
||||||
|
|
||||||
const { productServiceList: _productServiceList, worker: _worker, ...rest } = body;
|
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 nonExistEmployee = body.worker.filter((v) => typeof v !== "string");
|
||||||
const lastEmployee = await tx.runningNo.upsert({
|
const lastEmployee = await tx.runningNo.upsert({
|
||||||
where: {
|
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}")
|
@Put("{quotationId}")
|
||||||
|
|
|
||||||
|
|
@ -268,14 +268,24 @@ export class RequestDataActionController extends Controller {
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
tx.quotation.updateMany({
|
tx.quotation
|
||||||
where: {
|
.updateManyAndReturn({
|
||||||
requestData: {
|
where: {
|
||||||
every: { requestDataStatus: RequestDataStatus.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({
|
tx.taskOrder.updateMany({
|
||||||
where: {
|
where: {
|
||||||
taskList: {
|
taskList: {
|
||||||
|
|
@ -405,14 +415,25 @@ export class RequestDataActionController extends Controller {
|
||||||
data: { taskStatus: TaskStatus.Canceled },
|
data: { taskStatus: TaskStatus.Canceled },
|
||||||
});
|
});
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
tx.quotation.updateMany({
|
tx.quotation
|
||||||
where: {
|
.updateManyAndReturn({
|
||||||
requestData: {
|
where: {
|
||||||
every: { requestDataStatus: RequestDataStatus.Canceled },
|
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({
|
tx.taskOrder.updateMany({
|
||||||
where: {
|
where: {
|
||||||
taskList: {
|
taskList: {
|
||||||
|
|
@ -479,21 +500,31 @@ export class RequestDataActionController extends Controller {
|
||||||
where: { id: { in: completed } },
|
where: { id: { in: completed } },
|
||||||
data: { requestDataStatus: RequestDataStatus.Completed },
|
data: { requestDataStatus: RequestDataStatus.Completed },
|
||||||
});
|
});
|
||||||
await tx.quotation.updateMany({
|
await tx.quotation
|
||||||
where: {
|
.updateManyAndReturn({
|
||||||
quotationStatus: {
|
where: {
|
||||||
notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete],
|
quotationStatus: {
|
||||||
},
|
notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete],
|
||||||
requestData: {
|
},
|
||||||
every: {
|
requestData: {
|
||||||
requestDataStatus: {
|
every: {
|
||||||
in: [RequestDataStatus.Canceled, RequestDataStatus.Completed],
|
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);
|
// dataRecord.push(record);
|
||||||
return data;
|
return data;
|
||||||
});
|
});
|
||||||
|
|
@ -812,14 +843,25 @@ export class RequestListController extends Controller {
|
||||||
data: { taskStatus: TaskStatus.Canceled },
|
data: { taskStatus: TaskStatus.Canceled },
|
||||||
});
|
});
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
tx.quotation.updateMany({
|
tx.quotation
|
||||||
where: {
|
.updateManyAndReturn({
|
||||||
requestData: {
|
where: {
|
||||||
every: { requestDataStatus: RequestDataStatus.Canceled },
|
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({
|
tx.taskOrder.updateMany({
|
||||||
where: {
|
where: {
|
||||||
taskList: {
|
taskList: {
|
||||||
|
|
@ -887,19 +929,32 @@ export class RequestListController extends Controller {
|
||||||
where: { id: { in: completed } },
|
where: { id: { in: completed } },
|
||||||
data: { requestDataStatus: RequestDataStatus.Completed },
|
data: { requestDataStatus: RequestDataStatus.Completed },
|
||||||
});
|
});
|
||||||
await tx.quotation.updateMany({
|
await tx.quotation
|
||||||
where: {
|
.updateManyAndReturn({
|
||||||
quotationStatus: {
|
where: {
|
||||||
notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete],
|
quotationStatus: {
|
||||||
},
|
notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete],
|
||||||
requestData: {
|
},
|
||||||
every: {
|
requestData: {
|
||||||
requestDataStatus: { in: [RequestDataStatus.Canceled, RequestDataStatus.Completed] },
|
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;
|
return record;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -785,19 +785,31 @@ export class TaskActionController extends Controller {
|
||||||
where: { id: { in: completed } },
|
where: { id: { in: completed } },
|
||||||
data: { requestDataStatus: RequestDataStatus.Completed },
|
data: { requestDataStatus: RequestDataStatus.Completed },
|
||||||
});
|
});
|
||||||
await tx.quotation.updateMany({
|
await tx.quotation
|
||||||
where: {
|
.updateManyAndReturn({
|
||||||
quotationStatus: {
|
where: {
|
||||||
notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete],
|
quotationStatus: {
|
||||||
},
|
notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete],
|
||||||
requestData: {
|
},
|
||||||
every: {
|
requestData: {
|
||||||
requestDataStatus: { in: [RequestDataStatus.Canceled, RequestDataStatus.Completed] },
|
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