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:
24
src/store/companysetting/delete.js
Normal file
24
src/store/companysetting/delete.js
Normal 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');
|
||||
}
|
||||
};
|
||||
}
|
||||
31
src/store/companysetting/get.js
Normal file
31
src/store/companysetting/get.js
Normal 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);
|
||||
};
|
||||
}
|
||||
21
src/store/companysetting/list.js
Normal file
21
src/store/companysetting/list.js
Normal 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);
|
||||
};
|
||||
}
|
||||
36
src/store/companysetting/set.js
Normal file
36
src/store/companysetting/set.js
Normal 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]);
|
||||
};
|
||||
}
|
||||
9
src/store/companysetting/shapeSetting.js
Normal file
9
src/store/companysetting/shapeSetting.js
Normal file
@@ -0,0 +1,9 @@
|
||||
function shapeSetting(row) {
|
||||
return {
|
||||
key: row.key,
|
||||
value: row.value,
|
||||
frontend: Boolean(row.frontend),
|
||||
};
|
||||
}
|
||||
|
||||
export default shapeSetting;
|
||||
Reference in New Issue
Block a user