159 lines
6.4 KiB
TypeScript
159 lines
6.4 KiB
TypeScript
// SELECT count(*) FROM V_EtikettenGedruckt 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 '%$%'
|
|
|
|
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 Blocking {
|
|
HumanReadableId?: number;
|
|
subject?: string;
|
|
}
|
|
export default async function qualityBlockingMonitor(notifyData: any) {
|
|
createLog("info", "notify", "notify", `monitoring ${notifyData.name}`);
|
|
if (notifyData.emails === "") {
|
|
createLog(
|
|
"error",
|
|
"notify",
|
|
"notify",
|
|
`There are no emails set for ${notifyData.name}`
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
let blockQuery = `
|
|
SELECT
|
|
'Alert! new blocking order: #' + cast(HumanReadableId as varchar) + ' - ' + ArticleVariantDescription as subject,
|
|
cast([HumanReadableId] as varchar) as blockingNumber,
|
|
[ArticleVariantDescription] as article,
|
|
cast([CustomerHumanReadableId] as varchar) + ' - ' + [CustomerDescription] as customer,
|
|
convert(varchar(10), [test1_AlplaPROD2.0_Reporting].[reporting_blocking].[BlockingOrder].[BlockingDate], 101) + ' - ' + convert(varchar(5), [test1_AlplaPROD2.0_Reporting].[reporting_blocking].[BlockingOrder].[BlockingDate], 108) as blockingDate,
|
|
cast(ArticleVariantHumanReadableId as varchar) + ' - ' + ArticleVariantDescription as av,
|
|
case when [test1_AlplaPROD2.0_Reporting].[reporting_blocking].[BlockingOrder].Remark = '' or [test1_AlplaPROD2.0_Reporting].[reporting_blocking].[BlockingOrder].Remark is NULL then 'Please reach out to quality for the reason this was placed on hold as a remark was not entered during the blocking processs' else [test1_AlplaPROD2.0_Reporting].[reporting_blocking].[BlockingOrder].Remark end as remark,
|
|
cast(FORMAT(TotalAmountOfPieces, '###,###') as varchar) + ' / ' + cast(LoadingUnit as varchar) as peicesAndLoadingUnits,
|
|
[test1_AlplaPROD2.0_Reporting].[reporting_blocking].[BlockingOrder].ProductionLotHumanReadableId as lotNumber,
|
|
cast(IdGlobalBlockingDefectsGroup as varchar) + ' - ' + BD.Description as mainDefectGroup,
|
|
cast(IdGlobalBlockingDefect as varchar) + ' - ' + MD.Description as mainDefect,
|
|
sent=0,
|
|
lot.MachineLocation as line,
|
|
HumanReadableId
|
|
FROM [test1_AlplaPROD2.0_Reporting].[reporting_blocking].[BlockingOrder] (nolock)
|
|
|
|
/*** Join 1.0 table to get correct id info to link ***/
|
|
join
|
|
[AlplaPROD_test1].[dbo].[T_BlockingOrders] (nolock) AS BO
|
|
on [HumanReadableId] = BO.[IdBlockingOrder]
|
|
|
|
|
|
/*** Get the main defect info ***/
|
|
Inner join
|
|
[AlplaPROD_test1].[dbo].[T_BlockingDefectsGroups] (nolock) as BD
|
|
ON BO.IdMainDefectGroup = BD.IdBlockingDefectsGroup
|
|
|
|
INNER join
|
|
[AlplaPROD_test1].[dbo].[T_BlockingDefects] as MD
|
|
ON BO.IdMainDefect = MD.IdBlockingDefect
|
|
/*** get lot info ***/
|
|
|
|
left join
|
|
(SELECT [MachineLocation]
|
|
,[MachineDescription]
|
|
,[ProductionLotHumanReadableId]
|
|
FROM [test1_AlplaPROD2.0_Reporting].[reporting_productionControlling].[ProducedLot]) as lot
|
|
on [test1_AlplaPROD2.0_Reporting].[reporting_blocking].[BlockingOrder].ProductionLotHumanReadableId = lot.ProductionLotHumanReadableId
|
|
|
|
where [test1_AlplaPROD2.0_Reporting].[reporting_blocking].[BlockingOrder].[BlockingDate] between getdate() - 1 and getdate() + 1
|
|
and [test1_AlplaPROD2.0_Reporting].[reporting_blocking].[BlockingOrder].BlockingTrigger = 1
|
|
and HumanReadableId NOT IN ([sentBlockingOrders])
|
|
`;
|
|
|
|
//add the blocking orders in.
|
|
blockQuery = blockQuery.replaceAll(
|
|
"[sentBlockingOrders]",
|
|
notifyData.sentBlocking[0].sentBlockingOrders
|
|
);
|
|
|
|
let blocking: any;
|
|
try {
|
|
blocking = await query(blockQuery, "Quality Blocking");
|
|
//console.log(labels.length);
|
|
// const now = Date.now()
|
|
//console.log(blocking);
|
|
// console.log(blocking[0].blockingNumber > data.prodID);
|
|
if (
|
|
blocking.length > 0 &&
|
|
blocking[0].HumanReadableId > notifyData.notifiySettings.prodID
|
|
) {
|
|
//send the email :D
|
|
const emailSetup = {
|
|
emailTo: notifyData.emails,
|
|
subject:
|
|
blocking.length > 0
|
|
? `Alert! New blocking orders.`
|
|
: blocking[0].subject,
|
|
template: "qualityBlocking",
|
|
context: {
|
|
items: blocking,
|
|
},
|
|
};
|
|
|
|
const sentEmail = await sendEmail(emailSetup);
|
|
|
|
if (!sentEmail.success) {
|
|
createLog(
|
|
"error",
|
|
"nofity",
|
|
"notify",
|
|
"Failed to send email, will try again on next interval"
|
|
);
|
|
return;
|
|
}
|
|
|
|
// add the new blocking order to this
|
|
const newBlockingOrders = blocking.map(
|
|
(b: any) => b.HumanReadableId
|
|
);
|
|
|
|
//console.log(newBlockingOrders);
|
|
//console.log(sentBlocking[0].sentBlockingOrders);
|
|
// Ensure no duplicates
|
|
const uniqueOrders = Array.from(
|
|
new Set([
|
|
...notifyData.sentBlocking[0].sentBlockingOrders,
|
|
...newBlockingOrders,
|
|
])
|
|
);
|
|
|
|
// Update sentBlockingOrders
|
|
notifyData.sentBlocking[0].sentBlockingOrders = uniqueOrders;
|
|
|
|
//console.log(notifUpdate);
|
|
|
|
const { data, error } = await tryCatch(
|
|
db
|
|
.update(notifications)
|
|
.set({
|
|
lastRan: sql`NOW()`,
|
|
notifiySettings: {
|
|
...notifyData.notifiySettings,
|
|
prodID: blocking[0].HumanReadableId,
|
|
sentBlockingOrders: uniqueOrders,
|
|
},
|
|
})
|
|
.where(eq(notifications.name, notifyData.name))
|
|
);
|
|
}
|
|
} catch (err) {
|
|
createLog(
|
|
"error",
|
|
"notify",
|
|
"notify",
|
|
`Error from running the blocking query: ${err}`
|
|
);
|
|
}
|
|
}
|