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:
133
apps/api/src/modules/invitations/accept/use-case.test.ts
Normal file
133
apps/api/src/modules/invitations/accept/use-case.test.ts
Normal 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)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user