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

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