fix(notify): fixed to plantto plant that would cause multiple emails to be sent and never update
This commit is contained in:
@@ -7,15 +7,27 @@ import { query } from "../../../sqlServer/prodSqlServer.js";
|
||||
import { sqlQuerySelector } from "../../../sqlServer/utils/querySelector.utils.js";
|
||||
import { sendEmail } from "../sendMail.js";
|
||||
|
||||
let running = false;
|
||||
export default async function platToPlantEdi(notifyData: any) {
|
||||
createLog("info", "blocking", "notify", `monitoring ${notifyData.name}`);
|
||||
createLog("info", "plantToPlant", "notify", `monitoring ${notifyData.name}`);
|
||||
if (running) {
|
||||
createLog(
|
||||
"info",
|
||||
"plantToPlant",
|
||||
"notify",
|
||||
`Notifcation ${notifyData.name} is already running skipping`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const { data: noti, error: notiError } = await tryCatch(
|
||||
running = true;
|
||||
|
||||
const { data: noti, error: notiError } = (await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(notifications)
|
||||
.where(eq(notifications.name, notifyData.name)),
|
||||
);
|
||||
)) as any;
|
||||
|
||||
if (notiError) {
|
||||
createLog(
|
||||
@@ -27,7 +39,7 @@ export default async function platToPlantEdi(notifyData: any) {
|
||||
}
|
||||
|
||||
// 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 ?? "";
|
||||
let emails = noti[0]?.email ?? "";
|
||||
|
||||
const checkBol = sqlQuerySelector("checkBol.query");
|
||||
|
||||
@@ -41,14 +53,21 @@ export default async function platToPlantEdi(notifyData: any) {
|
||||
createLog("error", "edi", "notify", "Error in getting the bol query data");
|
||||
}
|
||||
|
||||
let ignoreBols: string[] = notifyData?.notifiySettings?.processedBol ?? [];
|
||||
let ignoreBols: string[] = noti[0]?.notifiySettings?.processedBol ?? [];
|
||||
|
||||
const joinBols = ignoreBols.join(",");
|
||||
|
||||
let updateQuery = noti[0]?.notifiySettings?.includeAll
|
||||
? checkBol?.query?.replace(
|
||||
"and a.bezeichnung like '%Alpla%'",
|
||||
"--and a.bezeichnung like '%Alpla%'",
|
||||
)
|
||||
: checkBol?.query;
|
||||
|
||||
const { data: b, error: bError } = (await tryCatch(
|
||||
query(
|
||||
checkBol?.query
|
||||
?.replace("[timeCheck]", notifyData.checkInterval ?? "30")
|
||||
updateQuery
|
||||
?.replace("[timeCheck]", noti[0]?.checkInterval ?? "30")
|
||||
.replace("[ignoreBols]", joinBols ?? 500) ?? "",
|
||||
"Check bol",
|
||||
),
|
||||
@@ -99,7 +118,6 @@ export default async function platToPlantEdi(notifyData: any) {
|
||||
|
||||
// console.log("Address: ", b.data[0].addressId ?? "0");
|
||||
|
||||
// if theres no email then just stop.
|
||||
if (b.data[0].addressId === "") return;
|
||||
|
||||
ignoreBols.push(bolNumber);
|
||||
@@ -120,9 +138,10 @@ export default async function platToPlantEdi(notifyData: any) {
|
||||
),
|
||||
)) as any;
|
||||
|
||||
if (noti[0]?.emails === "") return; // no default emails
|
||||
// setup the email to be sent :D
|
||||
const emailSetup = {
|
||||
email: address.data[0].email,
|
||||
email: `${noti[0]?.emails};${address.data[0].email ?? ""}`,
|
||||
subject: `New EDI transfer Created for BOL: ${bolNumber}`,
|
||||
template: "plantToPlantEdi",
|
||||
context: {
|
||||
@@ -140,18 +159,22 @@ export default async function platToPlantEdi(notifyData: any) {
|
||||
.update(notifications)
|
||||
.set({
|
||||
lastRan: sql`NOW()`,
|
||||
notifiySettings: { processedBol: ignoreBols },
|
||||
notifiySettings: {
|
||||
...noti[0]?.notifiySettings,
|
||||
processedBol: ignoreBols,
|
||||
},
|
||||
})
|
||||
.where(eq(notifications.name, notifyData.name));
|
||||
}
|
||||
|
||||
running = false;
|
||||
return {
|
||||
success: true,
|
||||
message: "All bols have been processed",
|
||||
data: [ignoreBols],
|
||||
};
|
||||
}
|
||||
|
||||
running = false;
|
||||
return {
|
||||
success: true,
|
||||
message: "No new bols have been created",
|
||||
|
||||
Reference in New Issue
Block a user