MCPcopy Index your code
hub / github.com/freeCodeCamp/freeCodeCamp / deleteResetModule

Function deleteResetModule

api/src/routes/protected/user.ts:593–645  ·  view source on GitHub ↗
(
  this: FastifyInstance,
  req: UpdateReqType<typeof schemas.resetModule>,
  reply: UpdateReplyType<typeof schemas.resetModule>
)

Source from the content-addressed store, hash-verified

591};
592
593async function deleteResetModule(
594 this: FastifyInstance,
595 req: UpdateReqType<typeof schemas.resetModule>,
596 reply: UpdateReplyType<typeof schemas.resetModule>
597) {
598 const logger = this.log.child({ req, res: reply });
599
600 const { blockIds } = req.body;
601 logger.info(
602 `User ${req.user?.id} requested module reset for blocks: ${blockIds.join(', ')}`
603 );
604
605 const resetSet = new Set(blockIds.flatMap(getChallengeIdsByBlock));
606
607 if (resetSet.size === 0) {
608 void reply.code(400);
609 return { message: 'No matching blocks found', type: 'error' };
610 }
611
612 const user = await this.prisma.user.findUniqueOrThrow({
613 where: { id: req.user!.id },
614 select: {
615 completedChallenges: true,
616 savedChallenges: true,
617 partiallyCompletedChallenges: true
618 }
619 });
620
621 const filteredCompletedChallenges = normalizeChallenges(
622 user.completedChallenges
623 ).filter(c => !resetSet.has(c.id));
624
625 const filteredSavedChallenges = user.savedChallenges.filter(
626 c => !resetSet.has(c.id)
627 );
628
629 const filteredPartiallyCompletedChallenges =
630 user.partiallyCompletedChallenges.filter(c => !resetSet.has(c.id));
631
632 await this.prisma.user.update({
633 where: { id: req.user!.id },
634 data: {
635 completedChallenges: filteredCompletedChallenges,
636 savedChallenges: filteredSavedChallenges,
637 partiallyCompletedChallenges: filteredPartiallyCompletedChallenges
638 },
639 select: {
640 id: true
641 }
642 });
643
644 return { removedChallengeIds: Array.from(resetSet) };
645}
646
647/**
648 * Generate a new authorization token for the given user, and invalidates any existing tokens.

Callers

nothing calls this directly

Calls 1

normalizeChallengesFunction · 0.85

Tested by

no test coverage detected