117 lines
4.7 KiB
JavaScript
117 lines
4.7 KiB
JavaScript
import { POS_GROUP_NAME } from '../../providers/mssql/queries/entity.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 MSSQL_CREATE_CUSTOMERGROUP,
|
|
SET_COMPANY as MSSQL_SET_CUSTOMERGROUP_COMPANY,
|
|
} from '../../providers/mssql/queries/customergroup.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 langEnsure from '../lang/ensure.js';
|
|
import taxEnsure from '../tax/ensure.js';
|
|
import makeRunner from '../runner.js';
|
|
|
|
export default function create(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,
|
|
* default language and tax rates, and links it to the given Google auth user.
|
|
*
|
|
* @param {object} input - The company creation input.
|
|
* @param {string} input.iso639 - The ISO 639 language code for the company.
|
|
* @param {string} input.name - The company name.
|
|
* @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.
|
|
*
|
|
* @example
|
|
* const company = await store.company.create({
|
|
* iso639: 'de',
|
|
* name: 'Acme',
|
|
* taxrate: 19,
|
|
* reducedtaxrate: 7,
|
|
* googleAuthId: 42,
|
|
* });
|
|
* // => { id, rootCategoryId, languageId, taxrateId, reducedtaxrateId, taxfreerateId, posCustomerGroupId }
|
|
*/
|
|
return async ({ iso639, name, taxrate, reducedtaxrate, googleAuthId }, tx) => {
|
|
const runCreate = async (runner) => {
|
|
|
|
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 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 runner.query(createCustGroupSql, {
|
|
name: POS_GROUP_NAME,
|
|
sort: 0,
|
|
});
|
|
const posCustomerGroupId = posGroup.rows[0]?.id ?? posGroup.insertId;
|
|
|
|
const result = await runner.query(createCompanySql, {
|
|
name,
|
|
rootCategoryId,
|
|
languageId,
|
|
taxrate: taxrateId,
|
|
reducedtaxrate: reducedtaxrateId,
|
|
taxfreerate: taxfreerateId,
|
|
posCustomerGroupId,
|
|
});
|
|
const companyId = result.rows[0]?.id ?? result.insertId;
|
|
|
|
await runner.query(setCustGroupCompanySql, {
|
|
id: posCustomerGroupId,
|
|
companyId,
|
|
});
|
|
|
|
await runner.query(addCompanyUserSql, {
|
|
companyId,
|
|
googleAuthId,
|
|
});
|
|
|
|
return {
|
|
id: companyId,
|
|
rootCategoryId,
|
|
languageId,
|
|
taxrateId,
|
|
reducedtaxrateId,
|
|
taxfreerateId,
|
|
posCustomerGroupId,
|
|
};
|
|
};
|
|
|
|
if (tx) {
|
|
return runCreate(makeRunner(provider, tx));
|
|
}
|
|
if (typeof provider?.transaction === 'function') {
|
|
return provider.transaction(runCreate);
|
|
}
|
|
return runCreate(makeRunner(provider));
|
|
};
|
|
}
|