feat(dm): standard forecast added in rest of migration to come
This commit is contained in:
95
backend/logistics/logistics.dm.forecast.route.ts
Normal file
95
backend/logistics/logistics.dm.forecast.route.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { Router } from "express";
|
||||
import multer from "multer";
|
||||
import { requireAuth } from "../middleware/auth.middleware.js";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { standardForecast } from "./logsitcs.dm.forecast.map.standard.js";
|
||||
|
||||
type ForecastResult = {
|
||||
success?: boolean;
|
||||
message?: string;
|
||||
data?: unknown;
|
||||
};
|
||||
|
||||
const r = Router();
|
||||
|
||||
const upload = multer({
|
||||
storage: multer.memoryStorage(),
|
||||
});
|
||||
|
||||
r.post("/", requireAuth, upload.single("file"), async (req, res) => {
|
||||
if (!req.file) {
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "dm",
|
||||
subModule: "forecast",
|
||||
message: "A file must be added to be able to run the forecast.",
|
||||
data: [],
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const { fileType } = req.body;
|
||||
|
||||
if (typeof fileType !== "string") {
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "dm",
|
||||
subModule: "forecast",
|
||||
message: "A fileType must be provided.",
|
||||
data: [],
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
//console.log("fileType:", req.body.fileType);
|
||||
|
||||
let result: ForecastResult;
|
||||
|
||||
switch (fileType) {
|
||||
case "standard":
|
||||
result = await standardForecast(req.file, req.user);
|
||||
break;
|
||||
|
||||
case "loreal":
|
||||
`result = await lorealForecast(req.file, req.user);`;
|
||||
result = { success: true, message: "standardForecast", data: [] };
|
||||
break;
|
||||
|
||||
case "pg":
|
||||
`result = await pNgForecast(req.file, req.user);`;
|
||||
result = { success: true, message: "standardForecast", data: [] };
|
||||
break;
|
||||
|
||||
case "energizer":
|
||||
`result = await energizerForecast(req.file, req.user);`;
|
||||
result = { success: true, message: "standardForecast", data: [] };
|
||||
break;
|
||||
|
||||
default:
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "dm",
|
||||
subModule: "forecast",
|
||||
message: `Invalid fileType: ${fileType}`,
|
||||
data: [],
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
return apiReturn(res, {
|
||||
success: result.success ?? false,
|
||||
level: result.success ? "info" : "error",
|
||||
module: "dm",
|
||||
subModule: "forecast",
|
||||
message: result.success
|
||||
? "The forecast was accepted by Alplaprod 2.0 please check to make sure everything processed properly."
|
||||
: (result.message as string),
|
||||
data: result.data ?? ([] as any),
|
||||
status: result.success ? 200 : 500,
|
||||
});
|
||||
});
|
||||
|
||||
export default r;
|
||||
49
backend/logistics/logistics.dm.postForecast.ts
Normal file
49
backend/logistics/logistics.dm.postForecast.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { forecastImport } from "../db/schema/forecastImports.schema.js";
|
||||
import { runProdApi } from "../utils/prodEndpoint.utils.js";
|
||||
import { returnFunc } from "../utils/returnHelper.utils.js";
|
||||
|
||||
export const postForecast = async (data: any, user: any) => {
|
||||
const forecast = await runProdApi(
|
||||
{
|
||||
method: "post",
|
||||
endpoint: "/public/v1.0/DemandManagement/DELFOR",
|
||||
data: [data],
|
||||
},
|
||||
"Forecast post",
|
||||
);
|
||||
|
||||
if (!forecast?.success) {
|
||||
return returnFunc({
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "dm",
|
||||
subModule: "forecast",
|
||||
message: forecast?.message ?? "Error in posting the forecast data",
|
||||
data: forecast?.data ?? [],
|
||||
notify: false,
|
||||
});
|
||||
}
|
||||
|
||||
if (forecast.success) {
|
||||
await db.insert(forecastImport).values({
|
||||
receivingPlantId: data.receivingPlantId ?? "test1",
|
||||
documentName: data.documentName ?? "forecast-data-missing",
|
||||
sender: data.sender ?? "lst-user",
|
||||
customerId: data.customerId ?? "0",
|
||||
rawData: data ?? [],
|
||||
add_user: user.username ?? undefined,
|
||||
upd_user: user.username ?? undefined,
|
||||
});
|
||||
|
||||
return returnFunc({
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "dm",
|
||||
subModule: "forecast",
|
||||
message: forecast?.message ?? "",
|
||||
data: data ?? [],
|
||||
notify: false,
|
||||
});
|
||||
}
|
||||
};
|
||||
79
backend/logistics/logistics.dm.template.route.ts
Normal file
79
backend/logistics/logistics.dm.template.route.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { format } from "date-fns";
|
||||
import { Router } from "express";
|
||||
import { requireAuth } from "../middleware/auth.middleware.js";
|
||||
import { excelTemplate, type Template } from "../utils/excelTemplates.utils.js";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
|
||||
const r = Router();
|
||||
|
||||
r.get("/", requireAuth, async (req, res) => {
|
||||
const { filename } = req.query;
|
||||
|
||||
const templateNames = ["orders", "forecast"];
|
||||
if (typeof filename !== "string" || !templateNames.includes(filename)) {
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "dm",
|
||||
subModule: "template",
|
||||
message: "You are required to pass over the template name.",
|
||||
data: [],
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
|
||||
const name = `${filename}-Template-${format(
|
||||
new Date(Date.now()),
|
||||
"M-d-yyyy",
|
||||
)}.xlsx`;
|
||||
|
||||
const standardHeaders = [
|
||||
"CustomerArticleNumber",
|
||||
"CustomerOrderNumber",
|
||||
"CustomerLineNumber",
|
||||
"CustomerRealeaseNumber",
|
||||
"Quantity",
|
||||
"DeliveryDate",
|
||||
"CustomerID",
|
||||
"Remark",
|
||||
// "InvoiceID",
|
||||
];
|
||||
|
||||
const forecastHeaders = [
|
||||
"CustomerArticleNumber",
|
||||
"Quantity",
|
||||
"RequirementDate",
|
||||
"CustomerID",
|
||||
];
|
||||
|
||||
const template = {
|
||||
name,
|
||||
headers: filename === "orders" ? standardHeaders : forecastHeaders,
|
||||
} as Template;
|
||||
|
||||
//console.log(template);
|
||||
const { data, error } = await tryCatch(excelTemplate(template));
|
||||
|
||||
if (error || !data) {
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "dm",
|
||||
subModule: "template",
|
||||
message: "There was an error creating the Excel template.",
|
||||
data: [],
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
|
||||
res.set({
|
||||
"Content-Type":
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"Content-Disposition": `attachment; filename="${name}"`,
|
||||
});
|
||||
|
||||
return res.status(200).send(data);
|
||||
});
|
||||
|
||||
export default r;
|
||||
21
backend/logistics/logistics.routes.ts
Normal file
21
backend/logistics/logistics.routes.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { Express } from "express";
|
||||
import { featureCheck } from "../middleware/featureActive.middleware.js";
|
||||
import forecast from "./logistics.dm.forecast.route.js";
|
||||
import createTemplate from "./logistics.dm.template.route.js";
|
||||
|
||||
export const setupLogisticsRoutes = (baseUrl: string, app: Express) => {
|
||||
//stats will be like this as we dont need to change this
|
||||
|
||||
app.use(
|
||||
`${baseUrl}/api/logistics/dm/template`,
|
||||
featureCheck("demandManagement"),
|
||||
createTemplate,
|
||||
);
|
||||
app.use(
|
||||
`${baseUrl}/api/logistics/dm/forecast`,
|
||||
featureCheck("demandManagement"),
|
||||
forecast,
|
||||
);
|
||||
|
||||
// all other system should be under /api/system/*
|
||||
};
|
||||
97
backend/logistics/logsitcs.dm.forecast.map.standard.ts
Normal file
97
backend/logistics/logsitcs.dm.forecast.map.standard.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import * as XLSX from "xlsx";
|
||||
import { excelDateStuff } from "../utils/excelToDate.utils.js";
|
||||
import { postForecast } from "./logistics.dm.postForecast.js";
|
||||
export const standardForecast = async (data: any, user: any) => {
|
||||
/**
|
||||
* Post a standard forecast based on the standard template.
|
||||
*/
|
||||
|
||||
const plantToken = process.env.PROD_PLANT_TOKEN;
|
||||
|
||||
//const arrayBuffer = await data.arrayBuffer();
|
||||
const buffer = Buffer.from(data.buffer);
|
||||
|
||||
const workbook = XLSX.read(buffer, { type: "buffer" });
|
||||
|
||||
const sheetName = workbook.SheetNames[0] as string;
|
||||
const sheet = workbook.Sheets[sheetName] as any;
|
||||
|
||||
const headers = [
|
||||
"CustomerArticleNumber",
|
||||
"Quantity",
|
||||
"RequirementDate",
|
||||
"CustomerID",
|
||||
];
|
||||
|
||||
const forecastData: any = XLSX.utils.sheet_to_json(sheet, {
|
||||
defval: "",
|
||||
header: headers,
|
||||
range: 1,
|
||||
});
|
||||
|
||||
const groupedByCustomer: any = forecastData.reduce((acc: any, item: any) => {
|
||||
const id = item.CustomerID;
|
||||
if (!acc[id]) {
|
||||
acc[id] = [];
|
||||
}
|
||||
acc[id].push(item);
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const foreCastData: any = [];
|
||||
|
||||
for (const [customerID, forecast] of Object.entries(groupedByCustomer)) {
|
||||
//console.log(`Running for Customer ID: ${customerID}`);
|
||||
const newForecast: any = forecast;
|
||||
|
||||
const predefinedObject = {
|
||||
receivingPlantId: plantToken,
|
||||
documentName: `ForecastFromLST-${new Date(Date.now()).toLocaleString(
|
||||
"en-US",
|
||||
)}`,
|
||||
sender: user.username || "lst-system",
|
||||
customerId: customerID,
|
||||
positions: [],
|
||||
};
|
||||
|
||||
// map everything out for each order
|
||||
const nForecast = newForecast.map((o: any) => {
|
||||
// const invoice = i.filter(
|
||||
// (i: any) => i.deliveryAddress === parseInt(customerID)
|
||||
// );
|
||||
// if (!invoice) {
|
||||
// return;
|
||||
// }
|
||||
return {
|
||||
customerArticleNo: o.CustomerArticleNumber,
|
||||
requirementDate: excelDateStuff(parseInt(o.RequirementDate)),
|
||||
quantity: o.Quantity,
|
||||
};
|
||||
});
|
||||
|
||||
// do that fun combining thing
|
||||
const updatedPredefinedObject = {
|
||||
...predefinedObject,
|
||||
positions: [...predefinedObject.positions, ...nForecast],
|
||||
};
|
||||
|
||||
//console.log(updatedPredefinedObject);
|
||||
|
||||
// post the orders to the server
|
||||
const posting: any = await postForecast(updatedPredefinedObject, user);
|
||||
|
||||
foreCastData.push({
|
||||
customer: customerID,
|
||||
//totalOrders: orders?.length(),
|
||||
success: posting.success,
|
||||
message: posting.message,
|
||||
data: posting.data,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: foreCastData[0].success,
|
||||
message: foreCastData[0].message,
|
||||
data: foreCastData,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user