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

@@ -6,6 +6,7 @@ export const ENTITY_COLUMNS = `
e.customergroup_id AS customergroupId,
e.first_name AS firstName,
e.name,
e.[number] AS number,
e.created_at AS createdAt,
e.updated_at AS updatedAt
`;
@@ -49,6 +50,16 @@ WHERE e.id = @id
AND cg.company_id = @companyId
`;
/** Occupied when another entity in the company already has this number. */
export const NUMBER_TAKEN_IN_COMPANY = `
SELECT TOP 1 e.id
FROM entity e
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
WHERE cg.company_id = @companyId
AND e.[number] = @number
AND (@excludeEntityId IS NULL OR e.id <> @excludeEntityId)
`;
/** Customer entity: company-scoped and not Lieferanten/POS. */
export const GET_CUSTOMER_BY_ID = `
SELECT
@@ -87,15 +98,16 @@ WHERE e.id = @id
`;
export const CREATE = `
INSERT INTO entity (customergroup_id, first_name, name)
INSERT INTO entity (customergroup_id, first_name, name, [number])
OUTPUT
INSERTED.id,
INSERTED.customergroup_id AS customergroupId,
INSERTED.first_name AS firstName,
INSERTED.name,
INSERTED.[number] AS number,
INSERTED.created_at AS createdAt,
INSERTED.updated_at AS updatedAt
VALUES (@customergroupId, @firstName, @name)
VALUES (@customergroupId, @firstName, @name, @number)
`;
export const UPDATE = `
@@ -103,12 +115,14 @@ UPDATE entity
SET
first_name = @firstName,
name = @name,
[number] = @number,
updated_at = SYSUTCDATETIME()
OUTPUT
INSERTED.id,
INSERTED.customergroup_id AS customergroupId,
INSERTED.first_name AS firstName,
INSERTED.name,
INSERTED.[number] AS number,
INSERTED.created_at AS createdAt,
INSERTED.updated_at AS updatedAt
WHERE id = @id

View File

@@ -40,7 +40,12 @@ export const TABLES = [
'meta',
];
export const PROCEDURES = ['getnextnumberwithprefixandpadding'];
export const PROCEDURES = [
'getnextnumberwithprefixandpadding',
'peeknextnumberwithprefixandpadding',
];
export const VIEWS = ['v_entity_company_number'];
/** Drop before tables — breaks company ↔ customergroup cycle for purge. */
export const CONSTRAINTS = [
@@ -54,6 +59,10 @@ export function buildPurgeSql() {
(name) =>
`IF OBJECT_ID(N'dbo.${name}', N'P') IS NOT NULL DROP PROCEDURE dbo.[${name}];`,
),
...VIEWS.map(
(name) =>
`IF OBJECT_ID(N'dbo.${name}', N'V') IS NOT NULL DROP VIEW dbo.[${name}];`,
),
...CONSTRAINTS,
...TABLES.map(
(name) =>