feat(forecast data): added in a historical forecast data set
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
meta {
|
||||||
|
name: ForecastData
|
||||||
|
type: http
|
||||||
|
seq: 1
|
||||||
|
}
|
||||||
|
|
||||||
|
post {
|
||||||
|
url: {{url}}/lst/api/logistics/dm/forecastData
|
||||||
|
body: none
|
||||||
|
auth: inherit
|
||||||
|
}
|
||||||
|
|
||||||
|
settings {
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
}
|
||||||
@@ -8,10 +8,63 @@
|
|||||||
* add_date
|
* add_date
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type IncomingForecastData = {
|
import { db } from "../../../../pkg/db/db.js";
|
||||||
CustomerArticleNumber: number;
|
import {
|
||||||
};
|
type ForecastData,
|
||||||
|
forecastData,
|
||||||
|
forecastDataSchema,
|
||||||
|
} from "../../../../pkg/db/schema/forecastEDIData.js";
|
||||||
|
import { prodQuery } from "../../../../pkg/prodSql/prodQuery.js";
|
||||||
|
import { activeArticle } from "../../../../pkg/prodSql/querys/datamart/article.js";
|
||||||
|
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
|
||||||
|
|
||||||
export const forecastData = async (data: IncomingForecastData) => {
|
export const forecastEdiData = async (data: ForecastData[]) => {
|
||||||
console.log(data);
|
//console.log(data);
|
||||||
|
const { data: article, error } = await tryCatch(
|
||||||
|
prodQuery(activeArticle, "forecast data active articles"),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Failed to get active articles",
|
||||||
|
error: error,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const forecaseEDIDATA = [];
|
||||||
|
for (let i = 0; i < data.length; i++) {
|
||||||
|
const activeAV = article?.data.filter(
|
||||||
|
(c: any) =>
|
||||||
|
c?.CustomerArticleNumber === data[i].customerArticleNo?.toString(),
|
||||||
|
);
|
||||||
|
const newData = data[i];
|
||||||
|
//console.log(activeAV[0].IdArtikelvarianten);
|
||||||
|
|
||||||
|
forecaseEDIDATA.push({
|
||||||
|
...newData,
|
||||||
|
article: activeAV[0].IdArtikelvarianten,
|
||||||
|
requirementDate: new Date(newData.requirementDate),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(forecaseEDIDATA[0]);
|
||||||
|
const { data: f, error: ef } = await tryCatch(
|
||||||
|
db.insert(forecastData).values(forecaseEDIDATA),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (ef) {
|
||||||
|
console.log(ef);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "There was an error posting the new data",
|
||||||
|
error: ef,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: "All forecast Data was posted",
|
||||||
|
data: [],
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import type { Express, Request, Response } from "express";
|
import type { Express, Request, Response } from "express";
|
||||||
|
import dm from "./routes/demandMgt/dmRoutes.js";
|
||||||
import labeling from "./routes/labeling/labelingRoutes.js";
|
import labeling from "./routes/labeling/labelingRoutes.js";
|
||||||
import schedule from "./routes/scheduler/scheduleRoutes.js";
|
import schedule from "./routes/scheduler/scheduleRoutes.js";
|
||||||
|
|
||||||
export const setupLogisticsRoutes = (app: Express, basePath: string) => {
|
export const setupLogisticsRoutes = (app: Express, basePath: string) => {
|
||||||
app.use(basePath + "/api/logistics/schedule", schedule);
|
app.use(basePath + "/api/logistics/schedule", schedule);
|
||||||
app.use(basePath + "/api/logistics/labeling", labeling);
|
app.use(basePath + "/api/logistics/labeling", labeling);
|
||||||
|
app.use(basePath + "/api/logistics/dm", dm);
|
||||||
|
|
||||||
// app.use(
|
// app.use(
|
||||||
// basePath + "/api/admin/users",
|
// basePath + "/api/admin/users",
|
||||||
|
|||||||
@@ -4,6 +4,6 @@ import forecastData from "./forecastEDIData.js";
|
|||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.use("/", requireAuth(), forecastData);
|
router.use("/", forecastData);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { Request, Response } from "express";
|
import type { Request, Response } from "express";
|
||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import z from "zod";
|
import z from "zod";
|
||||||
import { preprintLabels } from "../../controller/labeling/preprint.js";
|
import { forecastEdiData } from "../../controller/demandManagement/forecastEDIData.js";
|
||||||
|
|
||||||
export const Preprint = z.object({
|
export const Preprint = z.object({
|
||||||
scannerId: z.number(),
|
scannerId: z.number(),
|
||||||
@@ -15,13 +15,10 @@ export const Preprint = z.object({
|
|||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.post("/preprint", async (req: Request, res: Response) => {
|
router.post("/forecastData", async (req: Request, res: Response) => {
|
||||||
const parsed = Preprint.safeParse(req.body);
|
await forecastEdiData(req.body);
|
||||||
const print = await preprintLabels(req.body, req.user?.username || "");
|
|
||||||
|
|
||||||
res
|
res.status(200).json({ success: true, message: "Forecast Data", data: [] });
|
||||||
.status(200)
|
|
||||||
.json({ success: print.success, message: print.message, data: print.data });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -13,12 +13,11 @@ import { z } from "zod";
|
|||||||
|
|
||||||
export const forecastData = pgTable("forecast_Data", {
|
export const forecastData = pgTable("forecast_Data", {
|
||||||
id: uuid("id").defaultRandom().primaryKey(),
|
id: uuid("id").defaultRandom().primaryKey(),
|
||||||
customerArticleNumber: text("customer_article_number"),
|
customerArticleNo: text("customer_article_no"),
|
||||||
dateRequested: timestamp("date_requested").defaultNow(),
|
dateRequested: timestamp("date_requested").defaultNow(),
|
||||||
quantity: real("quantity"),
|
quantity: real("quantity"),
|
||||||
requestDate: timestamp("request_date").notNull(),
|
requirementDate: timestamp("requirement_date").notNull(),
|
||||||
article: integer("article"),
|
article: integer("article"),
|
||||||
customerID: integer("customer_id").notNull(),
|
|
||||||
createdAt: timestamp("created_at").defaultNow(),
|
createdAt: timestamp("created_at").defaultNow(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
|
import axios from "axios";
|
||||||
|
import { addDays } from "date-fns";
|
||||||
|
import XLSX from "xlsx";
|
||||||
import { db } from "../../../../../../../database/dbclient.js";
|
import { db } from "../../../../../../../database/dbclient.js";
|
||||||
import { settings } from "../../../../../../../database/schema/settings.js";
|
import { settings } from "../../../../../../../database/schema/settings.js";
|
||||||
import { tryCatch } from "../../../../../../globalUtils/tryCatch.js";
|
import { tryCatch } from "../../../../../../globalUtils/tryCatch.js";
|
||||||
import XLSX from "xlsx";
|
import { createLog } from "../../../../../logger/logger.js";
|
||||||
import { excelDateStuff } from "../../../../utils/excelDateStuff.js";
|
import { sendEmail } from "../../../../../notifications/controller/sendMail.js";
|
||||||
import { postForecast } from "../postForecast.js";
|
|
||||||
import { query } from "../../../../../sqlServer/prodSqlServer.js";
|
import { query } from "../../../../../sqlServer/prodSqlServer.js";
|
||||||
import { activeArticle } from "../../../../../sqlServer/querys/dataMart/article.js";
|
import { activeArticle } from "../../../../../sqlServer/querys/dataMart/article.js";
|
||||||
import { addDays } from "date-fns";
|
import { excelDateStuff } from "../../../../utils/excelDateStuff.js";
|
||||||
import { sendEmail } from "../../../../../notifications/controller/sendMail.js";
|
import { postForecast } from "../postForecast.js";
|
||||||
import { createLog } from "../../../../../logger/logger.js";
|
|
||||||
|
|
||||||
let customerID = 4;
|
let customerID = 4;
|
||||||
export const lorealForecast = async (data: any, user: any) => {
|
export const lorealForecast = async (data: any, user: any) => {
|
||||||
@@ -69,7 +70,7 @@ export const lorealForecast = async (data: any, user: any) => {
|
|||||||
const missingSku: any = [];
|
const missingSku: any = [];
|
||||||
|
|
||||||
const { data: a, error: ae } = await tryCatch(
|
const { data: a, error: ae } = await tryCatch(
|
||||||
query(activeArticle, "Loreal calling active av")
|
query(activeArticle, "Loreal calling active av"),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (ae) {
|
if (ae) {
|
||||||
@@ -108,8 +109,7 @@ export const lorealForecast = async (data: any, user: any) => {
|
|||||||
// checking to make sure there is a real av to add to.
|
// checking to make sure there is a real av to add to.
|
||||||
const activeAV = article.filter(
|
const activeAV = article.filter(
|
||||||
(c: any) =>
|
(c: any) =>
|
||||||
c?.CustomerArticleNumber ===
|
c?.CustomerArticleNumber === forcast.customerArticleNo.toString(),
|
||||||
forcast.customerArticleNo.toString()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (activeAV.length === 0) {
|
if (activeAV.length === 0) {
|
||||||
@@ -181,8 +181,7 @@ export const lorealForecast = async (data: any, user: any) => {
|
|||||||
// checking to make sure there is a real av to add to.
|
// checking to make sure there is a real av to add to.
|
||||||
const activeAV = article.filter(
|
const activeAV = article.filter(
|
||||||
(c: any) =>
|
(c: any) =>
|
||||||
c?.CustomerArticleNumber ===
|
c?.CustomerArticleNumber === forcast.customerArticleNo.toString(),
|
||||||
forcast.customerArticleNo.toString()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (activeAV.length === 0) {
|
if (activeAV.length === 0) {
|
||||||
@@ -209,19 +208,19 @@ export const lorealForecast = async (data: any, user: any) => {
|
|||||||
} else {
|
} else {
|
||||||
// compare dates and keep the earliest
|
// compare dates and keep the earliest
|
||||||
if (
|
if (
|
||||||
new Date(item.requirementDate) <
|
new Date(item.requirementDate) < new Date(acc[key].requirementDate)
|
||||||
new Date(acc[key].requirementDate)
|
|
||||||
) {
|
) {
|
||||||
acc[key] = item;
|
acc[key] = item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
}, {})
|
}, {}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const emailSetup = {
|
const emailSetup = {
|
||||||
email: "Blake.matthes@alpla.com; Stuart.Gladney@alpla.com; Harold.Mccalister@alpla.com; Jenn.Osbourn@alpla.com",
|
email:
|
||||||
|
"Blake.matthes@alpla.com; Stuart.Gladney@alpla.com; Harold.Mccalister@alpla.com; Jenn.Osbourn@alpla.com",
|
||||||
subject:
|
subject:
|
||||||
missedGrouped.length > 0
|
missedGrouped.length > 0
|
||||||
? `Alert! There are ${missedGrouped.length}, missing skus.`
|
? `Alert! There are ${missedGrouped.length}, missing skus.`
|
||||||
@@ -233,14 +232,14 @@ export const lorealForecast = async (data: any, user: any) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const { data: sentEmail, error: sendEmailError } = await tryCatch(
|
const { data: sentEmail, error: sendEmailError } = await tryCatch(
|
||||||
sendEmail(emailSetup)
|
sendEmail(emailSetup),
|
||||||
);
|
);
|
||||||
if (sendEmailError) {
|
if (sendEmailError) {
|
||||||
createLog(
|
createLog(
|
||||||
"error",
|
"error",
|
||||||
"blocking",
|
"blocking",
|
||||||
"notify",
|
"notify",
|
||||||
"Failed to send email, will try again on next interval"
|
"Failed to send email, will try again on next interval",
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
@@ -252,7 +251,7 @@ export const lorealForecast = async (data: any, user: any) => {
|
|||||||
const predefinedObject = {
|
const predefinedObject = {
|
||||||
receivingPlantId: plantToken[0].value,
|
receivingPlantId: plantToken[0].value,
|
||||||
documentName: `ForecastFromLST-${new Date(Date.now()).toLocaleString(
|
documentName: `ForecastFromLST-${new Date(Date.now()).toLocaleString(
|
||||||
"en-US"
|
"en-US",
|
||||||
)}`,
|
)}`,
|
||||||
sender: user.username || "lst-system",
|
sender: user.username || "lst-system",
|
||||||
customerId: customerID,
|
customerId: customerID,
|
||||||
@@ -276,6 +275,12 @@ export const lorealForecast = async (data: any, user: any) => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
// console.log(updatedPredefinedObject);
|
// console.log(updatedPredefinedObject);
|
||||||
|
|
||||||
|
// posting the data to the new backend so we can store the data.
|
||||||
|
axios.post(
|
||||||
|
`http://localhost:${process.env.NODE_ENV?.trim() != "production" ? "4200/lst" : "4000"}/api/logistics/dm/forecastData`,
|
||||||
|
ebmForecastData,
|
||||||
|
);
|
||||||
const posting: any = await postForecast(updatedPredefinedObject, user);
|
const posting: any = await postForecast(updatedPredefinedObject, user);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user