From d15820a2cca7a7a28bce416cf5deb921cbc75124 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Mon, 24 Feb 2025 18:14:09 +0100 Subject: [PATCH] feat(api): logging protected donation routes (#58934) --- api/src/routes/protected/donate.ts | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/api/src/routes/protected/donate.ts b/api/src/routes/protected/donate.ts index 0003124d380..b1ca1c223d7 100644 --- a/api/src/routes/protected/donate.ts +++ b/api/src/routes/protected/donate.ts @@ -29,11 +29,14 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( schema: schemas.updateStripeCard }, async req => { + const logger = fastify.log.child({ req }); const donation = await fastify.prisma.donation.findFirst({ where: { userId: req.user?.id, provider: 'stripe' } }); - if (!donation) + if (!donation) { + logger.error(`Stripe donation record not found: ${req.user?.id}`); throw Error(`Stripe donation record not found: ${req.user?.id}`); + } const { customerId, subscriptionId } = donation; const session = await stripe.checkout.sessions.create({ payment_method_types: ['card'], @@ -48,6 +51,7 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( success_url: `${HOME_LOCATION}/update-stripe-card?session_id={CHECKOUT_SESSION_ID}`, cancel_url: `${HOME_LOCATION}/update-stripe-card` }); + logger.info(`Stripe session created for user: ${req.user?.id}`); return { sessionId: session.id } as const; } ); @@ -58,12 +62,14 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( schema: schemas.addDonation }, async (req, reply) => { + const logger = fastify.log.child({ req }); try { const user = await fastify.prisma.user.findUnique({ where: { id: req.user?.id } }); if (user?.isDonating) { + logger.info(`User ${req.user?.id} is already donating.`); void reply.code(400); return { type: 'info', @@ -78,11 +84,14 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( } }); + logger.info(`User ${req.user?.id} is now donating`); + return { isDonating: true } as const; } catch (error) { - fastify.log.error(error); + logger.error(`User ${req.user?.id} failed to donate`); + logger.error(error); fastify.Sentry.captureException(error); void reply.code(500); return { @@ -99,6 +108,7 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( schema: schemas.chargeStripeCard }, async (req, reply) => { + const logger = fastify.log.child({ req }); try { const { paymentMethodId, amount, duration } = req.body; const id = req.user!.id; @@ -111,6 +121,9 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( const threeChallengesCompleted = user.completedChallenges.length >= 3; if (!threeChallengesCompleted) { + logger.warn( + `User ${id} has tried to donate before completing 3 challenges` + ); void reply.code(400); return { error: { @@ -121,6 +134,7 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( } if (user.isDonating) { + logger.info(`User ${id} is already donating.`); void reply.code(400); return reply.send({ error: { @@ -158,6 +172,7 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( expand: ['latest_invoice.payment_intent'] }); if (status === 'requires_source_action') { + logger.info(`User ${id} payment requires user action`); void reply.code(402); return reply.send({ error: { @@ -168,6 +183,7 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( } }); } else if (status === 'requires_source') { + logger.info(`User ${id} payment declined`); void reply.code(402); return reply.send({ error: { @@ -204,12 +220,15 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( } }); + logger.info(`User ${id} has successfully donated`); + return reply.send({ type: 'success', isDonating: true }); } catch (error) { - fastify.log.error(error); + logger.error(`User ${req.user?.id} failed to donate`); + logger.error(error); fastify.Sentry.captureException(error); void reply.code(500); return reply.send({