Fixes
This commit is contained in:
1 parent
04794aea54
commit
cfd31f398c
61 files changed
+2062
-254
No files matched your search
@@ -43,13 +43,20 @@ func (s *Service) SaveUpload(ctx context.Context, companyID, userID uuid.UUID, u
|
||||
if uploadDir == "" {
|
||||
return nil, ClientMsg("upload directory not configured")
|
||||
}
|
||||
kind = strings.ToLower(strings.TrimSpace(kind))
|
||||
safe := sanitizeFileName(originalName)
|
||||
lower := strings.ToLower(safe)
|
||||
if !strings.HasSuffix(lower, ".csv") {
|
||||
return nil, ClientMsg("only .csv uploads are allowed")
|
||||
isCSV := strings.HasSuffix(lower, ".csv") || strings.HasSuffix(lower, ".tsv")
|
||||
isXML := strings.HasSuffix(lower, ".xml") && kind == "feed"
|
||||
isJSON := strings.HasSuffix(lower, ".json")
|
||||
if !isCSV && !isXML && !isJSON {
|
||||
return nil, ClientMsg("only CSV, JSON or XML feed uploads are allowed")
|
||||
}
|
||||
if contentType != "" &&
|
||||
!strings.Contains(strings.ToLower(contentType), "csv") &&
|
||||
!strings.Contains(strings.ToLower(contentType), "json") &&
|
||||
!strings.Contains(strings.ToLower(contentType), "xml") &&
|
||||
!strings.Contains(strings.ToLower(contentType), "tab-separated") &&
|
||||
!strings.Contains(strings.ToLower(contentType), "text/plain") &&
|
||||
!strings.Contains(strings.ToLower(contentType), "octet-stream") {
|
||||
return nil, ClientMsg("invalid content type for CSV upload")
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// JSONImportAsCSV maps JSON arrays (including wrapped nested arrays) onto the
|
||||
// same validated batch import pipeline as CSV. Data must fit the catalog upload
|
||||
// limit. All rows are processed by the normal company-scoped upsert routines.
|
||||
func JSONImportAsCSV(r io.Reader, kind string) (io.Reader, error) {
|
||||
source, err := io.ReadAll(io.LimitReader(r, (6<<20)+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(source) > 6<<20 {
|
||||
return nil, ClientMsg("JSON catalog import exceeds 6 MiB")
|
||||
}
|
||||
var doc any
|
||||
dec := json.NewDecoder(bytes.NewReader(source))
|
||||
dec.UseNumber()
|
||||
if err := dec.Decode(&doc); err != nil {
|
||||
return nil, ClientMsg("invalid JSON import: " + err.Error())
|
||||
}
|
||||
items := findImportJSONArray(doc, kind, 0)
|
||||
if len(items) == 0 {
|
||||
return nil, ClientMsg("JSON must contain an array of " + kind)
|
||||
}
|
||||
header := []string{}
|
||||
switch kind {
|
||||
case "categories":
|
||||
header = []string{"name", "unique_id", "parent_unique_id", "description"}
|
||||
case "attributes":
|
||||
header = []string{"attribute_key", "name", "value_type", "unit", "example", "parent_key", "category_id"}
|
||||
case "products":
|
||||
header = []string{"gtin", "product_id", "name", "category", "description", "status"}
|
||||
default:
|
||||
return nil, ClientMsg("invalid catalog import kind")
|
||||
}
|
||||
// Keep original JSON keys too; importer uses known column names (GTIN/EAN, etc).
|
||||
keys := map[string]struct{}{}
|
||||
for _, k := range header {
|
||||
keys[k] = struct{}{}
|
||||
}
|
||||
for _, v := range items {
|
||||
if m, ok := v.(map[string]any); ok {
|
||||
for k := range m {
|
||||
keys[k] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
extras := make([]string, 0, len(keys))
|
||||
for k := range keys {
|
||||
found := false
|
||||
for _, h := range header {
|
||||
if k == h {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
extras = append(extras, k)
|
||||
}
|
||||
}
|
||||
sort.Strings(extras)
|
||||
header = append(header, extras...)
|
||||
var buf bytes.Buffer
|
||||
cw := csv.NewWriter(&buf)
|
||||
if err := cw.Write(header); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, v := range items {
|
||||
m, ok := v.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
rec := make([]string, len(header))
|
||||
for j, k := range header {
|
||||
val, exists := m[k]
|
||||
if !exists && (k == "gtin" || k == "product_id" || k == "name" || k == "unique_id" || k == "attribute_key") {
|
||||
aliases := map[string][]string{"gtin": {"ean", "barcode", "gtin"}, "product_id": {"sku", "productno", "productno2", "id"}, "name": {"title", "product", "product_name", "productname"}, "unique_id": {"id", "category_id", "uniqueId"}, "attribute_key": {"key", "attributeKey", "id"}}
|
||||
for _, alias := range aliases[k] {
|
||||
for source, value := range m {
|
||||
if strings.EqualFold(source, alias) && value != nil {
|
||||
val = value
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if exists {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !exists {
|
||||
for source, value := range m {
|
||||
if strings.EqualFold(source, k) {
|
||||
val = value
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if val == nil {
|
||||
continue
|
||||
}
|
||||
switch x := val.(type) {
|
||||
case string:
|
||||
rec[j] = x
|
||||
case json.Number:
|
||||
rec[j] = string(x)
|
||||
case bool:
|
||||
rec[j] = fmt.Sprint(x)
|
||||
default:
|
||||
b, _ := json.Marshal(x)
|
||||
rec[j] = string(b)
|
||||
}
|
||||
}
|
||||
if err := cw.Write(rec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
cw.Flush()
|
||||
if err := cw.Error(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewReader(buf.Bytes()), nil
|
||||
}
|
||||
func findImportJSONArray(doc any, kind string, depth int) []any {
|
||||
if depth > 6 {
|
||||
return nil
|
||||
}
|
||||
if items, ok := doc.([]any); ok {
|
||||
if len(items) > 0 {
|
||||
if _, ok := items[0].(map[string]any); ok {
|
||||
return items
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
m, ok := doc.(map[string]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
keys := []string{kind, "items", "results", "data", "records", "rows"}
|
||||
if kind == "products" {
|
||||
keys = append([]string{"SHOPITEM", "products", "product"}, keys...)
|
||||
}
|
||||
if kind == "categories" {
|
||||
keys = append([]string{"categories", "category"}, keys...)
|
||||
}
|
||||
if kind == "attributes" {
|
||||
keys = append([]string{"attributes", "specifications"}, keys...)
|
||||
}
|
||||
for _, k := range keys {
|
||||
for actual, value := range m {
|
||||
if strings.EqualFold(actual, k) {
|
||||
if a := findImportJSONArray(value, kind, depth+1); len(a) > 0 {
|
||||
return a
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(m) == 1 {
|
||||
for _, v := range m {
|
||||
return findImportJSONArray(v, kind, depth+1)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestV10JsonProducts(t *testing.T) {
|
||||
r, e := JSONImportAsCSV(strings.NewReader(`{"data":{"products":[{"EAN":"8595025395184","PRODUCT":"Ročna LED svetilka","PRODUCTNO":"1440093109","category":"Svetilke"}]}}`), "products")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
cr := csv.NewReader(r)
|
||||
h, _ := cr.Read()
|
||||
values, e := cr.Read()
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
m := map[string]string{}
|
||||
for i, k := range h {
|
||||
m[k] = values[i]
|
||||
}
|
||||
if m["gtin"] != "8595025395184" || m["name"] != "Ročna LED svetilka" || m["product_id"] != "1440093109" {
|
||||
t.Fatalf("wrong fields %v", m)
|
||||
}
|
||||
}
|
||||
func TestV10JsonCategories(t *testing.T) {
|
||||
r, e := JSONImportAsCSV(strings.NewReader(`{"categories":[{"name":"Svetilke","id":50}]}`), "categories")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
cr := csv.NewReader(r)
|
||||
h, _ := cr.Read()
|
||||
v, _ := cr.Read()
|
||||
m := map[string]string{}
|
||||
for i, k := range h {
|
||||
m[k] = v[i]
|
||||
}
|
||||
if m["unique_id"] != "50" {
|
||||
t.Fatalf("unique_id: %v", m)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user