fix
This commit is contained in:
1 parent
feebf7d3c1
commit
486502852e
41 files changed
+1311
-259
No files matched your search
@@ -11,6 +11,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/company"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
@@ -268,6 +269,14 @@ func (s *Service) ListSyncJobs(ctx context.Context, companyID, feedID uuid.UUID,
|
||||
|
||||
func (s *Service) runSync(ctx context.Context, companyID, feedID, jobID uuid.UUID, feed map[string]any) (syncStats, error) {
|
||||
var stats syncStats
|
||||
primaryLanguage := company.LoadLanguage(ctx, s.Pool, companyID)
|
||||
feedLanguage := primaryLanguage
|
||||
if opts, ok := feed["options"].(map[string]any); ok {
|
||||
if code, ok := opts["content_language"].(string); ok && company.IsAllowedLanguage(code) {
|
||||
feedLanguage = company.NormalizeLanguage(code)
|
||||
}
|
||||
}
|
||||
ctx = context.WithValue(ctx, syncLocaleKey{}, syncLocale{code: feedLanguage, primary: feedLanguage == primaryLanguage})
|
||||
urlStr, _ := feed["url"].(string)
|
||||
feedType, _ := feed["feed_type"].(string)
|
||||
|
||||
@@ -291,7 +300,7 @@ func (s *Service) runSync(ctx context.Context, companyID, feedID, jobID uuid.UUI
|
||||
if len(mappings) == 0 {
|
||||
return stats, ClientMsg("no field mappings defined for feed")
|
||||
}
|
||||
stats.MappingHash = sha256Hex([]byte(mustJSON(mappingsRaw)))
|
||||
stats.MappingHash = sha256Hex([]byte(mustJSON(mappingsRaw) + "|language=" + feedLanguage))
|
||||
|
||||
prev, err := s.lastSuccessfulSync(ctx, feedID)
|
||||
if err != nil {
|
||||
@@ -439,9 +448,16 @@ func (s *Service) runSync(ctx context.Context, companyID, feedID, jobID uuid.UUI
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
type syncLocaleKey struct{}
|
||||
type syncLocale struct {
|
||||
code string
|
||||
primary bool
|
||||
}
|
||||
|
||||
type existingProduct struct {
|
||||
ID uuid.UUID
|
||||
MappedData []byte
|
||||
ID uuid.UUID
|
||||
MappedData []byte
|
||||
SourceLocalizations []byte
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -450,17 +466,19 @@ const (
|
||||
upsertSQLInsertSet = `
|
||||
INSERT INTO raw_products (
|
||||
company_id, gtin, feed_id, raw_data, mapped_data, sync_job_id,
|
||||
is_processed, processing_status, updated_at
|
||||
is_processed, processing_status, updated_at, source_localizations
|
||||
)
|
||||
SELECT $1, v.gtin, $2, v.raw_data::jsonb, v.mapped_data::jsonb, $3, false, 'unprocessed', now()
|
||||
SELECT $1, v.gtin, $2, v.raw_data::jsonb, v.mapped_data::jsonb, $3, false, 'unprocessed', now(),
|
||||
jsonb_build_object($7::text, v.mapped_data::jsonb)
|
||||
FROM unnest($4::text[], $5::text[], $6::text[]) AS v(gtin, raw_data, mapped_data)
|
||||
ON CONFLICT (company_id, gtin) DO UPDATE SET
|
||||
feed_id = EXCLUDED.feed_id,
|
||||
raw_data = EXCLUDED.raw_data,
|
||||
mapped_data = EXCLUDED.mapped_data,
|
||||
raw_data = CASE WHEN $8 THEN EXCLUDED.raw_data ELSE raw_products.raw_data END,
|
||||
mapped_data = CASE WHEN $8 THEN EXCLUDED.mapped_data ELSE raw_products.mapped_data END,
|
||||
source_localizations = jsonb_set(COALESCE(raw_products.source_localizations,'{}'::jsonb),ARRAY[$7::text],EXCLUDED.mapped_data,true),
|
||||
sync_job_id = EXCLUDED.sync_job_id,
|
||||
is_processed = false,
|
||||
processing_status = 'unprocessed',
|
||||
is_processed = CASE WHEN $8 THEN false ELSE raw_products.is_processed END,
|
||||
processing_status = CASE WHEN $8 THEN 'unprocessed' ELSE raw_products.processing_status END,
|
||||
updated_at = now()`
|
||||
upsertSQLTouchSet = `
|
||||
UPDATE raw_products SET sync_job_id = $3, feed_id = $4, updated_at = now()
|
||||
@@ -468,11 +486,12 @@ const (
|
||||
upsertSQLUpdateSet = `
|
||||
UPDATE raw_products AS r SET
|
||||
feed_id = $2,
|
||||
raw_data = v.raw_data::jsonb,
|
||||
mapped_data = v.mapped_data::jsonb,
|
||||
raw_data = CASE WHEN $8 THEN v.raw_data::jsonb ELSE r.raw_data END,
|
||||
mapped_data = CASE WHEN $8 THEN v.mapped_data::jsonb ELSE r.mapped_data END,
|
||||
source_localizations = jsonb_set(COALESCE(r.source_localizations,'{}'::jsonb),ARRAY[$7::text],v.mapped_data::jsonb,true),
|
||||
sync_job_id = $3,
|
||||
is_processed = false,
|
||||
processing_status = 'unprocessed',
|
||||
is_processed = CASE WHEN $8 THEN false ELSE r.is_processed END,
|
||||
processing_status = CASE WHEN $8 THEN 'unprocessed' ELSE r.processing_status END,
|
||||
updated_at = now()
|
||||
FROM unnest($4::uuid[], $5::text[], $6::text[]) AS v(id, raw_data, mapped_data)
|
||||
WHERE r.id = v.id AND r.company_id = $1`
|
||||
@@ -499,7 +518,7 @@ type upsertOp struct {
|
||||
// can be applied with set-based UNNEST statements (≤3) in one pgx.Batch round-trip.
|
||||
// Touch keeps is_processed/processing_status when mapped_data is equal (canonical JSON).
|
||||
// Updates/inserts stamp mapped_data._sync_changes for seller filters (price/stock/…).
|
||||
func classifyUpsertOps(chunk []pendingProduct, existing map[string]existingProduct) (ops []upsertOp, skipped int) {
|
||||
func classifyUpsertOps(chunk []pendingProduct, existing map[string]existingProduct, locale ...syncLocale) (ops []upsertOp, skipped int) {
|
||||
ops = make([]upsertOp, 0, len(chunk))
|
||||
for _, p := range chunk {
|
||||
rawJSON, err := json.Marshal(p.RawData)
|
||||
@@ -523,17 +542,28 @@ func classifyUpsertOps(chunk []pendingProduct, existing map[string]existingProdu
|
||||
continue
|
||||
}
|
||||
// Category (and similar extras) live outside feed mappings — keep them.
|
||||
cleanMapped = preserveSyncedExtras(ex.MappedData, cleanMapped)
|
||||
existingMapped := ex.MappedData
|
||||
if len(locale) > 0 && !locale[0].primary {
|
||||
var all map[string]json.RawMessage
|
||||
if json.Unmarshal(ex.SourceLocalizations, &all) == nil {
|
||||
if val, exists := all[locale[0].code]; exists {
|
||||
existingMapped = val
|
||||
} else {
|
||||
existingMapped = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
cleanMapped = preserveSyncedExtras(existingMapped, cleanMapped)
|
||||
plainJSON, err := json.Marshal(cleanMapped)
|
||||
if err != nil {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
if bytesEqualJSON(stripSyncChangesBytes(ex.MappedData), plainJSON) {
|
||||
if bytesEqualJSON(stripSyncChangesBytes(existingMapped), plainJSON) {
|
||||
ops = append(ops, upsertOp{kind: upsertOpTouch, id: ex.ID})
|
||||
continue
|
||||
}
|
||||
changes := detectMappedChanges(ex.MappedData, cleanMapped)
|
||||
changes := detectMappedChanges(existingMapped, cleanMapped)
|
||||
withFlags := withSyncChanges(cleanMapped, changes)
|
||||
mappedJSON, err := json.Marshal(withFlags)
|
||||
if err != nil {
|
||||
@@ -615,7 +645,11 @@ func (s *Service) upsertChunk(ctx context.Context, companyID, feedID, jobID uuid
|
||||
return 0, 0, 0, deltas, err
|
||||
}
|
||||
|
||||
ops, skipped := classifyUpsertOps(chunk, existing)
|
||||
locale, ok := ctx.Value(syncLocaleKey{}).(syncLocale)
|
||||
if !ok {
|
||||
locale = syncLocale{code: company.LoadLanguage(ctx, s.Pool, companyID), primary: true}
|
||||
}
|
||||
ops, skipped := classifyUpsertOps(chunk, existing, locale)
|
||||
if len(ops) == 0 {
|
||||
deltas.Skipped = skipped
|
||||
return 0, 0, skipped, deltas, nil
|
||||
@@ -634,7 +668,7 @@ func (s *Service) upsertChunk(ctx context.Context, companyID, feedID, jobID uuid
|
||||
mappedCol[i] = string(op.mappedJSON)
|
||||
deltas.addChanges(op.changes)
|
||||
}
|
||||
batch.Queue(upsertSQLInsertSet, companyID, feedID, jobID, gtinCol, rawCol, mappedCol)
|
||||
batch.Queue(upsertSQLInsertSet, companyID, feedID, jobID, gtinCol, rawCol, mappedCol, locale.code, locale.primary)
|
||||
queued++
|
||||
}
|
||||
if len(touches) > 0 {
|
||||
@@ -655,7 +689,7 @@ func (s *Service) upsertChunk(ctx context.Context, companyID, feedID, jobID uuid
|
||||
mappedCol[i] = string(op.mappedJSON)
|
||||
deltas.addChanges(op.changes)
|
||||
}
|
||||
batch.Queue(upsertSQLUpdateSet, companyID, feedID, jobID, ids, rawCol, mappedCol)
|
||||
batch.Queue(upsertSQLUpdateSet, companyID, feedID, jobID, ids, rawCol, mappedCol, locale.code, locale.primary)
|
||||
queued++
|
||||
}
|
||||
|
||||
@@ -669,7 +703,7 @@ func (s *Service) upsertChunk(ctx context.Context, companyID, feedID, jobID uuid
|
||||
// Content inserts/updates stamp raw as unprocessed — drop catalog rows so
|
||||
// processed_products cannot outlive that reset (matches resetRawProducts).
|
||||
// Touches keep processing_status and must not invalidate catalog.
|
||||
if len(inserts) > 0 || len(updates) > 0 {
|
||||
if (len(inserts) > 0 || len(updates) > 0) && locale.primary {
|
||||
invalidateIDs := make([]uuid.UUID, 0, len(updates))
|
||||
for _, op := range updates {
|
||||
invalidateIDs = append(invalidateIDs, op.id)
|
||||
@@ -704,7 +738,7 @@ func (s *Service) loadExistingByGTIN(ctx context.Context, companyID uuid.UUID, g
|
||||
return out, nil
|
||||
}
|
||||
rows, err := s.Pool.Query(ctx, `
|
||||
SELECT id, gtin, mapped_data FROM raw_products
|
||||
SELECT id, gtin, mapped_data, COALESCE(source_localizations,'{}'::jsonb) FROM raw_products
|
||||
WHERE company_id = $1 AND gtin = ANY($2::text[])`, companyID, gtins)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -713,7 +747,7 @@ func (s *Service) loadExistingByGTIN(ctx context.Context, companyID uuid.UUID, g
|
||||
for rows.Next() {
|
||||
var ex existingProduct
|
||||
var gtin string
|
||||
if err := rows.Scan(&ex.ID, >in, &ex.MappedData); err != nil {
|
||||
if err := rows.Scan(&ex.ID, >in, &ex.MappedData, &ex.SourceLocalizations); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[gtin] = ex
|
||||
|
||||
Reference in new issue
Block a user