From 7ed29e7432e3b48d397ea89960977c91eda29575 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Thu, 2 Oct 2025 08:58:43 -0500 Subject: [PATCH] feat(lstv2): energizer forecast added with new format --- .../LstV2/Auth/Login.bru | 30 ++++++ .../LstV2/Auth/folder.bru | 8 ++ .../LstV2/DM/Forecast.bru | 20 ++++ .../LstV2/DM/folder.bru | 8 ++ .../LstV2/folder.bru | 12 +++ .../environments/lst.bru | 7 +- .../controller/dm/forecast/forecastIn.ts | 9 ++ .../dm/forecast/mappings/energizer.ts | 95 +++++++++++++++++++ .../controller/dm/forecast/postForecast.ts | 1 + 9 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 LogisticsSupportTool_API_DOCS/LstV2/Auth/Login.bru create mode 100644 LogisticsSupportTool_API_DOCS/LstV2/Auth/folder.bru create mode 100644 LogisticsSupportTool_API_DOCS/LstV2/DM/Forecast.bru create mode 100644 LogisticsSupportTool_API_DOCS/LstV2/DM/folder.bru create mode 100644 LogisticsSupportTool_API_DOCS/LstV2/folder.bru create mode 100644 lstV2/server/services/logistics/controller/dm/forecast/mappings/energizer.ts diff --git a/LogisticsSupportTool_API_DOCS/LstV2/Auth/Login.bru b/LogisticsSupportTool_API_DOCS/LstV2/Auth/Login.bru new file mode 100644 index 0000000..b8c3a37 --- /dev/null +++ b/LogisticsSupportTool_API_DOCS/LstV2/Auth/Login.bru @@ -0,0 +1,30 @@ +meta { + name: Login + type: http + seq: 1 +} + +post { + url: {{urlv2}}/api/auth/login + body: json + auth: inherit +} + +headers { + Content-Type: application/json +} + +body:json { + { + "username": "matthes01", + "password": "{{v2Password}}" + } +} + +script:post-response { + bru.setEnvVar("jwtV2",res.body.token) +} + +settings { + encodeUrl: true +} diff --git a/LogisticsSupportTool_API_DOCS/LstV2/Auth/folder.bru b/LogisticsSupportTool_API_DOCS/LstV2/Auth/folder.bru new file mode 100644 index 0000000..23fe239 --- /dev/null +++ b/LogisticsSupportTool_API_DOCS/LstV2/Auth/folder.bru @@ -0,0 +1,8 @@ +meta { + name: Auth + seq: 2 +} + +auth { + mode: inherit +} diff --git a/LogisticsSupportTool_API_DOCS/LstV2/DM/Forecast.bru b/LogisticsSupportTool_API_DOCS/LstV2/DM/Forecast.bru new file mode 100644 index 0000000..05bb528 --- /dev/null +++ b/LogisticsSupportTool_API_DOCS/LstV2/DM/Forecast.bru @@ -0,0 +1,20 @@ +meta { + name: Forecast + type: http + seq: 1 +} + +post { + url: {{urlv2}}/api/logistics/postforecastin + body: multipartForm + auth: inherit +} + +body:multipart-form { + postForecast: @file(C:\Users\matthes01\Downloads\Rolling Forecast .xlsx) + fileType: energizer +} + +settings { + encodeUrl: true +} diff --git a/LogisticsSupportTool_API_DOCS/LstV2/DM/folder.bru b/LogisticsSupportTool_API_DOCS/LstV2/DM/folder.bru new file mode 100644 index 0000000..8c20ca6 --- /dev/null +++ b/LogisticsSupportTool_API_DOCS/LstV2/DM/folder.bru @@ -0,0 +1,8 @@ +meta { + name: DM + seq: 1 +} + +auth { + mode: inherit +} diff --git a/LogisticsSupportTool_API_DOCS/LstV2/folder.bru b/LogisticsSupportTool_API_DOCS/LstV2/folder.bru new file mode 100644 index 0000000..f5ce718 --- /dev/null +++ b/LogisticsSupportTool_API_DOCS/LstV2/folder.bru @@ -0,0 +1,12 @@ +meta { + name: LstV2 + seq: 3 +} + +auth { + mode: bearer +} + +auth:bearer { + token: {{jwtV2}} +} diff --git a/LogisticsSupportTool_API_DOCS/environments/lst.bru b/LogisticsSupportTool_API_DOCS/environments/lst.bru index 3a7faa8..97388f5 100644 --- a/LogisticsSupportTool_API_DOCS/environments/lst.bru +++ b/LogisticsSupportTool_API_DOCS/environments/lst.bru @@ -1,4 +1,9 @@ vars { - url: https://usmcd1vms036.alpla.net + url: http://localhost:4200 session_cookie: + urlv2: http://localhost:3000 + jwtV2: } +vars:secret [ + v2Password +] diff --git a/lstV2/server/services/logistics/controller/dm/forecast/forecastIn.ts b/lstV2/server/services/logistics/controller/dm/forecast/forecastIn.ts index 02320a4..0752e7a 100644 --- a/lstV2/server/services/logistics/controller/dm/forecast/forecastIn.ts +++ b/lstV2/server/services/logistics/controller/dm/forecast/forecastIn.ts @@ -1,3 +1,4 @@ +import { energizerForecast } from "./mappings/energizer.js"; import { lorealForecast } from "./mappings/loralForecast.js"; import { pNgForecast } from "./mappings/pNgForecast.js"; import { standardForecast } from "./mappings/standardForcast.js"; @@ -40,6 +41,14 @@ export const forecastIn = async (data: any, user: any) => { orderData = pg.data; } + if (data["fileType"] === "energizer") { + //run the standard forecast in + const eg = await energizerForecast(data["postForecast"], user); + success = eg.success ?? false; + message = eg.message ?? "Error posting standard forecast"; + orderData = eg.data; + } + return { success, message, diff --git a/lstV2/server/services/logistics/controller/dm/forecast/mappings/energizer.ts b/lstV2/server/services/logistics/controller/dm/forecast/mappings/energizer.ts new file mode 100644 index 0000000..282516f --- /dev/null +++ b/lstV2/server/services/logistics/controller/dm/forecast/mappings/energizer.ts @@ -0,0 +1,95 @@ +import { db } from "../../../../../../../database/dbclient.js"; +import { settings } from "../../../../../../../database/schema/settings.js"; +import { tryCatch } from "../../../../../../globalUtils/tryCatch.js"; +import XLSX from "xlsx"; +import { postForecast } from "../postForecast.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"; + +export const energizerForecast = async (data: any, user: any) => { + /** + * Post a standard forecast based on the standard template. + */ + + const { data: s, error: e } = await tryCatch(db.select().from(settings)); + + if (e) { + return { + success: false, + message: `Error getting settings`, + data: e, + }; + } + + const plantToken = s.filter((s) => s.name === "plantToken"); + + const arrayBuffer = await data.arrayBuffer(); + const buffer = Buffer.from(arrayBuffer); + + const workbook = XLSX.read(buffer, { type: "buffer" }); + + const sheet: any = workbook.Sheets["Sheet1"]; + const range = XLSX.utils.decode_range(sheet["!ref"]); + + const headers = [ + "CustomerArticleNumber", + "Quantity", + "RequirementDate", + "CustomerID", + ]; + + // formatting the data + const rows = XLSX.utils.sheet_to_json(sheet, { header: 1 }) as any; + + const posting: any = []; + const customerId = 44; + + for (let i = 1; i < rows.length; i++) { + const row: any = rows[i]; + const material = row[0]; + + if (material == undefined) continue; + for (let j = 1; j < row.length; j++) { + const qty = row[j]; + + if (qty && qty !== 0) { + const requirementDate = rows[0][j]; // first row is dates + + posting.push({ + customerArticleNo: material, + quantity: qty, + requirementDate: new Date(requirementDate), + }); + } + } + } + + // the predefined data that will never change + const predefinedObject = { + receivingPlantId: plantToken[0].value, + documentName: `ForecastFromLST-${new Date(Date.now()).toLocaleString( + "en-US" + )}`, + sender: user.username || "lst-system", + customerId: customerId, + positions: [], + }; + + // add the new forecast to the predefined data + let updatedPredefinedObject = { + ...predefinedObject, + positions: [...predefinedObject.positions, ...posting], + }; + + //post it + const forecastData: any = await postForecast(updatedPredefinedObject, user); + + return { + success: forecastData.success, + message: forecastData.message, + data: forecastData.data, + }; +}; diff --git a/lstV2/server/services/logistics/controller/dm/forecast/postForecast.ts b/lstV2/server/services/logistics/controller/dm/forecast/postForecast.ts index 9584def..ba8567c 100644 --- a/lstV2/server/services/logistics/controller/dm/forecast/postForecast.ts +++ b/lstV2/server/services/logistics/controller/dm/forecast/postForecast.ts @@ -8,6 +8,7 @@ export const postForecast = async (data: any, user: any) => { ); //console.log(endpoint); + //console.log(req.body.orders[0]); try { const results = await axios({