37 lines
1.4 KiB
JavaScript
37 lines
1.4 KiB
JavaScript
import { DELETE_BY_COMPANY_AND_ID as MSSQL_DELETE } from '../../providers/mssql/queries/printeragent.js';
|
|
import { DELETE_BY_COMPANY_AND_ID as SQLITE_DELETE } from '../../providers/sqlite3/queries/printeragent.js';
|
|
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
|
import makeRunner from '../runner.js';
|
|
|
|
export default function del(provider) {
|
|
const access = resolveSelectedCompanyAccess(provider);
|
|
const sql = provider?.dialect === 'sqlite3' ? SQLITE_DELETE : MSSQL_DELETE;
|
|
|
|
/**
|
|
* Deletes a printer agent of the user's selected company.
|
|
*
|
|
* @param {number} googleAuthId - The id of the Google auth user.
|
|
* @param {number} id - The printer agent id.
|
|
* @param {object} [tx] - Optional transaction to run within.
|
|
* @returns {Promise<void>}
|
|
* @throws {Error} 'Printeragent not found' if the id is unknown in the company.
|
|
*
|
|
* @example
|
|
* await store.printeragent.delete(42, 5);
|
|
*/
|
|
return async (googleAuthId, id, tx) => {
|
|
const numericId = Number(id);
|
|
if (!Number.isInteger(numericId) || numericId <= 0) {
|
|
throw new Error('Printeragent not found');
|
|
}
|
|
|
|
const runner = makeRunner(provider, tx);
|
|
const { companyId } = await access(googleAuthId, runner);
|
|
|
|
const result = await runner.query(sql, { id: numericId, companyId });
|
|
if (!result.rowsAffected) {
|
|
throw new Error('Printeragent not found');
|
|
}
|
|
};
|
|
}
|