Compare commits
3 Commits
a4718d81c0
...
2e00f4adb3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e00f4adb3 | ||
|
|
60cf5873cd | ||
|
|
211acd651d |
5
API.md
5
API.md
@@ -404,6 +404,11 @@ The store is exposed as a `LibDashStore` instance (see `src/index.js`). Each met
|
||||
- `create(googleAuthId: number, input: {accountId: number, manifestId: number, type: string, amount: number}, tx?: object): Promise<{id: number, accountId: number, manifestId: number, type: string, amount: number}>`
|
||||
- `delete(googleAuthId: number, id: number, tx?: object): Promise<void>`
|
||||
- `listByManifestId(googleAuthId: number, manifestId: number, tx?: object): Promise<Array<{id: number, accountId: number, manifestId: number, type: string, amount: number}>>`
|
||||
- [printeragent](API.printeragent.md)
|
||||
- `create(googleAuthId: number, input: {name: string, pairingpin: string, sort?: number}, tx?: object): 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}>`
|
||||
- `delete(googleAuthId: number, id: number, tx?: object): Promise<void>`
|
||||
- `list(googleAuthId: number, tx?: object): Promise<Array<{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}>>`
|
||||
- `update(googleAuthId: number, input: {id: number, name?: string, sort?: number}, tx?: object): 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}>`
|
||||
- [retoure](API.retoure.md)
|
||||
- `create(googleAuthId: number, body: {number?: string, date: string, entityId?: number, externalNumber?: string, lines?: Array<object>}, tx?: object): Promise<object>`
|
||||
- `createShipment(googleAuthId: number, retoureId: number, input: object, tx?: object): Promise<object>`
|
||||
|
||||
117
API.printeragent.md
Normal file
117
API.printeragent.md
Normal file
@@ -0,0 +1,117 @@
|
||||
# printeragent
|
||||
|
||||
This document is auto-generated from the JSDoc comments in `src/store` by running `npm run api-doc`.
|
||||
|
||||
## `printeragent.create`
|
||||
|
||||
*Source: `src/store/printeragent/create.js`*
|
||||
|
||||
Creates an unpaired printer agent for the user's selected company. The agent registers itself later using the pairing pin.
|
||||
|
||||
**Parameters**
|
||||
|
||||
| Name | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| `googleAuthId` | `number` | The id of the Google auth user. |
|
||||
| `input` | `object` | The printer agent input. |
|
||||
| `input.name` | `string` | The display name. |
|
||||
| `input.pairingpin` | `string` | The pairing pin the agent uses to register. |
|
||||
| `[input.sort]` | `number` | Display order (integer, ascending). Default 0. |
|
||||
| `[tx]` | `object` | 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 created printer agent.
|
||||
|
||||
**Throws**
|
||||
|
||||
`Error` — 'Invalid printeragent' if name or pairingpin is missing/blank or sort is not an integer.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
const agent = await store.printeragent.create(42, { name: 'Bar printer', pairingpin: '123456' });
|
||||
```
|
||||
|
||||
## `printeragent.delete`
|
||||
|
||||
*Source: `src/store/printeragent/delete.js`*
|
||||
|
||||
Deletes a printer agent of the user's selected company.
|
||||
|
||||
**Parameters**
|
||||
|
||||
| Name | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| `googleAuthId` | `number` | The id of the Google auth user. |
|
||||
| `id` | `number` | The printer agent id. |
|
||||
| `[tx]` | `object` | Optional transaction to run within. |
|
||||
|
||||
**Returns**
|
||||
|
||||
`Promise<void>`
|
||||
|
||||
**Throws**
|
||||
|
||||
`Error` — 'Printeragent not found' if the id is unknown in the company.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
await store.printeragent.delete(42, 5);
|
||||
```
|
||||
|
||||
## `printeragent.list`
|
||||
|
||||
*Source: `src/store/printeragent/list.js`*
|
||||
|
||||
Lists the printer agents of the user's selected company.
|
||||
|
||||
**Parameters**
|
||||
|
||||
| Name | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| `googleAuthId` | `number` | The id of the Google auth user. |
|
||||
| `[tx]` | `object` | Optional transaction to run within. |
|
||||
|
||||
**Returns**
|
||||
|
||||
`Promise<Array<{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 printer agents.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
const agents = await store.printeragent.list(42);
|
||||
// => [{ id, companyId, name, token, pairingpin, pairedAt, clientserial, clientmodel, clientstate, agentversion, lastseenAt, updatedAt, sort }]
|
||||
```
|
||||
|
||||
## `printeragent.update`
|
||||
|
||||
*Source: `src/store/printeragent/update.js`*
|
||||
|
||||
Updates name and/or sort of a printer agent of the user's selected company.
|
||||
|
||||
**Parameters**
|
||||
|
||||
| Name | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| `googleAuthId` | `number` | The id of the Google auth user. |
|
||||
| `input` | `object` | The update input. |
|
||||
| `input.id` | `number` | The printer agent id. |
|
||||
| `[input.name]` | `string` | The new display name. |
|
||||
| `[input.sort]` | `number` | The new display order (integer). |
|
||||
| `[tx]` | `object` | 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**
|
||||
|
||||
```js
|
||||
const agent = await store.printeragent.update(42, { id: 5, name: 'Kitchen printer', sort: 1 });
|
||||
```
|
||||
47
scripts/test-printeragent.js
Normal file
47
scripts/test-printeragent.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtempSync, rmSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import LibDashStore from '../src/index.js';
|
||||
import { BetterSqlite3Provider } from '../src/providers/sqlite3/index.js';
|
||||
|
||||
const tempDir = mkdtempSync(join(tmpdir(), 'dash-printeragent-test-'));
|
||||
const provider = new BetterSqlite3Provider({ filename: join(tempDir, 'test.db'), storageDir: join(tempDir, 'storage') });
|
||||
await provider.initialize();
|
||||
const store = await new LibDashStore(provider);
|
||||
|
||||
try {
|
||||
const googleAuthId = await store.google_auth.add('printeragent-tester@example.com');
|
||||
const company = await store.company.create({ name: 'PA GmbH', iso639: 'de', taxrate: 19, reducedtaxrate: 7, googleAuthId });
|
||||
await store.google_auth.setSelectedCompany(googleAuthId, company.id);
|
||||
|
||||
assert.deepEqual(await store.printeragent.list(googleAuthId), []);
|
||||
await assert.rejects(store.printeragent.create(googleAuthId, { name: ' ', pairingpin: '1' }), /Invalid printeragent/);
|
||||
|
||||
const a = await store.printeragent.create(googleAuthId, { name: ' Bar ', pairingpin: '123456' });
|
||||
assert.equal(a.name, 'Bar');
|
||||
assert.equal(a.pairingpin, '123456');
|
||||
assert.equal(a.token, null);
|
||||
assert.equal(a.companyId, company.id);
|
||||
assert.ok(a.updatedAt);
|
||||
|
||||
assert.equal(a.sort, 0);
|
||||
const s1 = await store.printeragent.update(googleAuthId, { id: a.id, sort: 5 });
|
||||
assert.equal(s1.sort, 5);
|
||||
assert.equal(s1.name, 'Bar');
|
||||
await assert.rejects(store.printeragent.update(googleAuthId, { id: a.id }), /Invalid printeragent/);
|
||||
await assert.rejects(store.printeragent.update(googleAuthId, { id: a.id, sort: 1.5 }), /Invalid printeragent/);
|
||||
const b = await store.printeragent.update(googleAuthId, { id: a.id, name: 'Kitchen' });
|
||||
assert.equal(b.name, 'Kitchen');
|
||||
await assert.rejects(store.printeragent.update(googleAuthId, { id: 9999, name: 'x' }), /Printeragent not found/);
|
||||
await assert.rejects(store.printeragent.update(googleAuthId, { id: a.id, name: '' }), /Invalid printeragent/);
|
||||
|
||||
assert.equal((await store.printeragent.list(googleAuthId)).length, 1);
|
||||
await store.printeragent.delete(googleAuthId, a.id);
|
||||
await assert.rejects(store.printeragent.delete(googleAuthId, a.id), /Printeragent not found/);
|
||||
assert.deepEqual(await store.printeragent.list(googleAuthId), []);
|
||||
console.log('printeragent OK');
|
||||
} finally {
|
||||
await provider.close?.();
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
11
src/index.js
11
src/index.js
@@ -200,6 +200,10 @@ import printerList from './store/printer/list.js';
|
||||
import printerUpdate from './store/printer/update.js';
|
||||
import printerDelete from './store/printer/delete.js';
|
||||
import printerOrder from './store/printer/order.js';
|
||||
import printeragentList from './store/printeragent/list.js';
|
||||
import printeragentCreate from './store/printeragent/create.js';
|
||||
import printeragentUpdate from './store/printeragent/update.js';
|
||||
import printeragentDelete from './store/printeragent/delete.js';
|
||||
import modelconversationCreate from './store/modelconversation/create.js';
|
||||
import modelconversationGet from './store/modelconversation/get.js';
|
||||
import modelconversationList from './store/modelconversation/list.js';
|
||||
@@ -637,6 +641,13 @@ export default class LibDashStore {
|
||||
order: printerOrder(provider),
|
||||
};
|
||||
|
||||
this.printeragent = {
|
||||
list: printeragentList(provider),
|
||||
create: printeragentCreate(provider),
|
||||
update: printeragentUpdate(provider),
|
||||
delete: printeragentDelete(provider),
|
||||
};
|
||||
|
||||
this.modelconversation = {
|
||||
create: modelconversationCreate(provider),
|
||||
get: modelconversationGet(provider),
|
||||
|
||||
11
src/providers/mssql/downgrade/3_to_2.js
Normal file
11
src/providers/mssql/downgrade/3_to_2.js
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Schema 3 → 2 (MSSQL): drops the `printeragent` table.
|
||||
* @param {{ query: (sql: string, params?: object) => Promise<unknown>, assertVersion: (v: number) => Promise<void>, setVersion: (v: number) => Promise<void> }} ctx
|
||||
*/
|
||||
export default async function downgrade({ query, assertVersion, setVersion }) {
|
||||
await assertVersion(3);
|
||||
|
||||
await query(`DROP TABLE IF EXISTS printeragent;`);
|
||||
|
||||
await setVersion(2);
|
||||
}
|
||||
@@ -360,6 +360,11 @@ export class MssqlProvider {
|
||||
BEGIN
|
||||
ALTER TABLE customergroup ADD updated_at DATETIME2 NOT NULL CONSTRAINT DF_customergroup_updated_at DEFAULT SYSUTCDATETIME();
|
||||
END
|
||||
|
||||
IF OBJECT_ID(N'dbo.printeragent', N'U') IS NOT NULL AND COL_LENGTH('printeragent', 'sort') IS NULL
|
||||
BEGIN
|
||||
ALTER TABLE printeragent ADD sort INT NOT NULL CONSTRAINT DF_printeragent_sort DEFAULT 0;
|
||||
END
|
||||
`);
|
||||
} catch {
|
||||
// ignore
|
||||
|
||||
@@ -28,6 +28,7 @@ export const TABLES = [
|
||||
'posterminalobituary',
|
||||
'posterminalcategory',
|
||||
'posterminal',
|
||||
'printeragent',
|
||||
'printer',
|
||||
'numbers',
|
||||
'ledger',
|
||||
|
||||
60
src/providers/mssql/queries/printeragent.js
Normal file
60
src/providers/mssql/queries/printeragent.js
Normal file
@@ -0,0 +1,60 @@
|
||||
export const PRINTERAGENT_COLUMNS = `
|
||||
id,
|
||||
company_id AS companyId,
|
||||
name,
|
||||
token,
|
||||
pairingpin,
|
||||
paired_at AS pairedAt,
|
||||
clientserial,
|
||||
clientmodel,
|
||||
clientstate,
|
||||
agentversion,
|
||||
lastseen_at AS lastseenAt,
|
||||
updated_at AS updatedAt,
|
||||
sort
|
||||
`;
|
||||
|
||||
export const LIST_BY_COMPANY = `
|
||||
SELECT
|
||||
${PRINTERAGENT_COLUMNS}
|
||||
FROM printeragent
|
||||
WHERE company_id = @companyId
|
||||
ORDER BY sort, id
|
||||
`;
|
||||
|
||||
export const GET_BY_COMPANY_AND_ID = `
|
||||
SELECT
|
||||
${PRINTERAGENT_COLUMNS}
|
||||
FROM printeragent
|
||||
WHERE id = @id AND company_id = @companyId
|
||||
`;
|
||||
|
||||
export const CREATE = `
|
||||
INSERT INTO printeragent (company_id, name, pairingpin, sort)
|
||||
OUTPUT
|
||||
INSERTED.id,
|
||||
INSERTED.company_id AS companyId,
|
||||
INSERTED.name,
|
||||
INSERTED.token,
|
||||
INSERTED.pairingpin,
|
||||
INSERTED.paired_at AS pairedAt,
|
||||
INSERTED.clientserial,
|
||||
INSERTED.clientmodel,
|
||||
INSERTED.clientstate,
|
||||
INSERTED.agentversion,
|
||||
INSERTED.lastseen_at AS lastseenAt,
|
||||
INSERTED.updated_at AS updatedAt,
|
||||
INSERTED.sort
|
||||
VALUES (@companyId, @name, @pairingpin, @sort)
|
||||
`;
|
||||
|
||||
export const UPDATE = `
|
||||
UPDATE printeragent
|
||||
SET name = @name, sort = @sort, updated_at = SYSUTCDATETIME()
|
||||
WHERE id = @id AND company_id = @companyId
|
||||
`;
|
||||
|
||||
export const DELETE_BY_COMPANY_AND_ID = `
|
||||
DELETE FROM printeragent
|
||||
WHERE id = @id AND company_id = @companyId
|
||||
`;
|
||||
@@ -1011,6 +1011,30 @@ END
|
||||
`;
|
||||
|
||||
|
||||
export const INIT_PRINTERAGENT = `
|
||||
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'printeragent')
|
||||
BEGIN
|
||||
CREATE TABLE printeragent (
|
||||
id INT IDENTITY(1,1) PRIMARY KEY,
|
||||
company_id INT NOT NULL,
|
||||
name NVARCHAR(255) NOT NULL,
|
||||
token NVARCHAR(255) NULL,
|
||||
pairingpin NVARCHAR(50) NULL,
|
||||
paired_at DATETIME2 NULL,
|
||||
clientserial NVARCHAR(255) NULL,
|
||||
clientmodel NVARCHAR(255) NULL,
|
||||
clientstate NVARCHAR(255) NULL,
|
||||
agentversion NVARCHAR(255) NULL,
|
||||
lastseen_at DATETIME2 NULL,
|
||||
updated_at DATETIME2 NOT NULL CONSTRAINT DF_printeragent_updated_at DEFAULT SYSUTCDATETIME(),
|
||||
sort INT NOT NULL CONSTRAINT DF_printeragent_sort DEFAULT 0,
|
||||
CONSTRAINT FK_printeragent_company FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE UNIQUE INDEX UQ_printeragent_token ON printeragent(token) WHERE token IS NOT NULL;
|
||||
CREATE UNIQUE INDEX UQ_printeragent_pairingpin ON printeragent(pairingpin) WHERE pairingpin IS NOT NULL;
|
||||
END
|
||||
`;
|
||||
|
||||
export const INIT_LABELTEMPLATE = `
|
||||
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'labeltemplate')
|
||||
BEGIN
|
||||
@@ -1666,6 +1690,7 @@ export const SCHEMA_SCRIPTS = [
|
||||
INIT_POSTERMINALOBITUARY,
|
||||
INIT_POSTERMINALSYNCQUEUE,
|
||||
INIT_PRINTER,
|
||||
INIT_PRINTERAGENT,
|
||||
INIT_LABELTEMPLATE,
|
||||
INIT_FILE,
|
||||
INIT_MANIFESTATTACHMENT,
|
||||
|
||||
20
src/providers/mssql/upgrade/2_to_3.js
Normal file
20
src/providers/mssql/upgrade/2_to_3.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { INIT_PRINTERAGENT } from '../schema.js';
|
||||
|
||||
/**
|
||||
* Schema 2 → 3 (MSSQL): adds the `printeragent` table.
|
||||
* @param {{ query: (sql: string, params?: object) => Promise<unknown>, assertVersion: (v: number) => Promise<void>, setVersion: (v: number) => Promise<void> }} ctx
|
||||
*/
|
||||
export default async function upgrade({ query, assertVersion, setVersion }) {
|
||||
await assertVersion(2);
|
||||
|
||||
await query(INIT_PRINTERAGENT);
|
||||
|
||||
await query(`
|
||||
IF OBJECT_ID(N'dbo.printeragent', N'U') IS NOT NULL AND COL_LENGTH('printeragent', 'sort') IS NULL
|
||||
BEGIN
|
||||
ALTER TABLE printeragent ADD sort INT NOT NULL CONSTRAINT DF_printeragent_sort DEFAULT 0;
|
||||
END
|
||||
`);
|
||||
|
||||
await setVersion(3);
|
||||
}
|
||||
11
src/providers/sqlite3/downgrade/3_to_2.js
Normal file
11
src/providers/sqlite3/downgrade/3_to_2.js
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Schema 3 → 2 (SQLite): drops the `printeragent` table.
|
||||
* @param {{ exec: (sql: string) => void, assertVersion: (v: number) => Promise<void>, setVersion: (v: number) => Promise<void> }} ctx
|
||||
*/
|
||||
export default async function downgrade({ exec, assertVersion, setVersion }) {
|
||||
await assertVersion(3);
|
||||
|
||||
exec(`DROP TABLE IF EXISTS printeragent;`);
|
||||
|
||||
await setVersion(2);
|
||||
}
|
||||
@@ -438,6 +438,11 @@ export class BetterSqlite3Provider extends BetterSqlite3Executor {
|
||||
this.db.exec(`ALTER TABLE customergroup ADD COLUMN updated_at TEXT`);
|
||||
this.db.exec(`UPDATE customergroup SET updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') WHERE updated_at IS NULL`);
|
||||
}
|
||||
|
||||
const printeragentCols = this.db.pragma('table_info("printeragent")').map((c) => c.name);
|
||||
if (printeragentCols.length > 0 && !printeragentCols.includes('sort')) {
|
||||
this.db.exec(`ALTER TABLE printeragent ADD COLUMN sort INTEGER NOT NULL DEFAULT 0`);
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
|
||||
12
src/providers/sqlite3/queries/printeragent.js
Normal file
12
src/providers/sqlite3/queries/printeragent.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import * as source from '../../mssql/queries/printeragent.js';
|
||||
import { translateQueries } from './translate.js';
|
||||
|
||||
const queries = translateQueries(source, 'printeragent');
|
||||
export const {
|
||||
CREATE,
|
||||
DELETE_BY_COMPANY_AND_ID,
|
||||
GET_BY_COMPANY_AND_ID,
|
||||
LIST_BY_COMPANY,
|
||||
PRINTERAGENT_COLUMNS,
|
||||
UPDATE,
|
||||
} = queries;
|
||||
@@ -692,6 +692,29 @@ CREATE TABLE IF NOT EXISTS printer (
|
||||
`;
|
||||
|
||||
|
||||
export const INIT_PRINTERAGENT = `
|
||||
CREATE TABLE IF NOT EXISTS printeragent (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
company_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
token TEXT,
|
||||
pairingpin TEXT,
|
||||
paired_at TEXT,
|
||||
clientserial TEXT,
|
||||
clientmodel TEXT,
|
||||
clientstate TEXT,
|
||||
agentversion TEXT,
|
||||
lastseen_at TEXT,
|
||||
updated_at TEXT NOT NULL DEFAULT (${UTC_NOW}),
|
||||
sort INTEGER NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS UQ_printeragent_token
|
||||
ON printeragent(token) WHERE token IS NOT NULL;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS UQ_printeragent_pairingpin
|
||||
ON printeragent(pairingpin) WHERE pairingpin IS NOT NULL;
|
||||
`;
|
||||
|
||||
export const INIT_LABELTEMPLATE = `
|
||||
CREATE TABLE IF NOT EXISTS labeltemplate (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -1225,6 +1248,7 @@ export const SCHEMA_SCRIPTS = [
|
||||
INIT_POSTERMINALOBITUARY,
|
||||
INIT_POSTERMINALSYNCQUEUE,
|
||||
INIT_PRINTER,
|
||||
INIT_PRINTERAGENT,
|
||||
INIT_LABELTEMPLATE,
|
||||
INIT_FILE,
|
||||
INIT_MANIFESTATTACHMENT,
|
||||
|
||||
13
src/providers/sqlite3/upgrade/2_to_3.js
Normal file
13
src/providers/sqlite3/upgrade/2_to_3.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import { INIT_PRINTERAGENT } from '../schema.js';
|
||||
|
||||
/**
|
||||
* Schema 2 → 3 (SQLite): adds the `printeragent` table.
|
||||
* @param {{ exec: (sql: string) => void, assertVersion: (v: number) => Promise<void>, setVersion: (v: number) => Promise<void> }} ctx
|
||||
*/
|
||||
export default async function upgrade({ exec, assertVersion, setVersion }) {
|
||||
await assertVersion(2);
|
||||
|
||||
exec(INIT_PRINTERAGENT);
|
||||
|
||||
await setVersion(3);
|
||||
}
|
||||
20
src/store/printeragent/_shared.js
Normal file
20
src/store/printeragent/_shared.js
Normal file
@@ -0,0 +1,20 @@
|
||||
export function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
companyId: row.companyId,
|
||||
name: row.name,
|
||||
token: row.token ?? null,
|
||||
pairingpin: row.pairingpin ?? null,
|
||||
pairedAt: row.pairedAt ?? null,
|
||||
clientserial: row.clientserial ?? null,
|
||||
clientmodel: row.clientmodel ?? null,
|
||||
clientstate: row.clientstate ?? null,
|
||||
agentversion: row.agentversion ?? null,
|
||||
lastseenAt: row.lastseenAt ?? null,
|
||||
updatedAt: row.updatedAt,
|
||||
sort: row.sort,
|
||||
};
|
||||
}
|
||||
|
||||
export const PRINTERAGENT_TYPEDEF =
|
||||
'{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}';
|
||||
49
src/store/printeragent/create.js
Normal file
49
src/store/printeragent/create.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import {
|
||||
CREATE as MSSQL_CREATE,
|
||||
} from '../../providers/mssql/queries/printeragent.js';
|
||||
import {
|
||||
CREATE as SQLITE_CREATE,
|
||||
} 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 create(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_CREATE : MSSQL_CREATE;
|
||||
|
||||
/**
|
||||
* Creates an unpaired printer agent for the user's selected company.
|
||||
* The agent registers itself later using the pairing pin.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {object} input - The printer agent input.
|
||||
* @param {string} input.name - The display name.
|
||||
* @param {string} input.pairingpin - The pairing pin the agent uses to register.
|
||||
* @param {number} [input.sort] - Display order (integer, ascending). Default 0.
|
||||
* @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 created printer agent.
|
||||
* @throws {Error} 'Invalid printeragent' if name or pairingpin is missing/blank or sort is not an integer.
|
||||
*
|
||||
* @example
|
||||
* const agent = await store.printeragent.create(42, { name: 'Bar printer', pairingpin: '123456' });
|
||||
*/
|
||||
return async (googleAuthId, input = {}, tx) => {
|
||||
const name = typeof input.name === 'string' ? input.name.trim() : '';
|
||||
const pairingpin = input.pairingpin != null ? String(input.pairingpin).trim() : '';
|
||||
if (!name || !pairingpin) {
|
||||
throw new Error('Invalid printeragent');
|
||||
}
|
||||
|
||||
if (input.sort !== undefined && !Number.isInteger(input.sort)) {
|
||||
throw new Error('Invalid printeragent');
|
||||
}
|
||||
const sort = input.sort ?? 0;
|
||||
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const result = await runner.query(sql, { companyId, name, pairingpin, sort });
|
||||
return shape(result.rows[0]);
|
||||
};
|
||||
}
|
||||
36
src/store/printeragent/delete.js
Normal file
36
src/store/printeragent/delete.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import { DELETE_BY_COMPANY_AND_ID as MSSQL_DELETE } from '../../providers/mssql/queries/printeragent.js';
|
||||
import { DELETE_BY_COMPANY_AND_ID as SQLITE_DELETE } from '../../providers/sqlite3/queries/printeragent.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function del(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_DELETE : MSSQL_DELETE;
|
||||
|
||||
/**
|
||||
* Deletes a printer agent of the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - The printer agent id.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<void>}
|
||||
* @throws {Error} 'Printeragent not found' if the id is unknown in the company.
|
||||
*
|
||||
* @example
|
||||
* await store.printeragent.delete(42, 5);
|
||||
*/
|
||||
return async (googleAuthId, id, tx) => {
|
||||
const numericId = Number(id);
|
||||
if (!Number.isInteger(numericId) || numericId <= 0) {
|
||||
throw new Error('Printeragent not found');
|
||||
}
|
||||
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const result = await runner.query(sql, { id: numericId, companyId });
|
||||
if (!result.rowsAffected) {
|
||||
throw new Error('Printeragent not found');
|
||||
}
|
||||
};
|
||||
}
|
||||
29
src/store/printeragent/list.js
Normal file
29
src/store/printeragent/list.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { LIST_BY_COMPANY as MSSQL_LIST } from '../../providers/mssql/queries/printeragent.js';
|
||||
import { LIST_BY_COMPANY as SQLITE_LIST } 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 list(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_LIST : MSSQL_LIST;
|
||||
|
||||
/**
|
||||
* Lists the printer agents of the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<Array<{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 printer agents.
|
||||
*
|
||||
* @example
|
||||
* const agents = await store.printeragent.list(42);
|
||||
* // => [{ id, companyId, name, token, pairingpin, pairedAt, clientserial, clientmodel, clientstate, agentversion, lastseenAt, updatedAt, sort }]
|
||||
*/
|
||||
return async (googleAuthId, tx) => {
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const result = await runner.query(sql, { companyId });
|
||||
return result.rows.map(shape);
|
||||
};
|
||||
}
|
||||
67
src/store/printeragent/update.js
Normal file
67
src/store/printeragent/update.js
Normal file
@@ -0,0 +1,67 @@
|
||||
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) {
|
||||
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');
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
const row = await runner.query(getSql, { id, companyId });
|
||||
return shape(row.rows[0]);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user