Files
dashlibstorage/src/store/printeragent/update.js
2026-10-31 20:28:39 +01:00

72 lines
3.1 KiB
JavaScript

import {
GET_BY_COMPANY_AND_ID as MSSQL_GET,
UPDATE as MSSQL_UPDATE,
} from '../../providers/mssql/queries/printeragent.js';
import {
GET_BY_COMPANY_AND_ID as SQLITE_GET,
UPDATE as SQLITE_UPDATE,
} from '../../providers/sqlite3/queries/printeragent.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
import { shape } from './_shared.js';
export default function update(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const getSql = isSqlite ? SQLITE_GET : MSSQL_GET;
const updateSql = isSqlite ? SQLITE_UPDATE : MSSQL_UPDATE;
/**
* Updates name and/or sort of a printer agent of the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - The update input.
* @param {number} input.id - The printer agent id.
* @param {string} [input.name] - The new display name.
* @param {number} [input.sort] - The new display order (integer).
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, companyId: number, name: string, token: string|null, pairingpin: string|null, pairedAt: string|null, clientserial: string|null, clientmodel: string|null, clientstate: string|null, agentversion: string|null, lastseenAt: string|null, updatedAt: string, sort: number}>} The updated printer agent.
* @throws {Error} 'Invalid printeragent' if name is blank, sort is not an integer, or neither is given; 'Printeragent not found' if id is unknown in the company.
*
* @example
* const agent = await store.printeragent.update(42, { id: 5, name: 'Kitchen printer', sort: 1 });
*/
return async (googleAuthId, input = {}, tx) => {
const id = Number(input.id);
if (!Number.isInteger(id) || id <= 0) {
throw new Error('Printeragent not found');
}
if (input.name === undefined && input.sort === undefined && input.enabled === undefined) {
throw new Error('Invalid printeragent');
}
if (input.name !== undefined && (typeof input.name !== 'string' || !input.name.trim())) {
throw new Error('Invalid printeragent');
}
if (input.sort !== undefined && !Number.isInteger(input.sort)) {
throw new Error('Invalid printeragent');
}
if (input.enabled !== undefined && typeof input.enabled !== 'boolean') {
throw new Error('Invalid printeragent');
}
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const existing = await runner.query(getSql, { id, companyId });
if (!existing.rows[0]) {
throw new Error('Printeragent not found');
}
await runner.query(updateSql, {
id,
companyId,
name: input.name !== undefined ? input.name.trim() : existing.rows[0].name,
sort: input.sort !== undefined ? input.sort : existing.rows[0].sort,
enabled: input.enabled !== undefined ? (input.enabled ? 1 : 0) : existing.rows[0].enabled,
});
const row = await runner.query(getSql, { id, companyId });
return shape(row.rows[0]);
};
}