mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(api): logging protected donation routes (#58934)
This commit is contained in:
committed by
GitHub
parent
c2df388d93
commit
d15820a2cc
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user