Files
dashlibstorage/src/store/companysetting/set.js

48 lines
1.3 KiB
JavaScript

import { GET_BY_COMPANY_AND_KEY, UPSERT } from '../../sql/companysetting.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import shapeSetting from './shapeSetting.js';
/**
* Sets (upserts) a company setting by key.
*
* @param {object} pool - The mssql connection pool.
* @returns {Function} An async function that sets a company setting.
*
* @example
* const fn = set(pool);
* const setting = await fn(42, 1, { key: 'theme', value: 'dark', frontend: true });
* // => { key, value, frontend }
*/
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]);
};
}