This commit is contained in:
seb
2026-07-31 07:42:19 +02:00
parent 39c5820947
commit 4f98082061
6 changed files with 70 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
import { GET_TREE } from '../../sql/category.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function getTreeForCompany(pool) {
const access = resolveCompanyAccess(pool);
return async (authId, companyId) => {
const { rootCategoryId } = await access(authId, companyId);
const result = await pool
.request()
.input('rootId', rootCategoryId)
.query(GET_TREE);
return result.recordset;
};
}

View File

@@ -0,0 +1,24 @@
import { GET_COMPANY_ACCESS } from '../../sql/companyuser.js';
export default function resolveCompanyAccess(pool) {
return async (authId, companyId) => {
const result = await pool
.request()
.input('authId', authId)
.input('companyId', companyId)
.query(GET_COMPANY_ACCESS);
const row = result.recordset[0];
if (!row) {
throw new Error('Company not found');
}
if (!row.companyuserId) {
throw new Error('Not authorized');
}
if (!row.rootCategoryId) {
throw new Error('Company not found');
}
return { rootCategoryId: row.rootCategoryId };
};
}