This commit is contained in:
greeneclipse committed 2026-10-20 17:19:46 +02:00
1 parent 1c003e30a4
commit c83347b9a5
2 files changed
+41 -3

No files matched your search

+10 -3
View File
@@ -37,7 +37,7 @@ func headerIndex(headers []string, names ...string) int {
want[strings.ToLower(strings.TrimSpace(n))] = struct{}{}
}
for i, h := range headers {
if _, ok := want[strings.ToLower(strings.TrimSpace(h))]; ok {
if _, ok := want[strings.ToLower(strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(h), "\uFEFF")))]; ok {
return i
}
}
@@ -509,6 +509,13 @@ func (s *Service) MergeProductsByGTIN(ctx context.Context, companyID uuid.UUID)
return merge, err
}
// shouldMergeProductCSV reuses an existing GTIN for regular merge-enabled feeds
// and for non-primary-language imports, which add localizations to the same product.
// Generated no-GTIN identifiers must always create separate products.
func shouldMergeProductCSV(allowMerge, isPrimary bool, gtin string) bool {
return (allowMerge || !isPrimary) && !strings.HasPrefix(gtin, "nogtin-")
}
type productCSVRow struct {
gtin, productID, name, category, desc, status string
rawJSON string
@@ -517,7 +524,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)
_, primary := s.importLanguage(ctx, companyID)
merge, err := s.MergeProductsByGTIN(ctx, companyID)
if err != nil {
return res, err
@@ -576,7 +583,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 || lang != primary) && !strings.HasPrefix(gtin, "nogtin-"),
mergeable: shouldMergeProductCSV(merge, primary, gtin),
})
if len(batch) >= importCSVBatchSize {
if err := s.flushProductBatch(ctx, companyID, fileID, batch, &res); err != nil {
@@ -31,6 +31,37 @@ func TestHeaderIndexAndCell(t *testing.T) {
}
}
func TestHeaderIndexUTF8BOM(t *testing.T) {
for _, name := range []string{"\uFEFFgtin", " \uFEFFname ", "\uFEFFattribute_key"} {
want := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(name), "\uFEFF"))
if got := headerIndex([]string{name, "other"}, want); got != 0 {
t.Fatalf("UTF-8 BOM column %q: got %d want 0", name, got)
}
}
}
func TestShouldMergeProductCSV(t *testing.T) {
tests := []struct {
name, gtin string
allow, primary bool
want bool
}{
{name: "primary feed without merging", gtin: "3830000000017", primary: true, want: false},
{name: "primary feed with merging", gtin: "3830000000017", allow: true, primary: true, want: true},
{name: "secondary language without merging configured", gtin: "3830000000017", primary: false, want: true},
{name: "secondary language with merging configured", gtin: "3830000000017", allow: true, primary: false, want: true},
{name: "generated GTIN primary", gtin: "nogtin-test", allow: true, primary: true, want: false},
{name: "generated GTIN secondary", gtin: "nogtin-test", allow: true, primary: false, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := shouldMergeProductCSV(tt.allow, tt.primary, tt.gtin); got != tt.want {
t.Fatalf("mergeable = %t, want %t", got, tt.want)
}
})
}
}
func TestImportResultAddErrorCaps(t *testing.T) {
res := ImportResult{}
for i := 0; i < importCSVMaxErrors+20; i++ {