feat(forecast): migrated forecast over
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import * as XLSX from "xlsx";
|
||||
|
||||
export const standardForCastTemplate = async () => {
|
||||
/**
|
||||
* Creates the standard Template for bulk orders in
|
||||
*/
|
||||
|
||||
const headers = [
|
||||
["CustomerArticleNumber", "Quantity", "RequirementDate", "CustomerID"],
|
||||
];
|
||||
|
||||
// create a new workbook
|
||||
const wb = XLSX.utils.book_new();
|
||||
const ws = XLSX.utils.aoa_to_sheet(headers);
|
||||
//const ws2 = XLSX.utils.aoa_to_sheet(headers2);
|
||||
|
||||
const columnWidths = headers[0].map((header) => ({
|
||||
width: header.length + 2,
|
||||
}));
|
||||
|
||||
ws["!cols"] = columnWidths;
|
||||
|
||||
// append the worksheet to the workbook
|
||||
XLSX.utils.book_append_sheet(wb, ws, `Sheet1`);
|
||||
//XLSX.utils.book_append_sheet(wb, ws2, `Sheet2`);
|
||||
|
||||
// Write the excel file and trigger the download'
|
||||
XLSX.writeFile(wb, "BulkForecastTemplate");
|
||||
|
||||
// Write the workbook to a buffer and return it
|
||||
const excelBuffer = XLSX.write(wb, { bookType: "xlsx", type: "buffer" });
|
||||
|
||||
return excelBuffer;
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
import { standardForecast } from "./mappings/standardForcast.js";
|
||||
|
||||
export const forecastIn = async (data: any, user: any) => {
|
||||
/**
|
||||
* Bulk orders in, and custom file parsing.
|
||||
*/
|
||||
|
||||
let success = true;
|
||||
let message = "";
|
||||
let orderData: any = [];
|
||||
|
||||
// what type of order are we dealing with?
|
||||
if (data["fileType"] === "standard") {
|
||||
//run the standard forecast in
|
||||
const standard = await standardForecast(data["postPostForecast"], user);
|
||||
success = standard.success ?? false;
|
||||
message = standard.message ?? "Error posting standard forecast";
|
||||
orderData = standard.data;
|
||||
}
|
||||
|
||||
if (data["fileType"] === "energizer") {
|
||||
// orders in
|
||||
}
|
||||
|
||||
if (data["fileType"] === "loreal") {
|
||||
// orders in
|
||||
}
|
||||
|
||||
if (data["fileType"] === "pg") {
|
||||
// orders in
|
||||
}
|
||||
|
||||
return {
|
||||
success,
|
||||
message,
|
||||
data: orderData,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
export const standardForecast = async (data: any, user: any) => {
|
||||
/**
|
||||
* Post a standard forecast based on the standard template.
|
||||
*/
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Forecast Posted",
|
||||
data: [],
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,75 @@
|
||||
import axios from "axios";
|
||||
import { prodEndpointCreation } from "../../../../../globalUtils/createUrl.js";
|
||||
import { createLog } from "../../../../logger/logger.js";
|
||||
|
||||
export const postForecast = async (data: any, user: any) => {
|
||||
let endpoint = await prodEndpointCreation(
|
||||
"/public/v1.0/DemandManagement/DELFOR"
|
||||
);
|
||||
|
||||
//console.log(endpoint);
|
||||
//console.log(req.body.orders[0]);
|
||||
try {
|
||||
const results = await axios({
|
||||
url: endpoint,
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Basic ${user.prod}`,
|
||||
},
|
||||
// if a body is sent over it would be like below
|
||||
data: data,
|
||||
});
|
||||
|
||||
//console.log(results.data);
|
||||
//console.log(results.status);
|
||||
if (results.data.errors) {
|
||||
createLog(
|
||||
"error",
|
||||
"forecast",
|
||||
"logistics",
|
||||
`There was an error posting the Forecast: ${JSON.stringify(
|
||||
results.data.errors
|
||||
)}`
|
||||
);
|
||||
return {
|
||||
success: true,
|
||||
message: "Error processing forecast",
|
||||
data: results.data.errors[0].message,
|
||||
};
|
||||
}
|
||||
|
||||
if (results.status === 200) {
|
||||
createLog(
|
||||
"info",
|
||||
"forecast",
|
||||
"logistics",
|
||||
`Forcast was successfully posted: ${JSON.stringify(
|
||||
results.data
|
||||
)}`
|
||||
);
|
||||
return {
|
||||
success: true,
|
||||
message: "Success on posting forecast",
|
||||
data: results.data,
|
||||
};
|
||||
}
|
||||
} catch (error: any) {
|
||||
//console.log(`There is an error`, error);
|
||||
if (error) {
|
||||
//console.log(error.response.data);
|
||||
createLog(
|
||||
"error",
|
||||
"forecast",
|
||||
"logistics",
|
||||
`There was an error posting the Forecast: ${JSON.stringify(
|
||||
error.response.data
|
||||
)}`
|
||||
);
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error posting the Forecast",
|
||||
data: error.response.data,
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user