recator placement of code
This commit is contained in:
69
backend/datamart/datamart.routes.ts
Normal file
69
backend/datamart/datamart.routes.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { and, eq, gte, sql } from "drizzle-orm";
|
||||
import type { Express } from "express";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { datamart } from "../db/schema/datamart.schema.js";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import addQuery from "./datamartAdd.route.js";
|
||||
import updateQuery from "./datamartUpdate.route.js";
|
||||
import runQuery from "./getDatamart.route.js";
|
||||
|
||||
export const setupDatamartRoutes = (baseUrl: string, app: Express) => {
|
||||
// the sync callback.
|
||||
app.get(`${baseUrl}/api/datamart/sync`, async (req, res) => {
|
||||
const { time } = req.query;
|
||||
const now = new Date();
|
||||
|
||||
const minutes = parseInt(time as string, 10) || 15;
|
||||
const cutoff = new Date(now.getTime() - minutes * 60 * 1000);
|
||||
|
||||
const results = await db
|
||||
.select()
|
||||
.from(datamart)
|
||||
.where(time ? gte(datamart.upd_date, cutoff) : sql`true`);
|
||||
|
||||
return apiReturn(res, {
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "datamart",
|
||||
subModule: "query",
|
||||
message: `All Queries older than ${parseInt(process.env.QUERY_CHECK?.trim() || "15", 10)}min `,
|
||||
data: results,
|
||||
status: 200,
|
||||
});
|
||||
});
|
||||
|
||||
//setup all the routes
|
||||
|
||||
app.use(`${baseUrl}/api/datamart`, runQuery);
|
||||
app.use(`${baseUrl}/api/datamart`, addQuery);
|
||||
app.use(`${baseUrl}/api/datamart`, updateQuery);
|
||||
|
||||
// just sending a get on datamart will return all the queries that we can call.
|
||||
app.get(`${baseUrl}/api/datamart`, async (_, res) => {
|
||||
const queries = await db
|
||||
.select({
|
||||
id: datamart.id,
|
||||
name: datamart.name,
|
||||
description: datamart.description,
|
||||
options: datamart.options,
|
||||
version: datamart.version,
|
||||
upd_date: datamart.upd_date,
|
||||
})
|
||||
.from(datamart)
|
||||
.where(and(eq(datamart.active, true), eq(datamart.public, true)));
|
||||
|
||||
return apiReturn(
|
||||
res,
|
||||
{
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "datamart",
|
||||
subModule: "query",
|
||||
message: "All active queries we can run",
|
||||
data: queries,
|
||||
status: 200,
|
||||
},
|
||||
{ sheetName: 3 },
|
||||
);
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user