Compare commits
6 Commits
main
...
e7fb2df5c7
| Author | SHA1 | Date | |
|---|---|---|---|
| e7fb2df5c7 | |||
| f27fd3f6da | |||
| d3e2b106af | |||
| 769d2059ca | |||
| 53349fae83 | |||
| d8eb1559c8 |
@@ -10,30 +10,23 @@ import {
|
||||
or
|
||||
} from "drizzle-orm"
|
||||
|
||||
|
||||
|
||||
import {resourceConfig} from "../../utils/resource.config";
|
||||
import {useNextNumberRangeNumber} from "../../utils/functions";
|
||||
import {stafftimeentries} from "../../../db/schema";
|
||||
import { resourceConfig } from "../../utils/resource.config";
|
||||
import { useNextNumberRangeNumber } from "../../utils/functions";
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// SQL Volltextsuche auf mehreren Feldern
|
||||
// SQL Suche auf mehreren Feldern (Haupttabelle + Relationen)
|
||||
// -------------------------------------------------------------
|
||||
|
||||
|
||||
function buildSearchCondition(table: any, columns: string[], search: string) {
|
||||
function buildSearchCondition(columns: any[], search: string) {
|
||||
if (!search || !columns.length) return null
|
||||
|
||||
const term = `%${search.toLowerCase()}%`
|
||||
|
||||
const conditions = columns
|
||||
.map((colName) => table[colName])
|
||||
.filter(Boolean)
|
||||
.map((col) => ilike(col, term))
|
||||
|
||||
if (conditions.length === 0) return null
|
||||
|
||||
// @ts-ignore
|
||||
return or(...conditions)
|
||||
}
|
||||
|
||||
@@ -54,96 +47,86 @@ export default async function resourceRoutes(server: FastifyInstance) {
|
||||
asc?: string
|
||||
}
|
||||
|
||||
const {resource} = req.params as {resource: string}
|
||||
const table = resourceConfig[resource].table
|
||||
const { resource } = req.params as { resource: string }
|
||||
const config = resourceConfig[resource]
|
||||
const table = config.table
|
||||
|
||||
// WHERE-Basis
|
||||
let whereCond: any = eq(table.tenant, tenantId)
|
||||
let q = server.db.select().from(table).$dynamic()
|
||||
|
||||
// 🔍 SQL Search
|
||||
if(search) {
|
||||
const searchCond = buildSearchCondition(
|
||||
table,
|
||||
resourceConfig[resource].searchColumns,
|
||||
search.trim()
|
||||
)
|
||||
const searchCols: any[] = (config.searchColumns || []).map(c => table[c])
|
||||
|
||||
if (searchCond) {
|
||||
whereCond = and(whereCond, searchCond)
|
||||
}
|
||||
if (config.mtoLoad) {
|
||||
config.mtoLoad.forEach(rel => {
|
||||
const relConfig = resourceConfig[rel + "s"] || resourceConfig[rel]
|
||||
if (relConfig) {
|
||||
const relTable = relConfig.table
|
||||
|
||||
// FIX: Nur joinen, wenn es keine Self-Reference ist (verhindert ERROR 42712)
|
||||
if (relTable !== table) {
|
||||
// @ts-ignore
|
||||
q = q.leftJoin(relTable, eq(table[rel], relTable.id))
|
||||
if (relConfig.searchColumns) {
|
||||
relConfig.searchColumns.forEach(c => {
|
||||
if (relTable[c]) searchCols.push(relTable[c])
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Base Query
|
||||
let q = server.db.select().from(table).where(whereCond)
|
||||
if (search) {
|
||||
const searchCond = buildSearchCondition(searchCols, search.trim())
|
||||
if (searchCond) whereCond = and(whereCond, searchCond)
|
||||
}
|
||||
|
||||
q = q.where(whereCond)
|
||||
|
||||
// Sortierung
|
||||
if (sort) {
|
||||
const col = (table as any)[sort]
|
||||
if (col) {
|
||||
//@ts-ignore
|
||||
q = ascQuery === "true"
|
||||
? q.orderBy(asc(col))
|
||||
: q.orderBy(desc(col))
|
||||
q = ascQuery === "true" ? q.orderBy(asc(col)) : q.orderBy(desc(col))
|
||||
}
|
||||
}
|
||||
|
||||
const queryData = await q
|
||||
// Transformation: Falls Joins genutzt wurden, das Hauptobjekt extrahieren
|
||||
const rows = queryData.map(r => r[resource] || r.table || r);
|
||||
|
||||
// RELATION LOADING (MANY-TO-ONE)
|
||||
|
||||
let ids = {}
|
||||
let lists = {}
|
||||
let maps = {}
|
||||
let data = [...queryData]
|
||||
|
||||
if(resourceConfig[resource].mtoLoad) {
|
||||
resourceConfig[resource].mtoLoad.forEach(relation => {
|
||||
ids[relation] = [...new Set(queryData.map(r => r[relation]).filter(Boolean))];
|
||||
// RELATION LOADING
|
||||
let data = [...rows]
|
||||
if(config.mtoLoad) {
|
||||
let ids: any = {}
|
||||
let lists: any = {}
|
||||
let maps: any = {}
|
||||
config.mtoLoad.forEach(rel => {
|
||||
ids[rel] = [...new Set(rows.map(r => r[rel]).filter(Boolean))];
|
||||
})
|
||||
|
||||
for await (const relation of resourceConfig[resource].mtoLoad ) {
|
||||
console.log(relation)
|
||||
lists[relation] = ids[relation].length ? await server.db.select().from(resourceConfig[relation + "s"].table).where(inArray(resourceConfig[relation + "s"].table.id, ids[relation])) : []
|
||||
|
||||
for await (const rel of config.mtoLoad) {
|
||||
const relConf = resourceConfig[rel + "s"] || resourceConfig[rel];
|
||||
const relTab = relConf.table
|
||||
lists[rel] = ids[rel].length ? await server.db.select().from(relTab).where(inArray(relTab.id, ids[rel])) : []
|
||||
maps[rel] = Object.fromEntries(lists[rel].map((i: any) => [i.id, i]));
|
||||
}
|
||||
|
||||
resourceConfig[resource].mtoLoad.forEach(relation => {
|
||||
maps[relation] = Object.fromEntries(lists[relation].map(i => [i.id, i]));
|
||||
})
|
||||
|
||||
data = queryData.map(row => {
|
||||
let toReturn = {
|
||||
...row
|
||||
}
|
||||
|
||||
resourceConfig[resource].mtoLoad.forEach(relation => {
|
||||
toReturn[relation] = row[relation] ? maps[relation][row[relation]] : null
|
||||
data = rows.map(row => {
|
||||
let toReturn = { ...row }
|
||||
config.mtoLoad.forEach(rel => {
|
||||
toReturn[rel] = row[rel] ? maps[rel][row[rel]] : null
|
||||
})
|
||||
|
||||
return toReturn
|
||||
});
|
||||
}
|
||||
|
||||
if(resourceConfig[resource].mtmListLoad) {
|
||||
for await (const relation of resourceConfig[resource].mtmListLoad) {
|
||||
console.log(relation)
|
||||
console.log(resource.substring(0,resource.length-1))
|
||||
|
||||
const relationRows = await server.db.select().from(resourceConfig[relation].table).where(inArray(resourceConfig[relation].table[resource.substring(0,resource.length-1)],data.map(i => i.id)))
|
||||
|
||||
console.log(relationRows.length)
|
||||
|
||||
data = data.map(row => {
|
||||
let toReturn = {
|
||||
...row
|
||||
}
|
||||
|
||||
toReturn[relation] = relationRows.filter(i => i[resource.substring(0,resource.length-1)] === row.id)
|
||||
|
||||
return toReturn
|
||||
})
|
||||
|
||||
|
||||
if(config.mtmListLoad) {
|
||||
for await (const relation of config.mtmListLoad) {
|
||||
const relTable = resourceConfig[relation].table
|
||||
const parentKey = resource.substring(0, resource.length - 1)
|
||||
const relationRows = await server.db.select().from(relTable).where(inArray(relTable[parentKey], data.map(i => i.id)))
|
||||
data = data.map(row => ({
|
||||
...row,
|
||||
[relation]: relationRows.filter(i => i[parentKey] === row.id)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,212 +138,130 @@ export default async function resourceRoutes(server: FastifyInstance) {
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// PAGINATED LIST
|
||||
// -------------------------------------------------------------
|
||||
server.get("/resource/:resource/paginated", async (req, reply) => {
|
||||
try {
|
||||
const tenantId = req.user?.tenant_id;
|
||||
if (!tenantId) {
|
||||
return reply.code(400).send({ error: "No tenant selected" });
|
||||
}
|
||||
if (!tenantId) return reply.code(400).send({ error: "No tenant selected" });
|
||||
|
||||
const {resource} = req.params as {resource: string};
|
||||
|
||||
const {queryConfig} = req;
|
||||
const {
|
||||
pagination,
|
||||
sort,
|
||||
filters,
|
||||
paginationDisabled
|
||||
} = queryConfig;
|
||||
|
||||
const { search, distinctColumns } = req.query as {
|
||||
search?: string;
|
||||
distinctColumns?: string;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
let table = resourceConfig[resource].table
|
||||
const { resource } = req.params as { resource: string };
|
||||
const config = resourceConfig[resource];
|
||||
const table = config.table;
|
||||
|
||||
const { queryConfig } = req;
|
||||
const { pagination, sort, filters } = queryConfig;
|
||||
const { search, distinctColumns } = req.query as { search?: string; distinctColumns?: string; };
|
||||
|
||||
let whereCond: any = eq(table.tenant, tenantId);
|
||||
const searchCols: any[] = (config.searchColumns || []).map(c => table[c]);
|
||||
|
||||
let countQuery = server.db.select({ value: count(table.id) }).from(table).$dynamic();
|
||||
let mainQuery = server.db.select().from(table).$dynamic();
|
||||
|
||||
if(search) {
|
||||
const searchCond = buildSearchCondition(
|
||||
table,
|
||||
resourceConfig[resource].searchColumns,
|
||||
search.trim()
|
||||
)
|
||||
if (config.mtoLoad) {
|
||||
config.mtoLoad.forEach(rel => {
|
||||
const relConfig = resourceConfig[rel + "s"] || resourceConfig[rel];
|
||||
if (relConfig) {
|
||||
const relTable = relConfig.table;
|
||||
|
||||
if (searchCond) {
|
||||
whereCond = and(whereCond, searchCond)
|
||||
}
|
||||
// FIX: Self-Reference Check
|
||||
if (relTable !== table) {
|
||||
countQuery = countQuery.leftJoin(relTable, eq(table[rel], relTable.id));
|
||||
// @ts-ignore
|
||||
mainQuery = mainQuery.leftJoin(relTable, eq(table[rel], relTable.id));
|
||||
if (relConfig.searchColumns) {
|
||||
relConfig.searchColumns.forEach(c => {
|
||||
if (relTable[c]) searchCols.push(relTable[c]);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (search) {
|
||||
const searchCond = buildSearchCondition(searchCols, search.trim());
|
||||
if (searchCond) whereCond = and(whereCond, searchCond);
|
||||
}
|
||||
|
||||
if (filters) {
|
||||
for (const [key, val] of Object.entries(filters)) {
|
||||
const col = (table as any)[key];
|
||||
if (!col) continue;
|
||||
|
||||
if (Array.isArray(val)) {
|
||||
whereCond = and(whereCond, inArray(col, val));
|
||||
} else {
|
||||
whereCond = and(whereCond, eq(col, val as any));
|
||||
}
|
||||
whereCond = Array.isArray(val) ? and(whereCond, inArray(col, val)) : and(whereCond, eq(col, val as any));
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------
|
||||
// COUNT (for pagination)
|
||||
// -----------------------------------------------
|
||||
const totalRes = await server.db
|
||||
.select({ value: count(table.id) })
|
||||
.from(table)
|
||||
.where(whereCond);
|
||||
|
||||
const totalRes = await countQuery.where(whereCond);
|
||||
const total = Number(totalRes[0]?.value ?? 0);
|
||||
|
||||
// -----------------------------------------------
|
||||
// DISTINCT VALUES (regardless of pagination)
|
||||
// -----------------------------------------------
|
||||
const distinctValues: Record<string, any[]> = {};
|
||||
|
||||
if (distinctColumns) {
|
||||
for (const colName of distinctColumns.split(",").map(c => c.trim())) {
|
||||
const col = (table as any)[colName];
|
||||
if (!col) continue;
|
||||
|
||||
const rows = await server.db
|
||||
.select({ v: col })
|
||||
.from(table)
|
||||
.where(eq(table.tenant, tenantId));
|
||||
|
||||
const values = rows
|
||||
.map(r => r.v)
|
||||
.filter(v => v != null && v !== "");
|
||||
|
||||
distinctValues[colName] = [...new Set(values)].sort();
|
||||
}
|
||||
}
|
||||
|
||||
// PAGINATION
|
||||
const offset = pagination?.offset ?? 0;
|
||||
const limit = pagination?.limit ?? 100;
|
||||
|
||||
// SORTING
|
||||
let orderField: any = null;
|
||||
let direction: "asc" | "desc" = "asc";
|
||||
mainQuery = mainQuery.where(whereCond).offset(offset).limit(limit);
|
||||
|
||||
if (sort?.length > 0) {
|
||||
const s = sort[0];
|
||||
const col = (table as any)[s.field];
|
||||
if (col) {
|
||||
orderField = col;
|
||||
direction = s.direction === "asc" ? "asc" : "desc";
|
||||
mainQuery = s.direction === "asc" ? mainQuery.orderBy(asc(col)) : mainQuery.orderBy(desc(col));
|
||||
}
|
||||
}
|
||||
|
||||
// MAIN QUERY (Paginated)
|
||||
let q = server.db
|
||||
.select()
|
||||
.from(table)
|
||||
.where(whereCond)
|
||||
.offset(offset)
|
||||
.limit(limit);
|
||||
|
||||
if (orderField) {
|
||||
//@ts-ignore
|
||||
q = direction === "asc"
|
||||
? q.orderBy(asc(orderField))
|
||||
: q.orderBy(desc(orderField));
|
||||
}
|
||||
|
||||
const rows = await q;
|
||||
|
||||
if (!rows.length) {
|
||||
return {
|
||||
data: [],
|
||||
queryConfig: {
|
||||
...queryConfig,
|
||||
total,
|
||||
totalPages: 0,
|
||||
distinctValues
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
let data = [...rows]
|
||||
//Many to One
|
||||
if(resourceConfig[resource].mtoLoad) {
|
||||
let ids = {}
|
||||
let lists = {}
|
||||
let maps = {}
|
||||
resourceConfig[resource].mtoLoad.forEach(relation => {
|
||||
ids[relation] = [...new Set(rows.map(r => r[relation]).filter(Boolean))];
|
||||
})
|
||||
|
||||
for await (const relation of resourceConfig[resource].mtoLoad ) {
|
||||
lists[relation] = ids[relation].length ? await server.db.select().from(resourceConfig[relation + "s"].table).where(inArray(resourceConfig[relation + "s"].table.id, ids[relation])) : []
|
||||
const rawRows = await mainQuery;
|
||||
// Transformation für Drizzle Joins
|
||||
let rows = rawRows.map(r => r[resource] || r.table || r);
|
||||
|
||||
const distinctValues: Record<string, any[]> = {};
|
||||
if (distinctColumns) {
|
||||
for (const colName of distinctColumns.split(",").map(c => c.trim())) {
|
||||
const col = (table as any)[colName];
|
||||
if (!col) continue;
|
||||
const dRows = await server.db.select({ v: col }).from(table).where(eq(table.tenant, tenantId));
|
||||
distinctValues[colName] = [...new Set(dRows.map(r => r.v).filter(v => v != null && v !== ""))].sort();
|
||||
}
|
||||
}
|
||||
|
||||
resourceConfig[resource].mtoLoad.forEach(relation => {
|
||||
maps[relation] = Object.fromEntries(lists[relation].map(i => [i.id, i]));
|
||||
})
|
||||
|
||||
let data = [...rows];
|
||||
if (config.mtoLoad) {
|
||||
let ids: any = {};
|
||||
let lists: any = {};
|
||||
let maps: any = {};
|
||||
config.mtoLoad.forEach(rel => {
|
||||
ids[rel] = [...new Set(rows.map(r => r[rel]).filter(Boolean))];
|
||||
});
|
||||
for await (const rel of config.mtoLoad) {
|
||||
const relConf = resourceConfig[rel + "s"] || resourceConfig[rel];
|
||||
const relTab = relConf.table;
|
||||
lists[rel] = ids[rel].length ? await server.db.select().from(relTab).where(inArray(relTab.id, ids[rel])) : [];
|
||||
maps[rel] = Object.fromEntries(lists[rel].map((i: any) => [i.id, i]));
|
||||
}
|
||||
data = rows.map(row => {
|
||||
let toReturn = {
|
||||
...row
|
||||
}
|
||||
|
||||
resourceConfig[resource].mtoLoad.forEach(relation => {
|
||||
toReturn[relation] = row[relation] ? maps[relation][row[relation]] : null
|
||||
})
|
||||
|
||||
return toReturn
|
||||
let toReturn = { ...row };
|
||||
config.mtoLoad.forEach(rel => {
|
||||
toReturn[rel] = row[rel] ? maps[rel][row[rel]] : null;
|
||||
});
|
||||
return toReturn;
|
||||
});
|
||||
}
|
||||
|
||||
if(resourceConfig[resource].mtmListLoad) {
|
||||
for await (const relation of resourceConfig[resource].mtmListLoad) {
|
||||
console.log(relation)
|
||||
|
||||
const relationRows = await server.db.select().from(resourceConfig[relation].table).where(inArray(resourceConfig[relation].table[resource.substring(0,resource.length-1)],data.map(i => i.id)))
|
||||
|
||||
console.log(relationRows)
|
||||
|
||||
data = data.map(row => {
|
||||
let toReturn = {
|
||||
...row
|
||||
}
|
||||
|
||||
toReturn[relation] = relationRows.filter(i => i[resource.substring(0,resource.length-1)] === row.id)
|
||||
|
||||
return toReturn
|
||||
})
|
||||
|
||||
|
||||
if (config.mtmListLoad) {
|
||||
for await (const relation of config.mtmListLoad) {
|
||||
const relTable = resourceConfig[relation].table;
|
||||
const parentKey = resource.substring(0, resource.length - 1);
|
||||
const relationRows = await server.db.select().from(relTable).where(inArray(relTable[parentKey], data.map(i => i.id)));
|
||||
data = data.map(row => ({
|
||||
...row,
|
||||
[relation]: relationRows.filter(i => i[parentKey] === row.id)
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------
|
||||
// RETURN DATA
|
||||
// -----------------------------------------------
|
||||
return {
|
||||
data,
|
||||
queryConfig: {
|
||||
...queryConfig,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
distinctValues
|
||||
}
|
||||
queryConfig: { ...queryConfig, total, totalPages: Math.ceil(total / limit), distinctValues }
|
||||
};
|
||||
|
||||
} catch (err) {
|
||||
@@ -369,9 +270,8 @@ export default async function resourceRoutes(server: FastifyInstance) {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// DETAIL (mit JOINS)
|
||||
// DETAIL
|
||||
// -------------------------------------------------------------
|
||||
server.get("/resource/:resource/:id/:no_relations?", async (req, reply) => {
|
||||
try {
|
||||
@@ -379,7 +279,7 @@ export default async function resourceRoutes(server: FastifyInstance) {
|
||||
const tenantId = req.user?.tenant_id
|
||||
if (!tenantId) return reply.code(400).send({ error: "No tenant selected" })
|
||||
|
||||
const {resource, no_relations} = req.params as { resource: string, no_relations?: boolean }
|
||||
const { resource, no_relations } = req.params as { resource: string, no_relations?: boolean }
|
||||
const table = resourceConfig[resource].table
|
||||
|
||||
const projRows = await server.db
|
||||
@@ -391,40 +291,32 @@ export default async function resourceRoutes(server: FastifyInstance) {
|
||||
if (!projRows.length)
|
||||
return reply.code(404).send({ error: "Resource not found" })
|
||||
|
||||
// ------------------------------------
|
||||
// LOAD RELATIONS
|
||||
// ------------------------------------
|
||||
let data = { ...projRows[0] }
|
||||
|
||||
let ids = {}
|
||||
let lists = {}
|
||||
let maps = {}
|
||||
let data = {
|
||||
...projRows[0]
|
||||
}
|
||||
|
||||
if(!no_relations) {
|
||||
if(resourceConfig[resource].mtoLoad) {
|
||||
for await (const relation of resourceConfig[resource].mtoLoad ) {
|
||||
if(data[relation]) {
|
||||
data[relation] = (await server.db.select().from(resourceConfig[relation + "s"].table).where(eq(resourceConfig[relation + "s"].table.id, data[relation])))[0]
|
||||
if (!no_relations) {
|
||||
if (resourceConfig[resource].mtoLoad) {
|
||||
for await (const relation of resourceConfig[resource].mtoLoad) {
|
||||
if (data[relation]) {
|
||||
const relConf = resourceConfig[relation + "s"] || resourceConfig[relation];
|
||||
const relTable = relConf.table
|
||||
const relData = await server.db.select().from(relTable).where(eq(relTable.id, data[relation]))
|
||||
data[relation] = relData[0] || null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(resourceConfig[resource].mtmLoad) {
|
||||
for await (const relation of resourceConfig[resource].mtmLoad ) {
|
||||
console.log(relation)
|
||||
data[relation] = await server.db.select().from(resourceConfig[relation].table).where(eq(resourceConfig[relation].table[resource.substring(0,resource.length - 1)],id))
|
||||
if (resourceConfig[resource].mtmLoad) {
|
||||
for await (const relation of resourceConfig[resource].mtmLoad) {
|
||||
const relTable = resourceConfig[relation].table
|
||||
const parentKey = resource.substring(0, resource.length - 1)
|
||||
data[relation] = await server.db.select().from(relTable).where(eq(relTable[parentKey], id))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return data
|
||||
|
||||
} catch (err) {
|
||||
console.error("ERROR /resource/projects/:id", err)
|
||||
console.error("ERROR /resource/:resource/:id", err)
|
||||
return reply.code(500).send({ error: "Internal Server Error" })
|
||||
}
|
||||
})
|
||||
@@ -432,132 +324,59 @@ export default async function resourceRoutes(server: FastifyInstance) {
|
||||
// Create
|
||||
server.post("/resource/:resource", async (req, reply) => {
|
||||
try {
|
||||
if (!req.user?.tenant_id) {
|
||||
return reply.code(400).send({error: "No tenant selected"});
|
||||
}
|
||||
|
||||
const {resource} = req.params as { resource: string };
|
||||
if (!req.user?.tenant_id) return reply.code(400).send({ error: "No tenant selected" });
|
||||
const { resource } = req.params as { resource: string };
|
||||
const body = req.body as Record<string, any>;
|
||||
const config = resourceConfig[resource];
|
||||
const table = config.table;
|
||||
|
||||
const table = resourceConfig[resource].table
|
||||
let createData = { ...body, tenant: req.user.tenant_id, archived: false };
|
||||
|
||||
let createData = {
|
||||
...body,
|
||||
tenant: req.user.tenant_id,
|
||||
archived: false, // Standardwert
|
||||
}
|
||||
|
||||
console.log(resourceConfig[resource].numberRangeHolder)
|
||||
|
||||
if (resourceConfig[resource].numberRangeHolder && !body[resourceConfig[resource]]) {
|
||||
if (config.numberRangeHolder && !body[config.numberRangeHolder]) {
|
||||
const result = await useNextNumberRangeNumber(server, req.user.tenant_id, resource)
|
||||
console.log(result)
|
||||
createData[resourceConfig[resource].numberRangeHolder] = result.usedNumber
|
||||
}
|
||||
|
||||
const normalizeDate = (val: any) => {
|
||||
const d = new Date(val)
|
||||
return isNaN(d.getTime()) ? null : d
|
||||
createData[config.numberRangeHolder] = result.usedNumber
|
||||
}
|
||||
|
||||
const normalizeDate = (val: any) => { const d = new Date(val); return isNaN(d.getTime()) ? null : d; }
|
||||
Object.keys(createData).forEach((key) => {
|
||||
if(key.toLowerCase().includes("date") && key !== "deliveryDateType") createData[key] = normalizeDate(createData[key])
|
||||
if (key.toLowerCase().includes("date") && key !== "deliveryDateType") createData[key] = normalizeDate(createData[key])
|
||||
})
|
||||
|
||||
const [created] = await server.db
|
||||
.insert(table)
|
||||
.values(createData)
|
||||
.returning()
|
||||
|
||||
|
||||
/*await insertHistoryItem(server, {
|
||||
entity: resource,
|
||||
entityId: data.id,
|
||||
action: "created",
|
||||
created_by: req.user.user_id,
|
||||
tenant_id: req.user.tenant_id,
|
||||
oldVal: null,
|
||||
newVal: data,
|
||||
text: `${dataType.labelSingle} erstellt`,
|
||||
});*/
|
||||
|
||||
const [created] = await server.db.insert(table).values(createData).returning()
|
||||
return created;
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
reply.status(500)
|
||||
console.error(error);
|
||||
reply.status(500);
|
||||
}
|
||||
});
|
||||
|
||||
// UPDATE (inkl. Soft-Delete/Archive)
|
||||
// Update
|
||||
server.put("/resource/:resource/:id", async (req, reply) => {
|
||||
try {
|
||||
const {resource, id} = req.params as { resource: string; id: string }
|
||||
const { resource, id } = req.params as { resource: string; id: string }
|
||||
const body = req.body as Record<string, any>
|
||||
const tenantId = req.user?.tenant_id
|
||||
const userId = req.user?.user_id
|
||||
|
||||
const tenantId = (req.user as any)?.tenant_id
|
||||
const userId = (req.user as any)?.user_id
|
||||
|
||||
if (!tenantId || !userId) {
|
||||
return reply.code(401).send({error: "Unauthorized"})
|
||||
}
|
||||
if (!tenantId || !userId) return reply.code(401).send({ error: "Unauthorized" })
|
||||
|
||||
const table = resourceConfig[resource].table
|
||||
const normalizeDate = (val: any) => { const d = new Date(val); return isNaN(d.getTime()) ? null : d; }
|
||||
|
||||
//TODO: HISTORY
|
||||
|
||||
const normalizeDate = (val: any) => {
|
||||
const d = new Date(val)
|
||||
return isNaN(d.getTime()) ? null : d
|
||||
}
|
||||
|
||||
let data = {...body, updated_at: new Date().toISOString(), updated_by: userId}
|
||||
|
||||
let data = { ...body, updated_at: new Date().toISOString(), updated_by: userId }
|
||||
//@ts-ignore
|
||||
delete data.updatedBy
|
||||
//@ts-ignore
|
||||
delete data.updatedAt
|
||||
|
||||
console.log(data)
|
||||
delete data.updatedBy; delete data.updatedAt;
|
||||
|
||||
Object.keys(data).forEach((key) => {
|
||||
console.log(key)
|
||||
|
||||
if(key.includes("_at") || key.includes("At") || key.toLowerCase().includes("date")) {
|
||||
if ((key.includes("_at") || key.includes("At") || key.toLowerCase().includes("date")) && key !== "deliveryDateType") {
|
||||
data[key] = normalizeDate(data[key])
|
||||
}
|
||||
})
|
||||
|
||||
console.log(data)
|
||||
|
||||
const [updated] = await server.db
|
||||
.update(table)
|
||||
.set(data)
|
||||
.where(and(
|
||||
eq(table.id, id),
|
||||
eq(table.tenant, tenantId)))
|
||||
.returning()
|
||||
|
||||
//const diffs = diffObjects(oldItem, newItem);
|
||||
|
||||
|
||||
/*for (const d of diffs) {
|
||||
await insertHistoryItem(server, {
|
||||
entity: resource,
|
||||
entityId: id,
|
||||
action: d.type,
|
||||
created_by: userId,
|
||||
tenant_id: tenantId,
|
||||
oldVal: d.oldValue ? String(d.oldValue) : null,
|
||||
newVal: d.newValue ? String(d.newValue) : null,
|
||||
text: `Feld "${d.label}" ${d.typeLabel}: ${d.oldValue ?? ""} → ${d.newValue ?? ""}`,
|
||||
});
|
||||
}*/
|
||||
|
||||
const [updated] = await server.db.update(table).set(data).where(and(eq(table.id, id), eq(table.tenant, tenantId))).returning()
|
||||
return updated
|
||||
} catch (err) {
|
||||
console.log("ERROR /resource/projects/:id", err)
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ import {
|
||||
|
||||
export const resourceConfig = {
|
||||
projects: {
|
||||
searchColumns: ["name"],
|
||||
searchColumns: ["name","customerRef","projectNumber","notes"],
|
||||
mtoLoad: ["customer","plant","contract","projecttype"],
|
||||
mtmLoad: ["tasks", "files","createddocuments"],
|
||||
table: projects,
|
||||
@@ -61,6 +61,7 @@ export const resourceConfig = {
|
||||
},
|
||||
plants: {
|
||||
table: plants,
|
||||
searchColumns: ["name"],
|
||||
mtoLoad: ["customer"],
|
||||
mtmLoad: ["projects","tasks","files"],
|
||||
},
|
||||
|
||||
@@ -1387,7 +1387,7 @@ const saveDocument = async (state, resetup = false) => {
|
||||
endText: itemInfo.value.endText,
|
||||
rows: itemInfo.value.rows,
|
||||
contactPerson: itemInfo.value.contactPerson,
|
||||
linkedDocument: itemInfo.value.linkedDocument,
|
||||
createddocument: itemInfo.value.createddocument,
|
||||
agriculture: itemInfo.value.agriculture,
|
||||
letterhead: itemInfo.value.letterhead,
|
||||
usedAdvanceInvoices: itemInfo.value.usedAdvanceInvoices,
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
class="hidden lg:block"
|
||||
icon="i-heroicons-funnel"
|
||||
placeholder="Suche..."
|
||||
@change="tempStore.modifySearchString('createddocuments',searchString)"
|
||||
@keydown.esc="$event.target.blur()"
|
||||
>
|
||||
<template #trailing>
|
||||
@@ -30,22 +29,7 @@
|
||||
</template>
|
||||
</UDashboardNavbar>
|
||||
<UDashboardToolbar>
|
||||
|
||||
|
||||
<template #right>
|
||||
<!-- <USelectMenu
|
||||
v-model="selectedColumns"
|
||||
:options="templateColumns"
|
||||
by="key"
|
||||
class="hidden lg:block"
|
||||
icon="i-heroicons-adjustments-horizontal-solid"
|
||||
multiple
|
||||
@change="tempStore.modifyColumns('createddocuments',selectedColumns)"
|
||||
>
|
||||
<template #label>
|
||||
Spalten
|
||||
</template>
|
||||
</USelectMenu>-->
|
||||
<USelectMenu
|
||||
v-if="selectableFilters.length > 0"
|
||||
v-model="selectedFilters"
|
||||
@@ -157,6 +141,31 @@
|
||||
|
||||
<script setup>
|
||||
import dayjs from "dayjs";
|
||||
import { ref, computed, watch } from 'vue';
|
||||
|
||||
const dataStore = useDataStore()
|
||||
const tempStore = useTempStore()
|
||||
const router = useRouter()
|
||||
|
||||
const type = "createddocuments"
|
||||
const dataType = dataStore.dataTypes[type]
|
||||
|
||||
const items = ref([])
|
||||
const selectedItem = ref(0)
|
||||
const activeTabIndex = ref(0)
|
||||
|
||||
// Debounce-Logik für die Suche
|
||||
const searchString = ref(tempStore.searchStrings['createddocuments'] || '')
|
||||
const debouncedSearchString = ref(searchString.value)
|
||||
let debounceTimeout = null
|
||||
|
||||
watch(searchString, (newVal) => {
|
||||
clearTimeout(debounceTimeout)
|
||||
debounceTimeout = setTimeout(() => {
|
||||
debouncedSearchString.value = newVal
|
||||
tempStore.modifySearchString('createddocuments', newVal)
|
||||
}, 300) // 300ms warten nach dem letzten Tastendruck
|
||||
})
|
||||
|
||||
defineShortcuts({
|
||||
'/': () => {
|
||||
@@ -168,10 +177,6 @@ defineShortcuts({
|
||||
'Enter': {
|
||||
usingInput: true,
|
||||
handler: () => {
|
||||
// Zugriff auf das aktuell sichtbare Element basierend auf Tab und Selektion
|
||||
const currentList = getRowsForTab(selectedTypes.value[activeTabIndex.value]?.key || 'drafts')
|
||||
|
||||
// Fallback auf globale Liste falls nötig, aber Logik sollte auf Tab passen
|
||||
if (filteredRows.value[selectedItem.value]) {
|
||||
router.push(`/createDocument/show/${filteredRows.value[selectedItem.value].id}`)
|
||||
}
|
||||
@@ -193,17 +198,6 @@ defineShortcuts({
|
||||
}
|
||||
})
|
||||
|
||||
const dataStore = useDataStore()
|
||||
const tempStore = useTempStore()
|
||||
const router = useRouter()
|
||||
|
||||
const type = "createddocuments"
|
||||
const dataType = dataStore.dataTypes[type]
|
||||
|
||||
const items = ref([])
|
||||
const selectedItem = ref(0)
|
||||
const activeTabIndex = ref(0)
|
||||
|
||||
const setupPage = async () => {
|
||||
items.value = (await useEntities("createddocuments").select("*, customer(id,name), statementallocations(id,amount),linkedDocument(*)", "documentNumber", true, true))
|
||||
}
|
||||
@@ -222,10 +216,9 @@ const templateColumns = [
|
||||
{key: "dueDate", label: "Fällig"}
|
||||
]
|
||||
|
||||
// Eigene Spalten für Entwürfe: Referenz raus, Status rein
|
||||
const draftColumns = [
|
||||
{key: 'type', label: "Typ"},
|
||||
{key: 'state', label: "Status"}, // Status wieder drin
|
||||
{key: 'state', label: "Status"},
|
||||
{key: 'partner', label: "Kunde"},
|
||||
{key: "date", label: "Erstellt am"},
|
||||
{key: "amount", label: "Betrag"}
|
||||
@@ -242,31 +235,11 @@ const getColumnsForTab = (tabKey) => {
|
||||
}
|
||||
|
||||
const templateTypes = [
|
||||
{
|
||||
key: "drafts",
|
||||
label: "Entwürfe"
|
||||
},
|
||||
{
|
||||
key: "invoices",
|
||||
label: "Rechnungen"
|
||||
},
|
||||
/*,{
|
||||
key: "cancellationInvoices",
|
||||
label: "Stornorechnungen"
|
||||
},{
|
||||
key: "advanceInvoices",
|
||||
label: "Abschlagsrechnungen"
|
||||
},*/
|
||||
{
|
||||
key: "quotes",
|
||||
label: "Angebote"
|
||||
}, {
|
||||
key: "deliveryNotes",
|
||||
label: "Lieferscheine"
|
||||
}, {
|
||||
key: "confirmationOrders",
|
||||
label: "Auftragsbestätigungen"
|
||||
}
|
||||
{ key: "drafts", label: "Entwürfe" },
|
||||
{ key: "invoices", label: "Rechnungen" },
|
||||
{ key: "quotes", label: "Angebote" },
|
||||
{ key: "deliveryNotes", label: "Lieferscheine" },
|
||||
{ key: "confirmationOrders", label: "Auftragsbestätigungen" }
|
||||
]
|
||||
const selectedTypes = ref(tempStore.filters["createddocuments"] ? tempStore.filters["createddocuments"] : templateTypes)
|
||||
const types = computed(() => {
|
||||
@@ -274,10 +247,9 @@ const types = computed(() => {
|
||||
})
|
||||
|
||||
const selectItem = (item) => {
|
||||
console.log(item)
|
||||
if (item.state === "Entwurf") {
|
||||
router.push(`/createDocument/edit/${item.id}`)
|
||||
} else if (item.state !== "Entwurf") {
|
||||
} else {
|
||||
router.push(`/createDocument/show/${item.id}`)
|
||||
}
|
||||
}
|
||||
@@ -286,24 +258,19 @@ const displayCurrency = (value, currency = "€") => {
|
||||
return `${Number(value).toFixed(2).replace(".", ",")} ${currency}`
|
||||
}
|
||||
|
||||
const searchString = ref(tempStore.searchStrings['createddocuments'] || '')
|
||||
|
||||
const clearSearchString = () => {
|
||||
tempStore.clearSearchString('createddocuments')
|
||||
searchString.value = ''
|
||||
debouncedSearchString.value = ''
|
||||
}
|
||||
|
||||
const selectableFilters = ref(dataType.filters.map(i => i.name))
|
||||
const selectedFilters = ref(dataType.filters.filter(i => i.default).map(i => i.name) || [])
|
||||
|
||||
const filteredRows = computed(() => {
|
||||
let tempItems = items.value.filter(i => types.value.find(x => {
|
||||
// 1. Draft Tab Logic
|
||||
if (x.key === 'drafts') return i.state === 'Entwurf'
|
||||
|
||||
// 2. Global Draft Exclusion (drafts shouldn't be in other tabs)
|
||||
if (i.state === 'Entwurf' && x.key !== 'drafts') return false
|
||||
|
||||
// 3. Normal Type Logic
|
||||
if (x.key === 'invoices') return ['invoices', 'advanceInvoices', 'cancellationInvoices'].includes(i.type)
|
||||
return x.key === i.type
|
||||
}))
|
||||
@@ -324,22 +291,16 @@ const filteredRows = computed(() => {
|
||||
})
|
||||
}
|
||||
|
||||
tempItems = useSearch(searchString.value, tempItems)
|
||||
|
||||
return useSearch(searchString.value, tempItems.slice().reverse())
|
||||
// Hier nutzen wir nun den debounced Wert für die lokale Suche
|
||||
const results = useSearch(debouncedSearchString.value, tempItems.slice().reverse())
|
||||
return results
|
||||
})
|
||||
|
||||
const getRowsForTab = (tabKey) => {
|
||||
return filteredRows.value.filter(row => {
|
||||
if (tabKey === 'drafts') {
|
||||
return row.state === 'Entwurf'
|
||||
}
|
||||
|
||||
if (tabKey === 'drafts') return row.state === 'Entwurf'
|
||||
if (row.state === 'Entwurf') return false
|
||||
|
||||
if (tabKey === 'invoices') {
|
||||
return ['invoices', 'advanceInvoices', 'cancellationInvoices'].includes(row.type)
|
||||
}
|
||||
if (tabKey === 'invoices') return ['invoices', 'advanceInvoices', 'cancellationInvoices'].includes(row.type)
|
||||
return row.type === tabKey
|
||||
})
|
||||
}
|
||||
@@ -349,7 +310,4 @@ const isPaid = (item) => {
|
||||
item.statementallocations.forEach(allocation => amountPaid += allocation.amount)
|
||||
return Number(amountPaid.toFixed(2)) === useSum().getCreatedDocumentSum(item, items.value)
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
</style>
|
||||
</script>
|
||||
@@ -17,11 +17,18 @@ const dataStore = useDataStore()
|
||||
|
||||
const itemInfo = ref({})
|
||||
const linkedDocument =ref({})
|
||||
const links = ref([])
|
||||
|
||||
const setupPage = async () => {
|
||||
if(route.params) {
|
||||
if(route.params.id) itemInfo.value = await useEntities("createddocuments").selectSingle(route.params.id,"*,files(*),linkedDocument(*), statementallocations(bs_id)")
|
||||
|
||||
console.log(itemInfo.value)
|
||||
if(itemInfo.value.type === "invoices"){
|
||||
const createddocuments = await useEntities("createddocuments").select()
|
||||
console.log(createddocuments)
|
||||
links.value = createddocuments.filter(i => i.createddocument?.id === itemInfo.value.id)
|
||||
console.log(links.value)
|
||||
}
|
||||
|
||||
linkedDocument.value = await useFiles().selectDocument(itemInfo.value.files[0].id)
|
||||
}
|
||||
@@ -80,17 +87,23 @@ const openBankstatements = () => {
|
||||
>
|
||||
E-Mail
|
||||
</UButton>
|
||||
<UButton
|
||||
@click="router.push(`/createDocument/edit/?createddocument=${itemInfo.id}&loadMode=storno`)"
|
||||
variant="outline"
|
||||
color="rose"
|
||||
<UTooltip
|
||||
v-if="itemInfo.type === 'invoices' || itemInfo.type === 'advanceInvoices'"
|
||||
:text="links.find(i => i.type === 'cancellationInvoices') ? 'Bereits stoniert' : ''"
|
||||
>
|
||||
Stornieren
|
||||
</UButton>
|
||||
<UButton
|
||||
@click="router.push(`/createDocument/edit/?createddocument=${itemInfo.id}&loadMode=storno`)"
|
||||
variant="outline"
|
||||
color="rose"
|
||||
:disabled="links.find(i => i.type === 'cancellationInvoices')"
|
||||
>
|
||||
Stornieren
|
||||
</UButton>
|
||||
</UTooltip>
|
||||
|
||||
<UButton
|
||||
v-if="itemInfo.project"
|
||||
@click="router.push(`/standardEntity/projects/show/${itemInfo.project}`)"
|
||||
@click="router.push(`/standardEntity/projects/show/${itemInfo.project?.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
>
|
||||
@@ -98,12 +111,36 @@ const openBankstatements = () => {
|
||||
</UButton>
|
||||
<UButton
|
||||
v-if="itemInfo.customer"
|
||||
@click="router.push(`/standardEntity/customers/show/${itemInfo.customer}`)"
|
||||
@click="router.push(`/standardEntity/customers/show/${itemInfo.customer?.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
>
|
||||
Kunde
|
||||
</UButton>
|
||||
<UButton
|
||||
v-if="itemInfo.plant"
|
||||
@click="router.push(`/standardEntity/plants/show/${itemInfo.plant?.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
>
|
||||
Objekt
|
||||
</UButton>
|
||||
<UButton
|
||||
v-if="itemInfo.contract"
|
||||
@click="router.push(`/standardEntity/contracts/show/${itemInfo.contract?.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
>
|
||||
Vertrag
|
||||
</UButton>
|
||||
<UButton
|
||||
v-if="itemInfo.contact"
|
||||
@click="router.push(`/standardEntity/contacts/show/${itemInfo.contact?.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
>
|
||||
Ansprechpartner
|
||||
</UButton>
|
||||
<UButton
|
||||
v-if="itemInfo.createddocument"
|
||||
@click="router.push(`/createDocument/show/${itemInfo.createddocument}`)"
|
||||
|
||||
Reference in New Issue
Block a user