feat(labels): added listener for old app to push all labels to the new app
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { Client } from "pg";
|
||||
import { db } from "../db/db.js";
|
||||
import { prodlabels } from "../db/schema/prodLabels.js";
|
||||
import { tryCatch } from "../utils/tryCatch.js";
|
||||
import { createLogger } from "./logger.js";
|
||||
|
||||
type NewLog = {
|
||||
@@ -18,13 +21,13 @@ export const v1Listener = async () => {
|
||||
await client.connect();
|
||||
|
||||
// the notify channel to listen for logs on
|
||||
const channels = ["logs_channel", "users_channel", "orders_channel"];
|
||||
const channels = ["logs_channel", "label_channel", "orders_channel"];
|
||||
|
||||
for (const ch of channels) {
|
||||
await client.query(`LISTEN ${ch}`);
|
||||
}
|
||||
|
||||
console.log("Listening for:", channels.join(", "));
|
||||
log.info({ channels: channels }, "Listening for on channels");
|
||||
|
||||
// create the log function to be able to mimic what is coming over
|
||||
const logEvent = (newLog: string) => {
|
||||
@@ -50,6 +53,20 @@ export const v1Listener = async () => {
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const labelEvent = async (newLabel: string) => {
|
||||
const newLabelEvent: any = JSON.parse(newLabel);
|
||||
const { data, error } = await tryCatch(
|
||||
db.insert(prodlabels).values({
|
||||
printerID: newLabelEvent.printerID,
|
||||
printerName: newLabelEvent.printerName,
|
||||
line: newLabelEvent.line,
|
||||
runningNr: newLabelEvent.runningNr,
|
||||
status: newLabelEvent.status,
|
||||
add_user: newLabelEvent.add_user,
|
||||
}),
|
||||
);
|
||||
};
|
||||
client.on("notification", (msg) => {
|
||||
// msg.channel tells which channel it came from
|
||||
// msg.payload is whatever message you sent from the trigger
|
||||
@@ -57,8 +74,8 @@ export const v1Listener = async () => {
|
||||
case "logs_channel":
|
||||
logEvent(msg.payload || "");
|
||||
break;
|
||||
case "users_channel":
|
||||
console.log("👤 User event:", msg.payload);
|
||||
case "label_channel":
|
||||
labelEvent(msg.payload || "");
|
||||
break;
|
||||
case "orders_channel":
|
||||
console.log("🛒 Order event:", msg.payload);
|
||||
|
||||
Reference in New Issue
Block a user