u
This commit is contained in:
54
src/store/printeragent/update.js
Normal file
54
src/store/printeragent/update.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
GET_BY_COMPANY_AND_ID as MSSQL_GET,
|
||||
UPDATE_NAME as MSSQL_UPDATE_NAME,
|
||||
} from '../../providers/mssql/queries/printeragent.js';
|
||||
import {
|
||||
GET_BY_COMPANY_AND_ID as SQLITE_GET,
|
||||
UPDATE_NAME as SQLITE_UPDATE_NAME,
|
||||
} 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 updateNameSql = isSqlite ? SQLITE_UPDATE_NAME : MSSQL_UPDATE_NAME;
|
||||
|
||||
/**
|
||||
* Renames 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 {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}>} The updated printer agent.
|
||||
* @throws {Error} 'Invalid printeragent' if name is blank; 'Printeragent not found' if id is unknown in the company.
|
||||
*
|
||||
* @example
|
||||
* const agent = await store.printeragent.update(42, { id: 5, name: 'Kitchen printer' });
|
||||
*/
|
||||
return async (googleAuthId, input = {}, tx) => {
|
||||
const id = Number(input.id);
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
throw new Error('Printeragent not found');
|
||||
}
|
||||
const name = typeof input.name === 'string' ? input.name.trim() : '';
|
||||
if (!name) {
|
||||
throw new Error('Invalid printeragent');
|
||||
}
|
||||
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const result = await runner.query(updateNameSql, { id, companyId, name });
|
||||
if (!result.rowsAffected) {
|
||||
throw new Error('Printeragent not found');
|
||||
}
|
||||
|
||||
const row = await runner.query(getSql, { id, companyId });
|
||||
return shape(row.rows[0]);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user