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
|
||||
*/
|
||||
|
||||
type IncomingForecastData = {
|
||||
CustomerArticleNumber: number;
|
||||
};
|
||||
import { db } from "../../../../pkg/db/db.js";
|
||||
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) => {
|
||||
console.log(data);
|
||||
export const forecastEdiData = async (data: ForecastData[]) => {
|
||||
//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 dm from "./routes/demandMgt/dmRoutes.js";
|
||||
import labeling from "./routes/labeling/labelingRoutes.js";
|
||||
import schedule from "./routes/scheduler/scheduleRoutes.js";
|
||||
|
||||
export const setupLogisticsRoutes = (app: Express, basePath: string) => {
|
||||
app.use(basePath + "/api/logistics/schedule", schedule);
|
||||
app.use(basePath + "/api/logistics/labeling", labeling);
|
||||
app.use(basePath + "/api/logistics/dm", dm);
|
||||
|
||||
// app.use(
|
||||
// basePath + "/api/admin/users",
|
||||
|
||||
@@ -4,6 +4,6 @@ import forecastData from "./forecastEDIData.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.use("/", requireAuth(), forecastData);
|
||||
router.use("/", forecastData);
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { Router } from "express";
|
||||
import z from "zod";
|
||||
import { preprintLabels } from "../../controller/labeling/preprint.js";
|
||||
import { forecastEdiData } from "../../controller/demandManagement/forecastEDIData.js";
|
||||
|
||||
export const Preprint = z.object({
|
||||
scannerId: z.number(),
|
||||
@@ -15,13 +15,10 @@ export const Preprint = z.object({
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/preprint", async (req: Request, res: Response) => {
|
||||
const parsed = Preprint.safeParse(req.body);
|
||||
const print = await preprintLabels(req.body, req.user?.username || "");
|
||||
router.post("/forecastData", async (req: Request, res: Response) => {
|
||||
await forecastEdiData(req.body);
|
||||
|
||||
res
|
||||
.status(200)
|
||||
.json({ success: print.success, message: print.message, data: print.data });
|
||||
res.status(200).json({ success: true, message: "Forecast Data", data: [] });
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -13,12 +13,11 @@ import { z } from "zod";
|
||||
|
||||
export const forecastData = pgTable("forecast_Data", {
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
customerArticleNumber: text("customer_article_number"),
|
||||
customerArticleNo: text("customer_article_no"),
|
||||
dateRequested: timestamp("date_requested").defaultNow(),
|
||||
quantity: real("quantity"),
|
||||
requestDate: timestamp("request_date").notNull(),
|
||||
requirementDate: timestamp("requirement_date").notNull(),
|
||||
article: integer("article"),
|
||||
customerID: integer("customer_id").notNull(),
|
||||
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 { settings } from "../../../../../../../database/schema/settings.js";
|
||||
import { tryCatch } from "../../../../../../globalUtils/tryCatch.js";
|
||||
import XLSX from "xlsx";
|
||||
import { excelDateStuff } from "../../../../utils/excelDateStuff.js";
|
||||
import { postForecast } from "../postForecast.js";
|
||||
import { createLog } from "../../../../../logger/logger.js";
|
||||
import { sendEmail } from "../../../../../notifications/controller/sendMail.js";
|
||||
import { query } from "../../../../../sqlServer/prodSqlServer.js";
|
||||
import { activeArticle } from "../../../../../sqlServer/querys/dataMart/article.js";
|
||||
import { addDays } from "date-fns";
|
||||
import { sendEmail } from "../../../../../notifications/controller/sendMail.js";
|
||||
import { createLog } from "../../../../../logger/logger.js";
|
||||
import { excelDateStuff } from "../../../../utils/excelDateStuff.js";
|
||||
import { postForecast } from "../postForecast.js";
|
||||
|
||||
let customerID = 4;
|
||||
export const lorealForecast = async (data: any, user: any) => {
|
||||
@@ -69,7 +70,7 @@ export const lorealForecast = async (data: any, user: any) => {
|
||||
const missingSku: any = [];
|
||||
|
||||
const { data: a, error: ae } = await tryCatch(
|
||||
query(activeArticle, "Loreal calling active av")
|
||||
query(activeArticle, "Loreal calling active av"),
|
||||
);
|
||||
|
||||
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.
|
||||
const activeAV = article.filter(
|
||||
(c: any) =>
|
||||
c?.CustomerArticleNumber ===
|
||||
forcast.customerArticleNo.toString()
|
||||
c?.CustomerArticleNumber === forcast.customerArticleNo.toString(),
|
||||
);
|
||||
|
||||
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.
|
||||
const activeAV = article.filter(
|
||||
(c: any) =>
|
||||
c?.CustomerArticleNumber ===
|
||||
forcast.customerArticleNo.toString()
|
||||
c?.CustomerArticleNumber === forcast.customerArticleNo.toString(),
|
||||
);
|
||||
|
||||
if (activeAV.length === 0) {
|
||||
@@ -209,19 +208,19 @@ export const lorealForecast = async (data: any, user: any) => {
|
||||
} else {
|
||||
// compare dates and keep the earliest
|
||||
if (
|
||||
new Date(item.requirementDate) <
|
||||
new Date(acc[key].requirementDate)
|
||||
new Date(item.requirementDate) < new Date(acc[key].requirementDate)
|
||||
) {
|
||||
acc[key] = item;
|
||||
}
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {})
|
||||
}, {}),
|
||||
);
|
||||
|
||||
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:
|
||||
missedGrouped.length > 0
|
||||
? `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(
|
||||
sendEmail(emailSetup)
|
||||
sendEmail(emailSetup),
|
||||
);
|
||||
if (sendEmailError) {
|
||||
createLog(
|
||||
"error",
|
||||
"blocking",
|
||||
"notify",
|
||||
"Failed to send email, will try again on next interval"
|
||||
"Failed to send email, will try again on next interval",
|
||||
);
|
||||
return {
|
||||
success: false,
|
||||
@@ -252,7 +251,7 @@ export const lorealForecast = async (data: any, user: any) => {
|
||||
const predefinedObject = {
|
||||
receivingPlantId: plantToken[0].value,
|
||||
documentName: `ForecastFromLST-${new Date(Date.now()).toLocaleString(
|
||||
"en-US"
|
||||
"en-US",
|
||||
)}`,
|
||||
sender: user.username || "lst-system",
|
||||
customerId: customerID,
|
||||
@@ -276,6 +275,12 @@ export const lorealForecast = async (data: any, user: any) => {
|
||||
],
|
||||
};
|
||||
// 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);
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user