Files
dashlibstorage/src/store/posterminal/setOn.js
seb 70e0a9ddc4 Add entity customer numbers and scope APIs via selected company.
Entity gets an optional unique-per-company number with allocate/peek/isNumberFree helpers. Company-scoped store methods no longer take companyId and resolve google_auth.selected_company instead.
2026-08-07 04:29:35 +02:00

51 lines
1.5 KiB
JavaScript

import { GET_BY_ID, SET_ON, LIST_CATEGORY_IDS } from '../../sql/posterminal.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
/**
* Assigns a category to a posterminal (turns it on for that category).
*
* @param {object} pool - The mssql connection pool.
* @returns {Function} An async function that assigns a category to a posterminal.
*
* @example
* const fn = setOn(pool);
* const result = await fn(42, 5, 9);
* // => { id, categoryIds: [9, ...] }
*/
export default function setOn(pool) {
const access = resolveSelectedCompanyAccess(pool);
return async (googleAuthId, posterminalId, categoryId) => {
if (!Number.isFinite(posterminalId) || !Number.isFinite(categoryId)) {
throw new Error('Invalid posterminal or category');
}
const { companyId } = await access(googleAuthId);
const existing = await pool
.request()
.input('id', posterminalId)
.query(GET_BY_ID);
if (!existing.recordset[0] || existing.recordset[0].companyId !== companyId) {
throw new Error('Posterminal not found');
}
await pool
.request()
.input('posterminalId', posterminalId)
.input('categoryId', categoryId)
.query(SET_ON);
const result = await pool
.request()
.input('posterminalId', posterminalId)
.query(LIST_CATEGORY_IDS);
return {
id: posterminalId,
categoryIds: result.recordset.map((r) => r.categoryId),
};
};
}