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.
42 lines
1.0 KiB
JavaScript
42 lines
1.0 KiB
JavaScript
import { DELETE_BY_COMPANY_AND_ID } from '../../sql/brand.js';
|
|
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
|
|
|
/**
|
|
* Deletes a brand for a company by its id.
|
|
*
|
|
* @param {object} pool - The mssql connection pool.
|
|
* @returns {Function} An async function that deletes a brand.
|
|
*
|
|
* @example
|
|
* const fn = del(pool);
|
|
* await fn(42, 3);
|
|
*/
|
|
export default function del(pool) {
|
|
const access = resolveSelectedCompanyAccess(pool);
|
|
|
|
return async (googleAuthId, id) => {
|
|
if (!Number.isFinite(id)) {
|
|
throw new Error('Brand not found');
|
|
}
|
|
|
|
const { companyId } = await access(googleAuthId);
|
|
|
|
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;
|
|
}
|
|
};
|
|
}
|