Repository: freeCodeCamp/freeCodeCamp
File: api/src/utils/common-challenge-functions.ts (TypeScript)

Code before:
 * @param challengeId The id of the submitted challenge.
 * @param user The existing user record.
 * @param challenge The saveble challenge.
 * @returns Saved challenges.
 */
export function saveUserChallengeData(
  challengeId: string,
  user: user,
  challenge: Omit<SavedChallenge, 'lastSavedDate'>
) {
  const challengeToSave: SavedChallenge = {
    id: challengeId,
    lastSavedDate: Date.now(),
    files: challenge.files?.map(file =>
      pick(file, ['contents', 'key', 'name', 'ext', 'history'])
    ) as SavedChallengeFile[]
  };

  const savedIndex = user.savedChallenges.findIndex(
    ({ id }) => challengeId === id
  );

  if (savedIndex >= 0) {
    user.savedChallenges[savedIndex] = challengeToSave;
  } else {
    user.savedChallenges.push(challengeToSave);
  }

  return user.savedChallenges;
}

/**
 * Helper function to update a user's challenge data. Used in challenge
 * submission endpoints.
 *
 * @deprecated Create specific functions for each submission endpoint.
 * @param fastify The Fastify instance.
 * @param user The existing user record.
 * @param challengeId The id of the submitted challenge.
 * @param _completedChallenge The challenge submission.
 * @param timezone The user's timezone.
 * @returns Information about the update.
 */
export async function updateUserChallengeData(
  fastify: FastifyInstance,
  user: user,
  challengeId: string,
  _completedChallenge: CompletedChallenge,
  timezone?: string // TODO: is this required as its not given as a arg anywhere
) {
  const { files, completedDate: newProgressTimeStamp = Date.now() } =

Review comment (suggestion):
```suggestion
    savedChallenges[savedIndex] = challengeToSave;
  } else {
    savedChallenges.push(challengeToSave);
```

Code after:
 * @param challengeId The id of the submitted challenge.
 * @param savedChallenges The user's saved challenges array.
 * @param challenge The saveble challenge.
 * @returns Update or push the saved challenges.
 */
export function saveUserChallengeData(
  challengeId: string,
  savedChallenges: SavedChallenge[],
  challenge: Omit<SavedChallenge, 'lastSavedDate'>
) {
  const challengeToSave: SavedChallenge = {
    id: challengeId,
    lastSavedDate: Date.now(),
    files: challenge.files?.map(file =>
      pick(file, ['contents', 'key', 'name', 'ext', 'history'])
    )
  };

  const savedIndex = savedChallenges.findIndex(({ id }) => challengeId === id);

  if (savedIndex >= 0) {
    savedChallenges[savedIndex] = challengeToSave;
  } else {
    savedChallenges.push(challengeToSave);
  }

  return savedChallenges;
}

/**
 * Helper function to update a user's challenge data. Used in challenge
 * submission endpoints.
 *
 * @deprecated Create specific functions for each submission endpoint.
 * @param fastify The Fastify instance.
 * @param user The existing user record.
 * @param challengeId The id of the submitted challenge.
 * @param _completedChallenge The challenge submission.
 * @param timezone The user's timezone.
 * @returns Information about the update.
 */
export async function updateUserChallengeData(
  fastify: FastifyInstance,
  user: user,
  challengeId: string,
  _completedChallenge: CompletedChallenge,
  timezone?: string // TODO: is this required as its not given as a arg anywhere
) {
  const { files, completedDate: newProgressTimeStamp = Date.now() } =
    _completedChallenge;
  let completedChallenge: CompletedChallenge;