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,6 +1,6 @@
import { CREATE_MANIFEST, DELIVERY_TYPE } from '../../sql/delivery.js';
import request from '../request.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import taxEnsure from '../tax/ensure.js';
import {
loadDelivery,
@@ -17,14 +17,14 @@ import { assertSupplier, insertLines } from './writeLines.js';
*
* @example
* const fn = create(pool);
* const delivery = await fn(42, 1, { number: 'D-1', date: '2026-01-01', supplierId: 5, lines: [...] });
* const delivery = await fn(42, { number: 'D-1', date: '2026-01-01', supplierId: 5, lines: [...] });
* // => { id, number, type, entityId, date, lines: [...] }
*/
export default function create(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
const ensureTax = taxEnsure(pool);
return async (googleAuthId, companyId, body = {}) => {
return async (googleAuthId, body = {}) => {
const number = body.number?.trim?.() ? body.number.trim() : null;
const date = parseDeliveryDate(body.date);
if (!number || !date) {
@@ -33,7 +33,7 @@ export default function create(pool) {
const lines = normalizeLines(body.lines ?? []);
const { rootCategoryId } = await access(googleAuthId, companyId);
const { companyId, rootCategoryId } = await access(googleAuthId);
const tx = pool.transaction();
await tx.begin();

View File

@@ -1,5 +1,5 @@
import { DELETE_MANIFEST, DELIVERY_TYPE } from '../../sql/delivery.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
/**
* Deletes a delivery by its id within a company.
@@ -9,17 +9,17 @@ import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
*
* @example
* const fn = del(pool);
* await fn(42, 1, 5);
* await fn(42, 5);
*/
export default function del(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, companyId, id) => {
return async (googleAuthId, id) => {
if (!Number.isFinite(id)) {
throw new Error('Delivery not found');
}
await access(googleAuthId, companyId);
const { companyId } = await access(googleAuthId);
const result = await pool
.request()

View File

@@ -1,4 +1,4 @@
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import { loadDelivery } from './helpers.js';
/**
@@ -9,18 +9,18 @@ import { loadDelivery } from './helpers.js';
*
* @example
* const fn = getById(pool);
* const delivery = await fn(42, 1, 5);
* const delivery = await fn(42, 5);
* // => { id, number, type, entityId, date, lines: [...] }
*/
export default function getById(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, companyId, id) => {
return async (googleAuthId, id) => {
if (!Number.isFinite(id)) {
throw new Error('Delivery not found');
}
await access(googleAuthId, companyId);
const { companyId } = await access(googleAuthId);
const delivery = await loadDelivery(pool, null, companyId, id);
if (!delivery) {

View File

@@ -1,5 +1,5 @@
import { DELIVERY_TYPE, LIST_DELIVERIES } from '../../sql/delivery.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import { shapeSummary } from './helpers.js';
/**
@@ -10,14 +10,14 @@ import { shapeSummary } from './helpers.js';
*
* @example
* const fn = list(pool);
* const deliveries = await fn(42, 1);
* const deliveries = await fn(42);
* // => [{ id, number, type, entityId, date }]
*/
export default function list(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, companyId) => {
await access(googleAuthId, companyId);
return async (googleAuthId) => {
const { companyId } = await access(googleAuthId);
const result = await pool
.request()

View File

@@ -5,7 +5,7 @@ import {
UPDATE_MANIFEST,
} from '../../sql/delivery.js';
import request from '../request.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import taxEnsure from '../tax/ensure.js';
import {
loadDelivery,
@@ -23,14 +23,14 @@ import { assertSupplier, insertLines } from './writeLines.js';
*
* @example
* const fn = update(pool);
* const delivery = await fn(42, 1, 5, { number: 'D-2', lines: [...] });
* const delivery = await fn(42, 5, { number: 'D-2', lines: [...] });
* // => { id, number, type, entityId, date, lines: [...] }
*/
export default function update(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
const ensureTax = taxEnsure(pool);
return async (googleAuthId, companyId, id, patch = {}) => {
return async (googleAuthId, id, patch = {}) => {
if (!Number.isFinite(id)) {
throw new Error('Delivery not found');
}
@@ -52,7 +52,7 @@ export default function update(pool) {
normalizedLines = normalizeLines(patch.lines);
}
const { rootCategoryId } = await access(googleAuthId, companyId);
const { companyId, rootCategoryId } = await access(googleAuthId);
const tx = pool.transaction();
await tx.begin();