u
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
import { CREATE } from '../../sql/category.js';
|
||||
|
||||
export default function create(pool) {
|
||||
return async ({ parentId, sort }) => {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('parentId', parentId)
|
||||
.input('sort', sort)
|
||||
.query(CREATE);
|
||||
|
||||
return result.recordset[0].id;
|
||||
};
|
||||
}
|
||||
67
src/store/category/createUnderParent.js
Normal file
67
src/store/category/createUnderParent.js
Normal file
@@ -0,0 +1,67 @@
|
||||
import {
|
||||
CREATE,
|
||||
GET_NEXT_SIBLING_SORT,
|
||||
} from '../../sql/category.js';
|
||||
import { CREATE as CREATE_CATEGORYTEXT } from '../../sql/categorytext.js';
|
||||
import resolveCategoryAccess from './resolveCategoryAccess.js';
|
||||
import request from '../request.js';
|
||||
|
||||
function slugify(name) {
|
||||
return name
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.normalize('NFD')
|
||||
.replace(/\p{M}/gu, '')
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
}
|
||||
|
||||
export default function createUnderParent(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
|
||||
return async ({ googleAuthId, parentId, name }) => {
|
||||
const { languageId } = await access(googleAuthId, parentId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const sortResult = await request(pool, tx)
|
||||
.input('parentId', parentId)
|
||||
.query(GET_NEXT_SIBLING_SORT);
|
||||
|
||||
const sort = sortResult.recordset[0].sort;
|
||||
const slug = slugify(name);
|
||||
|
||||
const category = await request(pool, tx)
|
||||
.input('parentId', parentId)
|
||||
.input('sort', sort)
|
||||
.query(CREATE);
|
||||
|
||||
const { id, parentId: resolvedParentId } = category.recordset[0];
|
||||
|
||||
await request(pool, tx)
|
||||
.input('categoryId', id)
|
||||
.input('languageId', languageId)
|
||||
.input('name', name)
|
||||
.input('slug', slug)
|
||||
.query(CREATE_CATEGORYTEXT);
|
||||
|
||||
await tx.commit();
|
||||
|
||||
return {
|
||||
id,
|
||||
parentId: resolvedParentId,
|
||||
sort,
|
||||
name,
|
||||
};
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import { GET_TREE } from '../../sql/category.js';
|
||||
|
||||
export default function getTree(pool) {
|
||||
return async (rootId) => {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('rootId', rootId)
|
||||
.query(GET_TREE);
|
||||
|
||||
return result.recordset;
|
||||
};
|
||||
}
|
||||
@@ -1,17 +1,36 @@
|
||||
import { GET_TREE } from '../../sql/category.js';
|
||||
import {
|
||||
GET_ROOT_CATEGORY_ARTICLES,
|
||||
GET_TREE_FOR_COMPANY,
|
||||
} 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);
|
||||
return async (googleAuthId, companyId) => {
|
||||
const { rootCategoryId, languageId, posCustomerGroupId } = await access(
|
||||
googleAuthId,
|
||||
companyId,
|
||||
);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('rootId', rootCategoryId)
|
||||
.query(GET_TREE);
|
||||
const [treeResult, articlesResult] = await Promise.all([
|
||||
pool
|
||||
.request()
|
||||
.input('rootId', rootCategoryId)
|
||||
.input('languageId', languageId)
|
||||
.query(GET_TREE_FOR_COMPANY),
|
||||
pool
|
||||
.request()
|
||||
.input('rootCategoryId', rootCategoryId)
|
||||
.input('languageId', languageId)
|
||||
.input('posCustomerGroupId', posCustomerGroupId)
|
||||
.query(GET_ROOT_CATEGORY_ARTICLES),
|
||||
]);
|
||||
|
||||
return result.recordset;
|
||||
return {
|
||||
languageId,
|
||||
tree: treeResult.recordset,
|
||||
articles: articlesResult.recordset,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
28
src/store/category/resolveCategoryAccess.js
Normal file
28
src/store/category/resolveCategoryAccess.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { RESOLVE_CATEGORY_ACCESS } from '../../sql/category.js';
|
||||
|
||||
export default function resolveCategoryAccess(pool) {
|
||||
return async (googleAuthId, categoryId) => {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('categoryId', categoryId)
|
||||
.input('googleAuthId', googleAuthId)
|
||||
.query(RESOLVE_CATEGORY_ACCESS);
|
||||
|
||||
const row = result.recordset[0];
|
||||
if (!row) {
|
||||
throw new Error('Parent not found');
|
||||
}
|
||||
if (!row.companyId || !row.rootCategoryId || !row.languageId) {
|
||||
throw new Error('Parent not found');
|
||||
}
|
||||
if (!row.companyuserId) {
|
||||
throw new Error('Not authorized');
|
||||
}
|
||||
|
||||
return {
|
||||
companyId: row.companyId,
|
||||
rootCategoryId: row.rootCategoryId,
|
||||
languageId: row.languageId,
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -11,7 +11,7 @@ export default function create(pool) {
|
||||
const ensureLang = langEnsure(pool);
|
||||
const ensureTax = taxEnsure(pool);
|
||||
|
||||
return async ({ iso639, name, taxrate, reducedtaxrate, authId }) => {
|
||||
return async ({ iso639, name, taxrate, reducedtaxrate, googleAuthId }) => {
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
@@ -46,7 +46,7 @@ export default function create(pool) {
|
||||
|
||||
await request(pool, tx)
|
||||
.input('companyId', companyId)
|
||||
.input('authId', authId)
|
||||
.input('googleAuthId', googleAuthId)
|
||||
.query(ADD_COMPANYUSER);
|
||||
|
||||
await tx.commit();
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { LIST } from '../../sql/company.js';
|
||||
|
||||
export default function list(pool) {
|
||||
return async (authId) => {
|
||||
return async (googleAuthId) => {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('authId', authId)
|
||||
.input('googleAuthId', googleAuthId)
|
||||
.query(LIST);
|
||||
return result.recordset;
|
||||
};
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { ADD } from '../../sql/companyuser.js';
|
||||
|
||||
export default function add(pool) {
|
||||
return async (companyId, authId) => {
|
||||
return async (companyId, googleAuthId) => {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('companyId', companyId)
|
||||
.input('authId', authId)
|
||||
.input('googleAuthId', googleAuthId)
|
||||
.query(ADD);
|
||||
|
||||
return result.recordset[0].id;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { GET_COMPANY_ACCESS } from '../../sql/companyuser.js';
|
||||
|
||||
export default function resolveCompanyAccess(pool) {
|
||||
return async (authId, companyId) => {
|
||||
return async (googleAuthId, companyId) => {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('authId', authId)
|
||||
.input('googleAuthId', googleAuthId)
|
||||
.input('companyId', companyId)
|
||||
.query(GET_COMPANY_ACCESS);
|
||||
|
||||
@@ -15,10 +15,14 @@ export default function resolveCompanyAccess(pool) {
|
||||
if (!row.companyuserId) {
|
||||
throw new Error('Not authorized');
|
||||
}
|
||||
if (!row.rootCategoryId) {
|
||||
if (!row.rootCategoryId || !row.languageId) {
|
||||
throw new Error('Company not found');
|
||||
}
|
||||
|
||||
return { rootCategoryId: row.rootCategoryId };
|
||||
return {
|
||||
rootCategoryId: row.rootCategoryId,
|
||||
languageId: row.languageId,
|
||||
posCustomerGroupId: row.posCustomerGroupId ?? null,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { SET_SELECTED_COMPANY } from '../../sql/google_auth.js';
|
||||
|
||||
export default function setSelectedCompany(pool) {
|
||||
return async (id, companyId) => {
|
||||
return async (googleAuthId, companyId) => {
|
||||
await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('googleAuthId', googleAuthId)
|
||||
.input('companyId', companyId)
|
||||
.query(SET_SELECTED_COMPANY);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user