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), }; }; }