Redone
This commit is contained in:
@@ -1,34 +1,74 @@
|
|||||||
import {FastifyInstance} from "fastify";
|
import { FastifyInstance } from "fastify"
|
||||||
import {sortData} from "../utils/sort";
|
import { asc, desc } from "drizzle-orm"
|
||||||
|
import { sortData } from "../utils/sort"
|
||||||
|
|
||||||
|
// Schema imports
|
||||||
|
import { accounts, units,countrys } from "../../db/schema"
|
||||||
|
|
||||||
|
const TABLE_MAP: Record<string, any> = {
|
||||||
|
accounts,
|
||||||
|
units,
|
||||||
|
countrys,
|
||||||
|
}
|
||||||
|
|
||||||
export default async function resourceRoutesSpecial(server: FastifyInstance) {
|
export default async function resourceRoutesSpecial(server: FastifyInstance) {
|
||||||
// Liste
|
|
||||||
server.get("/resource-special/:resource", async (req, reply) => {
|
server.get("/resource-special/:resource", async (req, reply) => {
|
||||||
|
try {
|
||||||
if (!req.user?.tenant_id) {
|
if (!req.user?.tenant_id) {
|
||||||
return reply.code(400).send({ error: "No tenant selected" });
|
return reply.code(400).send({ error: "No tenant selected" })
|
||||||
}
|
}
|
||||||
|
|
||||||
const { resource } = req.params as { resource: string };
|
const { resource } = req.params as { resource: string }
|
||||||
|
|
||||||
if(!["accounts","units","countrys"].includes(resource)) return reply.code(400).send({ error: "No corrected special resource selected" });
|
// ❌ Wenn falsche Ressource
|
||||||
|
if (!TABLE_MAP[resource]) {
|
||||||
const {select, sort, asc } = req.query as { select?: string, sort?: string, asc?: string }
|
return reply.code(400).send({ error: "Invalid special resource" })
|
||||||
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 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
const table = TABLE_MAP[resource]
|
||||||
const sorted =sortData(data,sort,asc === "true" ? true : false)
|
|
||||||
|
|
||||||
return sorted;
|
const { select, sort, asc: ascQuery } = req.query as {
|
||||||
});
|
select?: string
|
||||||
|
sort?: string
|
||||||
|
asc?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------
|
||||||
|
// 📌 SELECT: wir ignorieren select string (wie Supabase)
|
||||||
|
// Drizzle kann kein dynamisches Select aus String!
|
||||||
|
// Wir geben IMMER alle Spalten zurück → kompatibel zum Frontend
|
||||||
|
// ---------------------------------------
|
||||||
|
|
||||||
|
let query = server.db.select().from(table)
|
||||||
|
|
||||||
|
// ---------------------------------------
|
||||||
|
// 📌 Sortierung
|
||||||
|
// ---------------------------------------
|
||||||
|
if (sort) {
|
||||||
|
const col = (table as any)[sort]
|
||||||
|
if (col) {
|
||||||
|
query =
|
||||||
|
ascQuery === "true"
|
||||||
|
? query.orderBy(asc(col))
|
||||||
|
: query.orderBy(desc(col))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await query
|
||||||
|
|
||||||
|
// Falls sort clientseitig wie früher notwendig ist:
|
||||||
|
const sorted = sortData(
|
||||||
|
data,
|
||||||
|
sort,
|
||||||
|
ascQuery === "true"
|
||||||
|
)
|
||||||
|
|
||||||
|
return sorted
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err)
|
||||||
|
return reply.code(500).send({ error: "Internal Server Error" })
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user