This commit is contained in:
seb
2026-07-31 09:00:14 +02:00
parent 4f98082061
commit 99dd76075e
24 changed files with 1477 additions and 1175 deletions

View File

@@ -5,9 +5,8 @@ import companyCreate from './store/company/create.js';
import companyUpdate from './store/company/update.js';
import companyDelete from './store/company/delete.js';
import companyList from './store/company/list.js';
import categoryGetTree from './store/category/getTree.js';
import categoryGetTreeForCompany from './store/category/getTreeForCompany.js';
import categoryCreate from './store/category/create.js';
import categoryCreateUnderParent from './store/category/createUnderParent.js';
import categoryUpdateParentId from './store/category/updateParentId.js';
import categoryDelete from './store/category/delete.js';
import langCreate from './store/lang/create.js';
@@ -45,9 +44,8 @@ export default class LibDashStore {
};
this.category = {
getTree: categoryGetTree(pool),
getTreeForCompany: categoryGetTreeForCompany(pool),
create: categoryCreate(pool),
createUnderParent: categoryCreateUnderParent(pool),
updateParentId: categoryUpdateParentId(pool),
delete: categoryDelete(pool),
};

View File

@@ -106,6 +106,7 @@ BEGIN
taxrate INT NULL,
reducedtaxrate INT NULL,
taxfreerate INT NULL,
poscustomergroup_id INT NULL,
CONSTRAINT UQ_company_name UNIQUE (name),
CONSTRAINT FK_company_root_category FOREIGN KEY (root_category_id) REFERENCES category(id) ON DELETE SET NULL,
CONSTRAINT FK_company_default_language FOREIGN KEY (default_language_id) REFERENCES language(id) ON DELETE SET NULL,
@@ -128,6 +129,17 @@ BEGIN
END
`;
export const INIT_COMPANY_POS_CUSTOMERGROUP_FK = `
IF NOT EXISTS (
SELECT 1 FROM sys.foreign_keys WHERE name = N'FK_company_poscustomergroup'
)
BEGIN
ALTER TABLE company
ADD CONSTRAINT FK_company_poscustomergroup
FOREIGN KEY (poscustomergroup_id) REFERENCES customergroup(id) ON DELETE NO ACTION;
END
`;
export const INIT_ENTITY = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'entity')
BEGIN
@@ -516,6 +528,7 @@ export const SCHEMA_SCRIPTS = [
INIT_COMPANY,
INIT_GOOGLE_AUTH,
INIT_CUSTOMERGROUP,
INIT_COMPANY_POS_CUSTOMERGROUP_FK,
INIT_ENTITY,
INIT_UNIT,
INIT_UNITTEXT,

View File

@@ -1,4 +1,4 @@
export const GET_TREE = `
export const GET_TREE_FOR_COMPANY = `
WITH tree AS (
SELECT id, parent_id, sort
FROM category
@@ -10,17 +10,122 @@ WITH tree AS (
FROM category c
INNER JOIN tree t ON c.parent_id = t.id
)
SELECT id, parent_id AS parentId, sort
FROM tree
ORDER BY sort, id
SELECT
t.id,
t.parent_id AS parentId,
t.sort,
ct.name,
p.thumbw AS thumbW,
p.thumbh AS thumbH,
p.thumbhash AS thumbHash
FROM tree t
LEFT JOIN categorytext ct
ON ct.category_id = t.id AND ct.language_id = @languageId
OUTER APPLY (
SELECT TOP 1 cp.pic_id
FROM categorypic cp
WHERE cp.category_id = t.id
ORDER BY cp.sort ASC, cp.id ASC
) first_pic
LEFT JOIN pic p ON p.id = first_pic.pic_id
ORDER BY t.sort, t.id
`;
export const GET_ROOT_CATEGORY_ARTICLES = `
SELECT
a.id,
a.sku,
at.name,
at.slug,
pr.net,
t.rate AS taxRate,
CASE
WHEN a.optionslist_id IS NOT NULL THEN (
SELECT COUNT(*)
FROM optionslistarticle ola
WHERE ola.optionslist_id = a.optionslist_id
)
ELSE 0
END AS optionsCount,
CASE
WHEN a.articlelist_id IS NOT NULL THEN (
SELECT COUNT(*)
FROM articlelistarticle ala
WHERE ala.articlelist_id = a.articlelist_id
)
ELSE 0
END AS articleListCount,
(
SELECT COUNT(*)
FROM article child
WHERE child.fatherarticle_id = a.id
) AS childCount,
pic.thumbw AS thumbW,
pic.thumbh AS thumbH,
pic.thumbhash AS thumbHash
FROM categoryarticle ca
INNER JOIN article a ON a.id = ca.article_id
LEFT JOIN articletext at
ON at.article_id = a.id AND at.language_id = @languageId
LEFT JOIN price pr
ON pr.article_id = a.id
AND pr.customergroup_id = @posCustomerGroupId
AND pr.minmultiplier = 1
LEFT JOIN tax t ON t.id = pr.tax_id
OUTER APPLY (
SELECT TOP 1 ap.pic_id
FROM articlepic ap
WHERE ap.article_id = a.id
ORDER BY ap.sort ASC, ap.id ASC
) first_pic
LEFT JOIN pic ON pic.id = first_pic.pic_id
WHERE ca.category_id = @rootCategoryId
ORDER BY a.id
`;
export const CREATE = `
INSERT INTO category (parent_id, sort)
OUTPUT INSERTED.id
OUTPUT INSERTED.id, INSERTED.parent_id AS parentId, INSERTED.sort
VALUES (@parentId, @sort)
`;
export const RESOLVE_CATEGORY_ACCESS = `
WITH ancestors AS (
SELECT id, parent_id
FROM category
WHERE id = @categoryId
UNION ALL
SELECT c.id, c.parent_id
FROM category c
INNER JOIN ancestors a ON c.id = a.parent_id
),
root AS (
SELECT TOP 1 id AS rootCategoryId
FROM ancestors
WHERE parent_id IS NULL
)
SELECT
p.id AS categoryId,
r.rootCategoryId,
co.id AS companyId,
co.default_language_id AS languageId,
cu.id AS companyuserId
FROM category p
LEFT JOIN root r ON 1 = 1
LEFT JOIN company co ON co.root_category_id = r.rootCategoryId
LEFT JOIN companyuser cu
ON cu.company_id = co.id AND cu.google_auth_id = @googleAuthId
WHERE p.id = @categoryId
`;
export const GET_NEXT_SIBLING_SORT = `
SELECT ISNULL(MAX(sort), -1) + 1 AS sort
FROM category
WHERE parent_id = @parentId
`;
export const UPDATE_PARENT_ID = `
UPDATE category
SET parent_id = @parentId

4
src/sql/categorytext.js Normal file
View File

@@ -0,0 +1,4 @@
export const CREATE = `
INSERT INTO categorytext (category_id, language_id, name, slug)
VALUES (@categoryId, @languageId, @name, @slug)
`;

View File

@@ -54,7 +54,7 @@ INNER JOIN companyuser cu ON cu.company_id = c.id
LEFT JOIN tax tr ON tr.id = c.taxrate
LEFT JOIN tax rtr ON rtr.id = c.reducedtaxrate
LEFT JOIN tax tfr ON tfr.id = c.taxfreerate
WHERE cu.google_auth_id = @authId
WHERE cu.google_auth_id = @googleAuthId
`;
export const DELETE = `

View File

@@ -8,17 +8,19 @@ WHERE ga.email = @email
export const GET_COMPANY_ACCESS = `
SELECT
c.root_category_id AS rootCategoryId,
c.default_language_id AS languageId,
c.poscustomergroup_id AS posCustomerGroupId,
cu.id AS companyuserId
FROM company c
LEFT JOIN companyuser cu
ON cu.company_id = c.id AND cu.google_auth_id = @authId
ON cu.company_id = c.id AND cu.google_auth_id = @googleAuthId
WHERE c.id = @companyId
`;
export const ADD = `
INSERT INTO companyuser (google_auth_id, company_id)
OUTPUT INSERTED.id
VALUES (@authId, @companyId)
VALUES (@googleAuthId, @companyId)
`;
export const DELETE = `

View File

@@ -24,5 +24,5 @@ WHERE email = @email
export const SET_SELECTED_COMPANY = `
UPDATE google_auth
SET selected_company = @companyId
WHERE id = @id
WHERE id = @googleAuthId
`;

View File

@@ -34,12 +34,19 @@ export const TABLES = [
export const PROCEDURES = ['getnextnumberwithprefixandpadding'];
/** Drop before tables — breaks company ↔ customergroup cycle for purge. */
export const CONSTRAINTS = [
`IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = N'FK_company_poscustomergroup')
ALTER TABLE company DROP CONSTRAINT FK_company_poscustomergroup;`,
];
export function buildPurgeSql() {
const drops = [
...PROCEDURES.map(
(name) =>
`IF OBJECT_ID(N'dbo.${name}', N'P') IS NOT NULL DROP PROCEDURE dbo.[${name}];`,
),
...CONSTRAINTS,
...TABLES.map(
(name) =>
`IF OBJECT_ID(N'dbo.${name}', N'U') IS NOT NULL DROP TABLE dbo.[${name}];`,

View File

@@ -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;
};
}

View 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;
}
};
}

View File

@@ -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;
};
}

View File

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

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

View File

@@ -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();

View File

@@ -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;
};

View File

@@ -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;

View File

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

View File

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