refactor(bulk orders): improvments and changes that i wanted in but missed

This commit is contained in:
2025-04-18 16:27:38 -05:00
parent dcfa96b710
commit ecb07c7e7c
4 changed files with 39 additions and 16 deletions

View File

@@ -50,6 +50,7 @@ export const standardOrders = async (data: any, user: any) => {
}; };
} }
const plantToken = s.filter((s) => s.name === "plantToken"); const plantToken = s.filter((s) => s.name === "plantToken");
const arrayBuffer = await data.arrayBuffer(); const arrayBuffer = await data.arrayBuffer();
const buffer = Buffer.from(arrayBuffer); const buffer = Buffer.from(arrayBuffer);
@@ -110,11 +111,15 @@ export const standardOrders = async (data: any, user: any) => {
// map everything out for each order // map everything out for each order
const nOrder = newOrders.map((o: any) => { const nOrder = newOrders.map((o: any) => {
const invoice = i.filter(
(i: any) => i.deliveryAddress === parseInt(customerID)
);
if (!invoice) {
return;
}
return { return {
customerId: parseInt(customerID), customerId: parseInt(customerID),
invoiceAddressId: i.filter( invoiceAddressId: invoice[0].invoiceAddress, // matched to the default invoice address
(i: any) => i.deliveryAddress === parseInt(customerID)
)[0].invoiceAddress, // matched to the default invoice address
customerOrderNo: o.CustomerOrderNumber, customerOrderNo: o.CustomerOrderNumber,
orderDate: new Date(Date.now()).toLocaleString("en-US"), orderDate: new Date(Date.now()).toLocaleString("en-US"),
positions: [ positions: [
@@ -143,8 +148,10 @@ export const standardOrders = async (data: any, user: any) => {
postedOrders.push({ postedOrders.push({
customer: customerID, customer: customerID,
//totalOrders: orders?.length(),
success: posting.success, success: posting.success,
message: posting.message, message: posting.message,
data: posting.data,
}); });
} }

View File

@@ -1,5 +1,5 @@
import { abbottOrders } from "./customMappings/abbottTruckList.js"; import { abbottOrders } from "./mappings/abbottTruckList.js";
import { standardOrders } from "./customMappings/standardOrders.js"; import { standardOrders } from "./mappings/standardOrders.js";
export const ordersIn = async (data: any, user: any) => { export const ordersIn = async (data: any, user: any) => {
/** /**
@@ -15,7 +15,7 @@ export const ordersIn = async (data: any, user: any) => {
// run the standard orders in // run the standard orders in
const standard = await standardOrders(data["postOrders"], user); const standard = await standardOrders(data["postOrders"], user);
success = standard.success ?? false; success = standard.success ?? false;
message = standard.message ?? "Error posting Abbott Orders"; message = standard.message ?? "Error posting Standard Orders";
orderData = standard.data; orderData = standard.data;
} }

View File

@@ -2,6 +2,7 @@ import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { responses } from "../../../../globalUtils/routeDefs/responses.js"; import { responses } from "../../../../globalUtils/routeDefs/responses.js";
import { ordersIn } from "../../controller/dm/ordersIn/ordersIn.js"; import { ordersIn } from "../../controller/dm/ordersIn/ordersIn.js";
import { verify } from "hono/jwt"; import { verify } from "hono/jwt";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
const app = new OpenAPIHono(); const app = new OpenAPIHono();
@@ -39,19 +40,34 @@ app.openapi(
// if (body["fileType"] === "standard") { // if (body["fileType"] === "standard") {
// console.log(`doing standard orders in.`); // console.log(`doing standard orders in.`);
// } // }
try { const { data: payload, error: pe } = await tryCatch(
const payload = await verify(token, process.env.JWT_SECRET!); verify(token, process.env.JWT_SECRET!)
const orders = await ordersIn(body, payload.user); );
return c.json({ if (pe) {
success: orders.success,
message: orders.message,
data: orders.data,
});
} catch (error) {
console.log(error);
return c.json({ success: false, message: "Unauthorized" }, 401); return c.json({ success: false, message: "Unauthorized" }, 401);
} }
const { data: orders, error } = await tryCatch(
ordersIn(body, payload.user)
);
if (error) {
return c.json(
{
success: false,
message: "Error posting forecast",
data: error,
},
400
);
}
return c.json({
success: orders?.success ?? false,
message: orders?.message ?? "Error posting forecast",
data: orders?.data ?? [],
});
} }
); );
export default app; export default app;