11import type { FastifyInstance } from 'fastify'
2- import type { Prisma } from '@prisma/client'
2+
3+
4+ type RawCard = { id : string , title : string , isDefault : boolean , cardLinks : { platformLink : unknown } [ ] } ;
35
46export async function listCards ( app : FastifyInstance , userId : string ) {
57 const cards = await app . prisma . card . findMany ( {
68 where : { userId } ,
79 take : 50 ,
810 include : { cardLinks : { include : { platformLink : true } , orderBy : { displayOrder : 'asc' } } } ,
911 orderBy : { createdAt : 'asc' } ,
10- } )
12+ } ) as unknown as RawCard [ ] ;
1113
12- return cards . map ( ( card : any ) => ( { id : card . id , title : card . title , isDefault : card . isDefault , links : card . cardLinks . map ( ( cl : any ) => cl . platformLink ) } ) )
14+ return cards . map ( ( card ) => ( { id : card . id , title : card . title , isDefault : card . isDefault , links : card . cardLinks . map ( ( cl ) => cl . platformLink ) } ) )
1315}
1416
1517export async function createCard ( app : FastifyInstance , userId : string , body : { title : string ; linkIds : string [ ] } ) {
@@ -34,12 +36,21 @@ export async function createCard(app: FastifyInstance, userId: string, body: { t
3436 include : { cardLinks : { include : { platformLink : true } , orderBy : { displayOrder : 'asc' } } } ,
3537 } )
3638 } , {
37- isolationLevel : 'Serializable' as Prisma . TransactionIsolationLevel
38- } )
39-
40- return { id : card . id , title : card . title , isDefault : card . isDefault , links : card . cardLinks . map ( ( cl : any ) => cl . platformLink ) }
41- } catch ( error : any ) {
42- if ( error . code === 'P2034' && attempt < MAX_RETRIES ) continue ;
39+ isolationLevel : 'Serializable'
40+ } ) as unknown as RawCard ;
41+
42+ return { id : card . id , title : card . title , isDefault : card . isDefault , links : card . cardLinks . map ( ( cl ) => cl . platformLink ) }
43+ } catch ( error : unknown ) {
44+ if (
45+ typeof error === 'object' &&
46+ error !== null &&
47+ 'code' in error &&
48+ ( error as { code : string } ) . code === 'P2034' &&
49+ attempt < MAX_RETRIES
50+ ) {
51+ continue ;
52+ }
53+ app . log . error ( error ) ;
4354 throw error ;
4455 }
4556 }
@@ -68,8 +79,9 @@ export async function updateCard(app: FastifyInstance, userId: string, id: strin
6879 } )
6980 }
7081
71- const updated = await app . prisma . card . findUnique ( { where : { id } , include : { cardLinks : { include : { platformLink : true } , orderBy : { displayOrder : 'asc' } } } } )
72- return { id : updated ! . id , title : updated ! . title , isDefault : updated ! . isDefault , links : updated ! . cardLinks . map ( ( cl : any ) => cl . platformLink ) }
82+ const updated = ( await app . prisma . card . findUnique ( { where : { id } , include : { cardLinks : { include : { platformLink : true } , orderBy : { displayOrder : 'asc' } } } } ) ) as unknown as RawCard | null ;
83+ if ( ! updated ) return null ;
84+ return { id : updated . id , title : updated . title , isDefault : updated . isDefault , links : updated . cardLinks . map ( ( cl ) => cl . platformLink ) }
7385}
7486
7587export async function deleteCard ( app : FastifyInstance , userId : string , id : string ) {
0 commit comments