Add canonicalItemId to items and mappings; propagate through API and UI ordering#61
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a stable “canonical” identity for items across immutable copies by adding canonicalItemId to Item and ItemCategoryMapping, then uses that canonical identity to drive user-centric ordering signals (most bought / recently bought) on the buy page. It also updates balance display behavior to account for allowOverdraw, and adjusts the screenshot workflow/scripts.
Changes:
- Add
canonicalItemIdto Prisma models + SQLite migration to backfill and index it. - Propagate
canonicalItemIdthrough item creation/copying and purchase mapping, and compute per-user purchase metrics ingetBuyable. - Update UI to support sorting modes (persisted via localStorage) and pass
allowOverdrawthroughBalancecall sites.
Reviewed changes
Copilot reviewed 14 out of 15 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/server/api/routers/items.ts | Sets/preserves canonicalItemId, writes it to purchase mappings, and computes user purchase aggregates for the buy page. |
| src/pages/buy.tsx | Adds persisted sort-mode selector and sorting logic using userOrderCount / userLastBoughtAt. |
| prisma/schema.prisma | Adds canonicalItemId fields and indexes to Item and ItemCategoryMapping. |
| prisma/migrations/20260507120000_add_canonical_item_id/migration.sql | Rebuilds/backfills tables to enforce canonicalItemId NOT NULL and create indexes. |
| src/components/General/Balance.tsx | Extends Balance with allowOverdraw-aware coloring. |
| src/pages/top-up.tsx | Passes allowOverdraw into Balance (plus formatting tweaks). |
| src/pages/split.tsx | Passes allowOverdraw into Balance. |
| src/pages/me/index.tsx | Displays “Kreditwürdig” badge and passes allowOverdraw into Balance. |
| src/components/PageComponents/UserOverview.tsx | Passes allowOverdraw into Balance for admin user list. |
| src/components/Layout/Header.tsx | Passes allowOverdraw into header balance display. |
| scripts/screenshots/seed.ts | Seeds canonicalItemId for items and purchase mappings. |
| .github/workflows/readme-screenshots.yml | Switches workflow to npm run screenshots:ci. |
| package.json | Adjusts screenshot/seed scripts and adds db:migrate:prod. |
| package-lock.json | Lockfile updates related to script changes. |
| README.md | Updates ToDo list entry. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+45
to
+49
| const recentOrders = await ctx.prisma.itemCategoryMapping.findMany({ | ||
| where: { Transaction: { userId: ctx.session.user.id, canceled: false, type: 0 } }, | ||
| select: { | ||
| canonicalItemId: true, | ||
| Transaction: { |
| } else if (props.balance < -150) { | ||
| color = "text-amber-700" | ||
| } else if (props.balance < 0) { | ||
| color = "text-blue-grey-600" |
Comment on lines
+12
to
+16
| type SortMode = "recent" | "alphabetic" | "mostBought" | ||
|
|
||
| const SORT_MODE_STORAGE_KEY = "buyPageSortMode" | ||
| const SORT_MODE_STORAGE_EVENT = "buyPageSortModeChange" | ||
| const sortModes: SortMode[] = ["recent", "alphabetic", "mostBought"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Description
canonicalItemIdcolumns toItemandItemCategoryMapping, backfill existing rows, enforceNOT NULLvia recreation inprisma/migrations/20260507120000_add_canonical_item_id/migration.sql, and add indexes oncanonicalItemId.prisma/schema.prismato includecanonicalItemIdonItemandItemCategoryMappingand add@@index([canonicalItemId])on both models.src/server/api/routers/items.tsto: importrandomUUID, setcanonicalItemIdwhen creating items (and use it as the new itemid), preservecanonicalItemIdwhen creating immutable copies inupdateItem, includecanonicalItemIdon createdItemCategoryMappingentries inbuyItem, and changegetBuyableto includeuserOrderCountby groupingItemCategoryMappingoncanonicalItemIdfor the current user.src/pages/buy.tsxto sort items byuserOrderCount(desc) and then by name.Testing
Codex Task