Files
dashlibstorage/src/store/printerjob/delete.js
2026-10-31 21:02:50 +01:00

47 lines
1.6 KiB
JavaScript

import {
DELETE_BY_COMPANY_AND_ID as MSSQL_DELETE,
GET_BY_ID_FOR_COMPANY as MSSQL_GET,
} from '../../providers/mssql/queries/printerjob.js';
import {
DELETE_BY_COMPANY_AND_ID as SQLITE_DELETE,
GET_BY_ID_FOR_COMPANY as SQLITE_GET,
} from '../../providers/sqlite3/queries/printerjob.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
import { shape } from './_shared.js';
export default function deleteJob(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const deleteSql = isSqlite ? SQLITE_DELETE : MSSQL_DELETE;
const getSql = isSqlite ? SQLITE_GET : MSSQL_GET;
/**
* Deletes a printer job belonging to the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - Printer job ID.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<object>} The deleted job's snapshot.
*/
return async (googleAuthId, id, tx) => {
const jId = Number(id);
if (!Number.isInteger(jId) || jId <= 0) {
throw new Error('Printerjob not found');
}
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const existing = await runner.query(getSql, { id: jId, companyId });
if (existing.rows.length === 0) {
throw new Error('Printerjob not found');
}
const job = shape(existing.rows[0]);
await runner.query(deleteSql, { id: jId, companyId });
return job;
};
}