From 6aa86a3b68c24e7de9b3cfe81c4b47c61d4475bf Mon Sep 17 00:00:00 2001 From: Jose Selesan Date: Fri, 16 Oct 2026 10:12:11 -0300 Subject: [PATCH] feat: add collaborative shopping lists with items and invitations API: lists, items and invitations modules (list, create, get, update, set-primary, items create/list, invitation create/accept/list-pending), BetterAuth session helper, list membership, mailer and Prisma schema plus migrations. Web: lists overview, list detail (items) and list management (settings) views, item and invitation forms/hooks, sign-up and invitation accept pages. Shared: shopping list DTOs. Tooling: ignore the generated TanStack Router cache (apps/web/.tanstack). --- .env.example | 6 + .gitignore | 1 + AGENTS.md | 20 +- apps/api/.env.example | 6 + apps/api/package.json | 1 + .../20261015205940_init/migration.sql | 157 +++++++++ .../migration.sql | 20 ++ .../api/prisma/migrations/migration_lock.toml | 3 + apps/api/prisma/schema.prisma | 158 +++++++-- apps/api/src/app.ts | 4 + apps/api/src/lib/auth-session.ts | 42 +++ apps/api/src/lib/auth.ts | 15 + apps/api/src/lib/emails/invitation.ts | 52 +++ apps/api/src/lib/list-membership.ts | 58 ++++ apps/api/src/lib/mailer.ts | 46 +++ apps/api/src/lib/web-app-url.ts | 13 + .../src/modules/invitations/accept/route.ts | 32 ++ .../invitations/accept/use-case.test.ts | 133 ++++++++ .../modules/invitations/accept/use-case.ts | 71 +++++ .../src/modules/invitations/create/route.ts | 27 ++ .../src/modules/invitations/create/schema.ts | 9 + .../modules/invitations/create/use-case.ts | 154 +++++++++ apps/api/src/modules/invitations/index.ts | 13 + .../modules/invitations/list-pending/route.ts | 21 ++ .../invitations/list-pending/use-case.ts | 38 +++ apps/api/src/modules/lists/create/route.ts | 26 ++ apps/api/src/modules/lists/create/schema.ts | 8 + .../src/modules/lists/create/use-case.test.ts | 84 +++++ apps/api/src/modules/lists/create/use-case.ts | 57 ++++ apps/api/src/modules/lists/get/route.ts | 26 ++ apps/api/src/modules/lists/get/use-case.ts | 46 +++ apps/api/src/modules/lists/index.ts | 19 ++ .../src/modules/lists/items/create/route.ts | 32 ++ .../src/modules/lists/items/create/schema.ts | 10 + .../modules/lists/items/create/use-case.ts | 44 +++ apps/api/src/modules/lists/items/index.ts | 11 + .../api/src/modules/lists/items/list/route.ts | 26 ++ .../src/modules/lists/items/list/use-case.ts | 34 ++ apps/api/src/modules/lists/items/serialize.ts | 28 ++ apps/api/src/modules/lists/list/route.ts | 15 + apps/api/src/modules/lists/list/use-case.ts | 28 ++ apps/api/src/modules/lists/schema.ts | 8 + apps/api/src/modules/lists/serialize.ts | 79 +++++ .../src/modules/lists/set-primary/route.ts | 26 ++ .../lists/set-primary/use-case.test.ts | 65 ++++ .../src/modules/lists/set-primary/use-case.ts | 31 ++ apps/api/src/modules/lists/update/route.ts | 30 ++ apps/api/src/modules/lists/update/schema.ts | 8 + apps/api/src/modules/lists/update/use-case.ts | 49 +++ apps/api/src/types.ts | 18 +- apps/web/index.html | 30 +- apps/web/package.json | 1 - .../src/components/items/create-item-form.tsx | 55 ++++ apps/web/src/components/items/item-list.tsx | 93 ++++++ apps/web/src/components/layout/app-header.tsx | 21 +- .../web/src/components/layout/app-sidebar.tsx | 24 +- apps/web/src/components/layout/nav-user.tsx | 6 +- .../src/components/lists/create-list-form.tsx | 81 +++++ apps/web/src/components/lists/list-card.tsx | 55 ++++ apps/web/src/components/theme-provider.tsx | 91 +++++- apps/web/src/components/theme-toggle.tsx | 10 +- apps/web/src/components/ui/sonner.tsx | 2 +- apps/web/src/hooks/use-invitations.ts | 41 +++ apps/web/src/hooks/use-items.ts | 27 ++ apps/web/src/hooks/use-list.ts | 14 + apps/web/src/hooks/use-lists.ts | 49 +++ apps/web/src/routes/(auth)/sign-in.tsx | 42 ++- apps/web/src/routes/(auth)/sign-up.tsx | 128 ++++++++ apps/web/src/routes/__root.tsx | 6 +- apps/web/src/routes/_authenticated/index.tsx | 220 +++++-------- .../_authenticated/lists/$listId/index.tsx | 86 +++++ .../_authenticated/lists/$listId/settings.tsx | 300 ++++++++++++++++++ .../src/routes/_authenticated/lists/index.tsx | 87 +++++ apps/web/src/routes/_authenticated/route.tsx | 7 +- .../src/routes/_authenticated/settings.tsx | 38 ++- apps/web/src/routes/_authenticated/tasks.tsx | 62 ++-- apps/web/src/routes/invitations/$token.tsx | 131 ++++++++ bun.lock | 14 +- packages/shared/src/index.ts | 1 + packages/shared/src/shopping-list.ts | 99 ++++++ turbo.json | 16 +- 81 files changed, 3476 insertions(+), 269 deletions(-) create mode 100644 apps/api/prisma/migrations/20261015205940_init/migration.sql create mode 100644 apps/api/prisma/migrations/20261015213257_add_list_items/migration.sql create mode 100644 apps/api/prisma/migrations/migration_lock.toml create mode 100644 apps/api/src/lib/auth-session.ts create mode 100644 apps/api/src/lib/emails/invitation.ts create mode 100644 apps/api/src/lib/list-membership.ts create mode 100644 apps/api/src/lib/mailer.ts create mode 100644 apps/api/src/lib/web-app-url.ts create mode 100644 apps/api/src/modules/invitations/accept/route.ts create mode 100644 apps/api/src/modules/invitations/accept/use-case.test.ts create mode 100644 apps/api/src/modules/invitations/accept/use-case.ts create mode 100644 apps/api/src/modules/invitations/create/route.ts create mode 100644 apps/api/src/modules/invitations/create/schema.ts create mode 100644 apps/api/src/modules/invitations/create/use-case.ts create mode 100644 apps/api/src/modules/invitations/index.ts create mode 100644 apps/api/src/modules/invitations/list-pending/route.ts create mode 100644 apps/api/src/modules/invitations/list-pending/use-case.ts create mode 100644 apps/api/src/modules/lists/create/route.ts create mode 100644 apps/api/src/modules/lists/create/schema.ts create mode 100644 apps/api/src/modules/lists/create/use-case.test.ts create mode 100644 apps/api/src/modules/lists/create/use-case.ts create mode 100644 apps/api/src/modules/lists/get/route.ts create mode 100644 apps/api/src/modules/lists/get/use-case.ts create mode 100644 apps/api/src/modules/lists/index.ts create mode 100644 apps/api/src/modules/lists/items/create/route.ts create mode 100644 apps/api/src/modules/lists/items/create/schema.ts create mode 100644 apps/api/src/modules/lists/items/create/use-case.ts create mode 100644 apps/api/src/modules/lists/items/index.ts create mode 100644 apps/api/src/modules/lists/items/list/route.ts create mode 100644 apps/api/src/modules/lists/items/list/use-case.ts create mode 100644 apps/api/src/modules/lists/items/serialize.ts create mode 100644 apps/api/src/modules/lists/list/route.ts create mode 100644 apps/api/src/modules/lists/list/use-case.ts create mode 100644 apps/api/src/modules/lists/schema.ts create mode 100644 apps/api/src/modules/lists/serialize.ts create mode 100644 apps/api/src/modules/lists/set-primary/route.ts create mode 100644 apps/api/src/modules/lists/set-primary/use-case.test.ts create mode 100644 apps/api/src/modules/lists/set-primary/use-case.ts create mode 100644 apps/api/src/modules/lists/update/route.ts create mode 100644 apps/api/src/modules/lists/update/schema.ts create mode 100644 apps/api/src/modules/lists/update/use-case.ts create mode 100644 apps/web/src/components/items/create-item-form.tsx create mode 100644 apps/web/src/components/items/item-list.tsx create mode 100644 apps/web/src/components/lists/create-list-form.tsx create mode 100644 apps/web/src/components/lists/list-card.tsx create mode 100644 apps/web/src/hooks/use-invitations.ts create mode 100644 apps/web/src/hooks/use-items.ts create mode 100644 apps/web/src/hooks/use-list.ts create mode 100644 apps/web/src/hooks/use-lists.ts create mode 100644 apps/web/src/routes/(auth)/sign-up.tsx create mode 100644 apps/web/src/routes/_authenticated/lists/$listId/index.tsx create mode 100644 apps/web/src/routes/_authenticated/lists/$listId/settings.tsx create mode 100644 apps/web/src/routes/_authenticated/lists/index.tsx create mode 100644 apps/web/src/routes/invitations/$token.tsx create mode 100644 packages/shared/src/shopping-list.ts diff --git a/.env.example b/.env.example index 4900783..b7c8ade 100644 --- a/.env.example +++ b/.env.example @@ -13,6 +13,12 @@ BETTER_AUTH_URL="http://localhost:4100" NODE_ENV="development" # Optional: Pino log level (defaults to "debug" outside production, "info" in production) # LOG_LEVEL="info" +# Resend API key used to send invitation emails. Without it, emails are logged instead. +# RESEND_API_KEY="re_xxxxxxxx" +# From address for outgoing email (must be a verified Resend sender) +# MAIL_FROM="Superlist " +# Public URL of the web app, used to build the accept-invitation links +WEB_APP_URL="http://localhost:7153" # --- apps/web --- # Public URL of the API consumed by the frontend diff --git a/.gitignore b/.gitignore index 869b83b..86a353f 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,5 @@ logs/ # generated apps/web/src/routeTree.gen.ts +apps/web/.tanstack/ apps/api/src/generated/ diff --git a/AGENTS.md b/AGENTS.md index d5c8c87..584dc24 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -5,8 +5,8 @@ Scaffold is in place and verified: Turborepo monorepo with `apps/api`, `apps/web` and `packages/shared`, CI on GitHub Actions, Biome as the only JS/TS tool and the text Bun lockfile committed. -Not done yet: no database migrations (`prisma/migrations` does not exist — no Postgres has been run against -the schema) and no product features. The only backend module is `health`, used as the reference example. +Not done yet: product features are just starting (sign-up, shopping lists, list items and invitations exist; +there is no Google login and no email verification). The `health` module is the reference example. ## Stack (source of truth: `STACK.md`) @@ -82,6 +82,12 @@ Run from the repo root. Verification order is `build`, then `lint` → `typechec - **TypeScript 6 specifics:** `baseUrl` no longer exists (aliases are declared with `paths` only), and `apps/web/tsconfig.app.json` enables `erasableSyntaxOnly` — no enums, namespaces or constructor parameter properties in the web app. Both apps use `@/*` → `src/*` and `verbatimModuleSyntax` (use `import type`). +- **Database naming (Prisma):** tables are **plural snake_case** (`shopping_lists`, `users`), + columns are **snake_case** (`owner_id`, `created_at`) and enum types are **snake_case** (`list_role`) — all + declared with `@@map` / `@map` in `schema.prisma`. The Prisma *model* and *field* names stay + PascalCase/camelCase, because BetterAuth and the generated client address models by those names, so a column + rename never changes the TypeScript API. Default constraint/index names follow the mapped names, so do not + set `map:` unless you have a reason to. - CI (`.github/workflows/ci.yml`) runs on PRs and pushes to `main`: `bun install --frozen-lockfile` → `db:generate` → `lint:ci` → `typecheck` → `test` → `build`, with Bun 1.4.0 and Node 22 for the Prisma CLI. - Generated files are gitignored and must never be edited by hand: `apps/web/src/routeTree.gen.ts`, @@ -191,8 +197,9 @@ export class CreateAlert { the generated migration under `apps/api/prisma/migrations` — never edit migration files by hand. The datasource has no `url` in the schema: it comes from `prisma.config.ts` (`env('DATABASE_URL')`). For production, `apps/api` also has `db:deploy` (`prisma migrate deploy`), which is not wired to a turbo task yet. -- The BetterAuth models (`User`, `Session`, `Account`, `Verification`) are already in the schema with lowercase - `@@map`ed table names; regenerate them with the BetterAuth CLI rather than by hand if the auth config changes. +- The BetterAuth models (`User`, `Session`, `Account`, `Verification`) are already in the schema and follow the + naming convention above (`users`, `sessions`, `accounts`, `verifications` with snake_case columns); regenerate + them with the BetterAuth CLI rather than by hand if the auth config changes, then re-apply the mapping. ## Shared package (`@superlist/shared`) @@ -227,6 +234,11 @@ apps/web/src/ prefetch), renders `QueryClientProvider` + `RouterProvider`, and registers the router type. `__root.tsx` wraps everything in `ThemeProvider` + `TooltipProvider`, mounts the Sonner `Toaster` and lazy-loads the router and query devtools in development only. +- **Theme**: hand-rolled in `src/components/theme-provider.tsx` — `useTheme()` exposes `theme`, `resolvedTheme` + and `setTheme`, and the provider toggles the `.dark` class and `color-scheme` on ``. The pre-paint script + in `apps/web/index.html` applies the stored theme before React mounts; both share the `superlist-theme` + storage key, so keep them in sync. Do not swap this for `next-themes`: it renders a `
diff --git a/apps/web/package.json b/apps/web/package.json index 0628d1a..cd255fe 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -25,7 +25,6 @@ "clsx": "^2.1.1", "cn": "^0.4.0", "lucide-react": "^1.55.0", - "next-themes": "^0.4.6", "radix-ui": "^1.8.0", "react": "^19.2.8", "react-dom": "^19.2.8", diff --git a/apps/web/src/components/items/create-item-form.tsx b/apps/web/src/components/items/create-item-form.tsx new file mode 100644 index 0000000..965211e --- /dev/null +++ b/apps/web/src/components/items/create-item-form.tsx @@ -0,0 +1,55 @@ +import { zodResolver } from '@hookform/resolvers/zod' +import { PlusIcon } from 'lucide-react' +import { useForm } from 'react-hook-form' +import { z } from 'zod' + +import { Button } from '@/components/ui/button' +import { Field, FieldError } from '@/components/ui/field' +import { Input } from '@/components/ui/input' +import { useCreateItem } from '@/hooks/use-items' + +const createItemSchema = z.object({ + name: z.string().trim().min(1, 'Escribe un artículo').max(120, 'Máximo 120 caracteres'), +}) + +type CreateItemValues = z.infer + +type CreateItemFormProps = { + listId: string + /** Focus the input when the form mounts, so the user can type right away. */ + autoFocus?: boolean +} + +/** Input to add an item to a list. Submits with Enter or the button. */ +export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProps) { + const createItem = useCreateItem(listId) + + const form = useForm({ + resolver: zodResolver(createItemSchema), + defaultValues: { name: '' }, + }) + + const onSubmit = form.handleSubmit((values) => { + createItem.mutate(values, { onSuccess: () => form.reset() }) + }) + + return ( +
+ + + + + + +
+ ) +} diff --git a/apps/web/src/components/items/item-list.tsx b/apps/web/src/components/items/item-list.tsx new file mode 100644 index 0000000..d6074c2 --- /dev/null +++ b/apps/web/src/components/items/item-list.tsx @@ -0,0 +1,93 @@ +import { CheckIcon, ShoppingBasketIcon } from 'lucide-react' + +import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert' +import { Card, CardContent } from '@/components/ui/card' +import { Skeleton } from '@/components/ui/skeleton' +import { useItems } from '@/hooks/use-items' +import { ApiError } from '@/lib/api-client' +import { cn } from '@/lib/utils' + +type ItemListProps = { + listId: string +} + +/** Items loaded from the API, pending ones first. */ +export function ItemList({ listId }: ItemListProps) { + const { data: items, isPending, error } = useItems(listId) + + if (error) { + return ( + + No pudimos cargar los artículos + + {error instanceof ApiError + ? (error.problem.detail ?? error.problem.title) + : 'Inténtalo de nuevo en unos segundos.'} + + + ) + } + + if (isPending) { + return ( +
+ + + +
+ ) + } + + if (items.length === 0) { + return ( + + +
+ +
+
+

La lista está vacía

+

+ Usa el campo de arriba para añadir tu primer artículo. +

+
+
+
+ ) + } + + return ( +
    + {items.map((item) => ( +
  • + + + + + + + {item.name} + + {item.quantity && ( + {item.quantity} + )} + + +
  • + ))} +
+ ) +} diff --git a/apps/web/src/components/layout/app-header.tsx b/apps/web/src/components/layout/app-header.tsx index 5773a5d..914522c 100644 --- a/apps/web/src/components/layout/app-header.tsx +++ b/apps/web/src/components/layout/app-header.tsx @@ -1,34 +1,35 @@ import { useRouterState } from '@tanstack/react-router' import { SearchIcon } from 'lucide-react' + import { ThemeToggle } from '@/components/theme-toggle' import { Separator } from '@/components/ui/separator' import { SidebarTrigger } from '@/components/ui/sidebar' -function usePageTitle() { - const pathname = useRouterState({ select: (state) => state.location.pathname }) - - if (pathname === '/') return 'Dashboard' - - const segment = pathname.split('/').filter(Boolean).at(0) ?? '' - return segment.charAt(0).toUpperCase() + segment.slice(1) +function titleFor(pathname: string) { + if (pathname === '/') return 'Panel' + if (pathname.startsWith('/lists')) return 'Listas' + if (pathname.startsWith('/tasks')) return 'Tareas' + if (pathname.startsWith('/settings')) return 'Ajustes' + return 'Superlist' } export function AppHeader() { - const title = usePageTitle() + const pathname = useRouterState({ select: (state) => state.location.pathname }) + const title = titleFor(pathname) return (
-

{title}

+

{title}

Superlist - Admin + Listas compartidas
@@ -53,11 +59,15 @@ export function AppSidebar() { - Platform + Plataforma {NAV_ITEMS.map((item) => ( - + {item.title} diff --git a/apps/web/src/components/layout/nav-user.tsx b/apps/web/src/components/layout/nav-user.tsx index f8df1c5..389d167 100644 --- a/apps/web/src/components/layout/nav-user.tsx +++ b/apps/web/src/components/layout/nav-user.tsx @@ -35,7 +35,7 @@ export function NavUser() { const navigate = useNavigate() const user = session?.user - const name = user?.name ?? 'Signed out' + const name = user?.name ?? 'Sin sesión' const email = user?.email ?? '' return ( @@ -79,7 +79,7 @@ export function NavUser() { void navigate({ to: '/settings' })}> - Account + Cuenta @@ -90,7 +90,7 @@ export function NavUser() { }} > - Sign out + Cerrar sesión diff --git a/apps/web/src/components/lists/create-list-form.tsx b/apps/web/src/components/lists/create-list-form.tsx new file mode 100644 index 0000000..1869f1e --- /dev/null +++ b/apps/web/src/components/lists/create-list-form.tsx @@ -0,0 +1,81 @@ +import { zodResolver } from '@hookform/resolvers/zod' +import { useState } from 'react' +import { useForm } from 'react-hook-form' +import { z } from 'zod' + +import { Alert, AlertDescription } from '@/components/ui/alert' +import { Button } from '@/components/ui/button' +import { Field, FieldError, FieldGroup, FieldLabel } from '@/components/ui/field' +import { Input } from '@/components/ui/input' +import { useCreateList } from '@/hooks/use-lists' +import { ApiError } from '@/lib/api-client' + +const createListSchema = z.object({ + name: z.string().trim().min(1, 'El nombre es obligatorio').max(80, 'Máximo 80 caracteres'), +}) + +type CreateListValues = z.infer + +type CreateListFormProps = { + onCreated?: () => void +} + +/** Inline form to create a shopping list. Used by the lists page. */ +export function CreateListForm({ onCreated }: CreateListFormProps) { + const [error, setError] = useState(null) + const createList = useCreateList() + + const form = useForm({ + resolver: zodResolver(createListSchema), + defaultValues: { name: '' }, + }) + + const onSubmit = form.handleSubmit((values) => { + setError(null) + + createList.mutate(values, { + onSuccess: () => { + form.reset() + onCreated?.() + }, + onError: (mutationError) => { + setError( + mutationError instanceof ApiError + ? (mutationError.problem.detail ?? mutationError.problem.title) + : 'No pudimos crear la lista.', + ) + }, + }) + }) + + return ( +
+ {error && ( + + {error} + + )} + + + + Nombre de la lista + + + + + + +
+ ) +} diff --git a/apps/web/src/components/lists/list-card.tsx b/apps/web/src/components/lists/list-card.tsx new file mode 100644 index 0000000..33b2013 --- /dev/null +++ b/apps/web/src/components/lists/list-card.tsx @@ -0,0 +1,55 @@ +import type { ShoppingListSummary } from '@superlist/shared' +import { Link } from '@tanstack/react-router' +import { SettingsIcon, StarIcon, UsersIcon } from 'lucide-react' + +import { Badge } from '@/components/ui/badge' +import { Button } from '@/components/ui/button' +import { Card, CardContent } from '@/components/ui/card' +import { useSetPrimaryList } from '@/hooks/use-lists' + +type ListCardProps = { + list: ShoppingListSummary +} + +/** A single list in the lists overview. Tapping it opens the list's items. */ +export function ListCard({ list }: ListCardProps) { + const setPrimary = useSetPrimaryList() + + return ( + + + +
+ {list.name} + {list.isPrimary && Principal} +
+
+ + + {list.memberCount} {list.memberCount === 1 ? 'persona' : 'personas'} + + {list.role === 'owner' ? 'Propietario' : 'Colaborador'} +
+ + + {!list.isPrimary && ( + + )} + + +
+
+ ) +} diff --git a/apps/web/src/components/theme-provider.tsx b/apps/web/src/components/theme-provider.tsx index 1028b06..f41369b 100644 --- a/apps/web/src/components/theme-provider.tsx +++ b/apps/web/src/components/theme-provider.tsx @@ -1,11 +1,84 @@ -import { ThemeProvider as NextThemesProvider } from 'next-themes' -import type { ComponentProps } from 'react' +import type { ReactNode } from 'react' +import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react' -/** Wraps `next-themes` so the theme is toggled by a class on ``. */ -export function ThemeProvider({ children, ...props }: ComponentProps) { - return ( - - {children} - - ) +export type Theme = 'light' | 'dark' | 'system' + +type ResolvedTheme = Exclude + +type ThemeContextValue = { + /** The preference the user picked, `system` included. */ + theme: Theme + /** The preference already resolved against the OS setting. */ + resolvedTheme: ResolvedTheme + setTheme: (theme: Theme) => void +} + +const STORAGE_KEY = 'superlist-theme' +const DARK_QUERY = '(prefers-color-scheme: dark)' + +/** + * This theme handling is hand-rolled on purpose. Libraries such as `next-themes` + * avoid the pre-paint flash by rendering a `