This commit is contained in:
seb
2026-10-30 18:06:03 +01:00
parent 2e00f4adb3
commit 745d0d0077
8 changed files with 270 additions and 0 deletions

View File

@@ -204,6 +204,11 @@ import printeragentList from './store/printeragent/list.js';
import printeragentCreate from './store/printeragent/create.js'; import printeragentCreate from './store/printeragent/create.js';
import printeragentUpdate from './store/printeragent/update.js'; import printeragentUpdate from './store/printeragent/update.js';
import printeragentDelete from './store/printeragent/delete.js'; import printeragentDelete from './store/printeragent/delete.js';
import printeragentFindByPairingpin from './store/printeragent/findByPairingpin.js';
import printeragentFindByToken from './store/printeragent/findByToken.js';
import printeragentPair from './store/printeragent/pair.js';
import printeragentUpdateState from './store/printeragent/updateState.js';
import printeragentTouchLastseen from './store/printeragent/touchLastseen.js';
import modelconversationCreate from './store/modelconversation/create.js'; import modelconversationCreate from './store/modelconversation/create.js';
import modelconversationGet from './store/modelconversation/get.js'; import modelconversationGet from './store/modelconversation/get.js';
import modelconversationList from './store/modelconversation/list.js'; import modelconversationList from './store/modelconversation/list.js';
@@ -646,6 +651,11 @@ export default class LibDashStore {
create: printeragentCreate(provider), create: printeragentCreate(provider),
update: printeragentUpdate(provider), update: printeragentUpdate(provider),
delete: printeragentDelete(provider), delete: printeragentDelete(provider),
findByPairingpin: printeragentFindByPairingpin(provider),
findByToken: printeragentFindByToken(provider),
pair: printeragentPair(provider),
updateState: printeragentUpdateState(provider),
touchLastseen: printeragentTouchLastseen(provider),
}; };
this.modelconversation = { this.modelconversation = {

View File

@@ -29,6 +29,61 @@ FROM printeragent
WHERE id = @id AND company_id = @companyId WHERE id = @id AND company_id = @companyId
`; `;
export const GET_BY_ID = `
SELECT
${PRINTERAGENT_COLUMNS}
FROM printeragent
WHERE id = @id
`;
export const FIND_BY_PAIRINGPIN = `
SELECT
${PRINTERAGENT_COLUMNS}
FROM printeragent
WHERE pairingpin = @pairingpin
`;
export const FIND_BY_TOKEN = `
SELECT
${PRINTERAGENT_COLUMNS}
FROM printeragent
WHERE token = @token
`;
export const PAIR = `
UPDATE printeragent
SET token = @token,
pairingpin = NULL,
paired_at = @pairedAt,
clientserial = @clientserial,
clientmodel = @clientmodel,
agentversion = @agentversion,
lastseen_at = SYSUTCDATETIME(),
updated_at = SYSUTCDATETIME()
WHERE id = @id
`;
export const UPDATE_STATE = `
UPDATE printeragent
SET clientstate = @clientstate,
lastseen_at = SYSUTCDATETIME(),
updated_at = SYSUTCDATETIME()
WHERE id = @id
`;
export const TOUCH_LASTSEEN = `
UPDATE printeragent
SET lastseen_at = SYSUTCDATETIME()
WHERE id = @id
`;
export const TOUCH_LASTSEEN_AND_VERSION = `
UPDATE printeragent
SET lastseen_at = SYSUTCDATETIME(),
agentversion = @agentversion
WHERE id = @id
`;
export const CREATE = ` export const CREATE = `
INSERT INTO printeragent (company_id, name, pairingpin, sort) INSERT INTO printeragent (company_id, name, pairingpin, sort)
OUTPUT OUTPUT

View File

@@ -5,8 +5,15 @@ const queries = translateQueries(source, 'printeragent');
export const { export const {
CREATE, CREATE,
DELETE_BY_COMPANY_AND_ID, DELETE_BY_COMPANY_AND_ID,
FIND_BY_PAIRINGPIN,
FIND_BY_TOKEN,
GET_BY_COMPANY_AND_ID, GET_BY_COMPANY_AND_ID,
GET_BY_ID,
LIST_BY_COMPANY, LIST_BY_COMPANY,
PAIR,
PRINTERAGENT_COLUMNS, PRINTERAGENT_COLUMNS,
TOUCH_LASTSEEN,
TOUCH_LASTSEEN_AND_VERSION,
UPDATE, UPDATE,
UPDATE_STATE,
} = queries; } = queries;

View File

@@ -0,0 +1,26 @@
import { FIND_BY_PAIRINGPIN as MSSQL_FIND } from '../../providers/mssql/queries/printeragent.js';
import { FIND_BY_PAIRINGPIN as SQLITE_FIND } from '../../providers/sqlite3/queries/printeragent.js';
import makeRunner from '../runner.js';
import { shape } from './_shared.js';
export default function findByPairingpin(provider) {
const sql = provider?.dialect === 'sqlite3' ? SQLITE_FIND : MSSQL_FIND;
/**
* Finds a printer agent by its pairing pin.
*
* @param {string} pairingpin - The pairing pin to search for.
* @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}|null>} The printer agent, or null if not found.
*
* @example
* const agent = await store.printeragent.findByPairingpin('123456');
*/
return async (pairingpin, tx) => {
if (!pairingpin) return null;
const result = await makeRunner(provider, tx).query(sql, {
pairingpin: String(pairingpin).trim(),
});
return result.rows[0] ? shape(result.rows[0]) : null;
};
}

View File

@@ -0,0 +1,27 @@
import { FIND_BY_TOKEN as MSSQL_FIND } from '../../providers/mssql/queries/printeragent.js';
import { FIND_BY_TOKEN as SQLITE_FIND } from '../../providers/sqlite3/queries/printeragent.js';
import makeRunner from '../runner.js';
import { shape } from './_shared.js';
export default function findByToken(provider) {
const sql = provider?.dialect === 'sqlite3' ? SQLITE_FIND : MSSQL_FIND;
/**
* Finds a printer agent by its authentication token.
*
* @param {string} token - The auth token to search for.
* @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}|null>} The printer agent, or null if not found.
*
* @example
* const agent = await store.printeragent.findByToken('abc...');
*/
return async (token, tx) => {
const trimmed = String(token ?? '').trim();
if (!trimmed) return null;
const result = await makeRunner(provider, tx).query(sql, {
token: trimmed,
});
return result.rows[0] ? shape(result.rows[0]) : null;
};
}

View File

@@ -0,0 +1,61 @@
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 clientserial = input.clientserial ? String(input.clientserial).trim() : null;
const clientmodel = input.clientmodel ? String(input.clientmodel).trim() : null;
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,
clientserial,
clientmodel,
agentversion,
});
const updated = await runner.query(getSql, { id: numericId });
if (!updated.rows[0]) {
throw new Error('Printeragent not found');
}
return shape(updated.rows[0]);
};
}

View File

@@ -0,0 +1,39 @@
import {
TOUCH_LASTSEEN as MSSQL_TOUCH,
TOUCH_LASTSEEN_AND_VERSION as MSSQL_TOUCH_VERSION,
} from '../../providers/mssql/queries/printeragent.js';
import {
TOUCH_LASTSEEN as SQLITE_TOUCH,
TOUCH_LASTSEEN_AND_VERSION as SQLITE_TOUCH_VERSION,
} from '../../providers/sqlite3/queries/printeragent.js';
import makeRunner from '../runner.js';
export default function touchLastseen(provider) {
const isSqlite = provider?.dialect === 'sqlite3';
const touchSql = isSqlite ? SQLITE_TOUCH : MSSQL_TOUCH;
const touchVersionSql = isSqlite ? SQLITE_TOUCH_VERSION : MSSQL_TOUCH_VERSION;
/**
* Updates lastseen_at (and optionally agentversion) for a printer agent.
*
* @param {number} id - The printer agent id.
* @param {string|null} [agentversion] - Optional agent version to update.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number}>}
*/
return async (id, agentversion, tx) => {
const numericId = Number(id);
if (!Number.isInteger(numericId) || numericId <= 0) {
throw new Error('Printeragent not found');
}
const runner = makeRunner(provider, tx);
const versionVal = agentversion ? String(agentversion).trim() : null;
if (versionVal) {
await runner.query(touchVersionSql, { id: numericId, agentversion: versionVal });
} else {
await runner.query(touchSql, { id: numericId });
}
return { id: numericId };
};
}

View File

@@ -0,0 +1,45 @@
import {
UPDATE_STATE as MSSQL_UPDATE_STATE,
GET_BY_ID as MSSQL_GET,
} from '../../providers/mssql/queries/printeragent.js';
import {
UPDATE_STATE as SQLITE_UPDATE_STATE,
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 updateState(provider) {
const isSqlite = provider?.dialect === 'sqlite3';
const updateSql = isSqlite ? SQLITE_UPDATE_STATE : MSSQL_UPDATE_STATE;
const getSql = isSqlite ? SQLITE_GET : MSSQL_GET;
/**
* Updates the client state of a printer agent.
*
* @param {number} id - The printer agent id.
* @param {string|null} clientstate - The state payload/string.
* @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, clientstate, tx) => {
const numericId = Number(id);
if (!Number.isInteger(numericId) || numericId <= 0) {
throw new Error('Printeragent not found');
}
const stateVal = clientstate == null ? null : (typeof clientstate === 'string' ? clientstate.trim() : JSON.stringify(clientstate));
const runner = makeRunner(provider, tx);
await runner.query(updateSql, {
id: numericId,
clientstate: stateVal,
});
const updated = await runner.query(getSql, { id: numericId });
if (!updated.rows[0]) {
throw new Error('Printeragent not found');
}
return shape(updated.rows[0]);
};
}