feat(ocme): added in ocme with endpoints base
This commit is contained in:
23
server/services/ocme/controller/getInfo.ts
Normal file
23
server/services/ocme/controller/getInfo.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import {db} from "../../../../database/dbclient.js";
|
||||
import {ocmeData} from "../../../../database/schema/ocme.js";
|
||||
import {differenceInMinutes} from "date-fns";
|
||||
import {createLog} from "../../logger/logger.js";
|
||||
|
||||
export const getInfo = async () => {
|
||||
let ocmeInfo: any = [];
|
||||
try {
|
||||
ocmeInfo = await db.select().from(ocmeData);
|
||||
|
||||
// add in the time difference
|
||||
ocmeInfo = ocmeInfo.map((o: any) => {
|
||||
const now = new Date(Date.now());
|
||||
const diff = differenceInMinutes(now, o.add_Date!);
|
||||
return {...o, waitingFor: diff};
|
||||
});
|
||||
} catch (error) {
|
||||
createLog("error", "ocme", "ocme", "There was an error trying to retrive the ocmeInfo.");
|
||||
throw Error("There was an error trying to retrive the.");
|
||||
}
|
||||
|
||||
return ocmeInfo;
|
||||
};
|
||||
13
server/services/ocme/controller/postRunningNr.ts
Normal file
13
server/services/ocme/controller/postRunningNr.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export const postLabelData = async (data: any) => {
|
||||
// if we have sscc we will do everything here and ignore the rn even it its sent over
|
||||
|
||||
if (data.sscc) {
|
||||
return {success: true, message: "sscc sent over", data: data};
|
||||
}
|
||||
|
||||
if (data.runningNr) {
|
||||
return {success: true, message: "runningNr sent over", data: data};
|
||||
} else {
|
||||
throw Error("Improper data was sent over");
|
||||
}
|
||||
};
|
||||
@@ -1,19 +1,19 @@
|
||||
import type {Context} from "hono";
|
||||
import {OpenAPIHono} from "@hono/zod-openapi";
|
||||
|
||||
export const ocmeService = async (c: Context) => {
|
||||
const url = new URL(c.req.url);
|
||||
// routes
|
||||
import getInfo from "./route/getInfo.js";
|
||||
import postRunningNr from "./route/postRunningNumber.js";
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
const ocmeUrl = `http://localhost:${process.env.OCME_PORT}${url.pathname.replace("/ocme", "")}`;
|
||||
const routes = [getInfo, postRunningNr] as const;
|
||||
|
||||
console.log(ocmeUrl);
|
||||
const ocmeResponse = await fetch(ocmeUrl, {
|
||||
method: c.req.method,
|
||||
headers: c.req.raw.headers,
|
||||
body: c.req.method !== "GET" && c.req.method !== "HEAD" ? await c.req.text() : undefined,
|
||||
});
|
||||
// app.route("/server", modules);
|
||||
const appRoutes = routes.forEach((route) => {
|
||||
app.route("/api/v1", route);
|
||||
});
|
||||
|
||||
return new Response(ocmeResponse.body, {
|
||||
status: ocmeResponse.status,
|
||||
headers: ocmeResponse.headers,
|
||||
});
|
||||
};
|
||||
app.all("/api/v1/*", (c) => {
|
||||
return c.json({success: false, message: "you have encounted an ocme route that dose not exist."});
|
||||
});
|
||||
|
||||
export default app;
|
||||
|
||||
79
server/services/ocme/route/getInfo.ts
Normal file
79
server/services/ocme/route/getInfo.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
|
||||
import {getInfo} from "../controller/getInfo.js";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
const AddSetting = z.object({
|
||||
name: z.string().openapi({example: "server"}),
|
||||
value: z.string().openapi({example: "localhost"}),
|
||||
description: z.string().openapi({example: "The server we are going to connect to"}),
|
||||
roles: z.string().openapi({example: "admin"}),
|
||||
module: z.string().openapi({example: "production"}),
|
||||
});
|
||||
|
||||
app.openapi(
|
||||
createRoute({
|
||||
tags: ["ocme"],
|
||||
summary: "Get all current info",
|
||||
method: "get",
|
||||
path: "/getinfo",
|
||||
request: {
|
||||
body: {
|
||||
content: {
|
||||
"application/json": {schema: AddSetting},
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
success: z.boolean().openapi({example: true}),
|
||||
message: z.string().openapi({example: "Starter"}),
|
||||
data: z.array(z.object({})).optional().openapi({example: []}),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Response message",
|
||||
},
|
||||
400: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
success: z.boolean().openapi({example: false}),
|
||||
message: z.string().optional().openapi({example: "Internal Server error"}),
|
||||
data: z.array(z.object({})).optional().openapi({example: []}),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Internal Server Error",
|
||||
},
|
||||
// 401: {
|
||||
// content: {
|
||||
// "application/json": {
|
||||
// schema: z.object({message: z.string().optional().openapi({example: "Unauthenticated"})}),
|
||||
// },
|
||||
// },
|
||||
// description: "Unauthorized",
|
||||
// },
|
||||
// 500: {
|
||||
// content: {
|
||||
// "application/json": {
|
||||
// schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
|
||||
// },
|
||||
// },
|
||||
// description: "Internal Server Error",
|
||||
// },
|
||||
},
|
||||
}),
|
||||
async (c) => {
|
||||
// make sure we have a vaid user being accessed thats really logged in
|
||||
try {
|
||||
return c.json({success: true, message: "Ocme Info", data: await getInfo()}, 200);
|
||||
} catch (error) {
|
||||
return c.json({success: false, message: "There was an error getting ocmeInfo data", data: error}, 400);
|
||||
}
|
||||
}
|
||||
);
|
||||
export default app;
|
||||
81
server/services/ocme/route/postRunningNumber.ts
Normal file
81
server/services/ocme/route/postRunningNumber.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
|
||||
import {getInfo} from "../controller/getInfo.js";
|
||||
import {postLabelData} from "../controller/postRunningNr.js";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
const PostRunningNr = z.object({
|
||||
sscc: z.string().optional().openapi({example: "00090103830005710997"}),
|
||||
runningNr: z.string().optional().openapi({example: "localhost"}),
|
||||
areaFrom: z.string().optional().openapi({example: "The server we are going to connect to"}),
|
||||
completed: z.boolean().optional().openapi({example: true}),
|
||||
});
|
||||
|
||||
app.openapi(
|
||||
createRoute({
|
||||
tags: ["ocme"],
|
||||
summary: "Post New running number to be picked up.",
|
||||
method: "post",
|
||||
path: "/postrunningnumber",
|
||||
request: {
|
||||
body: {
|
||||
content: {
|
||||
"application/json": {schema: PostRunningNr},
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
success: z.boolean().openapi({example: true}),
|
||||
message: z.string().openapi({example: "Starter"}),
|
||||
data: z.array(z.object({})).optional().openapi({example: []}),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Response message",
|
||||
},
|
||||
400: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
success: z.boolean().openapi({example: false}),
|
||||
message: z.string().optional().openapi({example: "Internal Server error"}),
|
||||
data: z.array(z.object({})).optional().openapi({example: []}),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Internal Server Error",
|
||||
},
|
||||
// 401: {
|
||||
// content: {
|
||||
// "application/json": {
|
||||
// schema: z.object({message: z.string().optional().openapi({example: "Unauthenticated"})}),
|
||||
// },
|
||||
// },
|
||||
// description: "Unauthorized",
|
||||
// },
|
||||
// 500: {
|
||||
// content: {
|
||||
// "application/json": {
|
||||
// schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
|
||||
// },
|
||||
// },
|
||||
// description: "Internal Server Error",
|
||||
// },
|
||||
},
|
||||
}),
|
||||
async (c) => {
|
||||
// make sure we have a vaid user being accessed thats really logged in
|
||||
try {
|
||||
const data = await c.req.json();
|
||||
const postPallet = await postLabelData(data);
|
||||
return c.json({success: postPallet.success, message: postPallet.message, data: postPallet.data}, 200);
|
||||
} catch (error) {
|
||||
return c.json({success: false, message: "There was an error getting ocmeInfo data", data: error}, 400);
|
||||
}
|
||||
}
|
||||
);
|
||||
export default app;
|
||||
Reference in New Issue
Block a user