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,20 +1,34 @@
import { POS_GROUP_NAME } from '../../providers/mssql/queries/entity.js';
import { CREATE } from '../../providers/mssql/queries/company.js';
import { CREATE as CREATE_CATEGORY } from '../../providers/mssql/queries/category.js';
import { CREATE as MSSQL_CREATE } from '../../providers/mssql/queries/company.js';
import { CREATE as SQLITE_CREATE } from '../../providers/sqlite3/queries/company.js';
import { CREATE as MSSQL_CREATE_CATEGORY } from '../../providers/mssql/queries/category.js';
import { CREATE as SQLITE_CREATE_CATEGORY } from '../../providers/sqlite3/queries/category.js';
import {
CREATE_WITHOUT_COMPANY as CREATE_CUSTOMERGROUP,
SET_COMPANY as SET_CUSTOMERGROUP_COMPANY,
CREATE_WITHOUT_COMPANY as MSSQL_CREATE_CUSTOMERGROUP,
SET_COMPANY as MSSQL_SET_CUSTOMERGROUP_COMPANY,
} from '../../providers/mssql/queries/customergroup.js';
import { ADD as ADD_COMPANYUSER } from '../../providers/mssql/queries/companyuser.js';
import {
CREATE_WITHOUT_COMPANY as SQLITE_CREATE_CUSTOMERGROUP,
SET_COMPANY as SQLITE_SET_CUSTOMERGROUP_COMPANY,
} from '../../providers/sqlite3/queries/customergroup.js';
import { ADD as MSSQL_ADD_COMPANYUSER } from '../../providers/mssql/queries/companyuser.js';
import { ADD as SQLITE_ADD_COMPANYUSER } from '../../providers/sqlite3/queries/companyuser.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 create(pool) {
const nameTaken = getByName(pool);
const ensureLang = langEnsure(pool);
const ensureTax = taxEnsure(pool);
export default function create(provider) {
const nameTaken = getByName(provider);
const ensureLang = langEnsure(provider);
const ensureTax = taxEnsure(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const createCategorySql = isSqlite ? SQLITE_CREATE_CATEGORY : MSSQL_CREATE_CATEGORY;
const createCustGroupSql = isSqlite ? SQLITE_CREATE_CUSTOMERGROUP : MSSQL_CREATE_CUSTOMERGROUP;
const createCompanySql = isSqlite ? SQLITE_CREATE : MSSQL_CREATE;
const setCustGroupCompanySql = isSqlite ? SQLITE_SET_CUSTOMERGROUP_COMPANY : MSSQL_SET_CUSTOMERGROUP_COMPANY;
const addCompanyUserSql = isSqlite ? SQLITE_ADD_COMPANYUSER : MSSQL_ADD_COMPANYUSER;
/**
* Creates a new company together with its root category, POS customer group,
@@ -26,6 +40,7 @@ export default function create(pool) {
* @param {number} input.taxrate - The standard tax rate in percent.
* @param {number} input.reducedtaxrate - The reduced tax rate in percent.
* @param {number} input.googleAuthId - The id of the Google auth user to link.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, rootCategoryId: number, languageId: number, taxrateId: number, reducedtaxrateId: number, taxfreerateId: number, posCustomerGroupId: number}>} The created company ids.
* @throws {Error} If the company name is already taken.
*
@@ -39,60 +54,52 @@ export default function create(pool) {
* });
* // => { id, rootCategoryId, languageId, taxrateId, reducedtaxrateId, taxfreerateId, posCustomerGroupId }
*/
return async ({ iso639, name, taxrate, reducedtaxrate, googleAuthId }) => {
const tx = pool.transaction();
await tx.begin();
try {
const existing = await nameTaken(name, tx);
return async ({ iso639, name, taxrate, reducedtaxrate, googleAuthId }, tx) => {
const runCreate = async (runner) => {
const existing = await nameTaken(name, runner);
if (existing) {
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 taxfreerateId = await ensureTax(0, tx);
const languageId = await ensureLang(iso639, runner);
const taxrateId = await ensureTax(taxrate, runner);
const reducedtaxrateId = await ensureTax(reducedtaxrate, runner);
const taxfreerateId = await ensureTax(0, runner);
const category = await request(pool, tx)
.input('parentId', null)
.input('sort', 0)
.query(CREATE_CATEGORY);
const rootCategoryId = category.recordset[0].id;
const category = await runner.query(createCategorySql, {
parentId: null,
sort: 0,
});
const rootCategoryId = category.rows[0]?.id ?? category.insertId;
// Circular FK: create POS group first (company_id null), company with
// poscustomergroup_id NOT NULL, then link the group back to the company.
const posGroup = await request(pool, tx)
.input('name', POS_GROUP_NAME)
.input('sort', 0)
.query(CREATE_CUSTOMERGROUP);
const posGroup = await runner.query(createCustGroupSql, {
name: POS_GROUP_NAME,
sort: 0,
});
const posCustomerGroupId = posGroup.rows[0]?.id ?? posGroup.insertId;
const posCustomerGroupId = posGroup.recordset[0].id;
const result = await runner.query(createCompanySql, {
name,
rootCategoryId,
languageId,
taxrate: taxrateId,
reducedtaxrate: reducedtaxrateId,
taxfreerate: taxfreerateId,
posCustomerGroupId,
});
const companyId = result.rows[0]?.id ?? result.insertId;
const result = await request(pool, tx)
.input('name', name)
.input('rootCategoryId', rootCategoryId)
.input('languageId', languageId)
.input('taxrate', taxrateId)
.input('reducedtaxrate', reducedtaxrateId)
.input('taxfreerate', taxfreerateId)
.input('posCustomerGroupId', posCustomerGroupId)
.query(CREATE);
await runner.query(setCustGroupCompanySql, {
id: posCustomerGroupId,
companyId,
});
const companyId = result.recordset[0].id;
await request(pool, tx)
.input('id', posCustomerGroupId)
.input('companyId', companyId)
.query(SET_CUSTOMERGROUP_COMPANY);
await request(pool, tx)
.input('companyId', companyId)
.input('googleAuthId', googleAuthId)
.query(ADD_COMPANYUSER);
await tx.commit();
await runner.query(addCompanyUserSql, {
companyId,
googleAuthId,
});
return {
id: companyId,
@@ -103,13 +110,14 @@ export default function create(pool) {
taxfreerateId,
posCustomerGroupId,
};
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
};
if (tx) {
return runCreate(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(runCreate);
}
return runCreate(makeRunner(provider));
};
}

View File

@@ -1,19 +1,22 @@
import { DELETE } from '../../providers/mssql/queries/company.js';
import { DELETE as MSSQL_DELETE } from '../../providers/mssql/queries/company.js';
import { DELETE as SQLITE_DELETE } from '../../providers/sqlite3/queries/company.js';
import makeRunner from '../runner.js';
export default function del(provider) {
const sql =
provider?.dialect === 'sqlite3' ? SQLITE_DELETE : MSSQL_DELETE;
export default function del(pool) {
/**
* Deletes a company by its id.
*
* @param {number} companyId - The id of the company to delete.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<void>}
*
* @example
* await store.company.delete(1);
*/
return async (companyId) => {
await pool
.request()
.input('companyId', companyId)
.query(DELETE);
return async (companyId, tx) => {
await makeRunner(provider, tx).query(sql, { companyId });
};
}

View File

@@ -1,7 +1,11 @@
import { GET_BY_ID } from '../../providers/mssql/queries/company.js';
import request from '../request.js';
import { GET_BY_ID as MSSQL_GET_BY_ID } from '../../providers/mssql/queries/company.js';
import { GET_BY_ID as SQLITE_GET_BY_ID } from '../../providers/sqlite3/queries/company.js';
import makeRunner from '../runner.js';
export default function getById(provider) {
const sql =
provider?.dialect === 'sqlite3' ? SQLITE_GET_BY_ID : MSSQL_GET_BY_ID;
export default function getById(pool) {
/**
* Returns a single company by its id.
*
@@ -14,10 +18,8 @@ export default function getById(pool) {
* // => { id, name, ... }
*/
return async (id, tx) => {
const result = await request(pool, tx)
.input('id', id)
.query(GET_BY_ID);
const result = await makeRunner(provider, tx).query(sql, { id });
return result.recordset[0];
return result.rows[0];
};
}

View File

@@ -1,7 +1,11 @@
import { GET_BY_NAME } from '../../providers/mssql/queries/company.js';
import request from '../request.js';
import { GET_BY_NAME as MSSQL_GET_BY_NAME } from '../../providers/mssql/queries/company.js';
import { GET_BY_NAME as SQLITE_GET_BY_NAME } from '../../providers/sqlite3/queries/company.js';
import makeRunner from '../runner.js';
export default function getByName(provider) {
const sql =
provider?.dialect === 'sqlite3' ? SQLITE_GET_BY_NAME : MSSQL_GET_BY_NAME;
export default function getByName(pool) {
/**
* Returns a single company by its name.
*
@@ -14,10 +18,8 @@ export default function getByName(pool) {
* // => { id, name, ... }
*/
return async (name, tx) => {
const result = await request(pool, tx)
.input('name', name)
.query(GET_BY_NAME);
const result = await makeRunner(provider, tx).query(sql, { name });
return result.recordset[0];
return result.rows[0];
};
}

View File

@@ -1,21 +1,23 @@
import { LIST } from '../../providers/mssql/queries/company.js';
import { LIST as MSSQL_LIST } from '../../providers/mssql/queries/company.js';
import { LIST as SQLITE_LIST } from '../../providers/sqlite3/queries/company.js';
import makeRunner from '../runner.js';
export default function list(provider) {
const sql = provider?.dialect === 'sqlite3' ? SQLITE_LIST : MSSQL_LIST;
export default function list(pool) {
/**
* Lists the companies accessible to the given Google auth user.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<Array<object>>} The accessible company rows.
*
* @example
* const companies = await store.company.list(42);
* // => [{ id, name, ... }]
*/
return async (googleAuthId) => {
const result = await pool
.request()
.input('googleAuthId', googleAuthId)
.query(LIST);
return result.recordset;
return async (googleAuthId, tx) => {
const result = await makeRunner(provider, tx).query(sql, { googleAuthId });
return result.rows;
};
}

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));
};
}