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_ID } from '../../sql/posterminal.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
/**
* Deletes a posterminal 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('Posterminal not found');
}
await access(googleAuthId, companyId);
const { companyId } = await access(googleAuthId);
const result = await pool
.request()

View File

@@ -1,5 +1,5 @@
import { LIST_BY_COMPANY } from '../../sql/posterminal.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
/**
* Lists the posterminals of a company.
@@ -9,14 +9,14 @@ import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
*
* @example
* const fn = list(pool);
* const terminals = await fn(42, 1);
* const terminals = await fn(42);
* // => [{ id, name, token, pairingpin }]
*/
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

@@ -1,5 +1,5 @@
import { GET_BY_ID, SET_OFF, LIST_CATEGORY_IDS } from '../../sql/posterminal.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
/**
* Removes a category assignment from a posterminal (turns it off).
@@ -9,18 +9,18 @@ import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
*
* @example
* const fn = setOff(pool);
* const result = await fn(42, 1, 5, 9);
* const result = await fn(42, 5, 9);
* // => { id, categoryIds: [...] }
*/
export default function setOff(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, companyId, posterminalId, categoryId) => {
return async (googleAuthId, posterminalId, categoryId) => {
if (!Number.isFinite(posterminalId) || !Number.isFinite(categoryId)) {
throw new Error('Invalid posterminal or category');
}
await access(googleAuthId, companyId);
const { companyId } = await access(googleAuthId);
const existing = await pool
.request()

View File

@@ -1,5 +1,5 @@
import { GET_BY_ID, SET_OFF_ALL, LIST_CATEGORY_IDS } from '../../sql/posterminal.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
/**
* Removes all category assignments from a posterminal.
@@ -9,18 +9,18 @@ import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
*
* @example
* const fn = setOffAll(pool);
* const result = await fn(42, 1, 5);
* const result = await fn(42, 5);
* // => { id, categoryIds: [] }
*/
export default function setOffAll(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, companyId, posterminalId) => {
return async (googleAuthId, posterminalId) => {
if (!Number.isFinite(posterminalId)) {
throw new Error('Invalid posterminal');
}
await access(googleAuthId, companyId);
const { companyId } = await access(googleAuthId);
// Verify posterminal belongs to company
const pt = await pool

View File

@@ -1,5 +1,5 @@
import { GET_BY_ID, SET_ON, LIST_CATEGORY_IDS } from '../../sql/posterminal.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
/**
* Assigns a category to a posterminal (turns it on for that category).
@@ -9,18 +9,18 @@ import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
*
* @example
* const fn = setOn(pool);
* const result = await fn(42, 1, 5, 9);
* const result = await fn(42, 5, 9);
* // => { id, categoryIds: [9, ...] }
*/
export default function setOn(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, companyId, posterminalId, categoryId) => {
return async (googleAuthId, posterminalId, categoryId) => {
if (!Number.isFinite(posterminalId) || !Number.isFinite(categoryId)) {
throw new Error('Invalid posterminal or category');
}
await access(googleAuthId, companyId);
const { companyId } = await access(googleAuthId);
const existing = await pool
.request()

View File

@@ -1,5 +1,5 @@
import { GET_BY_ID, SET_ON_ALL, LIST_CATEGORY_IDS } from '../../sql/posterminal.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import { GET_COMPANY_ROOT_CATEGORY } from '../../sql/company.js';
/**
@@ -10,18 +10,18 @@ import { GET_COMPANY_ROOT_CATEGORY } from '../../sql/company.js';
*
* @example
* const fn = setOnAll(pool);
* const result = await fn(42, 1, 5);
* const result = await fn(42, 5);
* // => { id, categoryIds: [...] }
*/
export default function setOnAll(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, companyId, posterminalId) => {
return async (googleAuthId, posterminalId) => {
if (!Number.isFinite(posterminalId)) {
throw new Error('Invalid posterminal');
}
await access(googleAuthId, companyId);
const { companyId } = await access(googleAuthId);
// Verify posterminal belongs to company
const pt = await pool

View File

@@ -1,5 +1,5 @@
import { GET_BY_ID, CREATE, UPDATE } from '../../sql/posterminal.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
/**
* Creates or updates a posterminal for a company.
@@ -9,13 +9,13 @@ import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
*
* @example
* const fn = upsert(pool);
* const terminal = await fn(42, 1, { name: 'Kasse 1', pairingpin: '1234', token: 'abc' });
* const terminal = await fn(42, { name: 'Kasse 1', pairingpin: '1234', token: 'abc' });
* // => { id, name, token, pairingpin }
*/
export default function upsert(pool) {
const access = resolveCompanyAccess(pool);
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, companyId, input = {}) => {
return async (googleAuthId, input = {}) => {
const name = input.name?.trim();
if (!name) {
throw new Error('Invalid posterminal');
@@ -27,7 +27,7 @@ export default function upsert(pool) {
const token = input.token ?? null;
const id = input.id ?? null;
await access(googleAuthId, companyId);
const { companyId } = await access(googleAuthId);
if (id) {
const existing = await pool