refactor(printdelay): added in a change to allow override the actualy time
This commit is contained in:
@@ -1,40 +1,41 @@
|
||||
import {
|
||||
text,
|
||||
pgTable,
|
||||
numeric,
|
||||
index,
|
||||
timestamp,
|
||||
boolean,
|
||||
uuid,
|
||||
uniqueIndex,
|
||||
jsonb,
|
||||
boolean,
|
||||
index,
|
||||
jsonb,
|
||||
numeric,
|
||||
pgTable,
|
||||
text,
|
||||
timestamp,
|
||||
uniqueIndex,
|
||||
uuid,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
|
||||
export const printerData = pgTable(
|
||||
"printerData",
|
||||
{
|
||||
printer_id: uuid("printer_id").defaultRandom().primaryKey(),
|
||||
humanReadableId: text("humanReadableId"),
|
||||
name: text("name").notNull(),
|
||||
ipAddress: text("ipAddress"),
|
||||
port: numeric("port"),
|
||||
status: text("status"),
|
||||
statusText: text("statusText"),
|
||||
lastTimePrinted: timestamp("lastTimePrinted").notNull().defaultNow(),
|
||||
assigned: boolean("assigned").default(false),
|
||||
remark: text("remark"),
|
||||
printDelay: numeric("printDelay").default("90"),
|
||||
monitorState: boolean("monitorState").default(false),
|
||||
processes: jsonb("processes").default([]),
|
||||
add_Date: timestamp("add_Date").defaultNow(),
|
||||
upd_date: timestamp("upd_date").defaultNow(),
|
||||
},
|
||||
(table) => [
|
||||
//uniqueIndex("emailUniqueIndex").on(sql`lower(${table.email})`),
|
||||
uniqueIndex("humanReadableId").on(table.humanReadableId),
|
||||
]
|
||||
"printerData",
|
||||
{
|
||||
printer_id: uuid("printer_id").defaultRandom().primaryKey(),
|
||||
humanReadableId: text("humanReadableId"),
|
||||
name: text("name").notNull(),
|
||||
ipAddress: text("ipAddress"),
|
||||
port: numeric("port"),
|
||||
status: text("status"),
|
||||
statusText: text("statusText"),
|
||||
lastTimePrinted: timestamp("lastTimePrinted").notNull().defaultNow(),
|
||||
assigned: boolean("assigned").default(false),
|
||||
remark: text("remark"),
|
||||
printDelay: numeric("printDelay").default("90"),
|
||||
monitorState: boolean("monitorState").default(false),
|
||||
processes: jsonb("processes").default([]),
|
||||
printDelayOverride: boolean("print_delay_override").default(false), // this will be more for if we have the lot time active but want to over ride this single line for some reason
|
||||
add_Date: timestamp("add_Date").defaultNow(),
|
||||
upd_date: timestamp("upd_date").defaultNow(),
|
||||
},
|
||||
(table) => [
|
||||
//uniqueIndex("emailUniqueIndex").on(sql`lower(${table.email})`),
|
||||
uniqueIndex("humanReadableId").on(table.humanReadableId),
|
||||
],
|
||||
);
|
||||
|
||||
// Schema for inserting a user - can be used to validate API requests
|
||||
|
||||
@@ -1,111 +1,119 @@
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { and, eq, sql } from "drizzle-orm";
|
||||
import { db } from "../../../../database/dbclient.js";
|
||||
import { printerData } from "../../../../database/schema/printers.js";
|
||||
import { settings } from "../../../../database/schema/settings.js";
|
||||
import { delay } from "../../../globalUtils/delay.js";
|
||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||
import { createLog } from "../../logger/logger.js";
|
||||
import { delay } from "../../../globalUtils/delay.js";
|
||||
import { getPrinters } from "../controller/printers/getPrinters.js";
|
||||
|
||||
//**The logic here will be when the setting is active to utilize the lots times it will update the printDelay in the printer data table */
|
||||
export const printerDelayByLot = async (lot: any) => {
|
||||
const { data: settingData, error: settingError } = await tryCatch(
|
||||
db.select().from(settings)
|
||||
);
|
||||
const { data: settingData, error: settingError } = await tryCatch(
|
||||
db.select().from(settings),
|
||||
);
|
||||
|
||||
if (settingError) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting the settings.",
|
||||
settingError,
|
||||
};
|
||||
}
|
||||
// get the plantToken
|
||||
const printDelay = settingData.filter((n) => n.name === "lotPrintDelay");
|
||||
if (settingError) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting the settings.",
|
||||
settingError,
|
||||
};
|
||||
}
|
||||
// get the plantToken
|
||||
const printDelay = settingData.filter((n) => n.name === "lotPrintDelay");
|
||||
|
||||
const ignorePrinters = [
|
||||
"Autolabeler",
|
||||
"pdf24",
|
||||
"PDF24",
|
||||
"zecchetti_1",
|
||||
"zecchetti2",
|
||||
];
|
||||
const printers = (await getPrinters()) as any;
|
||||
const p = printers.data;
|
||||
const ignorePrinters = [
|
||||
"Autolabeler",
|
||||
"pdf24",
|
||||
"PDF24",
|
||||
"zecchetti_1",
|
||||
"zecchetti2",
|
||||
];
|
||||
const printers = (await getPrinters()) as any;
|
||||
const p = printers.data;
|
||||
|
||||
if (printDelay[0].value === "0") {
|
||||
for (let i = 0; i < p.length; i++) {
|
||||
if (p[i].printDelay > 90) {
|
||||
if (ignorePrinters.includes(p[i].name)) continue;
|
||||
if (printDelay[0].value === "0") {
|
||||
for (let i = 0; i < p.length; i++) {
|
||||
if (p[i].printDelay > 90) {
|
||||
if (ignorePrinters.includes(p[i].name)) continue;
|
||||
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.update(printerData)
|
||||
.set({
|
||||
printDelay: "90",
|
||||
upd_date: sql`NOW()`,
|
||||
})
|
||||
.where(
|
||||
eq(printerData.humanReadableId, lot[i].printerID)
|
||||
)
|
||||
);
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.update(printerData)
|
||||
.set({
|
||||
printDelay: "90",
|
||||
upd_date: sql`NOW()`,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(printerData.humanReadableId, lot[i].printerID),
|
||||
eq(printerData.printDelayOverride, false),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// if (data) {
|
||||
// createLog(
|
||||
// "info",
|
||||
// "printers",
|
||||
// "ocp",
|
||||
// `${printerData.name} had its delay time updated to 90 seconds `
|
||||
// );
|
||||
// }
|
||||
// if (data) {
|
||||
// createLog(
|
||||
// "info",
|
||||
// "printers",
|
||||
// "ocp",
|
||||
// `${printerData.name} had its delay time updated to 90 seconds `
|
||||
// );
|
||||
// }
|
||||
|
||||
// if (error) {
|
||||
// createLog(
|
||||
// "error",
|
||||
// "printers",
|
||||
// "ocp",
|
||||
// `${printerData.name} encountered an error updating the printer delay time `
|
||||
// );
|
||||
// }
|
||||
await delay(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (error) {
|
||||
// createLog(
|
||||
// "error",
|
||||
// "printers",
|
||||
// "ocp",
|
||||
// `${printerData.name} encountered an error updating the printer delay time `
|
||||
// );
|
||||
// }
|
||||
await delay(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (printDelay[0].value === "1") {
|
||||
for (let i = 0; i < lot.length; i++) {
|
||||
if (ignorePrinters.includes(lot[i].PrinterName)) continue;
|
||||
if (printDelay[0].value === "1") {
|
||||
for (let i = 0; i < lot.length; i++) {
|
||||
if (ignorePrinters.includes(lot[i].PrinterName)) continue;
|
||||
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.update(printerData)
|
||||
.set({
|
||||
printDelay: lot[i].timeTOmakeInSeconds,
|
||||
upd_date: sql`NOW()`,
|
||||
})
|
||||
.where(eq(printerData.humanReadableId, lot[i].printerID))
|
||||
);
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.update(printerData)
|
||||
.set({
|
||||
printDelay: lot[i].timeTOmakeInSeconds,
|
||||
upd_date: sql`NOW()`,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(printerData.humanReadableId, lot[i].printerID),
|
||||
eq(printerData.printDelayOverride, false),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// if (data) {
|
||||
// createLog(
|
||||
// "info",
|
||||
// "printers",
|
||||
// "ocp",
|
||||
// `${lot[i].PrinterName} had its delay time updated to ${lot[
|
||||
// i
|
||||
// ].timeTOmakeInSeconds.toFixed(2)} seconds `
|
||||
// );
|
||||
// }
|
||||
// if (data) {
|
||||
// createLog(
|
||||
// "info",
|
||||
// "printers",
|
||||
// "ocp",
|
||||
// `${lot[i].PrinterName} had its delay time updated to ${lot[
|
||||
// i
|
||||
// ].timeTOmakeInSeconds.toFixed(2)} seconds `
|
||||
// );
|
||||
// }
|
||||
|
||||
// if (error) {
|
||||
// createLog(
|
||||
// "error",
|
||||
// "printers",
|
||||
// "ocp",
|
||||
// `${lot[i].PrinterName} encountered an error updating the printer delay time `
|
||||
// );
|
||||
// }
|
||||
await delay(500);
|
||||
}
|
||||
}
|
||||
// if (error) {
|
||||
// createLog(
|
||||
// "error",
|
||||
// "printers",
|
||||
// "ocp",
|
||||
// `${lot[i].PrinterName} encountered an error updating the printer delay time `
|
||||
// );
|
||||
// }
|
||||
await delay(500);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user