1- import type { Prisma } from '@prisma/client' ;
1+ import { CardVisibility , type Prisma } from '@prisma/client' ;
2+
3+ import { generateUniqueSlug } from '../utils/slug' ;
4+
25import type { FastifyInstance } from 'fastify' ;
36
47type CardLinkResponse = { platformLink : unknown } ;
@@ -25,17 +28,27 @@ export async function listCards(app: FastifyInstance, userId: string): Promise<C
2528 return cards . map ( mapCard ) ;
2629}
2730
28- export async function createCard ( app : FastifyInstance , userId : string , body : { title : string ; linkIds : string [ ] } ) : Promise < CardResponse > {
29- if ( body . linkIds . length > 0 ) {
30- const ownedLinks = await app . prisma . platformLink . findMany ( {
31- where : { id : { in : body . linkIds } , userId } ,
32- select : { id : true } ,
33- } ) ;
31+ export async function createCard ( app : FastifyInstance , userId : string , body : { title : string ; linkIds : string [ ] , description ?: string , visibility ?: CardVisibility } ) : Promise < CardResponse > {
32+ const { title , description , linkIds , visibility} = body
33+
34+ const ownedLinks = await app . prisma . platformLink . findMany ( {
35+ where : { id : { in : linkIds } , userId } ,
36+ select : { id : true } ,
37+ } ) ;
38+
39+ if ( ownedLinks . length !== linkIds . length ) {
40+ throw Object . assign ( new Error ( 'Link ownership mismatch' ) , { code : 'OWNERSHIP' } ) ;
41+ }
42+
43+ const finalSlug = await generateUniqueSlug ( title , async ( slug ) => {
44+ const existing = await app . prisma . card . findUnique ( {
45+ where : {
46+ slug
47+ }
48+ } )
49+ return ! ! existing
50+ } )
3451
35- if ( ownedLinks . length !== body . linkIds . length ) {
36- throw Object . assign ( new Error ( 'Link ownership mismatch' ) , { code : 'OWNERSHIP' } ) ;
37- }
38- }
3952
4053 const maxRetries = 3 ;
4154 for ( let attempt = 1 ; attempt <= maxRetries ; attempt ++ ) {
@@ -47,10 +60,13 @@ export async function createCard(app: FastifyInstance, userId: string, body: { t
4760 return tx . card . create ( {
4861 data : {
4962 userId,
50- title : body . title ,
63+ title,
64+ slug : finalSlug ,
5165 isDefault : cardCount === 0 ,
66+ description,
67+ visibility : visibility ?? CardVisibility . PUBLIC ,
5268 cardLinks : {
53- create : body . linkIds . map ( ( linkId , index ) => ( { platformLinkId : linkId , displayOrder : index } ) ) ,
69+ create : linkIds . map ( ( linkId , index ) => ( { platformLinkId : linkId , displayOrder : index } ) ) ,
5470 } ,
5571 } ,
5672 include : { cardLinks : { include : { platformLink : true } , orderBy : { displayOrder : 'asc' } } } ,
@@ -72,9 +88,8 @@ export async function createCard(app: FastifyInstance, userId: string, body: { t
7288 ) {
7389 continue ;
7490 }
75-
7691 app . log . error ( error ) ;
77- throw error ;
92+ throw error
7893 }
7994 }
8095
0 commit comments