diff --git a/server/services/notifications/controller/notifications/bow2henkelincoming.ts b/server/services/notifications/controller/notifications/bow2henkelincoming.ts new file mode 100644 index 0000000..b965352 --- /dev/null +++ b/server/services/notifications/controller/notifications/bow2henkelincoming.ts @@ -0,0 +1,98 @@ +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 { bow2incoming } from "../../../sqlServer/querys/notifications/bow2henkel.js"; + +const notification = async (notifyData: any) => { + /** + * Pass the entire notification over + */ + createLog("debug", "reprinting", "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; + } + + //let labels: Labels[]; + + const { data: l, error: labelError } = await tryCatch( + query( + bow2incoming.replace( + "[time]", + notifyData.notifiySettings.processTime + ), + "Label Reprints" + ) + ); + const labels: any = l?.data as any; + if (labelError) { + createLog( + "error", + "reprinting", + "notify", + `Failed to get the labels: ${labelError}` + ); + return; + } + + if (labels.length > 0) { + //send the email :D + const emailSetup = { + email: notifyData.emails, + subject: "Alert! New incoming goods has been received", + template: "bow2IncomingGoods", + context: { + items: labels, + time: notifyData.notifiySettings.processTime, + }, + }; + + 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: labels[0].IdEtikettenHistorie, + }, + }) + .where(eq(notifications.name, notifyData.name)) + ); + } else { + return; + } +}; + +export default notification; diff --git a/server/services/notifications/utils/masterNotifications.ts b/server/services/notifications/utils/masterNotifications.ts index ab302d2..edd6057 100644 --- a/server/services/notifications/utils/masterNotifications.ts +++ b/server/services/notifications/utils/masterNotifications.ts @@ -107,6 +107,16 @@ export const note: any = [ releases: [1, 2, 3], }, }, + { + name: "bow2henkelincoming", + description: + "Checks for new incoming goods orders to be completed and sends an email for what truck and carrier it was", + checkInterval: 15, + timeType: "min", + emails: "blake.matthes@alpla.com", + active: false, + notifiySettings: { processTime: 15 }, + }, ]; export const notificationCreate = async () => { diff --git a/server/services/notifications/utils/views/bow2IncomingGoods.hbs b/server/services/notifications/utils/views/bow2IncomingGoods.hbs new file mode 100644 index 0000000..29af25a --- /dev/null +++ b/server/services/notifications/utils/views/bow2IncomingGoods.hbs @@ -0,0 +1,38 @@ + + +
+ + + {{!-- --}} + {{> styles}} + + +All,
+New incomings goods have been received in the last {{time}}min.
+| Truck Number | +Carrier | +Add Date | +
|---|---|---|
| {{truckNumber}} | +{{carrier}} | +{{Add_Date}} | +
Thank you,
+LST Team
+