Add supplier CRUD on entity with Lieferanten group and entityhistory

This commit is contained in:
seb
2026-08-06 03:56:32 +02:00
parent 561ba5bb99
commit 4f46967f28
17 changed files with 1853 additions and 1338 deletions

View File

@@ -0,0 +1,54 @@
import { CREATE } from '../../sql/entity.js';
import { INSERT as INSERT_HISTORY } from '../../sql/entityhistory.js';
import request from '../request.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import ensureLieferantenGroup from './ensureLieferantenGroup.js';
export default function create(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, { name } = {}) => {
const cleaned = name?.trim();
if (!cleaned) {
throw new Error('Name required');
}
const { companyuserId } = await access(googleAuthId, companyId);
const tx = pool.transaction();
await tx.begin();
try {
const customergroupId = await ensureLieferantenGroup(pool, companyId, tx);
const created = await request(pool, tx)
.input('customergroupId', customergroupId)
.input('name', cleaned)
.query(CREATE);
const row = created.recordset[0];
await request(pool, tx)
.input('entityId', row.id)
.input('companyuserId', companyuserId)
.input('text', 'Erstellt')
.query(INSERT_HISTORY);
await tx.commit();
return {
id: row.id,
name: row.name,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
}
};
}