This commit is contained in:
seb
2026-09-25 12:30:45 +02:00
parent b5255bdd17
commit 607e6cc0ac
10 changed files with 98 additions and 124 deletions

View File

@@ -56,14 +56,14 @@ export const GET_CATEGORY_ID_BY_SKU = `
SELECT TOP 1 ca.category_id AS categoryId, a.id AS articleId
FROM article a
INNER JOIN categoryarticle ca ON ca.article_id = a.id
WHERE a.sku = @sku
WHERE a.sku = @sku AND (@companyId IS NULL OR a.company_id = @companyId)
`;
/** Resolve an article id by sku (used to wire father/child compound links). */
export const GET_ID_BY_SKU = `
SELECT TOP 1 id AS articleId
FROM article
WHERE sku = @sku
WHERE sku = @sku AND (@companyId IS NULL OR company_id = @companyId)
`;
/** True if an EAN is already assigned to any article (duplicate guard). */
@@ -78,7 +78,7 @@ export const UPDATE_FATHER_BY_SKU = `
UPDATE article
SET fatherarticle_id = @fatherArticleId,
fathersort = COALESCE(@fathersort, 0)
WHERE sku = @sku
WHERE sku = @sku AND (@companyId IS NULL OR company_id = @companyId)
`;
export const SET_IS_FATHER = `
@@ -95,41 +95,22 @@ WHERE id = @articleId AND fatherarticle_id = @fatherArticleId
/** SKU → article/category + company auth in one round-trip. */
export const RESOLVE_ARTICLE_ACCESS_BY_SKU = `
WITH article_cat AS (
SELECT TOP 1 a.id AS articleId, ca.category_id AS categoryId
FROM article a
INNER JOIN categoryarticle ca ON ca.article_id = a.id
WHERE a.sku = @sku
),
ancestors AS (
SELECT c.id, c.parent_id
FROM category c
INNER JOIN article_cat ac ON c.id = ac.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
ac.articleId,
ac.categoryId,
r.rootCategoryId,
SELECT TOP 1
a.id AS articleId,
ca.category_id AS categoryId,
co.root_category_id AS rootCategoryId,
co.id AS companyId,
co.default_language_id AS languageId,
co.poscustomergroup_id AS posCustomerGroupId,
cu.id AS companyuserId
FROM article_cat ac
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
FROM article a
INNER JOIN company co ON co.id = a.company_id
INNER JOIN google_auth ga ON ga.id = @googleAuthId
INNER JOIN companyuser cu
ON cu.company_id = a.company_id AND cu.google_auth_id = @googleAuthId
LEFT JOIN categoryarticle ca ON ca.article_id = a.id
WHERE a.sku = @sku
AND (ga.selected_company IS NULL OR a.company_id = ga.selected_company)
`;
export const UPDATE_WEIGHT = `
@@ -163,9 +144,9 @@ WHERE id = @articleId
`;
export const CREATE = `
INSERT INTO article (sku, unit_id, unitamount, weight)
INSERT INTO article (company_id, sku, unit_id, unitamount, weight)
OUTPUT INSERTED.id
VALUES (@sku, @unitId, @unitamount, @weight)
VALUES (@companyId, @sku, @unitId, @unitamount, @weight)
`;
export const DELETE_EANS_BY_ARTICLE = `
@@ -342,42 +323,23 @@ ORDER BY a.id
* Returns one row with articleJson, or empty if not found/unauthorized.
*/
export const GET_ARTICLE_BY_SKU_FULL = `
;WITH article_cat AS (
SELECT TOP 1 a.id AS articleId, ca.category_id AS categoryId
FROM article a
INNER JOIN categoryarticle ca ON ca.article_id = a.id
WHERE a.sku = @sku
),
ancestors AS (
SELECT c.id, c.parent_id
FROM category c
INNER JOIN article_cat ac ON c.id = ac.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
),
auth AS (
SELECT
ac.articleId,
ac.categoryId,
r.rootCategoryId,
;WITH auth AS (
SELECT TOP 1
a.id AS articleId,
ca.category_id AS categoryId,
co.root_category_id AS rootCategoryId,
co.id AS companyId,
co.default_language_id AS languageId,
co.poscustomergroup_id AS posCustomerGroupId,
cu.id AS companyuserId
FROM article_cat ac
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
FROM article a
INNER JOIN company co ON co.id = a.company_id
INNER JOIN google_auth ga ON ga.id = @googleAuthId
INNER JOIN companyuser cu
ON cu.company_id = a.company_id AND cu.google_auth_id = @googleAuthId
LEFT JOIN categoryarticle ca ON ca.article_id = a.id
WHERE a.sku = @sku
AND (ga.selected_company IS NULL OR a.company_id = ga.selected_company)
)
SELECT
auth.articleId,