providerrefactor part1

This commit is contained in:
seb
2026-09-21 03:33:55 +02:00
parent 53a20c4353
commit 4ed6260e91
57 changed files with 1662 additions and 145 deletions

View File

@@ -0,0 +1,187 @@
import * as source from '../../mssql/queries/article.js';
import { translateQueries } from './translate.js';
const queries = translateQueries(source, 'article');
export const {
ADD_CATEGORY, ADD_EAN, ARTICLE_LIST_COLUMNS, CREATE,
DELETE_CATEGORIES_BY_ARTICLE, DELETE_EANS_BY_ARTICLE, EAN_EXISTS,
GET_BRAND_BY_COMPANY_AND_ID,
GET_CATEGORY_ID_BY_SKU, GET_CATEGORY_TREE_FLAT, GET_ID_BY_SKU,
SET_FATHER_SORT,
SET_IS_FATHER, UPDATE_BRAND, UPDATE_FATHER_BY_SKU, UPDATE_UNIT,
UPDATE_UNITAMOUNT, UPDATE_UNIT_AND_AMOUNT, UPDATE_WEIGHT,
} = queries;
export const ARTICLE_LIST_JOINS = `
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
LEFT JOIN pic ON pic.id = (
SELECT ap.pic_id FROM articlepic ap
WHERE ap.article_id = a.id ORDER BY ap.sort, ap.id LIMIT 1
)
LEFT JOIN article father ON father.id = a.fatherarticle_id
`;
export const RESOLVE_ARTICLE_ACCESS_BY_SKU = `
WITH RECURSIVE article_cat AS (
SELECT 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 LIMIT 1
), 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 id AS rootCategoryId FROM ancestors WHERE parent_id IS NULL LIMIT 1
)
SELECT ac.articleId, ac.categoryId, r.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
`;
const JSON_EANS = `(SELECT COALESCE(json_group_array(json_object('ean', ean)), '[]')
FROM (SELECT ean FROM articleean WHERE article_id = a.id ORDER BY ean))`;
const JSON_CATEGORIES = `(SELECT COALESCE(json_group_array(json_object('category_id', category_id)), '[]')
FROM (SELECT category_id FROM categoryarticle WHERE article_id = a.id ORDER BY category_id))`;
export const SEARCH_ARTICLES = `
WITH RECURSIVE category_tree AS (
SELECT id FROM category WHERE id = @rootCategoryId
UNION ALL SELECT c.id FROM category c
INNER JOIN category_tree ct ON c.parent_id = ct.id
)
SELECT a.id, a.sku, at.name,
${JSON_EANS} AS eansJson,
${JSON_CATEGORIES} AS categoryIdsJson
FROM article a
LEFT JOIN articletext at ON at.article_id = a.id AND at.language_id = @languageId
WHERE EXISTS (
SELECT 1 FROM categoryarticle ca
INNER JOIN category_tree ct ON ct.id = ca.category_id
WHERE ca.article_id = a.id
)
ORDER BY a.id
`;
export const GET_ARTICLE_EXPORT = `
WITH RECURSIVE category_tree AS (
SELECT id FROM category WHERE id = @categoryId
UNION ALL SELECT c.id FROM category c
INNER JOIN category_tree ct ON c.parent_id = ct.id
)
SELECT DISTINCT a.id, a.sku, at.name, at.slug, u.si AS unitSi,
ut.symbol AS unitSymbol, a.unitamount, b.name AS brandName,
pr.net, t.rate AS taxRate,
${JSON_EANS} AS eansJson,
${JSON_CATEGORIES} AS categoryIdsJson,
(SELECT COALESCE(json_group_array(json_object('hash', hash)), '[]')
FROM (SELECT p.hash FROM articlepic ap INNER JOIN pic p ON p.id = ap.pic_id
WHERE ap.article_id = a.id ORDER BY ap.sort, ap.id)) AS fullHashesJson
FROM article a
INNER JOIN categoryarticle ca ON ca.article_id = a.id
INNER JOIN category_tree ct ON ct.id = ca.category_id
LEFT JOIN articletext at ON at.article_id = a.id AND at.language_id = @languageId
LEFT JOIN unit u ON u.id = a.unit_id
LEFT JOIN unittext ut ON ut.unit_id = u.id AND ut.language_id = @languageId
LEFT JOIN brand b ON b.id = a.brand_id
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
ORDER BY a.id
`;
export const GET_ARTICLE_BY_SKU_FULL = `
WITH RECURSIVE article_cat AS (
SELECT 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 LIMIT 1
), 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 id AS rootCategoryId FROM ancestors WHERE parent_id IS NULL LIMIT 1
), auth AS (
SELECT ac.articleId, ac.categoryId, r.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
)
SELECT auth.*,
json_object(
'id', a.id, 'sku', a.sku, 'name', at.name, 'slug', at.slug,
'unitamount', a.unitamount, 'weight', a.weight,
'unitSi', u.si, 'unitBasesi', u.basesi,
'unitBasesifactor', u.basesifactor, 'unitSymbol', ut.symbol,
'unitName', ut.name, 'brandName', b.name,
'brandThumbW', bp.thumbw, 'brandThumbH', bp.thumbh,
'brandThumbHash', bp.thumbhash,
'optionsCount', (SELECT COUNT(*) FROM optionslistarticle WHERE optionslist_id = a.optionslist_id),
'articleListCount', (SELECT COUNT(*) FROM articlelistarticle WHERE articlelist_id = a.articlelist_id),
'childCount', (SELECT COUNT(*) FROM article WHERE fatherarticle_id = a.id),
'fatherArticleId', a.fatherarticle_id, 'fatherSku', father.sku,
'categoryIdsJson', json(${JSON_CATEGORIES}),
'photosJson', json((SELECT COALESCE(json_group_array(json_object(
'sort', sort, 'fullHash', hash, 'fullW', w, 'fullH', h,
'fullSize', size, 'thumbW', thumbw, 'thumbH', thumbh,
'thumbHash', thumbhash
)), '[]') FROM (
SELECT ap.sort, p.hash, p.w, p.h, p.size, p.thumbw, p.thumbh, p.thumbhash
FROM articlepic ap INNER JOIN pic p ON p.id = ap.pic_id
WHERE ap.article_id = a.id ORDER BY ap.sort, ap.id
))),
'eansJson', json(${JSON_EANS}),
'pricesJson', json((SELECT COALESCE(json_group_array(json_object(
'minmultiplier', minmultiplier, 'net', net, 'taxRate', rate,
'customergroupId', customergroup_id
)), '[]') FROM (
SELECT pr.minmultiplier, pr.net, t.rate, pr.customergroup_id
FROM price pr INNER JOIN tax t ON t.id = pr.tax_id
WHERE pr.article_id = a.id ORDER BY pr.customergroup_id, pr.minmultiplier
))),
'suppliersJson', json((SELECT COALESCE(json_group_array(json_object(
'id', id, 'articleId', article_id, 'entityId', entity_id,
'supplierName', supplier_name, 'sku', sku, 'pu', pu,
'stock', stock, 'sort', sort, 'priceId', price_id,
'price', CASE WHEN price_id IS NULL THEN NULL ELSE json_object(
'net', net, 'taxId', tax_id, 'taxRate', tax_rate
) END
)), '[]') FROM (
SELECT s.id, s.article_id, s.entity_id, e.name AS supplier_name,
s.sku, s.pu, s.stock, s.sort, s.price_id,
pr.net, pr.tax_id, t.rate AS tax_rate
FROM articlesupplier s INNER JOIN entity e ON e.id = s.entity_id
LEFT JOIN price pr ON pr.id = s.price_id
LEFT JOIN tax t ON t.id = pr.tax_id
WHERE s.article_id = a.id ORDER BY s.sort, s.id
)))
) AS articleJson
FROM auth
INNER JOIN article a ON a.id = auth.articleId
LEFT JOIN articletext at ON at.article_id = a.id AND at.language_id = auth.languageId
LEFT JOIN unit u ON u.id = a.unit_id
LEFT JOIN unittext ut ON ut.unit_id = u.id AND ut.language_id = auth.languageId
LEFT JOIN brand b ON b.id = a.brand_id
LEFT JOIN pic bp ON bp.id = (
SELECT pic_id FROM brandpic WHERE brand_id = b.id ORDER BY sort, id LIMIT 1
)
LEFT JOIN article father ON father.id = a.fatherarticle_id
`;