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