feat: add collaborative shopping lists with items and invitations
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:
Jose Selesan
2026-10-16 10:12:11 -03:00
parent 964a3c7217
commit 6aa86a3b68
81 changed files with 3476 additions and 269 deletions

View File

@@ -0,0 +1,32 @@
import { Hono } from 'hono'
import { z } from 'zod'
import { requireSession } from '@/lib/auth-session'
import { prisma } from '@/lib/prisma'
import { resultJson } from '@/lib/result-json'
import { validate } from '@/lib/validate'
import type { AppEnv } from '@/types'
import { AcceptInvitation } from './use-case'
const TokenParamSchema = z.object({
token: z.uuid('El identificador de la invitación no es válido'),
})
/** POST /invitations/:token/accept — accepts an invitation for the signed-in user. */
export const acceptInvitationRoute = new Hono<AppEnv>().post(
'/:token/accept',
requireSession,
validate.param(TokenParamSchema),
async (c) => {
const { token } = c.req.valid('param')
const user = c.get('user')
const result = await new AcceptInvitation(prisma).execute({
token,
userId: user.id,
email: user.email,
})
return resultJson(c, result)
},
)

View File

@@ -0,0 +1,133 @@
import { describe, expect, it } from 'bun:test'
import type { Database } from '@/lib/prisma'
import { AcceptInvitation } from './use-case'
const NOW = new Date('2026-10-15T00:00:00.000Z')
const INVITATION = {
id: 'inv-1',
listId: 'list-1',
email: 'beto@ejemplo.com',
status: 'pending',
expiresAt: new Date('2026-10-20T00:00:00.000Z'),
list: { id: 'list-1', name: 'Compra semanal' },
}
function createDb({
invitation,
membership = null,
}: {
invitation?: typeof INVITATION
membership?: unknown
}) {
const updates: unknown[] = []
const created: unknown[] = []
const db = {
shoppingListInvitation: {
findUnique: async () => invitation,
update: async (args: unknown) => {
updates.push(args)
return args
},
},
shoppingListMember: {
findUnique: async () => membership,
create: async (args: unknown) => {
created.push(args)
return args
},
},
$transaction: async (operations: Promise<unknown>[]) => Promise.all(operations),
}
return { db: db as unknown as Database, updates, created }
}
describe('AcceptInvitation', () => {
it('answers 404 for an unknown token', async () => {
const { db } = createDb({ invitation: undefined })
const result = await new AcceptInvitation(db, () => NOW).execute({
token: 'nope',
userId: 'user-1',
email: 'beto@ejemplo.com',
})
expect(result.ok).toBe(false)
if (result.ok) throw new Error('expected the use case to fail')
expect(result.error.status).toBe(404)
})
it('rejects an invitation addressed to another email', async () => {
const { db, created } = createDb({ invitation: INVITATION })
const result = await new AcceptInvitation(db, () => NOW).execute({
token: INVITATION.id,
userId: 'user-1',
email: 'ana@ejemplo.com',
})
expect(result.ok).toBe(false)
if (result.ok) throw new Error('expected the use case to fail')
expect(result.error.status).toBe(403)
expect(created).toHaveLength(0)
})
it('is idempotent for somebody who is already a member', async () => {
const { db, updates } = createDb({
invitation: INVITATION,
membership: { id: 'member-1' },
})
const result = await new AcceptInvitation(db, () => NOW).execute({
token: INVITATION.id,
userId: 'user-1',
email: 'beto@ejemplo.com',
})
expect(result.ok).toBe(true)
if (!result.ok) throw new Error('expected the use case to succeed')
expect(result.value).toEqual({ listId: 'list-1', listName: 'Compra semanal' })
expect(updates).toHaveLength(0)
})
it('answers 400 and marks expired invitations', async () => {
const { db, updates, created } = createDb({
invitation: {
...INVITATION,
expiresAt: new Date('2026-10-01T00:00:00.000Z'),
},
})
const result = await new AcceptInvitation(db, () => NOW).execute({
token: INVITATION.id,
userId: 'user-1',
email: 'beto@ejemplo.com',
})
expect(result.ok).toBe(false)
if (result.ok) throw new Error('expected the use case to fail')
expect(result.error.status).toBe(400)
expect(updates).toHaveLength(1)
expect(created).toHaveLength(0)
})
it('grants access and marks the invitation as accepted', async () => {
const { db, updates, created } = createDb({ invitation: INVITATION })
const result = await new AcceptInvitation(db, () => NOW).execute({
token: INVITATION.id,
userId: 'user-1',
// Compared in lower case, so the casing of the session email does not matter.
email: 'Beto@Ejemplo.com',
})
expect(result.ok).toBe(true)
if (!result.ok) throw new Error('expected the use case to succeed')
expect(result.value).toEqual({ listId: 'list-1', listName: 'Compra semanal' })
expect(created).toEqual([
{
data: { listId: 'list-1', userId: 'user-1', role: 'collaborator' },
},
])
expect(updates).toHaveLength(1)
})
})

View File

@@ -0,0 +1,71 @@
import {
type AcceptInvitationResult,
Failure,
Ok,
type ProblemDetails,
problems,
type Result,
} from '@superlist/shared'
import type { Database } from '@/lib/prisma'
/** Accepts an invitation for the signed-in user, matching it by token and email. */
export class AcceptInvitation {
constructor(
private readonly db: Database,
private readonly now: () => Date = () => new Date(),
) {}
async execute(input: {
token: string
userId: string
email: string
}): Promise<Result<AcceptInvitationResult, ProblemDetails>> {
const invitation = await this.db.shoppingListInvitation.findUnique({
where: { token: input.token },
include: { list: { select: { id: true, name: true } } },
})
if (!invitation) {
return Failure(problems.notFound('La invitación no existe o fue cancelada.'))
}
if (invitation.email !== input.email.trim().toLowerCase()) {
return Failure(problems.forbidden('Esta invitación pertenece a otro correo electrónico.'))
}
const membership = await this.db.shoppingListMember.findUnique({
where: { listId_userId: { listId: invitation.listId, userId: input.userId } },
})
// Already a member (e.g. accepted at signup): idempotent success.
if (membership) {
return Ok({ listId: invitation.list.id, listName: invitation.list.name })
}
if (invitation.status !== 'pending') {
return Failure(problems.conflict('Esta invitación ya no está disponible.'))
}
if (invitation.expiresAt.getTime() < this.now().getTime()) {
await this.db.shoppingListInvitation.update({
where: { id: invitation.id },
data: { status: 'expired' },
})
return Failure(problems.badRequest('Esta invitación ha expirado.'))
}
await this.db.$transaction([
this.db.shoppingListMember.create({
data: { listId: invitation.listId, userId: input.userId, role: 'collaborator' },
}),
this.db.shoppingListInvitation.update({
where: { id: invitation.id },
data: { status: 'accepted', acceptedAt: this.now() },
}),
])
return Ok({ listId: invitation.list.id, listName: invitation.list.name })
}
}