Repository: freeCodeCamp/freeCodeCamp
File: api-server/src/server/boot/user.js (JavaScript)

Code before:
  const transcriptUrlRegex = /\/transcript\/([^/]+)\/?/;
  const id = transcriptUrlRegex.exec(url.pathname)?.[1];
  return `https://learn.microsoft.com/api/profiles/transcript/share/${
    id ?? ''
  }`;
};

function createPostMsUsername(app) {
  const { MsUsername } = app.models;

  return async function postMsUsername(req, res) {
    // example msTranscriptUrl: https://learn.microsoft.com/en-us/users/mot01/transcript/8u6awert43q1plo
    // the last part is the transcript ID
    // the username is irrelevant, and retrieved from the MS API response

    const { msTranscriptUrl } = req.body;

    if (!msTranscriptUrl) {
      return res.status(400).json({
        type: 'error',
        message: 'flash.ms.transcript.link-err-1'
      });
    }

    const msTranscriptId = getMsTranscriptApiUrl(msTranscriptUrl);
    const msTranscriptApiUrl = `https://learn.microsoft.com/api/profiles/transcript/share/${msTranscriptId}`;

    try {
      const msApiRes = await fetch(msTranscriptApiUrl);

      if (!msApiRes.ok) {
        return res
          .status(404)
          .json({ type: 'error', message: 'flash.ms.transcript.link-err-2' });
      }

      const { userName } = await msApiRes.json();

      if (!userName) {
        return res
          .status(500)
          .json({ type: 'error', message: 'flash.ms.transcript.link-err-3' });
      }

      // Don't create if username is used by another fCC account
      const usernameUsed = await MsUsername.findOne({
        where: { msUsername: userName }
      });

      if (usernameUsed) {
        return res

Review comment (suggestion):
```suggestion
    const msTranscriptApiUrl = getMsTranscriptApiUrl(msTranscriptUrl);
```

The new function is returning the whole URL, not the transcript ID. So we were making requests to `https://learn.microsoft.com/api/profiles/transcript/share/https://learn.microsoft.com/api/profiles/transcript/share/abcdefghijklmno` (not correct)

Code after:
  const transcriptUrlRegex = /\/transcript\/([^/]+)\/?/;
  const id = transcriptUrlRegex.exec(url.pathname)?.[1];
  return `https://learn.microsoft.com/api/profiles/transcript/share/${
    id ?? ''
  }`;
};

function createPostMsUsername(app) {
  const { MsUsername } = app.models;

  return async function postMsUsername(req, res) {
    // example msTranscriptUrl: https://learn.microsoft.com/en-us/users/mot01/transcript/8u6awert43q1plo
    // the last part is the transcript ID
    // the username is irrelevant, and retrieved from the MS API response

    const { msTranscriptUrl } = req.body;

    if (!msTranscriptUrl) {
      return res.status(400).json({
        type: 'error',
        message: 'flash.ms.transcript.link-err-1'
      });
    }

    const msTranscriptApiUrl = getMsTranscriptApiUrl(msTranscriptUrl);

    try {
      const msApiRes = await fetch(msTranscriptApiUrl);

      if (!msApiRes.ok) {
        return res
          .status(404)
          .json({ type: 'error', message: 'flash.ms.transcript.link-err-2' });
      }

      const { userName } = await msApiRes.json();

      if (!userName) {
        return res
          .status(500)
          .json({ type: 'error', message: 'flash.ms.transcript.link-err-3' });
      }

      // Don't create if username is used by another fCC account
      const usernameUsed = await MsUsername.findOne({
        where: { msUsername: userName }
      });

      if (usernameUsed) {
        return res
          .status(403)