Add entity customer numbers and scope APIs via selected company.

Entity gets an optional unique-per-company number with allocate/peek/isNumberFree helpers. Company-scoped store methods no longer take companyId and resolve google_auth.selected_company instead.
This commit is contained in:
seb
2026-08-07 04:29:35 +02:00
parent 1e89e19863
commit 70e0a9ddc4
78 changed files with 940 additions and 772 deletions

View File

@@ -1,5 +1,5 @@
import { DELETE_BY_COMPANY_AND_SI } from '../../sql/unit.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
/**
* Deletes a unit for a company by its SI code.
@@ -9,17 +9,17 @@ import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
*
* @example
* const fn = del(pool);
* await fn(42, 1, 'l');
* await fn(42, 'l');
*/
export default function del(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, companyId, si) => {
return async (googleAuthId, si) => {
if (!si?.trim()) {
throw new Error('Unit not found');
}
await access(googleAuthId, companyId);
const { companyId } = await access(googleAuthId);
try {
const result = await pool

View File

@@ -1,5 +1,5 @@
import { LIST_BY_COMPANY } from '../../sql/unit.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import shapeUnit from './shapeUnit.js';
/**
@@ -10,14 +10,14 @@ import shapeUnit from './shapeUnit.js';
*
* @example
* const fn = list(pool);
* const units = await fn(42, 1);
* const units = await fn(42);
* // => [{ id, si, basesi, basesifactor, symbol, name }]
*/
export default function list(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, companyId) => {
const { languageId } = await access(googleAuthId, companyId);
return async (googleAuthId) => {
const { companyId, languageId } = await access(googleAuthId);
const result = await pool
.request()

View File

@@ -9,7 +9,7 @@ import {
GET_BY_UNIT_AND_LANGUAGE,
UPDATE_BY_UNIT_AND_LANGUAGE,
} from '../../sql/unittext.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import request from '../request.js';
import shapeUnit from './shapeUnit.js';
@@ -38,15 +38,15 @@ function validateUnit({ si, basesi, basesifactor, symbol, name }) {
*
* @example
* const fn = upsert(pool);
* const unit = await fn(42, 1, { si: 'l', basesi: 'm3', basesifactor: 0.001, symbol: 'L', name: 'Liter' });
* const unit = await fn(42, { si: 'l', basesi: 'm3', basesifactor: 0.001, symbol: 'L', name: 'Liter' });
* // => { id, si, basesi, basesifactor, symbol, name }
*/
export default function upsert(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, companyId, input) => {
return async (googleAuthId, input) => {
const unit = validateUnit(input);
const { languageId } = await access(googleAuthId, companyId);
const { companyId, languageId } = await access(googleAuthId);
const tx = pool.transaction();
await tx.begin();