/** * each endpoint will be something like * /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 *{ * "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", * "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 */ import { eq } from "drizzle-orm"; import { db } from "../db/db.controller.js"; import { datamart } from "../db/schema/datamart.schema.js"; import { prodQuery } from "../prodSql/prodSqlQuery.controller.js"; import { returnFunc } from "../utils/returnHelper.utils.js"; import { tryCatch } from "../utils/trycatch.utils.js"; type Data = { name: string; options: string; }; export const runDatamartQuery = async (data: Data) => { // search the query db for the query by name const { data: queryInfo, error: qIe } = await tryCatch( db.select().from(datamart).where(eq(datamart.name, data.name)), ); if (qIe) { return returnFunc({ success: false, level: "error", module: "datamart", subModule: "query", message: `Error getting ${data.name} info`, data: [qIe], notify: false, }); } // create the query with no changed just to have it here let datamartQuery = queryInfo[0]?.query || ""; // split the criteria by "," then and then update the query if (data.options !== "") { const params = new URLSearchParams(data.options); for (const [rawKey, rawValue] of params.entries()) { const key = rawKey.trim(); const value = rawValue.trim(); datamartQuery = datamartQuery.replaceAll(`[${key}]`, value); } } const { data: queryRun, error } = await tryCatch( prodQuery(datamartQuery, `Running datamart query: ${data.name}`), ); if (error) { return returnFunc({ success: false, level: "error", module: "datamart", subModule: "query", message: `Data for: ${data.name} encountered an error while trying to get it`, data: [error], notify: false, }); } if (!queryRun.success) { return returnFunc({ success: false, level: "error", module: "datamart", subModule: "query", message: queryRun.message, data: queryRun.data, notify: false, }); } return returnFunc({ success: true, level: "info", module: "datamart", subModule: "query", message: `Data for: ${data.name}`, data: queryRun.data, notify: false, }); };