From b471f2914fda83eb71c37183e13f211ceacc8f8b Mon Sep 17 00:00:00 2001 From: Clarence Date: Tue, 1 Dec 2026 10:59:06 +0800 Subject: [PATCH 1/4] fix(plan): default program Stamp card, Merqo account link, legacy starter as Pro Co-Authored-By: Claude Sonnet 5.5 --- CHANGELOG.md | 7 +++ docs/DEPLOY.md | 2 +- src/app/api/merqo/README.md | 2 +- src/app/api/merqo/vendor-provision/README.md | 2 +- src/app/dashboard/plan/page.tsx | 18 +++++-- src/app/dashboard/plan/plan-page.dom.test.tsx | 5 +- src/lib/merqo-entitlement-contract.test.ts | 47 ++++++++++++++++++ src/lib/merqo-entitlement-contract.ts | 9 ++++ .../migrations/0073_default_program_name.sql | 48 +++++++++++++++++++ supabase/migrations/README.md | 1 + supabase/tests/rls.test.sql | 11 +++-- 11 files changed, 140 insertions(+), 12 deletions(-) create mode 100644 src/lib/merqo-entitlement-contract.test.ts create mode 100644 supabase/migrations/0073_default_program_name.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index a0f4f1b7..f82be8e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to loopkit are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). +## [Unreleased] + +### Fixed + +- New push-provisioned vendors get a default stamp program named "Stamp card" instead of "Starter" (migration 0073), which read as the retired pricing tier. Existing programs keep their names. +- The Plan page links "Merqo account" to the Merqo dashboard, and the legacy "starter" entitlement tier is normalised to Pro instead of being rejected for loopkit. + ## [0.6.0] - 2026-12-01 ### Added diff --git a/docs/DEPLOY.md b/docs/DEPLOY.md index d9aee5c7..f0bbe918 100644 --- a/docs/DEPLOY.md +++ b/docs/DEPLOY.md @@ -205,7 +205,7 @@ Do the steps in order: **A (Supabase) → B (Vercel) → C (attach to merqo)**. - apply `0032_loopkit_provision_default_program.sql` — adds `provision_default_program(p_vendor_id)`, a service-role-only SECURITY DEFINER function (never granted to `authenticated`) that seeds a default - "Starter" stamp program for a push-provisioned vendor, since + "Stamp card" stamp program for a push-provisioned vendor, since `create_program` is keyed on the calling session's `auth.uid()` and can't run on a vendor's behalf. Advisory-lock-guarded against a double-provision race, idempotent on `loopkit.programs`. Backs diff --git a/src/app/api/merqo/README.md b/src/app/api/merqo/README.md index 1e7bc73f..fd18b214 100644 --- a/src/app/api/merqo/README.md +++ b/src/app/api/merqo/README.md @@ -13,7 +13,7 @@ qkit earn-config, and push-provisions a vendor onto loopkit. - `vendor-activity/` — `GET` endpoint resolving one vendor's programs, cards, and stamp/redeem activity by email, for merqo's cross-kit `/admin/vendors/[email]` detail view. -- `vendor-provision/` — `POST` endpoint that creates a free-tier `vendors` row (and a default "Starter" stamp program) for a vendor id the Merqo hub is one-click-activating on loopkit. +- `vendor-provision/` — `POST` endpoint that creates a free-tier `vendors` row (and a default "Stamp card" stamp program) for a vendor id the Merqo hub is one-click-activating on loopkit. - `vendor-status/` ## Connectivity diff --git a/src/app/api/merqo/vendor-provision/README.md b/src/app/api/merqo/vendor-provision/README.md index aebb0bde..98c97729 100644 --- a/src/app/api/merqo/vendor-provision/README.md +++ b/src/app/api/merqo/vendor-provision/README.md @@ -2,7 +2,7 @@ ## Purpose -Push-provisioning endpoint: Merqo hub calls this to activate loopkit for a vendor who hasn't signed up directly, keyed on their existing (shared) `auth.users.id`. Creates the vendor row AND a default "Starter" stamp program (loopkit's own `create_program` RPC can't be used here — see `provision_default_program`'s migration comment) so the vendor is genuinely active immediately, not left without a program. +Push-provisioning endpoint: Merqo hub calls this to activate loopkit for a vendor who hasn't signed up directly, keyed on their existing (shared) `auth.users.id`. Creates the vendor row AND a default "Stamp card" stamp program (loopkit's own `create_program` RPC can't be used here — see `provision_default_program`'s migration comment) so the vendor is genuinely active immediately, not left without a program. ## Contents diff --git a/src/app/dashboard/plan/page.tsx b/src/app/dashboard/plan/page.tsx index a0cf6530..24e15648 100644 --- a/src/app/dashboard/plan/page.tsx +++ b/src/app/dashboard/plan/page.tsx @@ -13,7 +13,12 @@ import { Badge } from "@merqo/ui/primitives"; // merqo-ui's tierLabel lives in a client-only module and crashes when called // from a Server Component, so this page keeps its own copy of the labels. -const TIER_LABEL = { free: "Free", starter: "Starter", pro: "Pro" } as const; +const TIER_LABEL = { free: "Free", pro: "Pro" } as const; + +// The ladder only accepts "qkit" or "all_access" as a highlight and the +// catalog has no loopkit-only plan; the qkit plan includes full loopkit, so +// a Pro vendor sits on that entry. Loopkit's own wire tier is "paid". +const PAID_SLUG = "qkit"; export const metadata = { title: "Plan" }; @@ -77,13 +82,20 @@ export default async function PlanPage({ {pro ? "You're on Pro, unlimited loyalty programs are unlocked. " : "You're on Free, with one active loyalty program. "} - Change plan from your Merqo account. + Change plan from your{" "} + + Merqo account + + . {catalog ? ( diff --git a/src/app/dashboard/plan/plan-page.dom.test.tsx b/src/app/dashboard/plan/plan-page.dom.test.tsx index b4abbf33..8eea7ece 100644 --- a/src/app/dashboard/plan/plan-page.dom.test.tsx +++ b/src/app/dashboard/plan/plan-page.dom.test.tsx @@ -82,9 +82,8 @@ describe("PlanPage", () => { expect(card).toHaveTextContent("qkit"); expect(card).not.toHaveTextContent("All-Access"); expect(screen.queryByText(/event pass/i)).not.toBeInTheDocument(); - expect( - screen.getByText(/change plan from your merqo account/i), - ).toBeInTheDocument(); + const link = screen.getByRole("link", { name: /merqo account/i }); + expect(link).toHaveAttribute("href", expect.stringMatching(/\/dashboard$/)); }); it("sends the kit bearer to the plan catalog when the secret is set", async () => { diff --git a/src/lib/merqo-entitlement-contract.test.ts b/src/lib/merqo-entitlement-contract.test.ts new file mode 100644 index 00000000..3ba80a65 --- /dev/null +++ b/src/lib/merqo-entitlement-contract.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from "vitest"; +import { entitlementPayloadSchema } from "./merqo-entitlement-contract"; +import { isPaidEntitlement } from "./entitlement-cache"; + +const base = { + email: "v@example.com", + founder: false, + stats_window: "90d", + event_pass: null, + valid_until: null, + issued_at: "2026-12-01T00:00:00Z", +}; + +describe("entitlementPayloadSchema legacy starter tier", () => { + it("normalises starter to paid for loopkit so the vendor is Pro", () => { + const parsed = entitlementPayloadSchema.parse({ + ...base, + kit: "loopkit", + tier: "starter", + }); + expect(parsed.tier).toBe("paid"); + expect(isPaidEntitlement(parsed)).toBe(true); + }); + + it("normalises starter to qkit for the qkit kit", () => { + const parsed = entitlementPayloadSchema.parse({ + ...base, + kit: "qkit", + tier: "starter", + }); + expect(parsed.tier).toBe("qkit"); + }); + + it("still accepts free and rejects the qkit tier for loopkit", () => { + expect( + entitlementPayloadSchema.parse({ ...base, kit: "loopkit", tier: "free" }) + .tier, + ).toBe("free"); + expect( + entitlementPayloadSchema.safeParse({ + ...base, + kit: "loopkit", + tier: "qkit", + }).success, + ).toBe(false); + }); +}); diff --git a/src/lib/merqo-entitlement-contract.ts b/src/lib/merqo-entitlement-contract.ts index 57be3725..92447ef3 100644 --- a/src/lib/merqo-entitlement-contract.ts +++ b/src/lib/merqo-entitlement-contract.ts @@ -6,6 +6,10 @@ const isoDate = z.string().datetime({ offset: true }); * Wire contract for merqo's entitlement push (POST /api/merqo/entitlement). * Copied verbatim from merqo's `src/lib/entitlements/contract.ts`; keep the * two in step. + * + * "starter" is a retired tier still accepted on the wire so deploy order + * across repos does not matter; it is normalised to the Pro path (the same + * as qkit) before the per-kit check, so it never lands as Free. */ export const entitlementPayloadSchema = z .object({ @@ -18,6 +22,11 @@ export const entitlementPayloadSchema = z valid_until: isoDate.nullable(), issued_at: isoDate, }) + .transform((p) => + p.tier === "starter" + ? { ...p, tier: p.kit === "qkit" ? ("qkit" as const) : ("paid" as const) } + : p, + ) .refine( (p) => p.kit === "qkit" diff --git a/supabase/migrations/0073_default_program_name.sql b/supabase/migrations/0073_default_program_name.sql new file mode 100644 index 00000000..dac78aca --- /dev/null +++ b/supabase/migrations/0073_default_program_name.sql @@ -0,0 +1,48 @@ +-- 0073: the default program seeded for push-provisioned vendors is renamed +-- from "Starter" to "Stamp card". "Starter" is the retired pricing tier and +-- read as a plan name on the vendor's dashboard. Only the inserted name +-- changes; signature, security definer, search_path, advisory lock, +-- idempotency and grants are identical to 0032. Existing programs keep +-- their names. +create or replace function loopkit.provision_default_program(p_vendor_id uuid) +returns uuid +language plpgsql security definer set search_path = '' as $$ +declare v_id uuid; +begin + -- Transaction-scoped advisory lock keyed on the vendor id: closes a TOCTOU + -- race where two concurrent calls for the same vendor (e.g. two browser + -- tabs double-clicking "Activate") both pass the `where not exists` check + -- below under READ COMMITTED and both insert, creating two active default + -- programs. Released automatically at transaction end (pg_advisory_xact_lock); + -- does not change this function's signature or return behavior. + perform pg_advisory_xact_lock(hashtextextended(p_vendor_id::text, 0)); + + -- config must duplicate stamps_required/reward_text (matching what + -- buildProgramFields in src/lib/program.ts produces for a stamp card): + -- resolveStampConfig (src/lib/engine/index.ts) reads programs.config as-is + -- whenever it has any keys at all, with no fallback to the table columns — + -- a config that sets only points_per_visit/variant would leave + -- stamps_required/reward_text undefined for every engine computation. + -- + -- Idempotent keyed on loopkit.programs, NOT on loopkit.vendors: the route + -- caller's own vendors-row insert can no-op (23505) for reasons unrelated + -- to program state (e.g. a vendor who signed up email/password-first, or + -- who already has a real, customized program from /setup's create_program + -- path) — a vendors-row conflict must never imply "already provisioned". + -- `where not exists` makes this call safe to run unconditionally on every + -- provision attempt; a null return means the vendor already had a program + -- and nothing was created, not an error. + insert into loopkit.programs + (vendor_id, type, name, stamps_required, reward_text, config, active) + select p_vendor_id, 'stamp', 'Stamp card', 10, '1 free item', + '{"stamps_required": 10, "reward_text": "1 free item", "points_per_visit": 1, "variant": "dots"}'::jsonb, + true + where not exists (select 1 from loopkit.programs where vendor_id = p_vendor_id) + returning id into v_id; + return v_id; +end; +$$; + +revoke all on function loopkit.provision_default_program(uuid) from public; +grant execute on function loopkit.provision_default_program(uuid) to service_role; +-- deliberately NOT granted to authenticated diff --git a/supabase/migrations/README.md b/supabase/migrations/README.md index 0938bf2e..5707096b 100644 --- a/supabase/migrations/README.md +++ b/supabase/migrations/README.md @@ -81,5 +81,6 @@ exception. - `0068_drop_vendor_pro.sql`: drops `loopkit.vendor_pro`. Apply `0066` to `0068` together. - `0069_drop_pricing.sql`: drops `loopkit.pricing`; merqo owns plans and pricing. - `0070_drop_upgrade_requests.sql`: drops `loopkit.upgrade_requests`; the upgrade inbox is gone. +- `0073_default_program_name.sql`: `provision_default_program` now names the seeded program "Stamp card" instead of "Starter" (retired tier name); existing programs are not renamed. Prepared audit migrations require database validation and rollout review before deployment. diff --git a/supabase/tests/rls.test.sql b/supabase/tests/rls.test.sql index 1e0ded1f..6e2cbc5f 100644 --- a/supabase/tests/rls.test.sql +++ b/supabase/tests/rls.test.sql @@ -12,7 +12,7 @@ -- Runs in ONE rolled-back transaction with inline fixtures (fixed UUIDs). begin; -select plan(87); +select plan(88); -- ── Fixtures (created under the default/superuser test role → RLS + grants -- are bypassed here, same as inserting via the table owner) ───────────────── @@ -200,7 +200,7 @@ select ok( -- provision_default_program idempotency (migration 0032 fix): keyed on -- loopkit.programs existence, not loopkit.vendors, so a vendor who already -- has ANY program (e.g. from /setup's create_program path, unrelated to --- this RPC) never gets a second "Starter" program silently added. +-- this RPC) never gets a second default program silently added. reset role; set local role service_role; @@ -219,6 +219,11 @@ select is( 1, 'vendor A now has exactly one program'); +select is( + (select name from loopkit.programs where vendor_id = '00000000-0000-0000-0000-00000000000a'), + 'Stamp card', + 'provisioned default program is named Stamp card'); + -- Vendor B: simulate an existing program created directly (NOT via this -- RPC — standing in for /setup's create_program path), then re-provision. insert into loopkit.programs (vendor_id, type, name, stamps_required, reward_text, config, active) @@ -233,7 +238,7 @@ select is( select is( (select count(*)::int from loopkit.programs where vendor_id = '00000000-0000-0000-0000-00000000000b'), 1, - 'vendor B still has exactly one program (no second Starter program added)'); + 'vendor B still has exactly one program (no second default program added)'); -- provision_default_program TOCTOU fix (migration 0032): a transaction-scoped -- advisory lock keyed on p_vendor_id closes the read-then-write race between From f065faaceebb4addfd0fd34ffd719e5cb997eac2 Mon Sep 17 00:00:00 2001 From: Clarence Date: Tue, 1 Dec 2026 20:18:42 +0800 Subject: [PATCH 2/4] test: raise vitest timeout so the pre-push gate survives a loaded host Co-Authored-By: Claude Sonnet 5.5 --- vitest.config.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/vitest.config.ts b/vitest.config.ts index a6e9a352..75c7b175 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -14,6 +14,9 @@ export default defineConfig({ test: { globals: true, maxWorkers: 2, + // jsdom form tests pass in under a second on a quiet machine but exceed the + // 5s default when the pre-push gate shares a loaded host. + testTimeout: 30000, environment: "node", // @merqo/ui entries import extensionless `next/server`, which Node's ESM // loader rejects; inlining lets Vite resolve it. From ab6ca966a0d0bef19e73b0504384b669619e1e58 Mon Sep 17 00:00:00 2001 From: Clarence Date: Tue, 1 Dec 2026 21:33:36 +0800 Subject: [PATCH 3/4] test: raise vitest hook timeout for the eslint config suite Co-Authored-By: Claude Sonnet 5.5 --- vitest.config.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/vitest.config.ts b/vitest.config.ts index 75c7b175..7628da60 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -17,6 +17,7 @@ export default defineConfig({ // jsdom form tests pass in under a second on a quiet machine but exceed the // 5s default when the pre-push gate shares a loaded host. testTimeout: 30000, + hookTimeout: 180000, environment: "node", // @merqo/ui entries import extensionless `next/server`, which Node's ESM // loader rejects; inlining lets Vite resolve it. From f0941b6cb04cfbe732e6e65a81dd454092190070 Mon Sep 17 00:00:00 2001 From: Clarence Date: Tue, 1 Dec 2026 22:13:09 +0800 Subject: [PATCH 4/4] docs: describe Stamp card, plan page link and starter normalisation in READMEs Co-Authored-By: Claude Sonnet 5.5 --- README.md | 2 ++ docs/README.md | 2 ++ src/app/dashboard/plan/README.md | 2 ++ src/lib/README.md | 2 ++ supabase/tests/README.md | 2 ++ 5 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 45da6235..03ca7e7e 100644 --- a/README.md +++ b/README.md @@ -551,3 +551,5 @@ Vendor-facing plan-limit copy states what the plan includes and never asks the v Route loading skeletons come from `@merqo/ui` (`ListSkeleton`, `StatTileSkeleton`, `DataTableSkeleton`) under `components/loading-header.tsx`. The counter and setup edit-impact dialogs use the shared `ConfirmDialog`, and the counter empty state uses `EmptyState`. The login and reset-password forms stay local because their zod and React Hook Form validation, wordmark shell and reset-sent notice are not covered by the shared forms. Touch targets on setup chips and overview program links reach 44px on coarse pointers, and visit bars are tappable. Customer-facing selects and buttons (`birthday-field.tsx`, `check-form.tsx`, dashboard `scan-button.tsx`) use the shared `@merqo/ui` Select and Button primitives with 44px touch targets. `/c` without a shop link shows a next-step message and a link home. + +New vendors get a default program named "Stamp card" (migration 0073); the plan page links to the Merqo account and treats the legacy "starter" tier as Pro. diff --git a/docs/README.md b/docs/README.md index d7712a16..afb8d100 100644 --- a/docs/README.md +++ b/docs/README.md @@ -17,3 +17,5 @@ spec/plan development history. ## Parent [loopkit](../README.md) + +Deploy notes refer to the default program new vendors receive as "Stamp card". diff --git a/src/app/dashboard/plan/README.md b/src/app/dashboard/plan/README.md index ae62503d..b82b8a1f 100644 --- a/src/app/dashboard/plan/README.md +++ b/src/app/dashboard/plan/README.md @@ -19,3 +19,5 @@ Vendor billing/plan page at `/dashboard/plan`, shows Free vs. Pro feature compar Each page exports its own `metadata.title`, shown through the root `loopkit | %s` template. UI copy is plain: no emoji, no em dashes, no contractions in errors. - `page.tsx` renders its title row with `PageHeader`; `loading.tsx` uses `ListSkeleton`. + +The "Merqo account" line is a link built from `merqoBaseUrl()`, and the pricing ladder highlights the paid slug. diff --git a/src/lib/README.md b/src/lib/README.md index a6acd500..c46e5f49 100644 --- a/src/lib/README.md +++ b/src/lib/README.md @@ -156,3 +156,5 @@ card updates are not a transactional snapshot across separate HTTP reads. `merqo-vendor-activity.ts`'s `computeVendorActivity(isPro, { programs, cards, events }, nowMs)` and `program-config.ts`'s `buildChanceConfig(variant, segments, { pityCeiling, rewardText, scratchCoverStyle })` take grouped arguments. Colour values for the mechanic art live in `tokens/`. Empty activity cells use "-" and errors spell out "could not". + +`merqo-entitlement-contract.ts` normalises the legacy "starter" tier to paid (loopkit) or qkit so such a vendor is never treated as Free. diff --git a/supabase/tests/README.md b/supabase/tests/README.md index 8dc0a16d..b6789636 100644 --- a/supabase/tests/README.md +++ b/supabase/tests/README.md @@ -39,3 +39,5 @@ RLS or concurrent transaction behavior. Report database execution separately. ## Parent [supabase](../README.md) + +`rls.test.sql` asserts the provisioned default program is named "Stamp card".