feat(api): logging protected certificate routes (#58933)

This commit is contained in:
Oliver Eyton-Williams
2025-02-24 18:15:23 +01:00
committed by GitHub
parent d15820a2cc
commit a8fd25e9fd
+9 -2
View File
@@ -266,9 +266,11 @@ export const protectedCertificateRoutes: FastifyPluginCallbackTypebox = (
}
},
async (req, reply) => {
const logger = fastify.log.child({ req });
const { certSlug } = req.body;
if (!isKnownCertSlug(certSlug) || !isCertAllowed(certSlug)) {
logger.warn(`Unknown certificate slug "${certSlug}"`);
void reply.code(400);
return {
response: {
@@ -289,7 +291,7 @@ export const protectedCertificateRoutes: FastifyPluginCallbackTypebox = (
if (!user) {
void reply.code(500);
fastify.log.error('User not found');
logger.error(`User with id ${req.user?.id} not found`);
fastify.Sentry.captureException(Error('User not found'));
return {
type: 'danger',
@@ -302,6 +304,7 @@ export const protectedCertificateRoutes: FastifyPluginCallbackTypebox = (
// TODO: Discuss if this is a requirement still
if (!user.name) {
logger.warn(`${user.id} does not have a name property`);
void reply.code(400);
return {
response: {
@@ -314,6 +317,7 @@ export const protectedCertificateRoutes: FastifyPluginCallbackTypebox = (
}
if (user[certType]) {
logger.info(`${user.id} has already claimed ${certName}`);
void reply.code(200);
return {
response: {
@@ -335,6 +339,7 @@ export const protectedCertificateRoutes: FastifyPluginCallbackTypebox = (
);
if (!hasCompletedTestRequirements) {
logger.info(`${user.id} has not completed the tests for ${certName}`);
void reply.code(400);
return {
response: {
@@ -415,14 +420,16 @@ export const protectedCertificateRoutes: FastifyPluginCallbackTypebox = (
// Failed email should not prevent successful response.
try {
logger.info(`Sending congratulations email to ${user.id}`);
// TODO(POST-MVP): Ensure Camper knows they **have** claimed the cert, but the email failed to send.
await fastify.sendEmail(notifyUser);
} catch (e) {
fastify.log.error(e);
logger.error(e);
fastify.Sentry.captureException(e);
}
}
logger.info(`${user.id} has claimed ${certName}`);
void reply.code(200);
return {
response: {