feat(notifcations): pallets booked as waste brought back in via cycle count

This commit is contained in:
2025-06-20 11:18:13 -05:00
parent 7df512acaa
commit 095d724e65
2 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
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 { sendEmail } from "../sendMail.js";
import { palletsRemovedAswaste } from "../../../sqlServer/querys/notifications/palletsRemovedAsWaste.js";
import { format } from "date-fns-tz";
export interface Labels {
IdEtikettenHistorie?: number;
}
const notification = async (notifyData: any) => {
/**
* Pass the entire notification over
*/
createLog(
"info",
"wastebooking",
"notify",
`monitoring ${notifyData.name}`
);
// validate if there are any emails.
if (notifyData.emails === "") {
createLog(
"error",
"reprinting",
"notify",
`There are no emails set for ${notifyData.name}`
);
return;
}
const { data: l, error: palletError } = await tryCatch(
query(palletsRemovedAswaste, "Removed as waste check")
);
const pallets: any = l?.data as any;
if (palletError) {
createLog(
"error",
"reprinting",
"notify",
`Failed to get the labels: ${palletError}`
);
return;
}
console.log(pallets);
if (pallets.length > 0) {
//send the email :D
const emailSetup = {
email: notifyData.emails,
subject: `Alert! ${
pallets.length > 1 ? "Some pallets were" : "A pallet was "
} brought back in`,
template: "palletBookedAsWaste",
context: {
items: pallets.map((i: any) => {
return {
...i,
lastMovingDate: format(i.lastMovingDate, "M/d/yyyy"),
};
}),
},
};
const sentEmail = await sendEmail(emailSetup);
if (!sentEmail.success) {
createLog(
"error",
"reprinting",
"notify",
"Failed to send email, will try again on next interval"
);
return;
}
// // update the last time we ran and the prod id
// const notifUpdate = {
// prodID: labels[0].IdEtikettenHistorie,
// lastRan: nowDate(),
// };
// update the last time ran
const { data, error } = await tryCatch(
db
.update(notifications)
.set({
lastRan: sql`NOW()`,
notifiySettings: {
...notifyData.notifiySettings,
prodID: pallets[0].runningnumber,
},
})
.where(eq(notifications.name, notifyData.name))
);
} else {
return;
}
};
export default notification;

View File

@@ -0,0 +1,17 @@
export const palletsRemovedAswaste = `
select * from (select IdArtikelVarianten as av
,ArtikelVariantenAlias as alias
,Lfdnr as runningnumber
,case when GesperrtAktivSum = 1 then 'Blocked' else 'Released' end as palletStatus
,BewegungsDatumMax as lastMovingDate
--,*
from AlplaPROD_test1.dbo.V_LagerPositionenBarcodes (nolock) )x
where runningnumber in (
SELECT
[HumanReadableId]
FROM [test1_AlplaPROD2.0_Reporting].[reporting_blocking].[BlockedItem] (nolock)
where state = 4
) and palletStatus = 'Released'
`;