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