This commit is contained in:
seb
2026-10-19 23:56:56 +02:00
parent a5eee3f3d0
commit 4b34902b92
8 changed files with 15 additions and 37 deletions

View File

@@ -44,7 +44,7 @@ Creates a new company together with its root category, POS customer group, defau
| --- | --- | --- | | --- | --- | --- |
| `input` | `object` | The company creation input. | | `input` | `object` | The company creation input. |
| `input.iso639` | `string` | The ISO 639 language code for the company. | | `input.iso639` | `string` | The ISO 639 language code for the company. |
| `input.name` | `string` | The company name (must be unique). | | `input.name` | `string` | The company name. |
| `input.taxrate` | `number` | The standard tax rate in percent. | | `input.taxrate` | `number` | The standard tax rate in percent. |
| `input.reducedtaxrate` | `number` | The reduced tax rate in percent. | | `input.reducedtaxrate` | `number` | The reduced tax rate in percent. |
| `input.googleAuthId` | `number` | The id of the Google auth user to link. | | `input.googleAuthId` | `number` | The id of the Google auth user to link. |
@@ -54,10 +54,6 @@ Creates a new company together with its root category, POS customer group, defau
`Promise<{id: number, rootCategoryId: number, languageId: number, taxrateId: number, reducedtaxrateId: number, taxfreerateId: number, posCustomerGroupId: number}>` — The created company ids. `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.
**Example** **Example**
```js ```js
@@ -208,7 +204,7 @@ Updates an existing company's name, language and tax rates.
| `id` | `number` | The id of the company to update. | | `id` | `number` | The id of the company to update. |
| `input` | `object` | The update input. | | `input` | `object` | The update input. |
| `input.iso639` | `string` | The ISO 639 language code. | | `input.iso639` | `string` | The ISO 639 language code. |
| `input.name` | `string` | The new company name (must be unique). | | `input.name` | `string` | The new company name. |
| `input.taxrate` | `number` | The standard tax rate in percent. | | `input.taxrate` | `number` | The standard tax rate in percent. |
| `input.reducedtaxrate` | `number` | The reduced tax rate in percent. | | `input.reducedtaxrate` | `number` | The reduced tax rate in percent. |
| `[tx]` | `object` | Optional transaction to run within. | | `[tx]` | `object` | Optional transaction to run within. |
@@ -219,7 +215,7 @@ Updates an existing company's name, language and tax rates.
**Throws** **Throws**
`Error` — If the company is not found or the name is taken. `Error` — If the company is not found.
**Example** **Example**

2
API.md
View File

@@ -31,7 +31,7 @@ The store is exposed as a `LibDashStore` instance (see `src/index.js`). Each met
- **categorypic**: `id PK, category_id->category, pic_id->pic, sort:i=0, UQ(category_id, pic_id), UQ(category_id, sort)` - **categorypic**: `id PK, category_id->category, pic_id->pic, sort:i=0, UQ(category_id, pic_id), UQ(category_id, sort)`
- **categoryproperty**: `id PK, category_id->category, property_id->propertyname, value:txt, sort:i=0` - **categoryproperty**: `id PK, category_id->category, property_id->propertyname, value:txt, sort:i=0`
- **categorytext**: `id PK, category_id->category, language_id->language, name:s(255), slug:s(255), seo?:txt, desc?:txt, UQ(category_id, language_id)` - **categorytext**: `id PK, category_id->category, language_id->language, name:s(255), slug:s(255), seo?:txt, desc?:txt, UQ(category_id, language_id)`
- **company**: `id PK, name:s(255) UQ, root_category_id?->category, default_language_id?->language, taxrate?->tax, reducedtaxrate?->tax, taxfreerate?->tax, poscustomergroup_id->customergroup` - **company**: `id PK, name:s(255), root_category_id?->category, default_language_id?->language, taxrate?->tax, reducedtaxrate?->tax, taxfreerate?->tax, poscustomergroup_id->customergroup`
- **companyhistory**: `id PK, company_id->company, companyuser_id?->companyuser, date:dt, text:txt` - **companyhistory**: `id PK, company_id->company, companyuser_id?->companyuser, date:dt, text:txt`
- **companysetting**: `id PK, company_id->company, key:s(255), value:txt, frontend:b=0, writeonly:b=0, UQ(company_id, key)` - **companysetting**: `id PK, company_id->company, key:s(255), value:txt, frontend:b=0, writeonly:b=0, UQ(company_id, key)`
- **companyuser**: `id PK, google_auth_id->google_auth, company_id->company, UQ(google_auth_id, company_id)` - **companyuser**: `id PK, google_auth_id->google_auth, company_id->company, UQ(google_auth_id, company_id)`

View File

@@ -42,12 +42,11 @@ Company-scoped methods take **no** `companyId` argument: they use `google_auth.s
For `POST /api/company`: For `POST /api/company`:
1. Fails if `name` is taken (`Company name not available`) 1. Ensures language by `iso639` (get or create)
2. Ensures language by `iso639` (get or create) 2. Ensures tax rows for `taxrate`, `reducedtaxrate`, and `0` → `taxfreerateId`
3. Ensures tax rows for `taxrate`, `reducedtaxrate`, and `0` → `taxfreerateId` 3. Creates root category (`parentId = null`)
4. Creates root category (`parentId = null`) 4. Creates POS `customergroup` (name `POS`), then inserts company with `poscustomergroup_id` (**NOT NULL**), then links the group via `customergroup.company_id`
5. Creates POS `customergroup` (name `POS`), then inserts company with `poscustomergroup_id` (**NOT NULL**), then links the group via `customergroup.company_id` 5. Links creator as `companyuser` (`googleAuthId` → `company_id`)
6. Links creator as `companyuser` (`googleAuthId` → `company_id`)
`company.poscustomergroup_id` is required (POS pricing in `getTreeForCompany` / category articles). Circular FK with `customergroup.company_id`: the group is inserted first with `company_id` null, then linked after the company row exists. `company.poscustomergroup_id` is required (POS pricing in `getTreeForCompany` / category articles). Circular FK with `customergroup.company_id`: the group is inserted first with `company_id` null, then linked after the company row exists.
@@ -59,7 +58,7 @@ Returns only companies where `googleAuthId` has a `companyuser` row.
For `PATCH /api/company/:id`: For `PATCH /api/company/:id`:
1. Fails if company missing / name taken by another company 1. Fails if company missing
2. Ensures lang + tax rates 2. Ensures lang + tax rates
3. Updates `name`, `default_language_id`, `taxrate`, `reducedtaxrate` 3. Updates `name`, `default_language_id`, `taxrate`, `reducedtaxrate`
(`taxfreerateId` / root category unchanged) (`taxfreerateId` / root category unchanged)

View File

@@ -347,7 +347,7 @@ const SCHEMA_TABLES = [
['categorypic', 'id PK, category_id->category, pic_id->pic, sort:i=0, UQ(category_id, pic_id), UQ(category_id, sort)'], ['categorypic', 'id PK, category_id->category, pic_id->pic, sort:i=0, UQ(category_id, pic_id), UQ(category_id, sort)'],
['categoryproperty', 'id PK, category_id->category, property_id->propertyname, value:txt, sort:i=0'], ['categoryproperty', 'id PK, category_id->category, property_id->propertyname, value:txt, sort:i=0'],
['categorytext', 'id PK, category_id->category, language_id->language, name:s(255), slug:s(255), seo?:txt, desc?:txt, UQ(category_id, language_id)'], ['categorytext', 'id PK, category_id->category, language_id->language, name:s(255), slug:s(255), seo?:txt, desc?:txt, UQ(category_id, language_id)'],
['company', 'id PK, name:s(255) UQ, root_category_id?->category, default_language_id?->language, taxrate?->tax, reducedtaxrate?->tax, taxfreerate?->tax, poscustomergroup_id->customergroup'], ['company', 'id PK, name:s(255), root_category_id?->category, default_language_id?->language, taxrate?->tax, reducedtaxrate?->tax, taxfreerate?->tax, poscustomergroup_id->customergroup'],
['companyhistory', 'id PK, company_id->company, companyuser_id?->companyuser, date:dt, text:txt'], ['companyhistory', 'id PK, company_id->company, companyuser_id?->companyuser, date:dt, text:txt'],
['companysetting', 'id PK, company_id->company, key:s(255), value:txt, frontend:b=0, writeonly:b=0, UQ(company_id, key)'], ['companysetting', 'id PK, company_id->company, key:s(255), value:txt, frontend:b=0, writeonly:b=0, UQ(company_id, key)'],
['companyuser', 'id PK, google_auth_id->google_auth, company_id->company, UQ(google_auth_id, company_id)'], ['companyuser', 'id PK, google_auth_id->google_auth, company_id->company, UQ(google_auth_id, company_id)'],

View File

@@ -118,7 +118,6 @@ BEGIN
reducedtaxrate INT NULL, reducedtaxrate INT NULL,
taxfreerate INT NULL, taxfreerate INT NULL,
poscustomergroup_id INT NOT NULL, poscustomergroup_id INT NOT NULL,
CONSTRAINT UQ_company_name UNIQUE (name),
CONSTRAINT FK_company_root_category FOREIGN KEY (root_category_id) REFERENCES category(id) ON DELETE SET NULL, CONSTRAINT FK_company_root_category FOREIGN KEY (root_category_id) REFERENCES category(id) ON DELETE SET NULL,
CONSTRAINT FK_company_default_language FOREIGN KEY (default_language_id) REFERENCES language(id) ON DELETE SET NULL, CONSTRAINT FK_company_default_language FOREIGN KEY (default_language_id) REFERENCES language(id) ON DELETE SET NULL,
CONSTRAINT FK_company_taxrate FOREIGN KEY (taxrate) REFERENCES tax(id) ON DELETE NO ACTION, CONSTRAINT FK_company_taxrate FOREIGN KEY (taxrate) REFERENCES tax(id) ON DELETE NO ACTION,

View File

@@ -53,7 +53,7 @@ CREATE TABLE IF NOT EXISTS pic (
export const INIT_COMPANY = ` export const INIT_COMPANY = `
CREATE TABLE IF NOT EXISTS company ( CREATE TABLE IF NOT EXISTS company (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL COLLATE NOCASE UNIQUE, name TEXT NOT NULL COLLATE NOCASE,
root_category_id INTEGER, root_category_id INTEGER,
default_language_id INTEGER, default_language_id INTEGER,
taxrate INTEGER, taxrate INTEGER,

View File

@@ -13,13 +13,11 @@ import {
} from '../../providers/sqlite3/queries/customergroup.js'; } from '../../providers/sqlite3/queries/customergroup.js';
import { ADD as MSSQL_ADD_COMPANYUSER } from '../../providers/mssql/queries/companyuser.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 { ADD as SQLITE_ADD_COMPANYUSER } from '../../providers/sqlite3/queries/companyuser.js';
import getByName from './getByName.js';
import langEnsure from '../lang/ensure.js'; import langEnsure from '../lang/ensure.js';
import taxEnsure from '../tax/ensure.js'; import taxEnsure from '../tax/ensure.js';
import makeRunner from '../runner.js'; import makeRunner from '../runner.js';
export default function create(provider) { export default function create(provider) {
const nameTaken = getByName(provider);
const ensureLang = langEnsure(provider); const ensureLang = langEnsure(provider);
const ensureTax = taxEnsure(provider); const ensureTax = taxEnsure(provider);
@@ -36,13 +34,12 @@ export default function create(provider) {
* *
* @param {object} input - The company creation input. * @param {object} input - The company creation input.
* @param {string} input.iso639 - The ISO 639 language code for the company. * @param {string} input.iso639 - The ISO 639 language code for the company.
* @param {string} input.name - The company name (must be unique). * @param {string} input.name - The company name.
* @param {number} input.taxrate - The standard tax rate in percent. * @param {number} input.taxrate - The standard tax rate in percent.
* @param {number} input.reducedtaxrate - The reduced 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 {number} input.googleAuthId - The id of the Google auth user to link.
* @param {object} [tx] - Optional transaction to run within. * @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. * @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.
* *
* @example * @example
* const company = await store.company.create({ * const company = await store.company.create({
@@ -56,10 +53,6 @@ export default function create(provider) {
*/ */
return async ({ iso639, name, taxrate, reducedtaxrate, googleAuthId }, tx) => { return async ({ iso639, name, taxrate, reducedtaxrate, googleAuthId }, tx) => {
const runCreate = async (runner) => { const runCreate = async (runner) => {
const existing = await nameTaken(name, runner);
if (existing) {
throw new Error('Company name not available');
}
const languageId = await ensureLang(iso639, runner); const languageId = await ensureLang(iso639, runner);
const taxrateId = await ensureTax(taxrate, runner); const taxrateId = await ensureTax(taxrate, runner);

View File

@@ -1,14 +1,12 @@
import { UPDATE as MSSQL_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 { UPDATE as SQLITE_UPDATE } from '../../providers/sqlite3/queries/company.js';
import getById from './getById.js'; import getById from './getById.js';
import getByName from './getByName.js';
import langEnsure from '../lang/ensure.js'; import langEnsure from '../lang/ensure.js';
import taxEnsure from '../tax/ensure.js'; import taxEnsure from '../tax/ensure.js';
import makeRunner from '../runner.js'; import makeRunner from '../runner.js';
export default function update(provider) { export default function update(provider) {
const byId = getById(provider); const byId = getById(provider);
const byName = getByName(provider);
const ensureLang = langEnsure(provider); const ensureLang = langEnsure(provider);
const ensureTax = taxEnsure(provider); const ensureTax = taxEnsure(provider);
const isSqlite = provider?.dialect === 'sqlite3'; const isSqlite = provider?.dialect === 'sqlite3';
@@ -20,12 +18,12 @@ export default function update(provider) {
* @param {number} id - The id of the company to update. * @param {number} id - The id of the company to update.
* @param {object} input - The update input. * @param {object} input - The update input.
* @param {string} input.iso639 - The ISO 639 language code. * @param {string} input.iso639 - The ISO 639 language code.
* @param {string} input.name - The new company name (must be unique). * @param {string} input.name - The new company name.
* @param {number} input.taxrate - The standard tax rate in percent. * @param {number} input.taxrate - The standard tax rate in percent.
* @param {number} input.reducedtaxrate - The reduced tax rate in percent. * @param {number} input.reducedtaxrate - The reduced tax rate in percent.
* @param {object} [tx] - Optional transaction to run within. * @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. * @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. * @throws {Error} If the company is not found.
* *
* @example * @example
* const result = await store.company.update(1, { iso639: 'de', name: 'Acme', taxrate: 19, reducedtaxrate: 7 }); * const result = await store.company.update(1, { iso639: 'de', name: 'Acme', taxrate: 19, reducedtaxrate: 7 });
@@ -38,13 +36,6 @@ export default function update(provider) {
throw new Error('Company not found'); throw new Error('Company not found');
} }
if (name !== company.name) {
const taken = await byName(name, runner);
if (taken) {
throw new Error('Company name not available');
}
}
const languageId = await ensureLang(iso639, runner); const languageId = await ensureLang(iso639, runner);
const taxrateId = await ensureTax(taxrate, runner); const taxrateId = await ensureTax(taxrate, runner);
const reducedtaxrateId = await ensureTax(reducedtaxrate, runner); const reducedtaxrateId = await ensureTax(reducedtaxrate, runner);