feat(notifications): migrated all from v1
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
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";
|
||||
|
||||
export interface Labels {
|
||||
IdEtikettenHistorie?: number;
|
||||
}
|
||||
const notification = async (notifyData: any) => {
|
||||
/**
|
||||
* Pass the entire notification over
|
||||
*/
|
||||
createLog("info", "notify", "notify", `monitoring ${notifyData.name}`);
|
||||
|
||||
// validate if there are any emails.
|
||||
if (notifyData.emails === "") {
|
||||
createLog(
|
||||
"error",
|
||||
"notify",
|
||||
"notify",
|
||||
`There are no emails set for ${notifyData.name}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// well set a backup default time here
|
||||
let timeCheck = `DATEADD(SECOND, -30, getdate()) `;
|
||||
|
||||
// set the time of getting the label
|
||||
if (notifyData.timeType === "sec") {
|
||||
timeCheck = `DATEADD(SECOND, -${notifyData.checkTime}, getdate()) `;
|
||||
} else if (notifyData.timeType === "min") {
|
||||
timeCheck = `DATEADD(MINUTE, -${notifyData.checkTime}, getdate()) `;
|
||||
}
|
||||
|
||||
let reprintQuery = `
|
||||
SELECT
|
||||
IdArtikelvarianten as av,
|
||||
ArtikelVariantenBez as alias,
|
||||
LfdNr as runningNumber,
|
||||
CONVERT(VARCHAR, CAST(Add_Date AS DATETIME), 100) Add_Date,
|
||||
Add_User,
|
||||
CONVERT(VARCHAR, CAST(Upd_Date AS DATETIME), 100) Upd_Date,
|
||||
Upd_User,
|
||||
EtikettenDruckerBezeichnung as printer,
|
||||
AnzahlGedruckterKopien as totalPrinted
|
||||
FROM Alplaprod_test1.dbo.V_EtikettenGedruckt (nolock)
|
||||
where AnzahlGedruckterKopien > 2
|
||||
and CONVERT(varchar(5), Add_Date,108) not like CONVERT(varchar(5), Upd_Date,108)
|
||||
and Upd_Date > DATEADD(SECOND, -30, getdate())
|
||||
and VpkVorschriftBez not like '%$%'
|
||||
`;
|
||||
|
||||
//update the time check
|
||||
reprintQuery = reprintQuery.replaceAll(
|
||||
"DATEADD(SECOND, -30, getdate()) ",
|
||||
timeCheck
|
||||
);
|
||||
|
||||
//let labels: Labels[];
|
||||
|
||||
const { data: labels, error: labelError } = await tryCatch(
|
||||
query(reprintQuery, "Label Reprints")
|
||||
);
|
||||
|
||||
if (labels.length > 0) {
|
||||
//send the email :D
|
||||
const emailSetup = {
|
||||
emailTo: notifyData.emails,
|
||||
subject: "Alert! Label Reprinted",
|
||||
template: "reprintLabels",
|
||||
context: {
|
||||
items: labels,
|
||||
},
|
||||
};
|
||||
|
||||
const sentEmail = await sendEmail(emailSetup);
|
||||
|
||||
if (!sentEmail.success) {
|
||||
createLog(
|
||||
"error",
|
||||
"notify",
|
||||
"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 updateSettings = notifyData.notifiySettings;
|
||||
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.update(notifications)
|
||||
.set({
|
||||
lastRan: sql`NOW()`,
|
||||
notifiySettings: {
|
||||
...updateSettings,
|
||||
prodID: labels[0].IdEtikettenHistorie,
|
||||
},
|
||||
})
|
||||
.where(eq(notifications.name, notifyData.name))
|
||||
);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
export default notification;
|
||||
Reference in New Issue
Block a user