This commit is contained in:
seb
2026-09-22 07:25:16 +02:00
parent 4ed6260e91
commit cca131d83a
148 changed files with 5343 additions and 3466 deletions

View File

@@ -1,15 +1,18 @@
import { UPDATE } from '../../providers/mssql/queries/company.js';
import { UPDATE as MSSQL_UPDATE } from '../../providers/mssql/queries/company.js';
import { UPDATE as SQLITE_UPDATE } from '../../providers/sqlite3/queries/company.js';
import getById from './getById.js';
import getByName from './getByName.js';
import langEnsure from '../lang/ensure.js';
import taxEnsure from '../tax/ensure.js';
import request from '../request.js';
import makeRunner from '../runner.js';
export default function update(pool) {
const byId = getById(pool);
const byName = getByName(pool);
const ensureLang = langEnsure(pool);
const ensureTax = taxEnsure(pool);
export default function update(provider) {
const byId = getById(provider);
const byName = getByName(provider);
const ensureLang = langEnsure(provider);
const ensureTax = taxEnsure(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const updateSql = isSqlite ? SQLITE_UPDATE : MSSQL_UPDATE;
/**
* Updates an existing company's name, language and tax rates.
@@ -20,6 +23,7 @@ export default function update(pool) {
* @param {string} input.name - The new company name (must be unique).
* @param {number} input.taxrate - The standard tax rate in percent.
* @param {number} input.reducedtaxrate - The reduced tax rate in percent.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, rootCategoryId: number, languageId: number, taxrateId: number, reducedtaxrateId: number, taxfreerateId: number}>} The updated company ids.
* @throws {Error} If the company is not found or the name is taken.
*
@@ -27,36 +31,31 @@ export default function update(pool) {
* const result = await store.company.update(1, { iso639: 'de', name: 'Acme', taxrate: 19, reducedtaxrate: 7 });
* // => { id, rootCategoryId, languageId, taxrateId, reducedtaxrateId, taxfreerateId }
*/
return async (id, { iso639, name, taxrate, reducedtaxrate }) => {
const tx = pool.transaction();
await tx.begin();
try {
const company = await byId(id, tx);
return async (id, { iso639, name, taxrate, reducedtaxrate }, tx) => {
const runUpdate = async (runner) => {
const company = await byId(id, runner);
if (!company) {
throw new Error('Company not found');
}
if (name !== company.name) {
const taken = await byName(name, tx);
const taken = await byName(name, runner);
if (taken) {
throw new Error('Company name not available');
}
}
const languageId = await ensureLang(iso639, tx);
const taxrateId = await ensureTax(taxrate, tx);
const reducedtaxrateId = await ensureTax(reducedtaxrate, tx);
const languageId = await ensureLang(iso639, runner);
const taxrateId = await ensureTax(taxrate, runner);
const reducedtaxrateId = await ensureTax(reducedtaxrate, runner);
await request(pool, tx)
.input('id', id)
.input('name', name)
.input('languageId', languageId)
.input('taxrate', taxrateId)
.input('reducedtaxrate', reducedtaxrateId)
.query(UPDATE);
await tx.commit();
await runner.query(updateSql, {
id,
name,
languageId,
taxrate: taxrateId,
reducedtaxrate: reducedtaxrateId,
});
return {
id,
@@ -66,13 +65,14 @@ export default function update(pool) {
reducedtaxrateId,
taxfreerateId: company.taxfreerate,
};
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
};
if (tx) {
return runUpdate(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(runUpdate);
}
return runUpdate(makeRunner(provider));
};
}