This commit is contained in:
greeneclipse committed 2026-10-16 13:48:51 +02:00
1 parent feebf7d3c1
commit 486502852e
41 files changed
+1311 -259

No files matched your search

+45 -26
View File
@@ -124,6 +124,7 @@ func (s *Service) flushCategoryBatch(ctx context.Context, companyID uuid.UUID, b
if len(batch) == 0 {
return nil
}
lang, primary := s.importLanguage(ctx, companyID)
byUID := make(map[string]categoryCSVRow, len(batch))
order := make([]string, 0, len(batch))
@@ -184,12 +185,15 @@ func (s *Service) flushCategoryBatch(ctx context.Context, companyID uuid.UUID, b
if len(updIDs) > 0 {
_, err = tx.Exec(ctx, `
UPDATE categories AS c SET
name = v.name,
description = CASE WHEN v.description <> '' THEN v.description ELSE c.description END,
name = CASE WHEN $5 THEN v.name ELSE c.name END,
description = CASE WHEN $5 AND v.description <> '' THEN v.description ELSE c.description END,
localized_fields = jsonb_set(COALESCE(c.localized_fields, '{}'::jsonb), ARRAY[$6::text],
COALESCE(c.localized_fields -> ($6::text), '{}'::jsonb) ||
jsonb_strip_nulls(jsonb_build_object('name', NULLIF(v.name,''), 'description',NULLIF(v.description,''))), true),
updated_at = now()
FROM unnest($2::uuid[], $3::text[], $4::text[]) AS v(id, name, description)
WHERE c.id = v.id AND c.company_id = $1`,
companyID, updIDs, updNames, updDescs)
companyID, updIDs, updNames, updDescs, primary, lang)
if err != nil {
return err
}
@@ -228,12 +232,13 @@ func (s *Service) flushCategoryBatch(ctx context.Context, companyID uuid.UUID, b
}
rows, err := tx.Query(ctx, `
INSERT INTO categories (company_id, name, unique_id, parent_unique_id, description, path, level)
SELECT $1, v.name, v.unique_id, NULLIF(v.parent_unique_id, ''), NULLIF(v.description, ''), v.path, v.level
INSERT INTO categories (company_id, name, unique_id, parent_unique_id, description, path, level, localized_fields)
SELECT $1, v.name, v.unique_id, NULLIF(v.parent_unique_id, ''), NULLIF(v.description, ''), v.path, v.level,
jsonb_build_object($8::text, jsonb_strip_nulls(jsonb_build_object('name',v.name,'description',NULLIF(v.description,''))))
FROM unnest($2::text[], $3::text[], $4::text[], $5::text[], $6::text[], $7::int[])
AS v(name, unique_id, parent_unique_id, description, path, level)
RETURNING id, unique_id, COALESCE(path, ''), level`,
companyID, insNames, insUIDs, insParents, insDescs, insPaths, insLevels)
companyID, insNames, insUIDs, insParents, insDescs, insPaths, insLevels, lang)
if err != nil {
return err
}
@@ -396,6 +401,7 @@ func (s *Service) flushAttributeBatch(ctx context.Context, companyID uuid.UUID,
if len(batch) == 0 {
return nil
}
lang, primary := s.importLanguage(ctx, companyID)
byKey := make(map[string]attributeCSVRow, len(batch))
order := make([]string, 0, len(batch))
@@ -465,12 +471,14 @@ func (s *Service) flushAttributeBatch(ctx context.Context, companyID uuid.UUID,
if len(updIDs) > 0 {
_, err = tx.Exec(ctx, `
UPDATE attributes AS a SET
name = CASE WHEN v.name <> '' THEN v.name ELSE a.name END,
name = CASE WHEN $5 AND v.name <> '' THEN v.name ELSE a.name END,
value_type = CASE WHEN v.value_type <> '' THEN v.value_type ELSE a.value_type END,
localized_fields = jsonb_set(COALESCE(a.localized_fields,'{}'::jsonb), ARRAY[$6::text],
COALESCE(a.localized_fields -> ($6::text),'{}'::jsonb) || jsonb_build_object('name',v.name), true),
updated_at = now()
FROM unnest($2::uuid[], $3::text[], $4::text[]) AS v(id, name, value_type)
WHERE a.id = v.id AND a.company_id = $1`,
companyID, updIDs, updNames, updTypes)
companyID, updIDs, updNames, updTypes, primary, lang)
if err != nil {
return err
}
@@ -479,12 +487,13 @@ func (s *Service) flushAttributeBatch(ctx context.Context, companyID uuid.UUID,
if len(insKeys) > 0 {
ct, err := tx.Exec(ctx, `
INSERT INTO attributes (company_id, attribute_key, name, value_type, unit, example, parent_key)
INSERT INTO attributes (company_id, attribute_key, name, value_type, unit, example, parent_key, localized_fields)
SELECT $1, v.attribute_key, v.name, v.value_type,
NULLIF(v.unit, ''), NULLIF(v.example, ''), NULLIF(v.parent_key, '')
NULLIF(v.unit, ''), NULLIF(v.example, ''), NULLIF(v.parent_key, ''),
jsonb_build_object($8::text, jsonb_build_object('name',v.name))
FROM unnest($2::text[], $3::text[], $4::text[], $5::text[], $6::text[], $7::text[])
AS v(attribute_key, name, value_type, unit, example, parent_key)`,
companyID, insKeys, insNames, insTypes, insUnits, insExamples, insParents)
companyID, insKeys, insNames, insTypes, insUnits, insExamples, insParents, lang)
if err != nil {
return err
}
@@ -508,6 +517,7 @@ type productCSVRow struct {
func (s *Service) ImportProductsCSV(ctx context.Context, companyID uuid.UUID, r io.Reader, fileID *uuid.UUID) (ImportResult, error) {
res := ImportResult{}
lang, primary := s.importLanguage(ctx, companyID)
merge, err := s.MergeProductsByGTIN(ctx, companyID)
if err != nil {
return res, err
@@ -566,7 +576,7 @@ func (s *Service) ImportProductsCSV(ctx context.Context, companyID uuid.UUID, r
batch = append(batch, productCSVRow{
gtin: gtin, productID: productID, name: name, category: category,
desc: desc, status: status, rawJSON: string(rawData),
mergeable: merge && !strings.HasPrefix(gtin, "nogtin-"),
mergeable: (merge || lang != primary) && !strings.HasPrefix(gtin, "nogtin-"),
})
if len(batch) >= importCSVBatchSize {
if err := s.flushProductBatch(ctx, companyID, fileID, batch, &res); err != nil {
@@ -585,6 +595,7 @@ func (s *Service) flushProductBatch(ctx context.Context, companyID uuid.UUID, fi
if len(batch) == 0 {
return nil
}
lang, primary := s.importLanguage(ctx, companyID)
// Last row wins per GTIN so a single INSERT cannot hit the same unique key twice.
byGTIN := make(map[string]productCSVRow, len(batch))
@@ -677,13 +688,15 @@ func (s *Service) flushProductBatch(ctx context.Context, companyID uuid.UUID, fi
if len(updRawIDs) > 0 {
_, err = tx.Exec(ctx, `
UPDATE raw_products AS r SET
raw_data = v.raw_data::jsonb,
mapped_data = v.raw_data::jsonb,
raw_data = CASE WHEN $6 THEN v.raw_data::jsonb ELSE r.raw_data END,
mapped_data = CASE WHEN $6 THEN v.raw_data::jsonb ELSE r.mapped_data END,
source_localizations = jsonb_set(COALESCE(r.source_localizations,'{}'::jsonb), ARRAY[$5::text],
COALESCE(r.source_localizations -> ($5::text),'{}'::jsonb) || v.raw_data::jsonb, true),
file_id = COALESCE($3, r.file_id),
updated_at = now()
FROM unnest($2::uuid[], $4::text[]) AS v(id, raw_data)
WHERE r.id = v.id AND r.company_id = $1`,
companyID, updRawIDs, fileID, updRawJSON)
companyID, updRawIDs, fileID, updRawJSON, lang, primary)
if err != nil {
return err
}
@@ -694,12 +707,13 @@ func (s *Service) flushProductBatch(ctx context.Context, companyID uuid.UUID, fi
if len(insGTINs) > 0 {
rows, err := tx.Query(ctx, `
INSERT INTO raw_products (company_id, gtin, raw_data, mapped_data, processing_status, file_id)
SELECT $1, v.gtin, v.raw_data::jsonb, v.raw_data::jsonb, 'unprocessed', $2
INSERT INTO raw_products (company_id, gtin, raw_data, mapped_data, processing_status, file_id, source_localizations)
SELECT $1, v.gtin, v.raw_data::jsonb, v.raw_data::jsonb, 'unprocessed', $2,
jsonb_build_object($5::text, v.raw_data::jsonb)
FROM unnest($3::text[], $4::text[]) AS v(gtin, raw_data)
ON CONFLICT (company_id, gtin) DO NOTHING
RETURNING id, gtin`,
companyID, fileID, insGTINs, insJSON)
companyID, fileID, insGTINs, insJSON, lang)
if err != nil {
return err
}
@@ -803,15 +817,18 @@ func (s *Service) flushProductBatch(ctx context.Context, companyID uuid.UUID, fi
_, err = tx.Exec(ctx, `
UPDATE processed_products AS p SET
product_id = COALESCE(NULLIF(v.product_id, ''), p.product_id),
name = COALESCE(NULLIF(v.name, ''), p.name),
category = COALESCE(NULLIF(v.category, ''), p.category),
description = COALESCE(NULLIF(v.description, ''), p.description),
status = COALESCE(NULLIF(v.status, ''), p.status),
name = CASE WHEN $9 THEN COALESCE(NULLIF(v.name, ''), p.name) ELSE p.name END,
category = CASE WHEN $9 THEN COALESCE(NULLIF(v.category, ''), p.category) ELSE p.category END,
description = CASE WHEN $9 THEN COALESCE(NULLIF(v.description, ''), p.description) ELSE p.description END,
status = CASE WHEN $9 THEN COALESCE(NULLIF(v.status, ''), p.status) ELSE p.status END,
source_localizations = jsonb_set(COALESCE(p.source_localizations,'{}'::jsonb), ARRAY[$8::text],
COALESCE(p.source_localizations -> ($8::text),'{}'::jsonb) || jsonb_strip_nulls(jsonb_build_object(
'name',NULLIF(v.name,''),'category',NULLIF(v.category,''),'description',NULLIF(v.description,''))),true),
updated_at = now()
FROM unnest($2::uuid[], $3::text[], $4::text[], $5::text[], $6::text[], $7::text[])
AS v(id, product_id, name, category, description, status)
WHERE p.id = v.id AND p.company_id = $1`,
companyID, updProcIDs, updPIDs, updNames, updCats, updDescs, updStatuses)
companyID, updProcIDs, updPIDs, updNames, updCats, updDescs, updStatuses, lang, primary)
if err != nil {
return err
}
@@ -820,13 +837,15 @@ func (s *Service) flushProductBatch(ctx context.Context, companyID uuid.UUID, fi
if len(insRawIDs) > 0 {
_, err = tx.Exec(ctx, `
INSERT INTO processed_products (company_id, product_id, name, category, description, status, raw_product_id)
INSERT INTO processed_products (company_id, product_id, name, category, description, status, raw_product_id, source_localizations)
SELECT $1,
NULLIF(v.product_id, ''), NULLIF(v.name, ''), NULLIF(v.category, ''),
NULLIF(v.description, ''), v.status, v.raw_product_id
NULLIF(v.description, ''), v.status, v.raw_product_id,
jsonb_build_object($8::text, jsonb_strip_nulls(jsonb_build_object(
'name', NULLIF(v.name,''), 'category',NULLIF(v.category,''), 'description',NULLIF(v.description,''))))
FROM unnest($2::uuid[], $3::text[], $4::text[], $5::text[], $6::text[], $7::text[])
AS v(raw_product_id, product_id, name, category, description, status)`,
companyID, insRawIDs, insPIDs, insNames, insCats, insDescs, insStatuses)
companyID, insRawIDs, insPIDs, insNames, insCats, insDescs, insStatuses, lang)
if err != nil {
return err
}