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:
57
apps/api/src/modules/lists/create/use-case.ts
Normal file
57
apps/api/src/modules/lists/create/use-case.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { Ok, type ProblemDetails, type Result, type ShoppingListSummary } from '@superlist/shared'
|
||||
|
||||
import type { Database } from '@/lib/prisma'
|
||||
|
||||
import { toSummary } from '../serialize'
|
||||
|
||||
/** Creates a shopping list owned by the user. The first list becomes primary. */
|
||||
export class CreateShoppingList {
|
||||
constructor(private readonly db: Database) {}
|
||||
|
||||
async execute(input: {
|
||||
name: string
|
||||
userId: string
|
||||
}): Promise<Result<ShoppingListSummary, ProblemDetails>> {
|
||||
const existingCount = await this.db.shoppingListMember.count({
|
||||
where: { userId: input.userId },
|
||||
})
|
||||
|
||||
const list = await this.db.shoppingList.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
ownerId: input.userId,
|
||||
members: {
|
||||
create: {
|
||||
userId: input.userId,
|
||||
role: 'owner',
|
||||
isPrimary: existingCount === 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
_count: { select: { members: true } },
|
||||
members: { where: { userId: input.userId } },
|
||||
},
|
||||
})
|
||||
|
||||
const membership = list.members[0]
|
||||
|
||||
if (!membership) {
|
||||
throw new Error('Membership was not created for the new list')
|
||||
}
|
||||
|
||||
return Ok(
|
||||
toSummary({
|
||||
role: membership.role,
|
||||
isPrimary: membership.isPrimary,
|
||||
list: {
|
||||
id: list.id,
|
||||
name: list.name,
|
||||
createdAt: list.createdAt,
|
||||
updatedAt: list.updatedAt,
|
||||
_count: list._count,
|
||||
},
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user