123 lines
2.9 KiB
TypeScript
123 lines
2.9 KiB
TypeScript
import axios from "axios";
|
|
import { prodEndpointCreation } from "../../../../globalUtils/createUrl.js";
|
|
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
|
import { query } from "../../../sqlServer/prodSqlServer.js";
|
|
|
|
export const postAdjustment = async (data: any) => {
|
|
if (data.warehouseId === undefined) {
|
|
return {
|
|
sucess: false,
|
|
message: `Missing mandatory field: warehouseID`,
|
|
data: { error: `Missing mandatory field: warehouseID` },
|
|
};
|
|
}
|
|
|
|
if (data.laneId === undefined) {
|
|
return {
|
|
sucess: false,
|
|
message: `Missing mandatory field: locationID`,
|
|
data: { error: `Missing mandatory field: locationID` },
|
|
};
|
|
}
|
|
|
|
if (data.quantity == "0") {
|
|
return {
|
|
sucess: false,
|
|
message: `You entered 0 for the quantity to post, quantity needs to be at leave 1`,
|
|
data: {
|
|
error: `You entered 0 for the quantity to post, quantity needs to be at leave 1`,
|
|
},
|
|
};
|
|
}
|
|
|
|
const siloAdjustment = {
|
|
warehouseId: data.warehouseId,
|
|
laneId: data.laneId,
|
|
quantity: data.quantity,
|
|
};
|
|
|
|
// do we have warehousing turned on?
|
|
const { data: feature, error: featureError } = (await tryCatch(
|
|
query(
|
|
`SELECT [Id]
|
|
,[Feature]
|
|
,[Enabled]
|
|
,[ActivationDate]
|
|
FROM [test1_AlplaPROD2.0_Read].[support].[FeatureActivation] where [Feature] = 7`,
|
|
"feature switch check",
|
|
),
|
|
)) as any;
|
|
|
|
let prodUrl = "/public/v1.0/Warehousing/AdjustSiloStockLevel";
|
|
if (featureError) {
|
|
prodUrl = "/public/v1.0/Warehousing/AdjustSiloStockLevel";
|
|
}
|
|
|
|
if (feature?.data.length > 0) {
|
|
prodUrl = "/public/v1.1/Warehousing/Lane/AdjustSiloStockLevel";
|
|
}
|
|
// 1.0 "/public/v1.0/Warehousing/AdjustSiloStockLevel","
|
|
// 1.1 "/public/v1.1/Warehousing/Lane/AdjustSiloStockLevel"
|
|
|
|
let url = await prodEndpointCreation(prodUrl);
|
|
|
|
const { data: silo, error } = await tryCatch(
|
|
axios.post(url, siloAdjustment, {
|
|
headers: {
|
|
"X-API-Key": process.env.TEC_API_KEY || "",
|
|
"Content-Type": "application/json",
|
|
},
|
|
}),
|
|
);
|
|
let e = error as any;
|
|
if (e) {
|
|
console.log(e.response);
|
|
if (e.status === 401) {
|
|
const data = {
|
|
success: false,
|
|
message: `There was error posting the data: ${JSON.stringify(
|
|
e.response?.data,
|
|
)}`,
|
|
data: {
|
|
status: e.response?.status,
|
|
statusText: e.response?.statusText,
|
|
data: e.response?.data,
|
|
},
|
|
};
|
|
return data;
|
|
} else {
|
|
return {
|
|
success: false,
|
|
message: "Error in posting the silo adjustment.",
|
|
data: {
|
|
status: e.response?.status,
|
|
statusText: e.response?.statusText,
|
|
data: e.response?.data,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (silo?.status !== 200) {
|
|
return {
|
|
success: false,
|
|
message: "Error in posting the silo adjustment",
|
|
data: {
|
|
status: silo?.status,
|
|
statusText: silo?.statusText,
|
|
data: silo?.data,
|
|
},
|
|
};
|
|
} else {
|
|
return {
|
|
success: true,
|
|
message: "Adjustment was completed",
|
|
data: {
|
|
status: silo.status,
|
|
statusText: silo.statusText,
|
|
data: silo.data,
|
|
},
|
|
};
|
|
}
|
|
};
|