This commit is contained in:
seb
2026-09-22 07:25:16 +02:00
parent 4ed6260e91
commit cca131d83a
148 changed files with 5343 additions and 3466 deletions

View File

@@ -1,14 +1,32 @@
import { GET_BY_ID, SET_OFF_ALL, LIST_CATEGORY_IDS } from '../../providers/mssql/queries/posterminal.js';
import {
GET_BY_ID as MSSQL_GET_BY_ID,
SET_OFF_ALL as MSSQL_SET_OFF_ALL,
LIST_CATEGORY_IDS as MSSQL_LIST_CATEGORY_IDS,
} from '../../providers/mssql/queries/posterminal.js';
import {
GET_BY_ID as SQLITE_GET_BY_ID,
SET_OFF_ALL as SQLITE_SET_OFF_ALL,
LIST_CATEGORY_IDS as SQLITE_LIST_CATEGORY_IDS,
} from '../../providers/sqlite3/queries/posterminal.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function setOffAll(pool) {
const access = resolveSelectedCompanyAccess(pool);
export default function setOffAll(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const getByIdSql = isSqlite ? SQLITE_GET_BY_ID : MSSQL_GET_BY_ID;
const setOffAllSql = isSqlite ? SQLITE_SET_OFF_ALL : MSSQL_SET_OFF_ALL;
const listCatIdsSql = isSqlite
? SQLITE_LIST_CATEGORY_IDS
: MSSQL_LIST_CATEGORY_IDS;
/**
* Removes all category assignments from a posterminal.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} posterminalId - The id of the posterminal.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, categoryIds: Array<number>}>} The posterminal with an empty category list.
* @throws {Error} If the posterminal is not found.
*
@@ -16,36 +34,28 @@ export default function setOffAll(pool) {
* const result = await store.posterminal.setOffAll(42, 5);
* // => { id, categoryIds: [] }
*/
return async (googleAuthId, posterminalId) => {
return async (googleAuthId, posterminalId, tx) => {
if (!Number.isFinite(posterminalId)) {
throw new Error('Invalid posterminal');
}
const { companyId } = await access(googleAuthId);
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, tx);
// Verify posterminal belongs to company
const pt = await pool
.request()
.input('id', posterminalId)
.query(GET_BY_ID);
const pt = await runner.query(getByIdSql, { id: posterminalId });
if (!pt.recordset[0] || pt.recordset[0].companyId !== companyId) {
if (!pt.rows[0] || pt.rows[0].companyId !== companyId) {
throw new Error('Posterminal not found');
}
await pool
.request()
.input('posterminalId', posterminalId)
.query(SET_OFF_ALL);
await runner.query(setOffAllSql, { posterminalId });
const result = await pool
.request()
.input('posterminalId', posterminalId)
.query(LIST_CATEGORY_IDS);
const result = await runner.query(listCatIdsSql, { posterminalId });
return {
id: posterminalId,
categoryIds: result.recordset.map((r) => r.categoryId),
categoryIds: result.rows.map((r) => r.categoryId),
};
};
}