Add company-scoped brand and company settings API.

brand now carries a NOT NULL company_id (unique per (company_id, name))
with brand.list/upsert/delete backed by resolveCompanyAccess. Adds
companysetting key-value store (list/get/set/delete, frontend flag).
This commit is contained in:
seb
2026-08-01 06:59:27 +02:00
parent c839b2ba35
commit 787e411547
18 changed files with 1810 additions and 1192 deletions

View File

@@ -20,6 +20,13 @@ import articleSetPricesBySku from './store/article/setPricesBySku.js';
import unitList from './store/unit/list.js';
import unitUpsert from './store/unit/upsert.js';
import unitDelete from './store/unit/delete.js';
import companysettingList from './store/companysetting/list.js';
import companysettingGet from './store/companysetting/get.js';
import companysettingSet from './store/companysetting/set.js';
import companysettingDelete from './store/companysetting/delete.js';
import brandList from './store/brand/list.js';
import brandUpsert from './store/brand/upsert.js';
import brandDelete from './store/brand/delete.js';
import langCreate from './store/lang/create.js';
import langDelete from './store/lang/delete.js';
import langList from './store/lang/list.js';
@@ -95,6 +102,19 @@ export default class LibDashStore {
delete: unitDelete(pool),
};
this.companysetting = {
list: companysettingList(pool),
get: companysettingGet(pool),
set: companysettingSet(pool),
delete: companysettingDelete(pool),
};
this.brand = {
list: brandList(pool),
upsert: brandUpsert(pool),
delete: brandDelete(pool),
};
this.google_auth = {
add: googleAuthAdd(pool),
getByEmail: googleAuthGetByEmail(pool),

View File

@@ -212,8 +212,11 @@ IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'brand')
BEGIN
CREATE TABLE brand (
id INT IDENTITY(1,1) PRIMARY KEY,
company_id INT NOT NULL,
name NVARCHAR(255) NOT NULL,
gsprpinfo NVARCHAR(MAX) NULL
gsprpinfo NVARCHAR(MAX) NULL,
CONSTRAINT UQ_brand_company_name UNIQUE (company_id, name),
CONSTRAINT FK_brand_company FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE NO ACTION
);
END
`;
@@ -550,6 +553,21 @@ BEGIN
END
`;
export const INIT_COMPANYSETTING = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'companysetting')
BEGIN
CREATE TABLE companysetting (
id INT IDENTITY(1,1) PRIMARY KEY,
company_id INT NOT NULL,
[key] NVARCHAR(255) NOT NULL,
value NVARCHAR(MAX) NOT NULL,
frontend BIT NOT NULL CONSTRAINT DF_companysetting_frontend DEFAULT 0,
CONSTRAINT UQ_companysetting_company_key UNIQUE (company_id, [key]),
CONSTRAINT FK_companysetting_company FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE CASCADE
);
END
`;
/** DDL scripts in dependency order for schema init. */
export const SCHEMA_SCRIPTS = [
INIT_META,
@@ -585,4 +603,5 @@ export const SCHEMA_SCRIPTS = [
INIT_GET_NEXT_NUMBER_WITH_PREFIX_AND_PADDING,
INIT_COMPANYUSER,
INIT_SYNCPOS,
INIT_COMPANYSETTING,
];

36
src/sql/brand.js Normal file
View File

@@ -0,0 +1,36 @@
export const LIST_BY_COMPANY = `
SELECT
id,
name,
gsprpinfo
FROM brand
WHERE company_id = @companyId
ORDER BY name
`;
export const GET_BY_COMPANY_AND_NAME = `
SELECT
id,
name,
gsprpinfo
FROM brand
WHERE company_id = @companyId
AND name = @name
`;
export const CREATE = `
INSERT INTO brand (company_id, name, gsprpinfo)
OUTPUT INSERTED.id
VALUES (@companyId, @name, @gsprpinfo)
`;
export const UPDATE_BY_ID = `
UPDATE brand
SET name = @name, gsprpinfo = @gsprpinfo
WHERE id = @id AND company_id = @companyId
`;
export const DELETE_BY_COMPANY_AND_ID = `
DELETE FROM brand
WHERE id = @id AND company_id = @companyId
`;

47
src/sql/companysetting.js Normal file
View File

@@ -0,0 +1,47 @@
export const LIST_BY_COMPANY = `
SELECT
[key],
value,
CAST(frontend AS INT) AS frontend
FROM companysetting
WHERE company_id = @companyId
ORDER BY [key]
`;
export const LIST_BY_COMPANY_FRONTEND = `
SELECT
[key],
value,
CAST(frontend AS INT) AS frontend
FROM companysetting
WHERE company_id = @companyId
AND frontend = 1
ORDER BY [key]
`;
export const GET_BY_COMPANY_AND_KEY = `
SELECT
[key],
value,
CAST(frontend AS INT) AS frontend
FROM companysetting
WHERE company_id = @companyId
AND [key] = @key
`;
export const UPSERT = `
MERGE companysetting AS target
USING (SELECT @companyId AS company_id, @key AS [key]) AS src
ON target.company_id = src.company_id AND target.[key] = src.[key]
WHEN MATCHED THEN
UPDATE SET value = @value, frontend = @frontend
WHEN NOT MATCHED THEN
INSERT (company_id, [key], value, frontend)
VALUES (@companyId, @key, @value, @frontend);
`;
export const DELETE_BY_COMPANY_AND_KEY = `
DELETE FROM companysetting
WHERE company_id = @companyId
AND [key] = @key
`;

View File

@@ -2,6 +2,7 @@
export const TABLES = [
'companyuser',
'syncpos',
'companysetting',
'numbers',
'manifestarticle',
'manifest',

31
src/store/brand/delete.js Normal file
View File

@@ -0,0 +1,31 @@
import { DELETE_BY_COMPANY_AND_ID } from '../../sql/brand.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function del(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, id) => {
if (!Number.isFinite(id)) {
throw new Error('Brand not found');
}
await access(googleAuthId, companyId);
try {
const result = await pool
.request()
.input('id', id)
.input('companyId', companyId)
.query(DELETE_BY_COMPANY_AND_ID);
if (!result.rowsAffected[0]) {
throw new Error('Brand not found');
}
} catch (err) {
if (err.number === 547) {
throw new Error('Brand in use');
}
throw err;
}
};
}

17
src/store/brand/list.js Normal file
View File

@@ -0,0 +1,17 @@
import { LIST_BY_COMPANY } from '../../sql/brand.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function list(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId) => {
await access(googleAuthId, companyId);
const result = await pool
.request()
.input('companyId', companyId)
.query(LIST_BY_COMPANY);
return result.recordset;
};
}

51
src/store/brand/upsert.js Normal file
View File

@@ -0,0 +1,51 @@
import {
CREATE,
GET_BY_COMPANY_AND_NAME,
UPDATE_BY_ID,
} from '../../sql/brand.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function upsert(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, input = {}) => {
const name = input.name?.trim();
if (!name) {
throw new Error('Invalid brand');
}
const gsprpinfo = input.gsprpinfo ?? null;
await access(googleAuthId, companyId);
const existing = await pool
.request()
.input('companyId', companyId)
.input('name', name)
.query(GET_BY_COMPANY_AND_NAME);
if (existing.recordset[0]) {
const { id } = existing.recordset[0];
await pool
.request()
.input('id', id)
.input('companyId', companyId)
.input('name', name)
.input('gsprpinfo', gsprpinfo)
.query(UPDATE_BY_ID);
return { id, name, gsprpinfo };
}
const created = await pool
.request()
.input('companyId', companyId)
.input('name', name)
.input('gsprpinfo', gsprpinfo)
.query(CREATE);
return {
id: created.recordset[0].id,
name,
gsprpinfo,
};
};
}

View File

@@ -0,0 +1,24 @@
import { DELETE_BY_COMPANY_AND_KEY } from '../../sql/companysetting.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function del(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, key) => {
if (!key?.trim()) {
throw new Error('Setting not found');
}
await access(googleAuthId, companyId);
const result = await pool
.request()
.input('companyId', companyId)
.input('key', key.trim())
.query(DELETE_BY_COMPANY_AND_KEY);
if (!result.rowsAffected[0]) {
throw new Error('Setting not found');
}
};
}

View File

@@ -0,0 +1,31 @@
import { GET_BY_COMPANY_AND_KEY } from '../../sql/companysetting.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import shapeSetting from './shapeSetting.js';
export default function get(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, key, { frontendOnly = false } = {}) => {
if (!key?.trim()) {
return undefined;
}
await access(googleAuthId, companyId);
const result = await pool
.request()
.input('companyId', companyId)
.input('key', key.trim())
.query(GET_BY_COMPANY_AND_KEY);
const row = result.recordset[0];
if (!row) {
return undefined;
}
if (frontendOnly && !row.frontend) {
return undefined;
}
return shapeSetting(row);
};
}

View File

@@ -0,0 +1,21 @@
import {
LIST_BY_COMPANY,
LIST_BY_COMPANY_FRONTEND,
} from '../../sql/companysetting.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import shapeSetting from './shapeSetting.js';
export default function list(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, { frontendOnly = false } = {}) => {
await access(googleAuthId, companyId);
const result = await pool
.request()
.input('companyId', companyId)
.query(frontendOnly ? LIST_BY_COMPANY_FRONTEND : LIST_BY_COMPANY);
return result.recordset.map(shapeSetting);
};
}

View File

@@ -0,0 +1,36 @@
import { GET_BY_COMPANY_AND_KEY, UPSERT } from '../../sql/companysetting.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import shapeSetting from './shapeSetting.js';
export default function set(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, input = {}) => {
const key = input.key?.trim();
if (!key) {
throw new Error('Invalid setting');
}
if (typeof input.value !== 'string') {
throw new Error('Invalid setting');
}
const frontend = Boolean(input.frontend);
await access(googleAuthId, companyId);
await pool
.request()
.input('companyId', companyId)
.input('key', key)
.input('value', input.value)
.input('frontend', frontend)
.query(UPSERT);
const result = await pool
.request()
.input('companyId', companyId)
.input('key', key)
.query(GET_BY_COMPANY_AND_KEY);
return shapeSetting(result.recordset[0]);
};
}

View File

@@ -0,0 +1,9 @@
function shapeSetting(row) {
return {
key: row.key,
value: row.value,
frontend: Boolean(row.frontend),
};
}
export default shapeSetting;