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:
26
apps/api/src/modules/lists/set-primary/route.ts
Normal file
26
apps/api/src/modules/lists/set-primary/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 type { AppEnv } from '@/types'
|
||||
|
||||
import { ListParamsSchema } from '../schema'
|
||||
import { SetPrimaryList } from './use-case'
|
||||
|
||||
/** POST /lists/:listId/primary — marks the list as the user's primary one. */
|
||||
export const setPrimaryListRoute = new Hono<AppEnv>().post(
|
||||
'/:listId/primary',
|
||||
requireSession,
|
||||
validate.param(ListParamsSchema),
|
||||
async (c) => {
|
||||
const { listId } = c.req.valid('param')
|
||||
const result = await new SetPrimaryList(prisma).execute({
|
||||
listId,
|
||||
userId: c.get('user').id,
|
||||
})
|
||||
|
||||
return resultJson(c, result, { status: 204 })
|
||||
},
|
||||
)
|
||||
65
apps/api/src/modules/lists/set-primary/use-case.test.ts
Normal file
65
apps/api/src/modules/lists/set-primary/use-case.test.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { describe, expect, it } from 'bun:test'
|
||||
|
||||
import type { Database } from '@/lib/prisma'
|
||||
|
||||
import { SetPrimaryList } from './use-case'
|
||||
|
||||
function createDb(membership: unknown) {
|
||||
const calls: { cleared: unknown[]; marked: unknown[] } = { cleared: [], marked: [] }
|
||||
|
||||
const db = {
|
||||
shoppingListMember: {
|
||||
findUnique: async () => membership,
|
||||
updateMany: async (args: unknown) => {
|
||||
calls.cleared.push(args)
|
||||
return { count: 1 }
|
||||
},
|
||||
update: async (args: unknown) => {
|
||||
calls.marked.push(args)
|
||||
return args
|
||||
},
|
||||
},
|
||||
$transaction: async (operations: Promise<unknown>[]) => Promise.all(operations),
|
||||
}
|
||||
|
||||
return { db: db as unknown as Database, calls }
|
||||
}
|
||||
|
||||
describe('SetPrimaryList', () => {
|
||||
it('answers 404 when the user does not belong to the list', async () => {
|
||||
const { db, calls } = createDb(null)
|
||||
const result = await new SetPrimaryList(db).execute({
|
||||
listId: 'list-1',
|
||||
userId: 'user-1',
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(false)
|
||||
if (result.ok) throw new Error('expected the use case to fail')
|
||||
expect(result.error.status).toBe(404)
|
||||
expect(calls.cleared).toHaveLength(0)
|
||||
expect(calls.marked).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('clears the previous primary list and marks the new one', async () => {
|
||||
const { db, calls } = createDb({ isPrimary: false })
|
||||
const result = await new SetPrimaryList(db).execute({
|
||||
listId: 'list-2',
|
||||
userId: 'user-1',
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
|
||||
expect(calls.cleared).toEqual([
|
||||
{
|
||||
where: { userId: 'user-1', isPrimary: true, listId: { not: 'list-2' } },
|
||||
data: { isPrimary: false },
|
||||
},
|
||||
])
|
||||
expect(calls.marked).toEqual([
|
||||
{
|
||||
where: { listId_userId: { listId: 'list-2', userId: 'user-1' } },
|
||||
data: { isPrimary: true },
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
31
apps/api/src/modules/lists/set-primary/use-case.ts
Normal file
31
apps/api/src/modules/lists/set-primary/use-case.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Failure, Ok, type ProblemDetails, problems, type Result } from '@superlist/shared'
|
||||
|
||||
import type { Database } from '@/lib/prisma'
|
||||
|
||||
/** Marks one list as the user's primary list (per user, not per list). */
|
||||
export class SetPrimaryList {
|
||||
constructor(private readonly db: Database) {}
|
||||
|
||||
async execute(input: { listId: string; userId: string }): Promise<Result<void, 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.'))
|
||||
}
|
||||
|
||||
await this.db.$transaction([
|
||||
this.db.shoppingListMember.updateMany({
|
||||
where: { userId: input.userId, isPrimary: true, listId: { not: input.listId } },
|
||||
data: { isPrimary: false },
|
||||
}),
|
||||
this.db.shoppingListMember.update({
|
||||
where: { listId_userId: { listId: input.listId, userId: input.userId } },
|
||||
data: { isPrimary: true },
|
||||
}),
|
||||
])
|
||||
|
||||
return Ok(undefined)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user