Converted Routes
This commit is contained in:
328
src/routes/resources/customers.ts
Normal file
328
src/routes/resources/customers.ts
Normal file
@@ -0,0 +1,328 @@
|
||||
import { FastifyInstance } from "fastify"
|
||||
import {
|
||||
eq,
|
||||
ilike,
|
||||
asc,
|
||||
desc,
|
||||
and, count, inArray,
|
||||
} from "drizzle-orm"
|
||||
|
||||
import {
|
||||
customers,
|
||||
projects,
|
||||
plants,
|
||||
contracts,
|
||||
contacts,
|
||||
createddocuments,
|
||||
statementallocations,
|
||||
files,
|
||||
events,
|
||||
} from "../../../db/schema" // dein zentraler index.ts Export
|
||||
|
||||
export default async function customerRoutes(server: FastifyInstance) {
|
||||
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// LIST
|
||||
// -------------------------------------------------------------
|
||||
server.get("/resource/customers", async (req, reply) => {
|
||||
const tenantId = req.user?.tenant_id
|
||||
if (!tenantId) return reply.code(400).send({ error: "No tenant selected" })
|
||||
|
||||
const { search, sort, asc: ascQuery } = req.query as {
|
||||
search?: string
|
||||
sort?: string
|
||||
asc?: string
|
||||
}
|
||||
|
||||
// Basisquery
|
||||
let baseQuery = server.db
|
||||
.select()
|
||||
.from(customers)
|
||||
.where(eq(customers.tenant, tenantId))
|
||||
|
||||
// Suche
|
||||
if (search) {
|
||||
baseQuery = server.db
|
||||
.select()
|
||||
.from(customers)
|
||||
.where(
|
||||
and(
|
||||
eq(customers.tenant, tenantId),
|
||||
ilike(customers.name, `%${search}%`)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
// Sortierung
|
||||
if (sort) {
|
||||
const field = (customers as any)[sort]
|
||||
if (field) {
|
||||
// @ts-ignore
|
||||
baseQuery = baseQuery.orderBy(
|
||||
ascQuery === "true" ? asc(field) : desc(field)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const customerList = await baseQuery
|
||||
|
||||
return customerList
|
||||
})
|
||||
|
||||
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// PAGINATED
|
||||
// -------------------------------------------------------------
|
||||
server.get("/resource/customers/paginated", async (req, reply) => {
|
||||
try {
|
||||
const tenantId = req.user?.tenant_id
|
||||
if (!tenantId) {
|
||||
return reply.code(400).send({ error: "No tenant selected" })
|
||||
}
|
||||
|
||||
const queryConfig = req.queryConfig
|
||||
const {
|
||||
pagination,
|
||||
sort,
|
||||
filters,
|
||||
paginationDisabled
|
||||
} = queryConfig
|
||||
|
||||
const {
|
||||
select,
|
||||
search,
|
||||
searchColumns,
|
||||
distinctColumns
|
||||
} = req.query as {
|
||||
select?: string
|
||||
search?: string
|
||||
searchColumns?: string
|
||||
distinctColumns?: string
|
||||
}
|
||||
|
||||
// ----------------------------
|
||||
// WHERE CONDITIONS
|
||||
// ----------------------------
|
||||
let whereCond: any = eq(customers.tenant, tenantId)
|
||||
|
||||
if (filters) {
|
||||
for (const [key, val] of Object.entries(filters)) {
|
||||
const col = (customers as any)[key]
|
||||
if (!col) continue
|
||||
|
||||
if (Array.isArray(val)) {
|
||||
whereCond = and(whereCond, inArray(col, val))
|
||||
} else if (val === true || val === false || val === null) {
|
||||
whereCond = and(whereCond, eq(col, val))
|
||||
} else {
|
||||
whereCond = and(whereCond, eq(col, val))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (search && search.trim().length > 0) {
|
||||
const searchTerm = `%${search.trim().toLowerCase()}%`
|
||||
whereCond = and(
|
||||
whereCond,
|
||||
ilike(customers.name, searchTerm)
|
||||
)
|
||||
}
|
||||
|
||||
// ----------------------------
|
||||
// COUNT FIX (Drizzle-safe)
|
||||
// ----------------------------
|
||||
const totalRes = await server.db
|
||||
.select({ value: count(customers.id) })
|
||||
.from(customers)
|
||||
.where(whereCond)
|
||||
|
||||
const total = Number(totalRes[0]?.value ?? 0)
|
||||
|
||||
// ----------------------------
|
||||
// DISTINCT VALUES (optional)
|
||||
// ----------------------------
|
||||
const distinctValues: Record<string, any[]> = {}
|
||||
|
||||
if (distinctColumns) {
|
||||
for (const colName of distinctColumns.split(",").map(v => v.trim())) {
|
||||
const col = (customers as any)[colName]
|
||||
if (!col) continue
|
||||
|
||||
const rows = await server.db
|
||||
.select({ v: col })
|
||||
.from(customers)
|
||||
.where(eq(customers.tenant, tenantId))
|
||||
|
||||
const values = rows
|
||||
.map(r => r.v)
|
||||
.filter(v => v != null && v !== "")
|
||||
|
||||
distinctValues[colName] = [...new Set(values)].sort()
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------
|
||||
// PAGINATION
|
||||
// ----------------------------
|
||||
let offset = 0
|
||||
let limit = 999999
|
||||
|
||||
if (!paginationDisabled && pagination) {
|
||||
offset = pagination.offset
|
||||
limit = pagination.limit
|
||||
}
|
||||
|
||||
// ----------------------------
|
||||
// ORDER BY
|
||||
// ----------------------------
|
||||
let orderField = null
|
||||
let orderDirection: "asc" | "desc" = "asc"
|
||||
|
||||
if (sort?.length > 0) {
|
||||
const s = sort[0]
|
||||
const col = (customers as any)[s.field]
|
||||
if (col) {
|
||||
orderField = col
|
||||
orderDirection = s.direction === "asc" ? "asc" : "desc"
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------
|
||||
// QUERY DATA
|
||||
// ----------------------------
|
||||
let dataQuery = server.db
|
||||
.select()
|
||||
.from(customers)
|
||||
.where(whereCond)
|
||||
.offset(offset)
|
||||
.limit(limit)
|
||||
|
||||
if (orderField) {
|
||||
dataQuery =
|
||||
orderDirection === "asc"
|
||||
? dataQuery.orderBy(asc(orderField))
|
||||
: dataQuery.orderBy(desc(orderField))
|
||||
}
|
||||
|
||||
const data = await dataQuery
|
||||
|
||||
// ----------------------------
|
||||
// BUILD RETURN CONFIG
|
||||
// ----------------------------
|
||||
const totalPages = pagination?.limit
|
||||
? Math.ceil(total / pagination.limit)
|
||||
: 1
|
||||
|
||||
const enrichedConfig = {
|
||||
...queryConfig,
|
||||
total,
|
||||
totalPages,
|
||||
distinctValues,
|
||||
search: search || null,
|
||||
}
|
||||
|
||||
return {
|
||||
data,
|
||||
queryConfig: enrichedConfig,
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// DETAIL (mit ALLEN JOINS)
|
||||
// -------------------------------------------------------------
|
||||
server.get("/resource/customers/:id", async (req, reply) => {
|
||||
const { id } = req.params as { id: string }
|
||||
const tenantId = req.user?.tenant_id
|
||||
|
||||
if (!tenantId) return reply.code(400).send({ error: "No tenant selected" })
|
||||
|
||||
// --- 1) Customer selbst laden
|
||||
const customerRecord = await server.db
|
||||
.select()
|
||||
.from(customers)
|
||||
.where(and(eq(customers.id, Number(id)), eq(customers.tenant, tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!customerRecord.length) {
|
||||
return reply.code(404).send({ error: "Customer not found" })
|
||||
}
|
||||
|
||||
const customer = customerRecord[0]
|
||||
|
||||
|
||||
// --- 2) Relations:
|
||||
const [
|
||||
customerProjects,
|
||||
customerPlants,
|
||||
customerContracts,
|
||||
customerContacts,
|
||||
customerDocuments,
|
||||
customerFiles,
|
||||
customerEvents,
|
||||
] = await Promise.all([
|
||||
// Projekte, die dem Kunden zugeordnet sind
|
||||
server.db
|
||||
.select()
|
||||
.from(projects)
|
||||
.where(eq(projects.customer, Number(id))),
|
||||
|
||||
server.db
|
||||
.select()
|
||||
.from(plants)
|
||||
.where(eq(plants.customer, Number(id))),
|
||||
|
||||
server.db
|
||||
.select()
|
||||
.from(contracts)
|
||||
.where(eq(contracts.customer, Number(id))),
|
||||
|
||||
server.db
|
||||
.select()
|
||||
.from(contacts)
|
||||
.where(eq(contacts.customer, Number(id))),
|
||||
|
||||
// createddocuments + inner join statementallocations
|
||||
server.db
|
||||
.select({
|
||||
...createddocuments,
|
||||
allocations: statementallocations,
|
||||
})
|
||||
.from(createddocuments)
|
||||
.leftJoin(
|
||||
statementallocations,
|
||||
eq(statementallocations.cd_id, createddocuments.id)
|
||||
)
|
||||
.where(eq(createddocuments.customer, Number(id))),
|
||||
|
||||
server.db
|
||||
.select()
|
||||
.from(files)
|
||||
.where(eq(files.customer, Number(id))),
|
||||
|
||||
server.db
|
||||
.select()
|
||||
.from(events)
|
||||
.where(eq(events.customer, Number(id))),
|
||||
])
|
||||
|
||||
return {
|
||||
...customer,
|
||||
projects: customerProjects,
|
||||
plants: customerPlants,
|
||||
contracts: customerContracts,
|
||||
contacts: customerContacts,
|
||||
createddocuments: customerDocuments,
|
||||
files: customerFiles,
|
||||
events: customerEvents,
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user