diff --git a/src/index.ts b/src/index.ts index c1a4fa7..ab73502 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ import authPlugin from "./plugins/auth"; import adminRoutes from "./routes/admin"; import corsPlugin from "./plugins/cors"; import resourceRoutes from "./routes/resources"; +import resourceRoutesSpecial from "./routes/resourcesSpecial"; import fastifyCookie from "@fastify/cookie"; import historyRoutes from "./routes/history"; import fileRoutes from "./routes/files"; @@ -48,6 +49,7 @@ async function main() { await subApp.register(tenantRoutes); await subApp.register(adminRoutes); await subApp.register(resourceRoutes); + await subApp.register(resourceRoutesSpecial); await subApp.register(historyRoutes); await subApp.register(fileRoutes); diff --git a/src/routes/resources.ts b/src/routes/resources.ts index 6a7d596..d48ec9e 100644 --- a/src/routes/resources.ts +++ b/src/routes/resources.ts @@ -475,14 +475,13 @@ export default async function resourceRoutes(server: FastifyInstance) { .select(select || dataTypes[resource].supabaseSelectWithInformation) .eq("tenant", req.user.tenant_id) .eq("archived", false) - - const sorted =sortData(data,sort,asc === "true" ? true : false) - if (error) { console.log(error) return reply.code(400).send({ error: error.message }); } + const sorted =sortData(data,sort,asc === "true" ? true : false) + return sorted; }); @@ -493,9 +492,11 @@ export default async function resourceRoutes(server: FastifyInstance) { } const { resource, id, with_information } = req.params as { resource: string; id: string, with_information: boolean }; + const {select } = req.query as { select?: string } + // @ts-ignore - const { data, error } = await server.supabase.from(resource).select(with_information ? dataTypes[resource].supabaseSelectWithInformation : "*") + const { data, error } = await server.supabase.from(resource).select(select ? select : (with_information ? dataTypes[resource].supabaseSelectWithInformation : "*")) .eq("id", id) .eq("tenant", req.user.tenant_id) .eq("archived", false) // nur aktive holen diff --git a/src/routes/resourcesSpecial.ts b/src/routes/resourcesSpecial.ts new file mode 100644 index 0000000..d50a33d --- /dev/null +++ b/src/routes/resourcesSpecial.ts @@ -0,0 +1,33 @@ +import {FastifyInstance} from "fastify"; +import {sortData} from "../utils/sort"; + + +export default async function resourceRoutesSpecial(server: FastifyInstance) { + // Liste + server.get("/resource-special/:resource", async (req, reply) => { + if (!req.user?.tenant_id) { + return reply.code(400).send({ error: "No tenant selected" }); + } + + const { resource } = req.params as { resource: string }; + + if(!["accounts","units","countrys"].includes(resource)) return reply.code(400).send({ error: "No corrected special resource selected" }); + + const {select, sort, asc } = req.query as { select?: string, sort?: string, asc?: string } + console.log(select, sort, asc) + + + const { data, error } = await server.supabase + .from(resource) + //@ts-ignore + .select(select || "*") + if (error) { + console.log(error) + return reply.code(400).send({ error: error.message }); + } + + const sorted =sortData(data,sort,asc === "true" ? true : false) + + return sorted; + }); +} \ No newline at end of file