sqlite
This commit is contained in:
@@ -1,39 +1,46 @@
|
||||
import { DELETE_BY_COMPANY_AND_ID } from '../../providers/mssql/queries/customergroup.js';
|
||||
import { DELETE_BY_COMPANY_AND_ID as MSSQL_DELETE_BY_COMPANY_AND_ID } from '../../providers/mssql/queries/customergroup.js';
|
||||
import { DELETE_BY_COMPANY_AND_ID as SQLITE_DELETE_BY_COMPANY_AND_ID } from '../../providers/sqlite3/queries/customergroup.js';
|
||||
import { isForeignKeyConstraintError } from '../../providers/provider.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function del(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
export default function del(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const sql =
|
||||
provider?.dialect === 'sqlite3'
|
||||
? SQLITE_DELETE_BY_COMPANY_AND_ID
|
||||
: MSSQL_DELETE_BY_COMPANY_AND_ID;
|
||||
|
||||
/**
|
||||
* Deletes a customer group for the user's selected company by its id.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - The id of the customer group to delete.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<void>}
|
||||
* @throws {Error} If the customer group is not found.
|
||||
*
|
||||
* @example
|
||||
* await store.customergroup.delete(42, 5);
|
||||
*/
|
||||
return async (googleAuthId, id) => {
|
||||
return async (googleAuthId, id, tx) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Customer group not found');
|
||||
}
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId, tx);
|
||||
|
||||
try {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('companyId', companyId)
|
||||
.query(DELETE_BY_COMPANY_AND_ID);
|
||||
const result = await makeRunner(provider, tx).query(sql, {
|
||||
id,
|
||||
companyId,
|
||||
});
|
||||
|
||||
if (!result.rowsAffected[0]) {
|
||||
if (!result.rowsAffected) {
|
||||
throw new Error('Customer group not found');
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.number === 547) {
|
||||
if (isForeignKeyConstraintError(err)) {
|
||||
throw new Error('Customer group in use');
|
||||
}
|
||||
throw err;
|
||||
|
||||
Reference in New Issue
Block a user