package feeds import ( "context" "encoding/json" "github.com/google/uuid" ) const upsertFeedRowsSQL = ` INSERT INTO raw_product_feed_rows (company_id, gtin, feed_id, raw_data, mapped_data, content_hash) SELECT $1, v.gtin, $2, v.raw_data::jsonb, v.mapped_data::jsonb, v.content_hash FROM unnest($3::text[], $4::text[], $5::text[], $6::text[]) AS v(gtin, raw_data, mapped_data, content_hash) ON CONFLICT (company_id, gtin, feed_id) DO UPDATE SET raw_data = EXCLUDED.raw_data, mapped_data = EXCLUDED.mapped_data, content_hash = EXCLUDED.content_hash, updated_at = now() WHERE raw_product_feed_rows.content_hash IS DISTINCT FROM EXCLUDED.content_hash` // mergeChunkAcrossFeeds stores this feed's row for every EAN in the chunk, loads the // rows other feeds hold for the same EANs, and rewrites each pending product's // MappedData with the merge of all of them. It also fills the field-source metadata // that upsertChunk writes to raw_products. A product only this feed carries is left // exactly as the feed produced it. func (s *Service) mergeChunkAcrossFeeds(ctx context.Context, companyID, feedID uuid.UUID, chunk []pendingProduct) error { if len(chunk) == 0 { return nil } gtins := make([]string, len(chunk)) rawCol := make([]string, len(chunk)) mappedCol := make([]string, len(chunk)) hashCol := make([]string, len(chunk)) for i, p := range chunk { gtins[i] = p.GTIN rb, err := json.Marshal(p.RawData) if err != nil { return err } mb, err := json.Marshal(stripSyncChanges(p.MappedData)) if err != nil { return err } rawCol[i], mappedCol[i], hashCol[i] = string(rb), string(mb), p.ContentHash } if _, err := s.Pool.Exec(ctx, upsertFeedRowsSQL, companyID, feedID, gtins, rawCol, mappedCol, hashCol); err != nil { return err } // Rows other feeds hold for these EANs. rows, err := s.Pool.Query(ctx, ` SELECT r.gtin, r.feed_id, COALESCE(f.name, ''), r.mapped_data FROM raw_product_feed_rows r JOIN input_feeds f ON f.id = r.feed_id AND f.company_id = r.company_id WHERE r.company_id = $1 AND r.gtin = ANY($2::text[]) AND r.feed_id <> $3`, companyID, gtins, feedID) if err != nil { return err } defer rows.Close() others := map[string][]feedRowInput{} for rows.Next() { var gtin, name string var fid uuid.UUID var raw []byte if err := rows.Scan(>in, &fid, &name, &raw); err != nil { return err } var mapped map[string]any if err := json.Unmarshal(raw, &mapped); err != nil || mapped == nil { continue } others[gtin] = append(others[gtin], feedRowInput{FeedID: fid, FeedName: name, Mapped: mapped}) } if err := rows.Err(); err != nil { return err } var thisFeedName string if err := s.Pool.QueryRow(ctx, `SELECT COALESCE(name, '') FROM input_feeds WHERE id = $1 AND company_id = $2`, feedID, companyID).Scan(&thisFeedName); err != nil { return err } for i := range chunk { p := &chunk[i] all := append([]feedRowInput{{FeedID: feedID, FeedName: thisFeedName, Mapped: p.MappedData}}, others[p.GTIN]...) merged, sources := mergeFeedRows(all) if len(all) > 1 { merged["gtin"] = p.GTIN p.MappedData = merged } if sb, err := json.Marshal(sources); err == nil { p.Sources = string(sb) } if ib, err := json.Marshal(feedIDsOf(all)); err == nil { p.FeedIDsJSON = string(ib) } p.PrimaryFeed = sources.Primary.FeedID } return nil }