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

59 lines
2.1 KiB
JavaScript

import {
PAIR as MSSQL_PAIR,
GET_BY_ID as MSSQL_GET,
} from '../../providers/mssql/queries/printeragent.js';
import {
PAIR as SQLITE_PAIR,
GET_BY_ID as SQLITE_GET,
} from '../../providers/sqlite3/queries/printeragent.js';
import makeRunner from '../runner.js';
import { shape } from './_shared.js';
export default function pair(provider) {
const isSqlite = provider?.dialect === 'sqlite3';
const pairSql = isSqlite ? SQLITE_PAIR : MSSQL_PAIR;
const getSql = isSqlite ? SQLITE_GET : MSSQL_GET;
/**
* Completes device pairing for a printer agent.
*
* @param {number} id - The printer agent id.
* @param {object} input
* @param {string} input.token - Auth token for the paired device.
* @param {string} [input.clientserial] - Device serial identifier.
* @param {string} [input.clientmodel] - Device model name.
* @param {string} [input.agentversion] - Device agent software version.
* @param {string} [input.pairedAt] - Timestamp when paired.
* @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}>}
*/
return async (id, input = {}, tx) => {
const numericId = Number(id);
if (!Number.isInteger(numericId) || numericId <= 0) {
throw new Error('Printeragent not found');
}
const token = typeof input.token === 'string' ? input.token.trim() : '';
if (!token) {
throw new Error('Invalid printeragent');
}
const agentversion = input.agentversion ? String(input.agentversion).trim() : null;
const pairedAt = input.pairedAt ?? new Date().toISOString();
const runner = makeRunner(provider, tx);
await runner.query(pairSql, {
id: numericId,
token,
pairedAt,
agentversion,
});
const updated = await runner.query(getSql, { id: numericId });
if (!updated.rows[0]) {
throw new Error('Printeragent not found');
}
return shape(updated.rows[0]);
};
}