catalog search/sort, edit-page characteristics, preferred vendor per article
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
-- Add preferred vendor to catalog items
|
||||
ALTER TABLE ausruestung_artikel
|
||||
ADD COLUMN IF NOT EXISTS bevorzugter_lieferant_id INT REFERENCES lieferanten(id) ON DELETE SET NULL;
|
||||
@@ -55,26 +55,34 @@ async function deleteKategorie(id: number) {
|
||||
// Catalog Items (ausruestung_artikel)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function getItems(filters?: { kategorie?: string; kategorie_id?: number; aktiv?: boolean }) {
|
||||
async function getItems(filters?: { kategorie?: string; kategorie_id?: number; aktiv?: boolean; search?: string }) {
|
||||
const conditions: string[] = [];
|
||||
const params: unknown[] = [];
|
||||
|
||||
if (filters?.kategorie) {
|
||||
params.push(filters.kategorie);
|
||||
conditions.push(`kategorie = $${params.length}`);
|
||||
conditions.push(`aa.kategorie = $${params.length}`);
|
||||
}
|
||||
if (filters?.kategorie_id) {
|
||||
params.push(filters.kategorie_id);
|
||||
conditions.push(`kategorie_id = $${params.length}`);
|
||||
conditions.push(`aa.kategorie_id = $${params.length}`);
|
||||
}
|
||||
if (filters?.aktiv !== undefined) {
|
||||
params.push(filters.aktiv);
|
||||
conditions.push(`aktiv = $${params.length}`);
|
||||
conditions.push(`aa.aktiv = $${params.length}`);
|
||||
}
|
||||
if (filters?.search) {
|
||||
params.push(`%${filters.search}%`);
|
||||
conditions.push(`aa.bezeichnung ILIKE $${params.length}`);
|
||||
}
|
||||
|
||||
const where = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
||||
const result = await pool.query(
|
||||
`SELECT * FROM ausruestung_artikel ${where} ORDER BY kategorie, bezeichnung`,
|
||||
`SELECT aa.*, aa.bevorzugter_lieferant_id, l.name AS bevorzugter_lieferant_name
|
||||
FROM ausruestung_artikel aa
|
||||
LEFT JOIN lieferanten l ON l.id = aa.bevorzugter_lieferant_id
|
||||
${where}
|
||||
ORDER BY aa.kategorie, aa.bezeichnung`,
|
||||
params,
|
||||
);
|
||||
|
||||
@@ -102,7 +110,13 @@ async function getItems(filters?: { kategorie?: string; kategorie_id?: number; a
|
||||
}
|
||||
|
||||
async function getItemById(id: number) {
|
||||
const result = await pool.query('SELECT * FROM ausruestung_artikel WHERE id = $1', [id]);
|
||||
const result = await pool.query(
|
||||
`SELECT aa.*, aa.bevorzugter_lieferant_id, l.name AS bevorzugter_lieferant_name
|
||||
FROM ausruestung_artikel aa
|
||||
LEFT JOIN lieferanten l ON l.id = aa.bevorzugter_lieferant_id
|
||||
WHERE aa.id = $1`,
|
||||
[id],
|
||||
);
|
||||
if (!result.rows[0]) return null;
|
||||
|
||||
const row = result.rows[0];
|
||||
@@ -137,12 +151,13 @@ async function createItem(
|
||||
kategorie_id?: number;
|
||||
geschaetzter_preis?: number;
|
||||
aktiv?: boolean;
|
||||
bevorzugter_lieferant_id?: number | null;
|
||||
},
|
||||
userId: string,
|
||||
) {
|
||||
// Build column list dynamically based on whether kategorie_id column exists
|
||||
const cols = ['bezeichnung', 'beschreibung', 'kategorie', 'geschaetzter_preis', 'aktiv', 'erstellt_von'];
|
||||
const vals = [data.bezeichnung, data.beschreibung || null, data.kategorie || null, data.geschaetzter_preis || null, data.aktiv ?? true, userId];
|
||||
const cols = ['bezeichnung', 'beschreibung', 'kategorie', 'geschaetzter_preis', 'aktiv', 'erstellt_von', 'bevorzugter_lieferant_id'];
|
||||
const vals = [data.bezeichnung, data.beschreibung || null, data.kategorie || null, data.geschaetzter_preis || null, data.aktiv ?? true, userId, data.bevorzugter_lieferant_id ?? null];
|
||||
|
||||
if (data.kategorie_id) {
|
||||
cols.push('kategorie_id');
|
||||
@@ -166,6 +181,7 @@ async function updateItem(
|
||||
kategorie_id?: number | null;
|
||||
geschaetzter_preis?: number;
|
||||
aktiv?: boolean;
|
||||
bevorzugter_lieferant_id?: number | null;
|
||||
},
|
||||
_userId: string,
|
||||
) {
|
||||
@@ -196,6 +212,10 @@ async function updateItem(
|
||||
params.push(data.aktiv);
|
||||
fields.push(`aktiv = $${params.length}`);
|
||||
}
|
||||
if (data.bevorzugter_lieferant_id !== undefined) {
|
||||
params.push(data.bevorzugter_lieferant_id);
|
||||
fields.push(`bevorzugter_lieferant_id = $${params.length}`);
|
||||
}
|
||||
|
||||
if (fields.length === 0) {
|
||||
return getItemById(id);
|
||||
|
||||
Reference in New Issue
Block a user