Skip to content

Add canonicalItemId to items and mappings; propagate through API and UI ordering#61

Merged
13Bytes merged 10 commits into
mainfrom
codex/order-items-by-user-order-count
May 12, 2026
Merged

Add canonicalItemId to items and mappings; propagate through API and UI ordering#61
13Bytes merged 10 commits into
mainfrom
codex/order-items-by-user-order-count

Conversation

@13Bytes

@13Bytes 13Bytes commented May 7, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Preserve logical identity of items across immutable copies so orders can be grouped by the original (canonical) item.
  • Expose canonical identity to API and transactions so ordering metrics and copy semantics remain consistent.

Description

  • Add canonicalItemId columns to Item and ItemCategoryMapping, backfill existing rows, enforce NOT NULL via recreation in prisma/migrations/20260507120000_add_canonical_item_id/migration.sql, and add indexes on canonicalItemId.
  • Update prisma/schema.prisma to include canonicalItemId on Item and ItemCategoryMapping and add @@index([canonicalItemId]) on both models.
  • Modify server router src/server/api/routers/items.ts to: import randomUUID, set canonicalItemId when creating items (and use it as the new item id), preserve canonicalItemId when creating immutable copies in updateItem, include canonicalItemId on created ItemCategoryMapping entries in buyItem, and change getBuyable to include userOrderCount by grouping ItemCategoryMapping on canonicalItemId for the current user.
  • Adjust UI sorting in src/pages/buy.tsx to sort items by userOrderCount (desc) and then by name.

Testing

  • No automated tests were run as part of this change.

Codex Task

@13Bytes 13Bytes marked this pull request as ready for review May 12, 2026 22:14
Copilot AI review requested due to automatic review settings May 12, 2026 22:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 canonicalItemId to Prisma models + SQLite migration to backfill and index it.
  • Propagate canonicalItemId through item creation/copying and purchase mapping, and compute per-user purchase metrics in getBuyable.
  • Update UI to support sorting modes (persisted via localStorage) and pass allowOverdraw through Balance call 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 thread src/pages/buy.tsx
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"]
@13Bytes 13Bytes merged commit f63ecde into main May 12, 2026
5 checks passed
@13Bytes 13Bytes deleted the codex/order-items-by-user-order-count branch May 12, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants