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).
52 lines
1.2 KiB
JavaScript
52 lines
1.2 KiB
JavaScript
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,
|
|
};
|
|
};
|
|
}
|