Repository navigation
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
gkhngyk
left a comment
There was a problem hiding this comment.
Thanks for this. Verdict: changes requested. Reusing classifyMerchant works well, the thresholds match the issue, and CI is green. These are the changes we need before merging:
-
Migration number.
supabase/migrations/00204_shopping_merchant_classification.sql: open PRs already use 00204–00222. Please rename it to00223_shopping_merchant_classification.sql, or the next free number when you rebase. Then update the "Run after migration 00204" note inserver/src/scripts/backfill-shopping-card-merchants.js:4and rerunbash supabase/build-schema.sh. -
"This week" is a partial week.
00204_…sql:46:date_trunc('week', …)makes the current bucket the calendar week so far. On a Monday the share rests on a single day of runs. A brand with 30+ own cards a day clears the 30-card floor on day one, so one run without Amazon cards fires the alert: four weeks at 31% followed by 0/30 returns an alert today. Please use rolling 7-day windows ending atp_as_of, so every bucket is a full week:with weeks as ( select i, p_as_of - (i + 1) * interval '7 days' as from_ts, p_as_of - i * interval '7 days' as to_ts from generate_series(0, 4) as i ) -- join on c.created_at >= w.from_ts and c.created_at < w.to_ts -- and return (w.from_ts at time zone 'UTC')::date as week_start
The returned dates stay 7 days apart, so
isConsecutiveWeeksstill passes. Please also update the comments inshopping-amazon-share-alert.ts:13-16and above the effect inpage.tsx. -
RPC security.
00204_…sql:42and:72: our newer reads usesecurity definerwith an explicit organization check and no anon access, ascitation_gap_domains_dailydoes in00203_citation_gap_rollups.sql. As written, Supabase's default privileges also giveanonandpublicEXECUTE on the function. Please follow that pattern (plpgsql with#variable_conflict use_column, as in 00203):security definer set search_path to 'public' as $$ #variable_conflict use_column begin if auth.role() is distinct from 'service_role' and not exists ( select 1 from public.brands b join public.profiles pf on pf.organization_id = b.organization_id where b.id = p_brand_id and pf.id = auth.uid() ) then return; end if; return query ...; end; $$; revoke all on function public.shopping_amazon_share_weekly(uuid, timestamptz) from public, anon; grant execute on function public.shopping_amazon_share_weekly(uuid, timestamptz) to authenticated, service_role; notify pgrst, 'reload schema';
-
The 10-point boundary.
web/src/lib/shopping-amazon-share-alert.ts:64-68: in JS,0.18 - 0.08is0.09999999999999999, so a drop from 18% to 8% doesn't fire, although it is exactly 10 points and below half. Please compare with a small tolerance, e.g.absoluteDrop < AMAZON_SHARE_MIN_ABSOLUTE_DROP - 1e-9, and add that case as a fixture. -
Backfill paging.
server/src/scripts/backfill-shopping-card-merchants.js:82-100: running it twice gives the same result. However, offset paging updates every row again on each run, and it can skip rows if older cards are deleted mid-run. Please page by id and pick up only unclassified rows, so a rerun resumes where the last one stopped:let query = supabaseAdmin .from('prompt_result_shopping_cards') .select('id, brand_id, merchant_url') .is('merchant_kind', null) .not('merchant_url', 'is', null) .order('id', { ascending: true }) .limit(BATCH_SIZE); if (lastId) query = query.gt('id', lastId);
An optional
--allflag that drops themerchant_kindfilter would let us reclassify after a brand's domains change. Also, at line 40 please cache the promise indomainCacherather than the result. Then 20 rows of one brand in a slice make one request instead of 20. -
Small ones.
web/src/lib/actions/shopping.ts:184-193: the new function sits betweengetShoppingFilterOptions's doc comment and its declaration. Please move it above that comment.web/src/types/supabase.ts:1983: theUpdatetype lacksmerchant_kind,marketplaceandlisting_id. Please regenerate the types, or add them in alphabetical order as the generator does.page.tsx:258-260: amber is right for a warning. Please match the limit note inprompts/_suggestions-card.tsx(amber background withdark:variants) so the row stays consistent in dark mode.
gkhngyk
left a comment
There was a problem hiding this comment.
Thanks for this. Verdict: approved once the description is updated. Every point from the last review is fixed, the branch merges cleanly with main, and CI is green on the new head.
What I checked:
- Migration: it is now
00225_shopping_merchant_classification.sql, which is still free.schema.sqlmatches whatbuild-schema.shproduces from the merged tree. - The RPC:
- It uses rolling seven-day windows ending at
p_as_of, so a partial week can't trigger the alert any more. - It is
security definerwith the organization check, andanon/publicno longer have access. - It responds in 8–42 ms on our largest brands.
- It uses rolling seven-day windows ending at
- The rule: it uses the tolerance at the 10-point boundary, and the 18% to 8% fixture fires as expected.
- Backfill: it pages by id over unclassified rows, has the
--allflag and caches the domain promise. - The rest: the doc comment is in its place, the
Updatetype has the new columns, and the alert row matches our amber warning style in both themes. The new "last seven days" copy fits the windows.
One small thing before we merge:
- PR description. It still says migration 00204, "the current UTC week", and has the two validation boxes unticked. Please update it to 00225 and rolling seven-day windows, since it becomes the squash commit message. CI has confirmed the typecheck and the schema check, so both boxes can be ticked.
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good. The description now says 00225 and rolling seven-day windows, and the validation boxes are ticked. Since the last review, the feature code, the migration and the RPC are unchanged. The later commits are merges of main, the schema.sql separator fix, the supabase.ts field order and a comment rewrap.
00225 is applied on the hosted database. shopping_amazon_share_weekly returns its five weeks for a member in 17 ms and no rows for an outsider. Tested on top of the current main: web and server tests, tsc, lint and Prettier pass, and schema.sql matches a rebuild.
We'll run the merchant backfill after the next server deploy. Thanks!
Summary
p_as_offor classified cards on the brand's own products.Related issue
Closes #1284
Depends on the merchant-classification prerequisite also described in #1277; this PR implements only the storage/backfill needed for the alert, not the separate full destination breakdown and price comparison.
Type of change
Validation
npm run lintnpm run format:checknpm testAfter migration
00225_shopping_merchant_classification.sqlis applied, runnode src/scripts/backfill-shopping-card-merchants.jsfromserver/to classify historical cards so existing rolling windows can contribute to the alert. Use--allto reclassify all rows with merchant URLs after brand-domain changes.