This commit is contained in:
seb
2026-10-31 21:46:43 +01:00
parent 76ea6642f4
commit 55f04de8a8

View File

@@ -1,9 +1,11 @@
import {
LIST_BY_PRINTERAGENT as MSSQL_LIST,
LIST_BY_COMPANY as MSSQL_LIST_BY_COMPANY,
LIST_BY_PRINTERAGENT as MSSQL_LIST_BY_AGENT,
UPDATE as MSSQL_UPDATE,
} from '../../providers/mssql/queries/agentprinter.js';
import {
LIST_BY_PRINTERAGENT as SQLITE_LIST,
LIST_BY_COMPANY as SQLITE_LIST_BY_COMPANY,
LIST_BY_PRINTERAGENT as SQLITE_LIST_BY_AGENT,
UPDATE as SQLITE_UPDATE,
} from '../../providers/sqlite3/queries/agentprinter.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
@@ -12,23 +14,28 @@ import makeRunner from '../runner.js';
export default function order(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const listSql = isSqlite ? SQLITE_LIST : MSSQL_LIST;
const listCompanySql = isSqlite ? SQLITE_LIST_BY_COMPANY : MSSQL_LIST_BY_COMPANY;
const listAgentSql = isSqlite ? SQLITE_LIST_BY_AGENT : MSSQL_LIST_BY_AGENT;
const updateSql = isSqlite ? SQLITE_UPDATE : MSSQL_UPDATE;
/**
* Reorders agent printers for a given printer agent within the user's selected company.
* Reorders agent printers for a given printer agent or globally across the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input
* @param {number} input.printeragentId - The printer agent id.
* @param {number} [input.printeragentId] - Optional printer agent id. If omitted, reorders all printers in company.
* @param {number[]} input.agentprinterIds - Complete ordered list of agent printer IDs.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{agentprinterIds: number[]}>}
*/
return async (googleAuthId, input = {}, tx) => {
const pId = Number(input.printeragentId);
if (!Number.isInteger(pId) || pId <= 0) {
throw new Error('Printeragent not found');
const hasAgent = input.printeragentId != null;
let pId;
if (hasAgent) {
pId = Number(input.printeragentId);
if (!Number.isInteger(pId) || pId <= 0) {
throw new Error('Printeragent not found');
}
}
const ids = input.agentprinterIds;
@@ -39,7 +46,9 @@ export default function order(provider) {
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const existing = await runner.query(listSql, { companyId, printeragentId: pId });
const existing = hasAgent
? await runner.query(listAgentSql, { companyId, printeragentId: pId })
: await runner.query(listCompanySql, { companyId });
const existingIds = new Set(existing.rows.map((r) => r.id));
if (ids.length !== existingIds.size || ids.some((id) => !existingIds.has(Number(id)))) {