diff --git a/backend/src/datamart/datamart.controller.ts b/backend/src/datamart/datamart.controller.ts index df5a1d8..e973705 100644 --- a/backend/src/datamart/datamart.controller.ts +++ b/backend/src/datamart/datamart.controller.ts @@ -1,6 +1,6 @@ /** * each endpoint will be something like - * /api/datamart/{name}?{options} + * /api/datamart/{name}?{criteria} * * when getting the current queries we will need to map through the available queries we currently have and send back. * example @@ -8,7 +8,7 @@ * "name": "getopenorders", * "endpoint": "/api/datamart/getopenorders", * "description": "Returns open orders based on day count sent over, sDay 15 days in the past eDay 5 days in the future, can be left empty for this default days", - * "criteria": "sDay,eDay" + * "options": "sDay,eDay" * }, * * when a criteria is password over we will handle it by counting how many were passed up to 3 then deal with each one respectively diff --git a/backend/src/datamart/datamartAdd.route.ts b/backend/src/datamart/datamartAdd.route.ts index ff18d86..bc4ba3a 100644 --- a/backend/src/datamart/datamartAdd.route.ts +++ b/backend/src/datamart/datamartAdd.route.ts @@ -1,18 +1,54 @@ import { Router } from "express"; +import z from "zod"; +import type { NewDatamart } from "../db/schema/datamart.schema.js"; import { apiReturn } from "../utils/returnHelper.utils.js"; const r = Router(); -r.post("/add", async (_, res) => { - apiReturn(res, { - success: true, - level: "info", - module: "routes", - subModule: "prodSql", - message: "connect.message", - data: [{ connect: "" }], - status: 200, - }); +const newQuery = z.object({ + name: z.string().min(5), + description: z.string().min(30), + query: z.string().min(10), + options: z + .string() + .describe("This should be a set of keys separated by a comma") + .optional(), +}); + +r.post("/add", async (req, res) => { + try { + const v = newQuery.parse(req.body); + + const query: NewDatamart = { ...v }; + } catch (err) { + if (err instanceof z.ZodError) { + const flattened = z.flattenError(err); + // return res.status(400).json({ + // error: "Validation failed", + // details: flattened, + // }); + + return apiReturn(res, { + success: false, + level: "error", //connect.success ? "info" : "error", + module: "routes", + subModule: "auth", + message: "Validation failed", + data: [flattened.fieldErrors], + status: 400, //connect.success ? 200 : 400, + }); + } + + return apiReturn(res, { + success: true, + level: "info", + module: "routes", + subModule: "prodSql", + message: "connect.message", + data: [{ connect: "" }], + status: 200, + }); + } }); export default r; diff --git a/backend/src/db/schema/datamart.schema.ts b/backend/src/db/schema/datamart.schema.ts index 421445b..3d724a0 100644 --- a/backend/src/db/schema/datamart.schema.ts +++ b/backend/src/db/schema/datamart.schema.ts @@ -14,7 +14,7 @@ export const datamart = pgTable("datamart", { name: text("name"), description: text("description").notNull(), query: text("query"), - version: integer("version").notNull(), + version: integer("version").default(1).notNull(), active: boolean("active").default(true), options: text("checked").default(""), add_date: timestamp("add_date").defaultNow(), diff --git a/backend/src/utils/returnHelper.utils.ts b/backend/src/utils/returnHelper.utils.ts index a8c0bf5..6cefefa 100644 --- a/backend/src/utils/returnHelper.utils.ts +++ b/backend/src/utils/returnHelper.utils.ts @@ -11,7 +11,8 @@ interface Data { | "prodSql" | "query" | "sendmail" - | "auth"; + | "auth" + | "datamart"; level: "info" | "error" | "debug" | "fatal"; message: string; data?: unknown[];