import { DELETE_BY_COMPANY_AND_ID as MSSQL_DELETE } from '../../providers/mssql/queries/labeltemplate.js'; import { DELETE_BY_COMPANY_AND_ID as SQLITE_DELETE } from '../../providers/sqlite3/queries/labeltemplate.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 label template for the user's selected company. * * @param {number} googleAuthId - The id of the Google auth user. * @param {number} id - The label template ID to delete. * @param {object} [tx] - Optional transaction to run within. * @returns {Promise} * @throws {Error} If template is not found. */ return async (googleAuthId, id, tx) => { const numericId = Number(id); if (!Number.isInteger(numericId) || numericId <= 0) { throw new Error('Label template 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('Label template not found'); } }; }