feat: add collaborative shopping lists with items and invitations
Some checks failed
CI / Verify (push) Has been cancelled
Some checks failed
CI / Verify (push) Has been cancelled
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).
This commit is contained in:
32
apps/api/src/modules/lists/items/create/route.ts
Normal file
32
apps/api/src/modules/lists/items/create/route.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Hono } from 'hono'
|
||||
|
||||
import { requireSession } from '@/lib/auth-session'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { resultJson } from '@/lib/result-json'
|
||||
import { validate } from '@/lib/validate'
|
||||
import { ListParamsSchema } from '@/modules/lists/schema'
|
||||
import type { AppEnv } from '@/types'
|
||||
|
||||
import { CreateItemSchema } from './schema'
|
||||
import { CreateItem } from './use-case'
|
||||
|
||||
/** POST /lists/:listId/items — adds an item to a list the user belongs to. */
|
||||
export const createItemRoute = new Hono<AppEnv>().post(
|
||||
'/',
|
||||
requireSession,
|
||||
validate.param(ListParamsSchema),
|
||||
validate.json(CreateItemSchema),
|
||||
async (c) => {
|
||||
const { listId } = c.req.valid('param')
|
||||
const body = c.req.valid('json')
|
||||
const result = await new CreateItem(prisma).execute({
|
||||
listId,
|
||||
userId: c.get('user').id,
|
||||
name: body.name,
|
||||
quantity: body.quantity,
|
||||
notes: body.notes,
|
||||
})
|
||||
|
||||
return resultJson(c, result, { status: 201 })
|
||||
},
|
||||
)
|
||||
10
apps/api/src/modules/lists/items/create/schema.ts
Normal file
10
apps/api/src/modules/lists/items/create/schema.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
/** Body of `POST /lists/:listId/items`. */
|
||||
export const CreateItemSchema = z.object({
|
||||
name: z.string().trim().min(1, 'El nombre es obligatorio').max(120, 'Máximo 120 caracteres'),
|
||||
quantity: z.string().trim().max(40, 'Máximo 40 caracteres').optional(),
|
||||
notes: z.string().trim().max(280, 'Máximo 280 caracteres').optional(),
|
||||
})
|
||||
|
||||
export type CreateItemInput = z.infer<typeof CreateItemSchema>
|
||||
44
apps/api/src/modules/lists/items/create/use-case.ts
Normal file
44
apps/api/src/modules/lists/items/create/use-case.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
Failure,
|
||||
type ListItem,
|
||||
Ok,
|
||||
type ProblemDetails,
|
||||
problems,
|
||||
type Result,
|
||||
} from '@superlist/shared'
|
||||
|
||||
import type { Database } from '@/lib/prisma'
|
||||
|
||||
import { toListItem } from '../serialize'
|
||||
|
||||
/** Adds an item to a shopping list. Only members can add items. */
|
||||
export class CreateItem {
|
||||
constructor(private readonly db: Database) {}
|
||||
|
||||
async execute(input: {
|
||||
listId: string
|
||||
userId: string
|
||||
name: string
|
||||
quantity?: string
|
||||
notes?: string
|
||||
}): Promise<Result<ListItem, ProblemDetails>> {
|
||||
const membership = await this.db.shoppingListMember.findUnique({
|
||||
where: { listId_userId: { listId: input.listId, userId: input.userId } },
|
||||
})
|
||||
|
||||
if (!membership) {
|
||||
return Failure(problems.notFound('La lista no existe o no tienes acceso.'))
|
||||
}
|
||||
|
||||
const item = await this.db.listItem.create({
|
||||
data: {
|
||||
shoppingListId: input.listId,
|
||||
name: input.name,
|
||||
quantity: input.quantity || null,
|
||||
notes: input.notes || null,
|
||||
},
|
||||
})
|
||||
|
||||
return Ok(toListItem(item))
|
||||
}
|
||||
}
|
||||
11
apps/api/src/modules/lists/items/index.ts
Normal file
11
apps/api/src/modules/lists/items/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Hono } from 'hono'
|
||||
|
||||
import type { AppEnv } from '@/types'
|
||||
|
||||
import { createItemRoute } from './create/route'
|
||||
import { listItemsRoute } from './list/route'
|
||||
|
||||
/** `items` sub-module of `lists`: mounted at `/lists/:listId/items`. */
|
||||
export const listItemsModule = new Hono<AppEnv>()
|
||||
.route('/', listItemsRoute)
|
||||
.route('/', createItemRoute)
|
||||
26
apps/api/src/modules/lists/items/list/route.ts
Normal file
26
apps/api/src/modules/lists/items/list/route.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Hono } from 'hono'
|
||||
|
||||
import { requireSession } from '@/lib/auth-session'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { resultJson } from '@/lib/result-json'
|
||||
import { validate } from '@/lib/validate'
|
||||
import { ListParamsSchema } from '@/modules/lists/schema'
|
||||
import type { AppEnv } from '@/types'
|
||||
|
||||
import { ListItems } from './use-case'
|
||||
|
||||
/** GET /lists/:listId/items — every item of a list the user belongs to. */
|
||||
export const listItemsRoute = new Hono<AppEnv>().get(
|
||||
'/',
|
||||
requireSession,
|
||||
validate.param(ListParamsSchema),
|
||||
async (c) => {
|
||||
const { listId } = c.req.valid('param')
|
||||
const result = await new ListItems(prisma).execute({
|
||||
listId,
|
||||
userId: c.get('user').id,
|
||||
})
|
||||
|
||||
return resultJson(c, result)
|
||||
},
|
||||
)
|
||||
34
apps/api/src/modules/lists/items/list/use-case.ts
Normal file
34
apps/api/src/modules/lists/items/list/use-case.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import type { ListItem, ProblemDetails, Result } from '@superlist/shared'
|
||||
import { Failure, Ok, problems } from '@superlist/shared'
|
||||
|
||||
import type { Database } from '@/lib/prisma'
|
||||
|
||||
import { toListItem } from '../serialize'
|
||||
|
||||
/**
|
||||
* Lists every item of a shopping list, pending ones first.
|
||||
* Only members of the list can read it.
|
||||
*/
|
||||
export class ListItems {
|
||||
constructor(private readonly db: Database) {}
|
||||
|
||||
async execute(input: {
|
||||
listId: string
|
||||
userId: string
|
||||
}): Promise<Result<ListItem[], ProblemDetails>> {
|
||||
const membership = await this.db.shoppingListMember.findUnique({
|
||||
where: { listId_userId: { listId: input.listId, userId: input.userId } },
|
||||
})
|
||||
|
||||
if (!membership) {
|
||||
return Failure(problems.notFound('La lista no existe o no tienes acceso.'))
|
||||
}
|
||||
|
||||
const items = await this.db.listItem.findMany({
|
||||
where: { shoppingListId: input.listId },
|
||||
orderBy: [{ completed: 'asc' }, { order: 'asc' }, { createdAt: 'asc' }],
|
||||
})
|
||||
|
||||
return Ok(items.map(toListItem))
|
||||
}
|
||||
}
|
||||
28
apps/api/src/modules/lists/items/serialize.ts
Normal file
28
apps/api/src/modules/lists/items/serialize.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { ListItem } from '@superlist/shared'
|
||||
|
||||
type ItemRow = {
|
||||
id: string
|
||||
shoppingListId: string
|
||||
name: string
|
||||
completed: boolean
|
||||
quantity: string | null
|
||||
notes: string | null
|
||||
order: number
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
}
|
||||
|
||||
/** Maps a `ListItem` row to the DTO shared with the web app. */
|
||||
export function toListItem(item: ItemRow): ListItem {
|
||||
return {
|
||||
id: item.id,
|
||||
listId: item.shoppingListId,
|
||||
name: item.name,
|
||||
completed: item.completed,
|
||||
quantity: item.quantity,
|
||||
notes: item.notes,
|
||||
order: item.order,
|
||||
createdAt: item.createdAt.toISOString(),
|
||||
updatedAt: item.updatedAt.toISOString(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user