refactor(bulk orders): improvments and changes that i wanted in but missed
This commit is contained in:
@@ -50,6 +50,7 @@ export const standardOrders = async (data: any, user: any) => {
|
||||
};
|
||||
}
|
||||
const plantToken = s.filter((s) => s.name === "plantToken");
|
||||
|
||||
const arrayBuffer = await data.arrayBuffer();
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
|
||||
@@ -110,11 +111,15 @@ export const standardOrders = async (data: any, user: any) => {
|
||||
|
||||
// map everything out for each order
|
||||
const nOrder = newOrders.map((o: any) => {
|
||||
const invoice = i.filter(
|
||||
(i: any) => i.deliveryAddress === parseInt(customerID)
|
||||
);
|
||||
if (!invoice) {
|
||||
return;
|
||||
}
|
||||
return {
|
||||
customerId: parseInt(customerID),
|
||||
invoiceAddressId: i.filter(
|
||||
(i: any) => i.deliveryAddress === parseInt(customerID)
|
||||
)[0].invoiceAddress, // matched to the default invoice address
|
||||
invoiceAddressId: invoice[0].invoiceAddress, // matched to the default invoice address
|
||||
customerOrderNo: o.CustomerOrderNumber,
|
||||
orderDate: new Date(Date.now()).toLocaleString("en-US"),
|
||||
positions: [
|
||||
@@ -143,8 +148,10 @@ export const standardOrders = async (data: any, user: any) => {
|
||||
|
||||
postedOrders.push({
|
||||
customer: customerID,
|
||||
//totalOrders: orders?.length(),
|
||||
success: posting.success,
|
||||
message: posting.message,
|
||||
data: posting.data,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { abbottOrders } from "./customMappings/abbottTruckList.js";
|
||||
import { standardOrders } from "./customMappings/standardOrders.js";
|
||||
import { abbottOrders } from "./mappings/abbottTruckList.js";
|
||||
import { standardOrders } from "./mappings/standardOrders.js";
|
||||
|
||||
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
|
||||
const standard = await standardOrders(data["postOrders"], user);
|
||||
success = standard.success ?? false;
|
||||
message = standard.message ?? "Error posting Abbott Orders";
|
||||
message = standard.message ?? "Error posting Standard Orders";
|
||||
orderData = standard.data;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
|
||||
import { ordersIn } from "../../controller/dm/ordersIn/ordersIn.js";
|
||||
import { verify } from "hono/jwt";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
@@ -39,19 +40,34 @@ app.openapi(
|
||||
// if (body["fileType"] === "standard") {
|
||||
// console.log(`doing standard orders in.`);
|
||||
// }
|
||||
try {
|
||||
const payload = await verify(token, process.env.JWT_SECRET!);
|
||||
const orders = await ordersIn(body, payload.user);
|
||||
const { data: payload, error: pe } = await tryCatch(
|
||||
verify(token, process.env.JWT_SECRET!)
|
||||
);
|
||||
|
||||
return c.json({
|
||||
success: orders.success,
|
||||
message: orders.message,
|
||||
data: orders.data,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
if (pe) {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user