161 lines
4.1 KiB
TypeScript
161 lines
4.1 KiB
TypeScript
import { eq, sql } from "drizzle-orm";
|
|
import { db } from "../../../../../database/dbclient.js";
|
|
import { notifications } from "../../../../../database/schema/notifications.js";
|
|
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
|
import { createLog } from "../../../logger/logger.js";
|
|
import { query } from "../../../sqlServer/prodSqlServer.js";
|
|
import { sqlQuerySelector } from "../../../sqlServer/utils/querySelector.utils.js";
|
|
import { sendEmail } from "../sendMail.js";
|
|
|
|
export default async function platToPlantEdi(notifyData: any) {
|
|
createLog("info", "blocking", "notify", `monitoring ${notifyData.name}`);
|
|
|
|
const { data: noti, error: notiError } = await tryCatch(
|
|
db
|
|
.select()
|
|
.from(notifications)
|
|
.where(eq(notifications.name, notifyData.name)),
|
|
);
|
|
|
|
if (notiError) {
|
|
createLog(
|
|
"error",
|
|
"edi",
|
|
"notify",
|
|
"Error in getting the notification data",
|
|
);
|
|
}
|
|
|
|
// get the default emails they can be blank if as we will only add these to the end of the email from the full flow
|
|
let emails = notifyData.email ?? "";
|
|
|
|
const checkBol = sqlQuerySelector("checkBol.query");
|
|
|
|
if (!checkBol.success) {
|
|
createLog("error", "edi", "notify", "Error in getting the bol query data");
|
|
}
|
|
|
|
const pLinkedB = sqlQuerySelector("palletsLinkedToBol.query");
|
|
|
|
if (!pLinkedB.success) {
|
|
createLog("error", "edi", "notify", "Error in getting the bol query data");
|
|
}
|
|
|
|
let ignoreBols: string[] = notifyData?.notifiySettings?.processedBol ?? [];
|
|
|
|
const joinBols = ignoreBols.join(",");
|
|
|
|
const { data: b, error: bError } = (await tryCatch(
|
|
query(
|
|
checkBol?.query
|
|
?.replace("[timeCheck]", notifyData.checkInterval ?? "30")
|
|
.replace("[ignoreBols]", joinBols ?? 500) ?? "",
|
|
"Check bol",
|
|
),
|
|
)) as any;
|
|
|
|
if (bError) {
|
|
return {
|
|
success: false,
|
|
message: "Error getting newly created bols",
|
|
data: bError,
|
|
};
|
|
}
|
|
|
|
const planedByBol = new Map<string, string[]>();
|
|
|
|
for (const row of b.data) {
|
|
if (!planedByBol.has(row.bol)) {
|
|
planedByBol.set(row.bol, []);
|
|
}
|
|
|
|
planedByBol.get(row.bol)!.push(String(row.idladeplanung));
|
|
}
|
|
|
|
if (b.data.length > 0) {
|
|
// loop each bol in the system and get the bols only
|
|
for (const [bolNumber, idList] of planedByBol.entries()) {
|
|
//for (const bol of b.data) {
|
|
// run the process to get the the pallet numbers
|
|
|
|
const joinedIdLadeplanung = idList.join(",");
|
|
|
|
//console.log("BOL:", bolNumber);
|
|
//console.log("IDLadeplanung string:", joinedIdLadeplanung);
|
|
//console.log("IgnoreBols: ", joinBols);
|
|
|
|
// now get the pallets that are witing the ladeplanning
|
|
const { data: pallets, error: pError } = await tryCatch(
|
|
query(
|
|
pLinkedB?.query?.replace(
|
|
"[palLinkedToBol]",
|
|
joinedIdLadeplanung ?? "0",
|
|
) ?? "",
|
|
"Get Pallets linked in the bol",
|
|
),
|
|
);
|
|
|
|
//console.log(pallets);
|
|
|
|
// console.log("Address: ", b.data[0].addressId ?? "0");
|
|
|
|
// if theres no email then just stop.
|
|
if (b.data[0].addressId === "") return;
|
|
|
|
ignoreBols.push(bolNumber);
|
|
if (ignoreBols.length > 15) {
|
|
ignoreBols.splice(0, ignoreBols.length - 15);
|
|
}
|
|
|
|
// get the email address.
|
|
const checkBol = sqlQuerySelector("addressInfo.query");
|
|
|
|
const { data: address, error: aError } = (await tryCatch(
|
|
query(
|
|
checkBol?.query?.replace(
|
|
"[customerAddress]",
|
|
b.data[0].addressId ?? "0",
|
|
) ?? "",
|
|
"Get Pallets linked in the bol",
|
|
),
|
|
)) as any;
|
|
|
|
// setup the email to be sent :D
|
|
const emailSetup = {
|
|
email: address.data[0].email,
|
|
subject: `New EDI transfer Created for BOL: ${bolNumber}`,
|
|
template: "plantToPlantEdi",
|
|
context: {
|
|
items: pallets?.data ?? [],
|
|
bol: bolNumber,
|
|
//secondarySetting: notifyData.notifiySettings,
|
|
},
|
|
};
|
|
|
|
// send the email
|
|
await sendEmail(emailSetup);
|
|
|
|
// add the bols to be ignored
|
|
await db
|
|
.update(notifications)
|
|
.set({
|
|
lastRan: sql`NOW()`,
|
|
notifiySettings: { processedBol: ignoreBols },
|
|
})
|
|
.where(eq(notifications.name, notifyData.name));
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
message: "All bols have been processed",
|
|
data: [ignoreBols],
|
|
};
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
message: "No new bols have been created",
|
|
data: [],
|
|
};
|
|
}
|