@@ -62,7 +62,6 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
6262 } ) ;
6363
6464 // ─── List Cards ───
65-
6665 app . get ( '/' , async ( request : FastifyRequest , reply : FastifyReply ) : Promise < CardResponse [ ] | void > => {
6766 const userId = ( request . user as { id : string } ) . id ;
6867 try {
@@ -108,7 +107,6 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
108107 } ) ;
109108
110109 // ─── Delete Card ───
111-
112110 app . delete ( '/:id' , async ( request : FastifyRequest < { Params : CardParams } > , reply : FastifyReply ) : Promise < void > => {
113111 const userId = ( request . user as { id : string } ) . id ;
114112 const { id } = request . params ;
@@ -209,6 +207,10 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
209207 }
210208 } )
211209
210+ // TODO:
211+ // Determine view source dynamically (url, qr, app, etc.).
212+ // The shared card endpoint is currently used by multiple entry points,
213+ // so source should not be hardcoded to "link".
212214 //Get shared card
213215 app . get ( '/share/:slug' , async ( request : FastifyRequest < { Params : { slug : string } } > , reply : FastifyReply ) => {
214216 const paramsSlug = request . params . slug ;
@@ -256,6 +258,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
256258 }
257259 } )
258260
261+ //Generates qr
259262 app . get ( '/:id/qr' , async ( request : FastifyRequest < { Params : { id : string } } > , reply :FastifyReply ) => {
260263 const cardId = request . params . id
261264 const userId = request . user . id
@@ -274,18 +277,37 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
274277 error : error . message ,
275278 } ) ;
276279 }
277- if ( error ?. code === 'QR_ENABLED ' ) {
278- return reply . status ( 404 ) . send ( {
280+ if ( error ?. code === 'QR_DISABLED ' ) {
281+ return reply . status ( 403 ) . send ( {
279282 error : error . message ,
280283 } ) ;
281284 }
282285 if ( error ?. code === 'QR_IMAGE' ) {
283- return reply . status ( 403 ) . send ( {
286+ return reply . status ( 500 ) . send ( {
284287 error : error . message ,
285288 } ) ;
286289 }
287290 app . log . error ( error )
288291 handleDbError ( error , request , reply )
289292 }
290293 } )
294+
295+ //Get analytics
296+ app . get ( '/:id/analytics' , async ( request :FastifyRequest < { Params : { id :string } } > , reply : FastifyReply ) => {
297+ const cardId = request . params . id
298+ const userId = request . user . id
299+
300+ try {
301+ const analytics = await cardService . cardAnalytics ( app , userId , cardId )
302+ return reply . status ( 200 ) . send ( analytics )
303+ } catch ( error :any ) {
304+ if ( error ?. code === 'CARD_NOT_FOUND' ) {
305+ return reply . status ( 404 ) . send ( {
306+ error : error . message ,
307+ } ) ;
308+ }
309+ app . log . error ( error )
310+ handleDbError ( error , request , reply )
311+ }
312+ } )
291313}
0 commit comments