change detail notification task
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 5s

This commit is contained in:
Kanjana 2025-05-14 14:18:08 +07:00
parent 897ef335b4
commit 0aa20d3728
2 changed files with 112 additions and 58 deletions

View file

@ -614,12 +614,19 @@ export class TaskActionController extends Controller {
request: { request: {
include: { include: {
quotation: true, quotation: true,
employee: true,
},
},
productService: {
include: {
product: true,
}, },
}, },
}, },
}, },
}, },
}, },
taskOrder: true,
}, },
where: { where: {
step: v.step, step: v.step,
@ -640,14 +647,18 @@ export class TaskActionController extends Controller {
} }
if (v.taskStatus === TaskStatus.Failed) { if (v.taskStatus === TaskStatus.Failed) {
const code = record.requestWorkStep.requestWork.request.quotation.code; const taskCode = record.taskOrder.code;
const name = record.requestWorkStep.requestWork.request.quotation.workName; const taskName = record.taskOrder.taskName;
const productCode = record.requestWorkStep.requestWork.productService.product.code;
const productName = record.requestWorkStep.requestWork.productService.product.name;
const employeeName = `${record.requestWorkStep.requestWork.request.employee.namePrefix}.${record.requestWorkStep.requestWork.request.employee.firstNameEN} ${record.requestWorkStep.requestWork.request.employee.lastNameEN}`;
await tx.notification.create({ await tx.notification.create({
data: { data: {
title: "ใบรายการคำขอที่จัดการเกิดปัญหา / Task Failed", title: "ใบรายการคำขอที่จัดการเกิดปัญหา / Task Failed",
detail: `ใบรายการคำขอรหัส / code : ${code} - ${name} ใบรายการคำขอเกิดปัญหา`, detail: `ใบรายการคำขอรหัส ${taskCode}: ${taskName} รหัสสินค้า ${productCode}: ${productName} ของลูกจ้าง ${employeeName} เกิดข้อผิดพลาด`,
receiverId: record.requestWorkStep.requestWork.request.quotation.updatedByUserId, groupReceiver: { create: { name: "document_checker" } },
registeredBranchId: record.taskOrder.registeredBranchId,
}, },
}); });
} }

View file

@ -2,6 +2,7 @@ import dayjs from "dayjs";
import { CronJob } from "cron"; import { CronJob } from "cron";
import prisma from "../db"; import prisma from "../db";
import { Prisma } from "@prisma/client";
const jobs = [ const jobs = [
CronJob.from({ CronJob.from({
@ -55,6 +56,15 @@ const jobs = [
customer: true, customer: true,
}, },
}, },
quotationWorker: {
include: {
quotation: true,
},
orderBy: {
createdAt: "desc",
},
take: 1,
},
}, },
where: { where: {
employeePassport: { employeePassport: {
@ -65,8 +75,9 @@ const jobs = [
}, },
}); });
await employeeExpireData.map(async (record) => { await Promise.all(
const fullName = `${record.namePrefix}${record.firstNameEN} ${record.lastNameEN}`; employeeExpireData.map(async (record) => {
const fullName = `${record.namePrefix}.${record.firstNameEN} ${record.lastNameEN}`;
const expireDate = `${dayjs(record.employeePassport[0].expireDate).format("DD/MM")}/${dayjs(record.employeePassport[0].expireDate).year() + 543}`; const expireDate = `${dayjs(record.employeePassport[0].expireDate).format("DD/MM")}/${dayjs(record.employeePassport[0].expireDate).year() + 543}`;
const textDetail = `ลูกจ้างรหัส / code : ${record.code} ชื่อ : ${fullName} หนังสือเดินทางจะหมดอายุในวันที่ ${expireDate}`; const textDetail = `ลูกจ้างรหัส / code : ${record.code} ชื่อ : ${fullName} หนังสือเดินทางจะหมดอายุในวันที่ ${expireDate}`;
const duplicateText = await prisma.notification.findFirst({ const duplicateText = await prisma.notification.findFirst({
@ -75,24 +86,35 @@ const jobs = [
}, },
}); });
const dataNotification: Prisma.NotificationCreateArgs["data"] = {
title: "หนังสือเดินทางลูกจ้างหมดอายุ / Employee Passport Expire",
detail: textDetail,
};
if (record.quotationWorker && record.quotationWorker.length > 0) {
dataNotification.receiverId = record.quotationWorker[0].quotation.updatedByUserId;
dataNotification.registeredBranchId =
record.quotationWorker[0].quotation.registeredBranchId;
} else {
(dataNotification.groupReceiver = {
create: [{ name: "sale" }, { name: "head_of_sale" }],
}),
(dataNotification.registeredBranchId =
record.customerBranch.customer.registeredBranchId);
}
if (!duplicateText) { if (!duplicateText) {
await prisma.notification await prisma.notification
.create({ .create({
data: { data: dataNotification,
title: "หนังสือเดินทางลูกจ้างหมดอายุ / Employee Passport Expire",
detail: textDetail,
groupReceiver: {
create: [{ name: "sale" }, { name: "head_of_sale" }],
},
registeredBranchId: record.customerBranch.customer.registeredBranchId,
},
}) })
.then(() => console.log("[INFO]: Create notification employee passport expired, OK.")) .then(() => console.log("[INFO]: Create notification employee passport expired, OK."))
.catch((e) => .catch((e) =>
console.error("[ERR]: Create notification employee passport expired, FAILED.", e), console.error("[ERR]: Create notification employee passport expired, FAILED.", e),
); );
} }
}); }),
);
}, },
}), }),
CronJob.from({ CronJob.from({
@ -112,6 +134,15 @@ const jobs = [
customer: true, customer: true,
}, },
}, },
quotationWorker: {
include: {
quotation: true,
},
orderBy: {
createdAt: "desc",
},
take: 1,
},
}, },
where: { where: {
employeeVisa: { employeeVisa: {
@ -122,8 +153,9 @@ const jobs = [
}, },
}); });
await employeeVisaData.map(async (record) => { await Promise.all(
const fullName = `${record.namePrefix}${record.firstNameEN} ${record.lastNameEN}`; employeeVisaData.map(async (record) => {
const fullName = `${record.namePrefix}.${record.firstNameEN} ${record.lastNameEN}`;
const expireDate = `${dayjs(record.employeeVisa[0].expireDate).format("DD/MM")}/${dayjs(record.employeeVisa[0].expireDate).year() + 543}`; const expireDate = `${dayjs(record.employeeVisa[0].expireDate).format("DD/MM")}/${dayjs(record.employeeVisa[0].expireDate).year() + 543}`;
const textDetail = `ลูกจ้างรหัส / code : ${record.code} ชื่อ : ${fullName} ข้อมูลการตรวจลงตราจะหมดอายุในวันที่ ${expireDate}`; const textDetail = `ลูกจ้างรหัส / code : ${record.code} ชื่อ : ${fullName} ข้อมูลการตรวจลงตราจะหมดอายุในวันที่ ${expireDate}`;
const duplicateText = await prisma.notification.findFirst({ const duplicateText = await prisma.notification.findFirst({
@ -132,24 +164,35 @@ const jobs = [
}, },
}); });
const dataNotification: Prisma.NotificationCreateArgs["data"] = {
title: "ข้อมูลการตรวจลงตราลูกจ้างหมดอายุ / Employee Visa Expire",
detail: textDetail,
};
if (record.quotationWorker && record.quotationWorker.length > 0) {
dataNotification.receiverId = record.quotationWorker[0].quotation.updatedByUserId;
dataNotification.registeredBranchId =
record.quotationWorker[0].quotation.registeredBranchId;
} else {
(dataNotification.groupReceiver = {
create: [{ name: "sale" }, { name: "head_of_sale" }],
}),
(dataNotification.registeredBranchId =
record.customerBranch.customer.registeredBranchId);
}
if (!duplicateText) { if (!duplicateText) {
await prisma.notification await prisma.notification
.create({ .create({
data: { data: dataNotification,
title: "ข้อมูลการตรวจลงตราลูกจ้างหมดอายุ / Employee Visa Expire",
detail: textDetail,
groupReceiver: {
create: [{ name: "sale" }, { name: "head_of_sale" }],
},
registeredBranchId: record.customerBranch.customer.registeredBranchId,
},
}) })
.then(() => console.log("[INFO]: Create notification employee visa expired, OK.")) .then(() => console.log("[INFO]: Create notification employee visa expired, OK."))
.catch((e) => .catch((e) =>
console.error("[ERR]: Create notification employee visa expired, FAILED.", e), console.error("[ERR]: Create notification employee visa expired, FAILED.", e),
); );
} }
}); }),
);
}, },
}), }),
]; ];