diff --git a/server/services/logistics/controller/returnMaterial.ts b/server/services/logistics/controller/returnMaterial.ts new file mode 100644 index 0000000..1ed210e --- /dev/null +++ b/server/services/logistics/controller/returnMaterial.ts @@ -0,0 +1,101 @@ +import { ConsoleLogWriter } from "drizzle-orm"; +import { prodEndpointCreation } from "../../../globalUtils/createUrl.js"; +import { createLog } from "../../logger/logger.js"; +import { query } from "../../sqlServer/prodSqlServer.js"; +import { labelData } from "../../sqlServer/querys/materialHelpers/labelInfo.js"; +import axios from "axios"; +import { laneInfo } from "../../sqlServer/querys/materialHelpers/laneInfo.js"; +import { tryCatch } from "../../../globalUtils/tryCatch.js"; + +type Data = { + runningNr: string; + laneName: string; +}; +export const returnMaterial = async (data: Data, prod: any) => { + const { runningNr, laneName } = data; + // replace the rn + const rnReplace = labelData.replaceAll("[rn]", runningNr); + + // get the lane id by name + const laneQuery = laneInfo.replaceAll("[laneName]", laneName); + let barcode; + // get the barcode from the running number + try { + barcode = await query(rnReplace, "labelData"); + } catch (error) { + console.log(error); + createLog( + "error", + prod.user.username, + "logistics", + `Error getting barcode: ${error}` + ); + } + + const { data: laneData, error: laneError } = await tryCatch( + query(laneQuery, "laneInfo") + ); + + if (laneError) { + return { + success: false, + message: + "The lane you entered is either deactivated or dose not exist.", + laneError, + }; + } + + if (laneData) { + return { + success: false, + message: + "The lane you entered is either deactivated or dose not exist.", + }; + } + + if (laneData.length === 0) { + return { + success: false, + message: + "The lane you entered is either deactivated or dose not exist.", + }; + } + + if (barcode.length === 0) { + return { + success: false, + message: "The running number you've is not in stock.", + }; + //throw Error("The provided runningNr is not in stock"); + } + // create the url to post + const url = await prodEndpointCreation( + "/public/v1.0/IssueMaterial/ReturnPartiallyConsumedManualMaterial" + ); + + const returnSomething = { + laneId: laneData[0]?.laneID, + barcode: barcode[0]?.barcode, + }; + + try { + const results = await axios.post(url, returnSomething, { + headers: { + "Content-Type": "application/json", + Authorization: `Basic ${prod.user.prod}`, + }, + }); + //console.log(results); + return { + success: true, + message: "Material was returned", + status: results.status, + }; + } catch (error: any) { + return { + success: false, + status: 200, + message: error.response?.data.errors[0].message, + }; + } +}; diff --git a/server/services/logistics/logisticsService.ts b/server/services/logistics/logisticsService.ts index bb9dd04..27a9f5a 100644 --- a/server/services/logistics/logisticsService.ts +++ b/server/services/logistics/logisticsService.ts @@ -1,9 +1,10 @@ -import {OpenAPIHono} from "@hono/zod-openapi"; +import { OpenAPIHono } from "@hono/zod-openapi"; import comsumeMaterial from "./route/consumeMaterial.js"; +import returnMat from "./route/returnMaterial.js"; const app = new OpenAPIHono(); -const routes = [comsumeMaterial] as const; +const routes = [comsumeMaterial, returnMat] as const; // app.route("/server", modules); const appRoutes = routes.forEach((route) => { diff --git a/server/services/logistics/route/returnMaterial.ts b/server/services/logistics/route/returnMaterial.ts new file mode 100644 index 0000000..7e49786 --- /dev/null +++ b/server/services/logistics/route/returnMaterial.ts @@ -0,0 +1,70 @@ +import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi"; +import { authMiddleware } from "../../auth/middleware/authMiddleware.js"; +import { apiHit } from "../../../globalUtils/apiHits.js"; +import { verify } from "hono/jwt"; +import { returnMaterial } from "../controller/returnMaterial.js"; + +const app = new OpenAPIHono(); + +const responseSchema = z.object({ + success: z.boolean().optional().openapi({ example: true }), + message: z.string().optional().openapi({ example: "user access" }), +}); + +app.openapi( + createRoute({ + tags: ["logistics"], + summary: "Retrns material based on its running number and laneName", + method: "post", + path: "/return", + middleware: authMiddleware, + description: + "Provided a running number and Lane to return the material.", + responses: { + 200: { + content: { "application/json": { schema: responseSchema } }, + description: "stopped", + }, + 400: { + content: { "application/json": { schema: responseSchema } }, + description: "Failed to stop", + }, + 401: { + content: { "application/json": { schema: responseSchema } }, + description: "Failed to stop", + }, + }, + }), + async (c) => { + apiHit(c, { endpoint: "api/sqlProd/close" }); + const authHeader = c.req.header("Authorization"); + const token = authHeader?.split("Bearer ")[1] || ""; + + try { + const payload = await verify(token, process.env.JWT_SECRET!); + try { + //return apiReturn(c, true, access?.message, access?.data, 200); + const data = await c.req.json(); + const consume = await returnMaterial(data, payload); + return c.json( + { success: consume?.success, message: consume?.message }, + 200 + ); + } catch (error) { + //console.log(error); + //return apiReturn(c, false, "Error in setting the user access", error, 400); + return c.json( + { + success: false, + message: "Missing data please try again", + error, + }, + 400 + ); + } + } catch (error) { + return c.json({ success: false, message: "Unauthorized" }, 401); + } + } +); +export default app; diff --git a/server/services/sqlServer/querys/materialHelpers/laneInfo.ts b/server/services/sqlServer/querys/materialHelpers/laneInfo.ts new file mode 100644 index 0000000..7d9bdc9 --- /dev/null +++ b/server/services/sqlServer/querys/materialHelpers/laneInfo.ts @@ -0,0 +1,7 @@ +export const laneInfo = ` +select IdLagerAbteilung as laneID, +Bezeichnung as laneName +from AlplaPROD_test1.dbo.T_LagerAbteilungen +where Aktiv = 1 +and Bezeichnung = [laneName] +`;