feat(ocp): added in printers get and add

This commit is contained in:
2025-03-25 12:47:15 -05:00
parent 04eb2e3e14
commit f90066c090
15 changed files with 4990 additions and 6 deletions

View File

@@ -0,0 +1,21 @@
import { db } from "../../../../database/dbclient.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { printers } from "../../../../database/schema/printers.js";
export const getPrinters = async () => {
const currentTime = new Date(Date.now());
const { data: printerData, error: printerError } = await tryCatch(
db.select().from(printers)
);
if (printerError) {
return {
success: false,
message: "there was an error getting the printers",
data: printerError,
};
}
return { success: true, message: "Printers", data: printerData };
};

View File

@@ -0,0 +1,84 @@
import { db } from "../../../../database/dbclient.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { printers } from "../../../../database/schema/printers.js";
import { settings } from "../../../../database/schema/settings.js";
import axios from "axios";
import { lstAuth } from "../../../index.js";
import { prodEndpointCreation } from "../../../globalUtils/createUrl.js";
import { createLog } from "../../logger/logger.js";
import { sql } from "drizzle-orm";
export const updatePrinters = async () => {
const currentTime = new Date(Date.now());
// get the printers from prod
let url = await prodEndpointCreation(
"/public/v1.0/Administration/Printers"
);
const { data: prodPrinters, error: prodError } = await tryCatch(
axios.get(url, {
headers: {
Authorization: `Basic ${lstAuth}`,
"Content-Type": "application/json",
},
})
);
if (prodError) {
console.log(prodError);
return {
success: false,
message: "there was an error getting the printers.",
data: prodError,
};
}
// do the printer update into our db
const prodPrinterInfo = prodPrinters.data;
for (let i = 0; i < prodPrinterInfo.length; i++) {
const printerStuff: any = {
humanReadableId: prodPrinterInfo[i].humanReadableId,
name: prodPrinterInfo[i].name,
ipAddress: prodPrinterInfo[i].ipAddress,
port: prodPrinterInfo[i].port,
remark: prodPrinterInfo[i].remark,
};
const { data, error } = await tryCatch(
db
.insert(printers)
.values(printerStuff)
.onConflictDoUpdate({
target: printers.humanReadableId,
set: {
//humanReadableId: prodPrinterInfo[i].humanReadableId,
name: prodPrinterInfo[i].name,
ipAddress: prodPrinterInfo[i].ipAddress,
port: prodPrinterInfo[i].port,
remark: prodPrinterInfo[i].remark,
upd_date: sql`NOW()`,
},
})
);
if (error) {
createLog(
"error",
"lst",
"ocp",
`${
prodPrinterInfo[i].name
} encoutered and error adding/updating ${JSON.stringify(error)}`
);
}
createLog(
"info",
"lst",
"ocp",
`${prodPrinterInfo[i].name} were just added/updated.`
);
}
return { success: true, message: "Printers were just added or updated." };
};