144 lines
5.2 KiB
TypeScript
144 lines
5.2 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 { sendEmail } from "../sendMail.js";
|
|
import { query } from "../../../sqlServer/prodSqlServer.js";
|
|
|
|
export interface DownTime {
|
|
downTimeId?: number;
|
|
machineAlias?: string;
|
|
}
|
|
export default async function reprintLabelMonitor(notifyData: any) {
|
|
// we will over ride this with users that want to sub to this
|
|
// a new table will be called subalerts and link to the do a kinda linkn where the user wants it then it dose subId: 1, userID: x, notificationId: y. then in here we look up the userid to get the email :D
|
|
// this could then leave the emails in the notificaion blank and let users sub to it.
|
|
if (notifyData.emails === "") {
|
|
createLog(
|
|
"error",
|
|
"notify",
|
|
"notify",
|
|
`There are no emails set for ${notifyData.name}`
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
// console.log(data.secondarySetting[0].duration);
|
|
let dQuery = `
|
|
SELECT
|
|
[IdHistoryStillstandsereignis] as downTimeId
|
|
,DATEDIFF(MINUTE,b.[Startzeit], b.[Endzeit]) as totalDuration
|
|
--, b.[IdMaschine]
|
|
,x.[Bezeichnung] as machineAlias
|
|
--,b.[IdStillstandsGrund],
|
|
, c.CTO_Code
|
|
,c.Downtime_Description
|
|
--,b.[IdFehlermerkmal],
|
|
,case when g.DT_Group_Desc is null then 'Not assigned yet' else g.DT_Group_Desc end as groupDesc
|
|
,b.[Bemerkung] as remark
|
|
,CONVERT(VARCHAR, CAST(b.[Startzeit] AS DATETIME), 100) dtStart
|
|
,CONVERT(VARCHAR, CAST(b.[Endzeit] AS DATETIME), 100) dtEnd
|
|
|
|
FROM Alplaprod_test1.[dbo].[T_HistoryStillstandsereignis] (nolock)b
|
|
|
|
--get the machine info
|
|
left join
|
|
Alplaprod_test1.[dbo].[T_Maschine] (nolock)x
|
|
on b.IdMaschine = x.IdMaschine
|
|
|
|
-- add in the cto codes
|
|
left join
|
|
Alplaprod_test1.[dbo].[V_MES_Downtime_Reasons] (nolock)c
|
|
on b.IdStillstandsGrund = c.Local_Downtime_ID
|
|
|
|
left join
|
|
Alplaprod_test1.[dbo].[V_MES_Downtime_Characteristics] (nolock)g
|
|
on b.IdFehlermerkmal = g.Local_DT_Characteristic_Id
|
|
|
|
|
|
where DATEDIFF(MINUTE,b.[Startzeit],b.[Endzeit]) > ${
|
|
notifyData.notifiySettings
|
|
? notifyData.notifiySettings?.duration
|
|
: 10
|
|
}
|
|
and b.[Startzeit] > getDate() - ${
|
|
notifyData.notifiySettings
|
|
? notifyData.notifiySettings?.daysInPast
|
|
: 10
|
|
} --adding this date check in so we dont get everything possible
|
|
and c.CTO_Code not like 'a%'
|
|
and c.CTO_Code not like 'b%'
|
|
and c.CTO_Code not like 'c%'
|
|
and c.CTO_Code not like 'd%'
|
|
and c.CTO_Code not like 'e%'
|
|
and c.CTO_Code not like 'f%'
|
|
and c.CTO_Code not like 'y%'
|
|
order by IdHistoryStillstandsereignis desc
|
|
`;
|
|
|
|
//console.log(query);
|
|
let downTime: any; //DownTime[];
|
|
try {
|
|
downTime = await query(dQuery, "downTimeCheck");
|
|
//console.log(labels.length);
|
|
|
|
if (
|
|
downTime.length > 0 &&
|
|
downTime[0]?.downTimeId > notifyData.notifiySettings.prodID
|
|
) {
|
|
//send the email :D
|
|
const emailSetup = {
|
|
email: notifyData.emails,
|
|
subject: `Alert! Downtime recorded greater than ${
|
|
notifyData.notifiySettings?.duration
|
|
}min ${
|
|
downTime.length === 1
|
|
? `on ${downTime[0].machineAlias}`
|
|
: ""
|
|
}`,
|
|
template: "downTimeCheck",
|
|
context: {
|
|
items: downTime,
|
|
secondarySetting: notifyData.notifiySettings,
|
|
},
|
|
};
|
|
|
|
const sentEmail = await sendEmail(emailSetup);
|
|
|
|
if (!sentEmail.success) {
|
|
createLog(
|
|
"error",
|
|
"notify",
|
|
"notify",
|
|
"Failed to send email, will try again on next interval"
|
|
);
|
|
return;
|
|
}
|
|
|
|
const { data, error } = await tryCatch(
|
|
db
|
|
.update(notifications)
|
|
.set({
|
|
lastRan: sql`NOW()`,
|
|
notifiySettings: {
|
|
...notifyData.notifiySettings,
|
|
prodID: downTime[0].downTimeId,
|
|
},
|
|
})
|
|
.where(eq(notifications.name, notifyData.name))
|
|
);
|
|
}
|
|
} catch (err) {
|
|
createLog(
|
|
"error",
|
|
"notify",
|
|
"notify",
|
|
`Error from running the downtimeCheck query: ${err}`
|
|
);
|
|
}
|
|
}
|