u
This commit is contained in:
111
src/store/printer/create.js
Normal file
111
src/store/printer/create.js
Normal file
@@ -0,0 +1,111 @@
|
||||
import {
|
||||
CREATE as MSSQL_CREATE,
|
||||
GET_NEXT_SORT as MSSQL_GET_NEXT_SORT,
|
||||
} from '../../providers/mssql/queries/printer.js';
|
||||
import {
|
||||
CREATE as SQLITE_CREATE,
|
||||
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
|
||||
} from '../../providers/sqlite3/queries/printer.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
const VALID_TYPES = new Set(['esc/pos', 'zpl']);
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
company_id: row.company_id ?? row.companyId,
|
||||
address: row.address,
|
||||
type: row.type,
|
||||
model: row.model ?? null,
|
||||
serial: row.serial ?? null,
|
||||
sort: row.sort,
|
||||
desc: row.desc ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
function optionalString(value) {
|
||||
if (value == null || value === '') return null;
|
||||
const s = String(value).trim();
|
||||
return s || null;
|
||||
}
|
||||
|
||||
export default function create(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const getSortSql = isSqlite ? SQLITE_GET_NEXT_SORT : MSSQL_GET_NEXT_SORT;
|
||||
const createSql = isSqlite ? SQLITE_CREATE : MSSQL_CREATE;
|
||||
|
||||
/**
|
||||
* Creates a new printer for the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {object} input - Printer input.
|
||||
* @param {string} input.address - Printer address (network IP/host/path).
|
||||
* @param {string} input.type - Printer type ("esc/pos" or "zpl").
|
||||
* @param {string} [input.model] - Optional printer model name.
|
||||
* @param {string} [input.serial] - Optional printer serial number.
|
||||
* @param {number} [input.sort] - Optional sort order (auto-increments if omitted).
|
||||
* @param {string} [input.desc] - Optional description.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, company_id: number, address: string, type: string, model: string|null, serial: string|null, sort: number, desc: string|null}>}
|
||||
* @throws {Error} If address is missing, type is invalid, or sort is invalid.
|
||||
*
|
||||
* @example
|
||||
* const printer = await store.printer.create(googleAuthId, {
|
||||
* address: '192.168.1.200',
|
||||
* type: 'esc/pos',
|
||||
* model: 'TM-T88VI',
|
||||
* desc: 'Bar receipt printer',
|
||||
* });
|
||||
*/
|
||||
return async (googleAuthId, input = {}, tx) => {
|
||||
const address = typeof input.address === 'string' ? input.address.trim() : '';
|
||||
if (!address) {
|
||||
throw new Error('Address required');
|
||||
}
|
||||
|
||||
const type = typeof input.type === 'string' ? input.type.trim() : '';
|
||||
if (!type || !VALID_TYPES.has(type)) {
|
||||
throw new Error('Invalid printer type: must be "esc/pos" or "zpl"');
|
||||
}
|
||||
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
let sort = input.sort;
|
||||
if (sort === undefined || sort === null) {
|
||||
const next = await runner.query(getSortSql, { companyId });
|
||||
sort = next.rows[0].sort;
|
||||
} else if (!Number.isFinite(sort)) {
|
||||
throw new Error('Invalid sort');
|
||||
}
|
||||
|
||||
const model = optionalString(input.model);
|
||||
const serial = optionalString(input.serial);
|
||||
const desc = optionalString(input.desc);
|
||||
|
||||
const result = await runner.query(createSql, {
|
||||
companyId,
|
||||
address,
|
||||
type,
|
||||
model,
|
||||
serial,
|
||||
sort,
|
||||
desc,
|
||||
});
|
||||
|
||||
const row = result.rows[0] ?? {
|
||||
id: result.insertId,
|
||||
company_id: companyId,
|
||||
address,
|
||||
type,
|
||||
model,
|
||||
serial,
|
||||
sort,
|
||||
desc,
|
||||
};
|
||||
|
||||
return shape(row);
|
||||
};
|
||||
}
|
||||
38
src/store/printer/delete.js
Normal file
38
src/store/printer/delete.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import { DELETE_BY_COMPANY_AND_ID as MSSQL_DELETE } from '../../providers/mssql/queries/printer.js';
|
||||
import { DELETE_BY_COMPANY_AND_ID as SQLITE_DELETE } from '../../providers/sqlite3/queries/printer.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 for the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - The printer ID to delete.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<void>}
|
||||
* @throws {Error} If printer is not found.
|
||||
*/
|
||||
return async (googleAuthId, id, tx) => {
|
||||
const numericId = Number(id);
|
||||
if (!Number.isInteger(numericId) || numericId <= 0) {
|
||||
throw new Error('Printer 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('Printer not found');
|
||||
}
|
||||
};
|
||||
}
|
||||
53
src/store/printer/getById.js
Normal file
53
src/store/printer/getById.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import { GET_BY_COMPANY_AND_ID as MSSQL_GET_BY_ID } from '../../providers/mssql/queries/printer.js';
|
||||
import { GET_BY_COMPANY_AND_ID as SQLITE_GET_BY_ID } from '../../providers/sqlite3/queries/printer.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
company_id: row.company_id ?? row.companyId,
|
||||
address: row.address,
|
||||
type: row.type,
|
||||
model: row.model ?? null,
|
||||
serial: row.serial ?? null,
|
||||
sort: row.sort,
|
||||
desc: row.desc ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
export default function getById(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const sql =
|
||||
provider?.dialect === 'sqlite3' ? SQLITE_GET_BY_ID : MSSQL_GET_BY_ID;
|
||||
|
||||
/**
|
||||
* Retrieves a printer by its ID for the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - The printer ID.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, company_id: number, address: string, type: string, model: string|null, serial: string|null, sort: number, desc: string|null}|undefined>}
|
||||
*
|
||||
* @example
|
||||
* const printer = await store.printer.getById(googleAuthId, 1);
|
||||
* // => { id: 1, company_id: 1, address: '192.168.1.100', type: 'esc/pos', ... }
|
||||
*/
|
||||
return async (googleAuthId, id, tx) => {
|
||||
const numericId = Number(id);
|
||||
if (!Number.isInteger(numericId) || numericId <= 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const result = await runner.query(sql, {
|
||||
id: numericId,
|
||||
companyId,
|
||||
});
|
||||
|
||||
const row = result.rows[0];
|
||||
return row ? shape(row) : undefined;
|
||||
};
|
||||
}
|
||||
44
src/store/printer/list.js
Normal file
44
src/store/printer/list.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import { LIST_BY_COMPANY as MSSQL_LIST_BY_COMPANY } from '../../providers/mssql/queries/printer.js';
|
||||
import { LIST_BY_COMPANY as SQLITE_LIST_BY_COMPANY } from '../../providers/sqlite3/queries/printer.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
company_id: row.company_id ?? row.companyId,
|
||||
address: row.address,
|
||||
type: row.type,
|
||||
model: row.model ?? null,
|
||||
serial: row.serial ?? null,
|
||||
sort: row.sort,
|
||||
desc: row.desc ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
export default function list(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const sql =
|
||||
provider?.dialect === 'sqlite3'
|
||||
? SQLITE_LIST_BY_COMPANY
|
||||
: MSSQL_LIST_BY_COMPANY;
|
||||
|
||||
/**
|
||||
* Lists all printers for 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, company_id: number, address: string, type: string, model: string|null, serial: string|null, sort: number, desc: string|null}>>}
|
||||
*
|
||||
* @example
|
||||
* const printers = await store.printer.list(42);
|
||||
* // => [{ id: 1, company_id: 1, address: '192.168.1.100', type: 'esc/pos', ... }]
|
||||
*/
|
||||
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);
|
||||
};
|
||||
}
|
||||
76
src/store/printer/order.js
Normal file
76
src/store/printer/order.js
Normal file
@@ -0,0 +1,76 @@
|
||||
import {
|
||||
LIST_IDS_BY_COMPANY as MSSQL_LIST_IDS_BY_COMPANY,
|
||||
UPDATE_SORT as MSSQL_UPDATE_SORT,
|
||||
} from '../../providers/mssql/queries/printer.js';
|
||||
import {
|
||||
LIST_IDS_BY_COMPANY as SQLITE_LIST_IDS_BY_COMPANY,
|
||||
UPDATE_SORT as SQLITE_UPDATE_SORT,
|
||||
} from '../../providers/sqlite3/queries/printer.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function order(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const listIdsSql = isSqlite
|
||||
? SQLITE_LIST_IDS_BY_COMPANY
|
||||
: MSSQL_LIST_IDS_BY_COMPANY;
|
||||
const updateSortSql = isSqlite ? SQLITE_UPDATE_SORT : MSSQL_UPDATE_SORT;
|
||||
|
||||
/**
|
||||
* Reorders the printers of the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {object} input - The reorder input.
|
||||
* @param {Array<number>} input.printerIds - The printer ids in their new order.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{printerIds: Array<number>}>} The applied order.
|
||||
* @throws {Error} If the order is invalid.
|
||||
*/
|
||||
return async (googleAuthId, { printerIds } = {}, tx) => {
|
||||
if (!Array.isArray(printerIds)) {
|
||||
throw new Error('Invalid printer order');
|
||||
}
|
||||
|
||||
const runOrder = async (runner) => {
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const items = await runner.query(listIdsSql, { companyId });
|
||||
const existing = new Set(items.rows.map((r) => r.id));
|
||||
|
||||
if (printerIds.length !== existing.size) {
|
||||
throw new Error('Invalid printer order');
|
||||
}
|
||||
|
||||
const seen = new Set();
|
||||
for (const printerId of printerIds) {
|
||||
if (
|
||||
!Number.isFinite(printerId) ||
|
||||
!existing.has(printerId) ||
|
||||
seen.has(printerId)
|
||||
) {
|
||||
throw new Error('Invalid printer order');
|
||||
}
|
||||
seen.add(printerId);
|
||||
}
|
||||
|
||||
for (let sort = 0; sort < printerIds.length; sort++) {
|
||||
await runner.query(updateSortSql, {
|
||||
id: printerIds[sort],
|
||||
companyId,
|
||||
sort,
|
||||
});
|
||||
}
|
||||
|
||||
return { printerIds };
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return runOrder(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(runOrder);
|
||||
}
|
||||
return runOrder(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
124
src/store/printer/update.js
Normal file
124
src/store/printer/update.js
Normal file
@@ -0,0 +1,124 @@
|
||||
import {
|
||||
GET_BY_COMPANY_AND_ID as MSSQL_GET_BY_ID,
|
||||
UPDATE as MSSQL_UPDATE,
|
||||
} from '../../providers/mssql/queries/printer.js';
|
||||
import {
|
||||
GET_BY_COMPANY_AND_ID as SQLITE_GET_BY_ID,
|
||||
UPDATE as SQLITE_UPDATE,
|
||||
} from '../../providers/sqlite3/queries/printer.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
const VALID_TYPES = new Set(['esc/pos', 'zpl']);
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
company_id: row.company_id ?? row.companyId,
|
||||
address: row.address,
|
||||
type: row.type,
|
||||
model: row.model ?? null,
|
||||
serial: row.serial ?? null,
|
||||
sort: row.sort,
|
||||
desc: row.desc ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
function optionalString(value) {
|
||||
if (value == null || value === '') return null;
|
||||
const s = String(value).trim();
|
||||
return s || null;
|
||||
}
|
||||
|
||||
export default function update(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const getSql = isSqlite ? SQLITE_GET_BY_ID : MSSQL_GET_BY_ID;
|
||||
const updateSql = isSqlite ? SQLITE_UPDATE : MSSQL_UPDATE;
|
||||
|
||||
/**
|
||||
* Updates a printer for the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - The printer ID to update.
|
||||
* @param {object} input - Update payload.
|
||||
* @param {string} [input.address] - The printer address.
|
||||
* @param {string} [input.type] - The printer type ("esc/pos" or "zpl").
|
||||
* @param {string|null} [input.model] - The printer model.
|
||||
* @param {string|null} [input.serial] - The printer serial.
|
||||
* @param {number} [input.sort] - The sort order.
|
||||
* @param {string|null} [input.desc] - The description.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, company_id: number, address: string, type: string, model: string|null, serial: string|null, sort: number, desc: string|null}>}
|
||||
* @throws {Error} If printer not found, address is empty, type is invalid, or sort is invalid.
|
||||
*/
|
||||
return async (googleAuthId, id, input = {}, tx) => {
|
||||
const numericId = Number(id);
|
||||
if (!Number.isInteger(numericId) || numericId <= 0) {
|
||||
throw new Error('Printer not found');
|
||||
}
|
||||
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const existing = await runner.query(getSql, { id: numericId, companyId });
|
||||
const current = existing.rows[0];
|
||||
if (!current) {
|
||||
throw new Error('Printer not found');
|
||||
}
|
||||
|
||||
let address = current.address;
|
||||
if (input.address !== undefined) {
|
||||
const cleaned = typeof input.address === 'string' ? input.address.trim() : '';
|
||||
if (!cleaned) {
|
||||
throw new Error('Address required');
|
||||
}
|
||||
address = cleaned;
|
||||
}
|
||||
|
||||
let type = current.type;
|
||||
if (input.type !== undefined) {
|
||||
const cleaned = typeof input.type === 'string' ? input.type.trim() : '';
|
||||
if (!cleaned || !VALID_TYPES.has(cleaned)) {
|
||||
throw new Error('Invalid printer type: must be "esc/pos" or "zpl"');
|
||||
}
|
||||
type = cleaned;
|
||||
}
|
||||
|
||||
let sort = current.sort;
|
||||
if (input.sort !== undefined) {
|
||||
if (!Number.isFinite(input.sort)) {
|
||||
throw new Error('Invalid sort');
|
||||
}
|
||||
sort = input.sort;
|
||||
}
|
||||
|
||||
const model = input.model !== undefined ? optionalString(input.model) : (current.model ?? null);
|
||||
const serial = input.serial !== undefined ? optionalString(input.serial) : (current.serial ?? null);
|
||||
const desc = input.desc !== undefined ? optionalString(input.desc) : (current.desc ?? null);
|
||||
|
||||
const result = await runner.query(updateSql, {
|
||||
id: numericId,
|
||||
companyId,
|
||||
address,
|
||||
type,
|
||||
model,
|
||||
serial,
|
||||
sort,
|
||||
desc,
|
||||
});
|
||||
|
||||
const row = result.rows[0] ?? {
|
||||
id: numericId,
|
||||
company_id: companyId,
|
||||
address,
|
||||
type,
|
||||
model,
|
||||
serial,
|
||||
sort,
|
||||
desc,
|
||||
};
|
||||
|
||||
return shape(row);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user