This commit is contained in:
seb
2026-10-08 01:37:26 +02:00
parent 36dd81ee69
commit 69eb2c9a0d
18 changed files with 1479 additions and 1403 deletions

View File

@@ -10,7 +10,7 @@ export default function findByPairingpin(provider) {
*
* @param {string} pairingpin - The pairing pin to search for.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<object|null>} The posterminal row, or null if not found.
* @returns {Promise<{id: number, companyId: number, name: string, token: string|null, pairingpin: string|null, pairedAt: string|null, clientserial: string|null, maxknowntombstoneid: number|null, rootCategoryId: number|null, syncRootArticles: boolean|number, lastseenAt: string|null, updatedAt: string}|null>} The posterminal, or null if not found.
*/
return async (pairingpin, tx) => {
if (!pairingpin) return null;

View File

@@ -10,7 +10,7 @@ export default function findByToken(provider) {
*
* @param {string} token - The auth token to search for.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<object|null>} The posterminal row, or null if not found.
* @returns {Promise<{id: number, companyId: number, name: string, token: string|null, pairingpin: string|null, pairedAt: string|null, clientserial: string|null, maxknowntombstoneid: number|null, rootCategoryId: number|null, syncRootArticles: boolean|number, lastseenAt: string|null, updatedAt: string}|null>} The posterminal, or null if not found.
*/
return async (token, tx) => {
const trimmed = String(token ?? '').trim();

View File

@@ -12,11 +12,11 @@ export default function list(provider) {
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<Array<{id: number, name: string, token: string, pairingpin: string}>>} The posterminals.
* @returns {Promise<Array<{id: number, name: string, token: string|null, pairingpin: string|null, pairedAt: string|null, clientserial: string|null, maxknowntombstoneid: number|null, rootCategoryId: number|null, syncRootArticles: boolean|number, lastseenAt: string|null, updatedAt: string}>>} The posterminals.
*
* @example
* const terminals = await store.posterminal.list(42);
* // => [{ id, name, token, pairingpin }]
* // => [{ id, name, token, pairingpin, pairedAt, clientserial, maxknowntombstoneid, rootCategoryId, syncRootArticles, lastseenAt, updatedAt }]
*/
return async (googleAuthId, tx) => {
const { companyId } = await access(googleAuthId, tx);

View File

@@ -0,0 +1,19 @@
import { TOUCH_LASTSEEN as MSSQL_TOUCH } from '../../providers/mssql/queries/posterminal.js';
import { TOUCH_LASTSEEN as SQLITE_TOUCH } from '../../providers/sqlite3/queries/posterminal.js';
import makeRunner from '../runner.js';
export default function touchLastseen(provider) {
const sql = provider?.dialect === 'sqlite3' ? SQLITE_TOUCH : MSSQL_TOUCH;
/**
* Updates lastseen_at for a POS terminal to the current timestamp.
*
* @param {number} id - The posterminal id.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number}>}
*/
return async (id, tx) => {
await makeRunner(provider, tx).query(sql, { id: Number(id) });
return { id: Number(id) };
};
}

View File

@@ -37,16 +37,12 @@ export default function upsert(provider) {
* // => { id, name, token, pairingpin }
*/
return async (googleAuthId, input = {}, tx) => {
const name = input.name?.trim();
if (!name) {
throw new Error('Invalid posterminal');
}
const pairingpin = input.pairingpin?.trim();
if (!pairingpin) {
const id = input.id ?? null;
const name = input.name !== undefined ? input.name?.trim() : undefined;
if (name === '' || (!name && !id)) {
throw new Error('Invalid posterminal');
}
const token = input.token ?? null;
const id = input.id ?? null;
const pairedAt = input.pairedAt ?? null;
const clientserial = input.clientserial ?? null;
@@ -60,26 +56,67 @@ export default function upsert(provider) {
throw new Error('Posterminal not found');
}
const nameVal = name !== undefined ? name : existing.rows[0].name;
const pairingpin =
input.pairingpin !== undefined
? (input.pairingpin ? String(input.pairingpin).trim() : null)
: (existing.rows[0].pairingpin ?? null);
const maxknowntombstoneid =
input.maxknowntombstoneid !== undefined
? input.maxknowntombstoneid
: (existing.rows[0].maxknowntombstoneid ?? null);
const rootCategoryId =
input.rootCategoryId !== undefined
? (input.rootCategoryId != null ? Number(input.rootCategoryId) : null)
: (existing.rows[0].rootCategoryId ?? null);
const syncRootArticles =
input.syncRootArticles !== undefined
? (input.syncRootArticles ? 1 : 0)
: (existing.rows[0].syncRootArticles !== undefined ? (existing.rows[0].syncRootArticles ? 1 : 0) : 1);
const lastseenAt =
input.lastseenAt !== undefined
? input.lastseenAt
: (existing.rows[0].lastseenAt ?? null);
await runner.query(updateSql, {
id,
companyId,
name,
name: nameVal,
token,
pairingpin,
pairedAt,
clientserial,
maxknowntombstoneid,
rootCategoryId,
syncRootArticles,
lastseenAt,
});
return { id, name, token, pairingpin, pairedAt, clientserial, maxknowntombstoneid };
return {
id,
name: nameVal,
token,
pairingpin,
pairedAt,
clientserial,
maxknowntombstoneid,
rootCategoryId,
syncRootArticles,
lastseenAt,
};
}
const pairingpin = input.pairingpin ? String(input.pairingpin).trim() : null;
const maxknowntombstoneid = input.maxknowntombstoneid ?? null;
const rootCategoryId = input.rootCategoryId != null ? Number(input.rootCategoryId) : null;
const syncRootArticles = input.syncRootArticles !== undefined ? (input.syncRootArticles ? 1 : 0) : 1;
const lastseenAt = input.lastseenAt ?? null;
const created = await runner.query(createSql, {
companyId,
name,
@@ -88,6 +125,9 @@ export default function upsert(provider) {
pairedAt,
clientserial,
maxknowntombstoneid,
rootCategoryId,
syncRootArticles,
lastseenAt,
});
const row = created.rows[0] ?? {
@@ -98,16 +138,23 @@ export default function upsert(provider) {
pairedAt,
clientserial,
maxknowntombstoneid,
rootCategoryId,
syncRootArticles,
lastseenAt,
};
return {
id: row.id,
name: row.name,
token: row.token,
pairingpin: row.pairingpin,
pairingpin: row.pairingpin ?? null,
pairedAt: row.pairedAt ?? null,
clientserial: row.clientserial ?? null,
maxknowntombstoneid: row.maxknowntombstoneid ?? null,
rootCategoryId: row.rootCategoryId ?? null,
syncRootArticles: row.syncRootArticles !== undefined ? (row.syncRootArticles ? 1 : 0) : 1,
lastseenAt: row.lastseenAt ?? null,
updatedAt: row.updatedAt ?? null,
};
};
}