1+ import * as cardService from '../services/cardService'
12import { handleDbError } from '../utils/error.util.js' ;
23import { createCardSchema , updateCardSchema } from '../utils/validators.js' ;
3- import * as cardService from '../services/cardService'
44
5+ import type { CardResponse } from '../services/cardService' ;
56import type { Card } from '@devcard/shared' ;
6- import type { Prisma } from '@prisma/client' ;
77import type { FastifyInstance , FastifyRequest , FastifyReply } from 'fastify' ;
88
9-
109interface CreateCardBody {
1110 title : string ;
1211 linkIds : string [ ] ;
@@ -39,7 +38,7 @@ interface CardLinkWithPlatform {
3938 platformLink : PlatformLink ;
4039}
4140
42- interface CardWithLinks {
41+ interface _CardWithLinks {
4342 id : string ;
4443 userId : string ;
4544 title : string ;
@@ -54,12 +53,12 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
5453 const server = request . server as any ;
5554 if ( typeof server ?. authenticate === 'function' ) { await server . authenticate ( request , reply ) ; return }
5655 if ( typeof ( app as any ) . authenticate === 'function' ) { await ( app as any ) . authenticate ( request , reply ) ; return }
57- try { await request . jwtVerify ( ) } catch ( e ) { reply . status ( 401 ) . send ( { error : 'Unauthorized' } ) }
56+ try { await request . jwtVerify ( ) } catch ( _e ) { reply . status ( 401 ) . send ( { error : 'Unauthorized' } ) }
5857 } ) ;
5958
6059 // ─── List Cards ───
6160
62- app . get ( '/' , async ( request : FastifyRequest , reply : FastifyReply ) : Promise < Card [ ] | void > => {
61+ app . get ( '/' , async ( request : FastifyRequest , reply : FastifyReply ) : Promise < CardResponse [ ] | void > => {
6362 const userId = ( request . user as { id : string } ) . id ;
6463 try {
6564 return await cardService . listCards ( app , userId )
@@ -82,25 +81,25 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
8281 const card = await cardService . createCard ( app , userId , parsed . data )
8382 return reply . status ( 201 ) . send ( card )
8483 } catch ( error : any ) {
85- if ( error ?. code === 'OWNERSHIP' ) return reply . status ( 403 ) . send ( { error : 'One or more links do not belong to your account' } )
84+ if ( error ?. code === 'OWNERSHIP' ) { return reply . status ( 403 ) . send ( { error : 'One or more links do not belong to your account' } ) }
8685 return handleDbError ( error , request , reply )
8786 }
8887 } ) ;
8988
9089 // ─── Update Card ───
9190
92- app . put ( '/:id' , async ( request : FastifyRequest < { Params : CardParams ; Body : UpdateCardBody } > , reply : FastifyReply ) : Promise < Card | void > => {
91+ app . put ( '/:id' , async ( request : FastifyRequest < { Params : CardParams ; Body : UpdateCardBody } > , reply : FastifyReply ) : Promise < CardResponse > => {
9392 const userId = ( request . user as { id : string } ) . id ;
9493 const { id } = request . params ;
9594
9695 try {
9796 const parsed = updateCardSchema . safeParse ( request . body )
98- if ( ! parsed . success ) return reply . status ( 400 ) . send ( { error : 'Validation failed' , details : parsed . error . flatten ( ) } )
97+ if ( ! parsed . success ) { return reply . status ( 400 ) . send ( { error : 'Validation failed' , details : parsed . error . flatten ( ) } ) }
9998 const updated = await cardService . updateCard ( app , userId , id , parsed . data )
100- if ( ! updated ) return reply . status ( 404 ) . send ( { error : 'Card not found' } )
99+ if ( ! updated ) { return reply . status ( 404 ) . send ( { error : 'Card not found' } ) }
101100 return updated
102101 } catch ( error : any ) {
103- if ( error ?. code === 'OWNERSHIP' ) return reply . status ( 403 ) . send ( { error : 'One or more links do not belong to your account' } )
102+ if ( error ?. code === 'OWNERSHIP' ) { return reply . status ( 403 ) . send ( { error : 'One or more links do not belong to your account' } ) }
104103 return handleDbError ( error , request , reply )
105104 }
106105 } ) ;
@@ -112,11 +111,18 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
112111 const { id } = request . params ;
113112
114113 try {
115- const res = await cardService . deleteCard ( app , userId , id )
116- if ( res && ( res as any ) . code === 'NOT_FOUND' ) return reply . status ( 404 ) . send ( { error : 'Card not found' } )
117- if ( res && ( res as any ) . code === 'LAST_CARD' ) return reply . status ( 400 ) . send ( { error : 'Cannot delete the last remaining card. A user must have at least one card.' } )
114+ await cardService . deleteCard ( app , userId , id )
118115 return reply . status ( 204 ) . send ( )
119- } catch ( error ) {
116+ } catch ( error :any ) {
117+ if ( error ?. code === 'NOT_FOUND' ) {
118+ return reply . status ( 404 ) . send ( { error : 'Card not found' } ) ;
119+ }
120+
121+ if ( error ?. code === 'LAST_CARD' ) {
122+ return reply . status ( 400 ) . send ( {
123+ error : 'Cannot delete the last remaining card. A user must have at least one card.' ,
124+ } ) ;
125+ }
120126 return handleDbError ( error , request , reply )
121127 }
122128 } ) ;
@@ -129,7 +135,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
129135
130136 try {
131137 const resp = await cardService . setDefaultCard ( app , userId , id )
132- if ( ! resp ) return reply . status ( 404 ) . send ( { error : 'Card not found' } )
138+ if ( ! resp ) { return reply . status ( 404 ) . send ( { error : 'Card not found' } ) }
133139 return resp
134140 } catch ( error ) {
135141 return handleDbError ( error , request , reply )
0 commit comments