feat(purchase): historical data capture for alpla purchase
This commit is contained in:
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@@ -54,6 +54,7 @@
|
||||
"alpla",
|
||||
"alplamart",
|
||||
"alplaprod",
|
||||
"alplapurchase",
|
||||
"bookin",
|
||||
"Datamart",
|
||||
"dotenvx",
|
||||
|
||||
@@ -26,7 +26,7 @@ const createApp = async () => {
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
// well leave this active so we can monitor it to validate
|
||||
app.use(morgan("tiny"));
|
||||
app.use(morgan("dev"));
|
||||
app.set("trust proxy", true);
|
||||
app.use(lstCors());
|
||||
app.all(`${baseUrl}/api/auth/*splat`, toNodeHandler(auth));
|
||||
@@ -34,11 +34,11 @@ const createApp = async () => {
|
||||
setupRoutes(baseUrl, app);
|
||||
|
||||
app.use(
|
||||
baseUrl + "/app",
|
||||
`${baseUrl}/app`,
|
||||
express.static(join(__dirname, "../frontend/dist")),
|
||||
);
|
||||
|
||||
app.get(baseUrl + "/app/*splat", (_, res) => {
|
||||
app.get(`${baseUrl}/app/*splat`, (_, res) => {
|
||||
res.sendFile(join(__dirname, "../frontend/dist/index.html"));
|
||||
});
|
||||
|
||||
|
||||
38
backend/db/schema/alplapurchase.schema.ts
Normal file
38
backend/db/schema/alplapurchase.schema.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import {
|
||||
integer,
|
||||
jsonb,
|
||||
pgTable,
|
||||
text,
|
||||
timestamp,
|
||||
uuid,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
|
||||
import type { z } from "zod";
|
||||
|
||||
export const alplaPurchaseHistory = pgTable("alpla_purchase_history", {
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
apo: integer("apo"),
|
||||
revision: integer("revision"),
|
||||
confirmed: integer("confirmed"),
|
||||
status: integer("status"),
|
||||
statusText: integer("status_text"),
|
||||
journalNum: integer("journal_num"),
|
||||
add_date: timestamp("add_date").defaultNow(),
|
||||
upd_date: timestamp("upd_date").defaultNow(),
|
||||
add_user: text("add_user"),
|
||||
upd_user: text("upd_user"),
|
||||
remark: text("remark"),
|
||||
approvedStatus: text("approved_status"),
|
||||
position: jsonb("position").default([]),
|
||||
createdAt: timestamp("created_at").defaultNow(),
|
||||
});
|
||||
|
||||
export const alplaPurchaseHistorySchema =
|
||||
createSelectSchema(alplaPurchaseHistory);
|
||||
export const newAlplaPurchaseHistorySchema =
|
||||
createInsertSchema(alplaPurchaseHistory);
|
||||
|
||||
export type AlplaPurchaseHistory = z.infer<typeof alplaPurchaseHistorySchema>;
|
||||
export type NewAlplaPurchaseHistory = z.infer<
|
||||
typeof newAlplaPurchaseHistorySchema
|
||||
>;
|
||||
113
backend/notification/notification.alplapurchase.ts
Normal file
113
backend/notification/notification.alplapurchase.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { notifications } from "../db/schema/notifications.schema.js";
|
||||
import { prodQuery } from "../prodSql/prodSqlQuery.controller.js";
|
||||
import {
|
||||
type SqlQuery,
|
||||
sqlQuerySelector,
|
||||
} from "../prodSql/prodSqlQuerySelector.utils.js";
|
||||
import { returnFunc } from "../utils/returnHelper.utils.js";
|
||||
import { sendEmail } from "../utils/sendEmail.utils.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
const func = async (data: any, emails: string) => {
|
||||
// get the actual notification as items will be updated between intervals if no one touches
|
||||
const { data: l, error: le } = (await tryCatch(
|
||||
db.select().from(notifications).where(eq(notifications.id, data.id)),
|
||||
)) as any;
|
||||
|
||||
if (le) {
|
||||
return returnFunc({
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "notification",
|
||||
subModule: "query",
|
||||
message: `${data.name} encountered an error while trying to get initial info`,
|
||||
data: [le],
|
||||
notify: true,
|
||||
});
|
||||
}
|
||||
|
||||
// search the query db for the query by name
|
||||
const sqlQuery = sqlQuerySelector(`${data.name}`) as SqlQuery;
|
||||
// create the ignore audit logs ids
|
||||
const ignoreIds = l[0].options[0]?.auditId
|
||||
? `${l[0].options[0]?.auditId}`
|
||||
: "0";
|
||||
|
||||
// run the check
|
||||
const { data: queryRun, error } = await tryCatch(
|
||||
prodQuery(
|
||||
sqlQuery.query
|
||||
.replace("[intervalCheck]", l[0].interval)
|
||||
.replace("[ignoreList]", ignoreIds),
|
||||
`Running notification query: ${l[0].name}`,
|
||||
),
|
||||
);
|
||||
|
||||
if (error) {
|
||||
return returnFunc({
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "notification",
|
||||
subModule: "query",
|
||||
message: `Data for: ${l[0].name} encountered an error while trying to get it`,
|
||||
data: [error],
|
||||
notify: true,
|
||||
});
|
||||
}
|
||||
|
||||
if (queryRun.data.length > 0) {
|
||||
// update the latest audit id
|
||||
const { error: dbe } = await tryCatch(
|
||||
db
|
||||
.update(notifications)
|
||||
.set({ options: [{ auditId: `${queryRun.data[0].id}` }] })
|
||||
.where(eq(notifications.id, data.id)),
|
||||
);
|
||||
|
||||
if (dbe) {
|
||||
return returnFunc({
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "notification",
|
||||
subModule: "query",
|
||||
message: `Data for: ${l[0].name} encountered an error while trying to get it`,
|
||||
data: [dbe],
|
||||
notify: true,
|
||||
});
|
||||
}
|
||||
|
||||
// send the email
|
||||
|
||||
const sentEmail = await sendEmail({
|
||||
email: emails,
|
||||
subject: "Alert! Label Reprinted",
|
||||
template: "reprintLabels",
|
||||
context: {
|
||||
items: queryRun.data,
|
||||
},
|
||||
});
|
||||
|
||||
if (!sentEmail?.success) {
|
||||
return returnFunc({
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "email",
|
||||
subModule: "notification",
|
||||
message: `${l[0].name} failed to send the email`,
|
||||
data: [sentEmail],
|
||||
notify: true,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
console.log("doing nothing as there is nothing to do.");
|
||||
}
|
||||
// TODO send the error to systemAdmin users so they do not always need to be on the notifications.
|
||||
// these errors are defined per notification.
|
||||
};
|
||||
|
||||
export default func;
|
||||
@@ -16,6 +16,26 @@ const note: NewNotification[] = [
|
||||
interval: "10",
|
||||
options: [{ auditId: [0] }],
|
||||
},
|
||||
{
|
||||
name: "qualityBlocking",
|
||||
description:
|
||||
"Checks for new blocking orders that have been entered, recommend to get the most recent order in here before activating.",
|
||||
active: false,
|
||||
interval: "10",
|
||||
options: [{ sentBlockingOrders: [{ timeStamp: "0", blockingOrder: 1 }] }],
|
||||
},
|
||||
{
|
||||
name: "alplaPurchaseHistory",
|
||||
description:
|
||||
"Will check the alpla purchase data for any changes, if the req has not been sent already then we will send this, for a po or fresh order we will ignore. ",
|
||||
active: false,
|
||||
interval: "5",
|
||||
options: [
|
||||
{ sentReqs: [{ timeStamp: "0", req: 1, approved: false }] },
|
||||
{ sentAPOs: [{ timeStamp: "0", apo: 1 }] },
|
||||
{ sentRCT: [{ timeStamp: "0", rct: 1 }] },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export const createNotifications = async () => {
|
||||
|
||||
61
backend/prodSql/queries/alplapurchase.sql
Normal file
61
backend/prodSql/queries/alplapurchase.sql
Normal file
@@ -0,0 +1,61 @@
|
||||
use AlplaPROD_test1
|
||||
declare @intervalCheck as int = '[interval]'
|
||||
|
||||
/*
|
||||
Monitors alpla purchase for thing new. this will not update unless the order status is updated.
|
||||
this means if a user just reopens the order it will update but everything changed in the position will not be updated until the user reorders or cancels the po
|
||||
*/
|
||||
|
||||
select
|
||||
IdBestellung as apo
|
||||
,po.revision as revision
|
||||
,po.Bestaetigt as confirmed
|
||||
,po.status
|
||||
,case po.Status
|
||||
when 1 then 'Created'
|
||||
when 2 then 'Ordered'
|
||||
when 22 then 'Reopened'
|
||||
when 4 then 'Planned'
|
||||
when 5 then 'Partly Delivered'
|
||||
when 6 then 'Delivered'
|
||||
when 7 then 'Canceled'
|
||||
when 8 then 'Closed'
|
||||
else 'Unknown' end as statusText
|
||||
,po.Add_User
|
||||
,po.Add_Date
|
||||
,po.Upd_User
|
||||
,po.Upd_Date
|
||||
,po.Bemerkung as remark
|
||||
,po.IdJournal as journal -- use this to validate if we used it already.
|
||||
,isnull((
|
||||
select
|
||||
o.IdArtikelVarianten as av
|
||||
,a.Bezeichnung as alias
|
||||
,Lieferdatum as deliveryDate
|
||||
,cast(BestellMenge as decimal(18,2)) as qty
|
||||
,cast(BestellMengeVPK as decimal(18,0)) as pkg
|
||||
,cast(PreisProEinheit as decimal(18,0)) as price
|
||||
,PositionsStatus
|
||||
,case PositionsStatus
|
||||
when 1 then 'Created'
|
||||
when 2 then 'Ordered'
|
||||
when 22 then 'Reopened'
|
||||
when 4 then 'Planned'
|
||||
when 5 then 'Partly Delivered'
|
||||
when 6 then 'Delivered'
|
||||
when 7 then 'Canceled'
|
||||
when 8 then 'Closed'
|
||||
else 'Unknown' end as statusText
|
||||
,o.upd_user
|
||||
,o.upd_date
|
||||
from T_Bestellpositionen (nolock) as o
|
||||
|
||||
left join
|
||||
T_Artikelvarianten as a on
|
||||
a.IdArtikelvarianten = o.IdArtikelVarianten
|
||||
where o.IdBestellung = po.IdBestellung
|
||||
for json path
|
||||
), '[]') as postion
|
||||
--,*
|
||||
from T_Bestellungen (nolock) as po
|
||||
where po.Upd_Date > dateadd(MINUTE, -@intervalCheck, getdate())
|
||||
63
backend/purchase/purchase.controller.ts
Normal file
63
backend/purchase/purchase.controller.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* This will monitor alpla purchase
|
||||
*/
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { settings } from "../db/schema/settings.schema.js";
|
||||
import { createLogger } from "../logger/logger.controller.js";
|
||||
import { prodQuery } from "../prodSql/prodSqlQuery.controller.js";
|
||||
import {
|
||||
type SqlQuery,
|
||||
sqlQuerySelector,
|
||||
} from "../prodSql/prodSqlQuerySelector.utils.js";
|
||||
import { createCronJob } from "../utils/croner.utils.js";
|
||||
import { delay } from "../utils/delay.utils.js";
|
||||
import { returnFunc } from "../utils/returnHelper.utils.js";
|
||||
|
||||
const log = createLogger({ module: "purchase", subModule: "purchaseMonitor" });
|
||||
|
||||
export const monitorAlplaPurchase = async () => {
|
||||
const purchaseMonitor = await db
|
||||
.select()
|
||||
.from(settings)
|
||||
.where(eq(settings.name, "purchaseMonitor"));
|
||||
|
||||
const sqlQuery = sqlQuerySelector(`alplapurchase`) as SqlQuery;
|
||||
|
||||
if (!sqlQuery.success) {
|
||||
return returnFunc({
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "purchase",
|
||||
subModule: "query",
|
||||
message: `Error getting alpla purchase info`,
|
||||
data: [sqlQuery.message],
|
||||
notify: false,
|
||||
});
|
||||
}
|
||||
|
||||
if (purchaseMonitor[0]?.active) {
|
||||
createCronJob("opendock_sync", "* */5 * * * *", async () => {
|
||||
try {
|
||||
const result = await prodQuery(
|
||||
sqlQuery.query.replace(
|
||||
"[interval]",
|
||||
`'${purchaseMonitor[0]?.value || "5"}'`,
|
||||
),
|
||||
"Get release info",
|
||||
);
|
||||
|
||||
if (result.data.length) {
|
||||
await delay(500);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(
|
||||
{ error: e },
|
||||
"Error occurred while running the monitor job",
|
||||
);
|
||||
log.error({ error: e }, "Error occurred while running the monitor job");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -10,6 +10,7 @@ import { createNotifications } from "./notification/notifications.master.js";
|
||||
import { monitorReleaseChanges } from "./opendock/openDockRreleaseMonitor.utils.js";
|
||||
import { opendockSocketMonitor } from "./opendock/opendockSocketMonitor.utils.js";
|
||||
import { connectProdSql } from "./prodSql/prodSqlConnection.controller.js";
|
||||
import { monitorAlplaPurchase } from "./purchase/purchase.controller.js";
|
||||
import { setupSocketIORoutes } from "./socket.io/serverSetup.js";
|
||||
import { baseSettingValidationCheck } from "./system/settingsBase.controller.js";
|
||||
import { createCronJob } from "./utils/croner.utils.js";
|
||||
@@ -36,7 +37,7 @@ const start = async () => {
|
||||
// also we always want to have long lived processes inside a setting check.
|
||||
setTimeout(() => {
|
||||
if (systemSettings.filter((n) => n.name === "opendock_sync")[0]?.active) {
|
||||
log.info({}, "Opendock is not active");
|
||||
log.info({}, "Opendock is active");
|
||||
monitorReleaseChanges(); // this is od monitoring the db for all new releases
|
||||
opendockSocketMonitor();
|
||||
createCronJob("opendockAptCleanup", "0 30 5 * * *", () =>
|
||||
@@ -44,6 +45,10 @@ const start = async () => {
|
||||
);
|
||||
}
|
||||
|
||||
if (systemSettings.filter((n) => n.name === "purchaseMonitor")[0]?.active) {
|
||||
monitorAlplaPurchase();
|
||||
}
|
||||
|
||||
// these jobs below are system jobs and should run no matter what.
|
||||
createCronJob("JobAuditLogCleanUp", "0 0 5 * * *", () =>
|
||||
dbCleanup("jobs", 30),
|
||||
|
||||
@@ -66,6 +66,16 @@ const newSettings: NewSetting[] = [
|
||||
roles: ["admin"],
|
||||
seedVersion: 1,
|
||||
},
|
||||
{
|
||||
name: "purchaseMonitor",
|
||||
value: "5",
|
||||
active: false,
|
||||
description: "Monitors alpla purchase fo all changes",
|
||||
moduleName: "purchase",
|
||||
settingType: "feature",
|
||||
roles: ["admin"],
|
||||
seedVersion: 1,
|
||||
},
|
||||
|
||||
// standard settings
|
||||
{
|
||||
|
||||
@@ -11,7 +11,8 @@ interface Data<T = unknown[]> {
|
||||
| "utils"
|
||||
| "opendock"
|
||||
| "notification"
|
||||
| "email";
|
||||
| "email"
|
||||
| "purchase";
|
||||
subModule:
|
||||
| "db"
|
||||
| "labeling"
|
||||
|
||||
@@ -7,12 +7,7 @@ import { Suspense, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import type { Notifications } from "../../../types/notifications";
|
||||
import { Button } from "../../components/ui/button";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "../../components/ui/card";
|
||||
import { Card, CardContent } from "../../components/ui/card";
|
||||
import { Label } from "../../components/ui/label";
|
||||
import { Switch } from "../../components/ui/switch";
|
||||
import {
|
||||
@@ -297,16 +292,13 @@ function RouteComponent() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="space-y-2">
|
||||
<h1 className="text-2xl font-semibold">Settings</h1>
|
||||
<h1 className="text-2xl font-semibold">Notifications</h1>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Manage you settings and related data.
|
||||
Manage all notification settings and user subs.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>System Settings</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Tabs defaultValue="notifications" className="w-full">
|
||||
<TabsList>
|
||||
|
||||
15
migrations/0020_stale_ma_gnuci.sql
Normal file
15
migrations/0020_stale_ma_gnuci.sql
Normal file
@@ -0,0 +1,15 @@
|
||||
CREATE TABLE "alpla_purchase_history" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"apo" integer,
|
||||
"revision" integer,
|
||||
"confirmed" integer,
|
||||
"status" integer,
|
||||
"status_text" integer,
|
||||
"add_date" timestamp DEFAULT now(),
|
||||
"upd_date" timestamp DEFAULT now(),
|
||||
"add_user" text,
|
||||
"upd_user" text,
|
||||
"remark" text,
|
||||
"position" jsonb DEFAULT '[]'::jsonb,
|
||||
"created_at" timestamp DEFAULT now()
|
||||
);
|
||||
1423
migrations/meta/0020_snapshot.json
Normal file
1423
migrations/meta/0020_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -141,6 +141,13 @@
|
||||
"when": 1775159956510,
|
||||
"tag": "0019_large_thunderbird",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 20,
|
||||
"version": "7",
|
||||
"when": 1775566910220,
|
||||
"tag": "0020_stale_ma_gnuci",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user