fix(notify): fixed to plantto plant that would cause multiple emails to be sent and never update

This commit is contained in:
2026-03-11 15:30:53 -05:00
parent ea92422bb1
commit dcfa56bdb9
7 changed files with 46 additions and 21 deletions

View File

@@ -7,15 +7,27 @@ import { query } from "../../../sqlServer/prodSqlServer.js";
import { sqlQuerySelector } from "../../../sqlServer/utils/querySelector.utils.js"; import { sqlQuerySelector } from "../../../sqlServer/utils/querySelector.utils.js";
import { sendEmail } from "../sendMail.js"; import { sendEmail } from "../sendMail.js";
let running = false;
export default async function platToPlantEdi(notifyData: any) { 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 db
.select() .select()
.from(notifications) .from(notifications)
.where(eq(notifications.name, notifyData.name)), .where(eq(notifications.name, notifyData.name)),
); )) as any;
if (notiError) { if (notiError) {
createLog( 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 // 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"); 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"); 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(","); 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( const { data: b, error: bError } = (await tryCatch(
query( query(
checkBol?.query updateQuery
?.replace("[timeCheck]", notifyData.checkInterval ?? "30") ?.replace("[timeCheck]", noti[0]?.checkInterval ?? "30")
.replace("[ignoreBols]", joinBols ?? 500) ?? "", .replace("[ignoreBols]", joinBols ?? 500) ?? "",
"Check bol", "Check bol",
), ),
@@ -99,7 +118,6 @@ export default async function platToPlantEdi(notifyData: any) {
// console.log("Address: ", b.data[0].addressId ?? "0"); // console.log("Address: ", b.data[0].addressId ?? "0");
// if theres no email then just stop.
if (b.data[0].addressId === "") return; if (b.data[0].addressId === "") return;
ignoreBols.push(bolNumber); ignoreBols.push(bolNumber);
@@ -120,9 +138,10 @@ export default async function platToPlantEdi(notifyData: any) {
), ),
)) as any; )) as any;
if (noti[0]?.emails === "") return; // no default emails
// setup the email to be sent :D // setup the email to be sent :D
const emailSetup = { const emailSetup = {
email: address.data[0].email, email: `${noti[0]?.emails};${address.data[0].email ?? ""}`,
subject: `New EDI transfer Created for BOL: ${bolNumber}`, subject: `New EDI transfer Created for BOL: ${bolNumber}`,
template: "plantToPlantEdi", template: "plantToPlantEdi",
context: { context: {
@@ -140,18 +159,22 @@ export default async function platToPlantEdi(notifyData: any) {
.update(notifications) .update(notifications)
.set({ .set({
lastRan: sql`NOW()`, lastRan: sql`NOW()`,
notifiySettings: { processedBol: ignoreBols }, notifiySettings: {
...noti[0]?.notifiySettings,
processedBol: ignoreBols,
},
}) })
.where(eq(notifications.name, notifyData.name)); .where(eq(notifications.name, notifyData.name));
} }
running = false;
return { return {
success: true, success: true,
message: "All bols have been processed", message: "All bols have been processed",
data: [ignoreBols], data: [ignoreBols],
}; };
} }
running = false;
return { return {
success: true, success: true,
message: "No new bols have been created", message: "No new bols have been created",

View File

@@ -170,9 +170,9 @@ export const note: any = [
"This is the plant to plant edi that will send an edi to the email once it ships, the emails will be for the receiving plants", "This is the plant to plant edi that will send an edi to the email once it ships, the emails will be for the receiving plants",
checkInterval: 15, checkInterval: 15,
timeType: "min", timeType: "min",
emails: "", emails: "blake.matthes@alpla.com;Maritza.Hernandez@alpla.com",
active: false, active: false,
notifiySettings: { processedBol: [500] }, notifiySettings: { processedBol: [500], includeAll: false },
}, },
]; ];

View File

@@ -58,6 +58,7 @@ const jobNames: string[] = [
"invenotry check", // spelling error one of my stupids "invenotry check", // spelling error one of my stupids
"monitor_hold_monitor", "monitor_hold_monitor",
"Monitor_Silo_adjustments", "Monitor_Silo_adjustments",
"monitor_qualityLocMonitor", // validating with lima this is still needed
]; ];
export const sqlJobCleanUp = async () => { export const sqlJobCleanUp = async () => {

View File

@@ -9,9 +9,9 @@
<body> <body>
<p>All,</p> <p>All,</p>
<p>BOL: {{bol}} was created with the below pallets.</p> <p>BOL: {{bol}} was created with the below pallets.</p>
<p>Please head to stock and import the pallets via the normal incoming goods process.</p> <p>Please head to stock and import the pallets via the normal incoming goods process (now/immediately).</p>
<p>When encountering a discrepancy in pallets/cages received, please correct this after the pallets have been imported.</p> <p>When encountering a discrepancy in pallets/cages received, please correct this after the pallets have been imported.</p>
<p>This due to these being plant to plant the only way to correct this is bring them in then undo the incoming goods process.</p> <p>Due to these being plant to plant shipments, the only way to correct this is to bring them in then undo the incoming goods process.</p>
<br></br> <br></br>
<table > <table >
<thead> <thead>

View File

@@ -1,4 +1,4 @@
use [test3_AlplaPROD2.0_Read] use [test1_AlplaPROD2.0_Read]
select select
humanreadableId as addressId humanreadableId as addressId

View File

@@ -36,8 +36,8 @@ x.idjournal = e.idjournal
where idjournalStatus = 62 where idjournalStatus = 62
--and idadressen = 270 --and idadressen = 270
--and a.bezeichnung like '%Alpla%' -- we only want to monitor for addresses that are linked to alpla. and a.bezeichnung like '%Alpla%' -- we only want to monitor for addresses that are linked to alpla.
and JournalDatum > DATEADD(MINUTE, -[timeCheck], GETDATE()) and JournalDatum > DATEADD(MINUTE, -[timeCheck], GETDATE())
and e.idjournal not in ([ignoreBols]) and e.journalNummer not in ([ignoreBols])
and idauftrag > 1 -- this will ignore all incoming goodsv as we are really only looking for outbound deliveries and idauftrag > 1 -- this will ignore all incoming goodsv as we are really only looking for outbound deliveries
order by JournalDatum desc order by JournalDatum desc

View File

@@ -17,7 +17,7 @@ use AlplaPROD_test1
,av.articlehumanreadableid as article ,av.articlehumanreadableid as article
,av.ArticleDescription as alias ,av.ArticleDescription as alias
--,[SSCC_ReserveZiffer] --,[SSCC_ReserveZiffer]
,ROW_NUMBER() OVER (PARTITION BY p.[LfdNrJeArtikelKunde] ORDER BY p.upd_date DESC) AS RowNum --,ROW_NUMBER() OVER (PARTITION BY p.[LfdNrJeArtikelKunde] ORDER BY p.upd_date DESC) AS RowNum
--,* --,*
FROM [dbo].[T_EAIJournalLieferPosition] as p (nolock) FROM [dbo].[T_EAIJournalLieferPosition] as p (nolock)
@@ -27,10 +27,11 @@ use AlplaPROD_test1
-- l.IdProdPlanung = p.Beleg -- l.IdProdPlanung = p.Beleg
left join left join
[test3_AlplaPROD2.0_Read].labelling.InternalLabel as av on [test1_AlplaPROD2.0_Read].labelling.InternalLabel as av on
av.RunningNumber = p.[LfdNrJeArtikelKunde] av.RunningNumber = p.[LfdNrJeArtikelKunde]
) as a ) as a
where idladeplanung in ([palLinkedToBol]) and RowNum = 1 where idladeplanung in ([palLinkedToBol])
--and RowNum = 1
order by runningNr order by runningNr