feat(api/client): use server response as flash msg for c# (#51551)

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Tom
2023-09-25 08:24:53 -05:00
committed by GitHub
parent 5cc501bb0e
commit 0f49460547
9 changed files with 109 additions and 55 deletions

View File

@@ -701,7 +701,9 @@ function createMsTrophyChallengeCompleted(app) {
});
if (!msUser || !msUser.msUsername) {
throw new Error('Microsoft username not found.');
return res
.status(403)
.json({ type: 'error', message: 'flash.ms.trophy.err-1' });
}
const { msUsername } = msUser;
@@ -711,15 +713,24 @@ function createMsTrophyChallengeCompleted(app) {
);
if (!challenge) {
throw new Error('Challenge not found');
return res
.status(400)
.json({ type: 'error', message: 'flash.ms.trophy.err-2' });
}
const { msTrophyId = '' } = challenge;
const msTrophyApiUrl = `https://learn.microsoft.com/api/gamestatus/achievements/${msTrophyId}?username=${msUsername}&locale=en-us`;
const msApiRes = await fetch(msTrophyApiUrl);
const msTrophyJson = await msApiRes.json();
if (!msApiRes.ok) {
throw new Error('Unable to validate trophy');
if (!msApiRes.ok || msTrophyJson.awardType !== 'Trophy') {
return res.status(403).json({
type: 'error',
message: 'flash.ms.trophy.err-3',
variables: {
msUsername
}
});
}
const completedChallenge = pick(body, ['id']);
@@ -751,9 +762,10 @@ function createMsTrophyChallengeCompleted(app) {
});
});
} catch (e) {
log(e);
return res.status(500).json({
type: 'error',
message: e.message
message: 'flash.ms.trophy.err-4'
});
}
};

View File

@@ -123,9 +123,10 @@ function createPostMsUsername(app) {
const { msTranscriptUrl } = req.body;
if (!msTranscriptUrl) {
return res
.status(400)
.send('Please include a Microsoft transcript URL in request');
return res.status(400).json({
type: 'error',
message: 'flash.ms.transcript.link-err-1'
});
}
const msTranscriptId = msTranscriptUrl.split('/').pop();
@@ -135,19 +136,17 @@ function createPostMsUsername(app) {
const msApiRes = await fetch(msTranscriptApiUrl);
if (!msApiRes.ok) {
res.status(500);
throw new Error(
'An error occurred trying to get your Microsoft transcript'
);
return res
.status(404)
.json({ type: 'error', message: 'flash.ms.transcript.link-err-2' });
}
const { userName } = await msApiRes.json();
if (!userName) {
res.status(500);
throw new Error(
'An error occured trying to link your Microsoft account'
);
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
@@ -156,7 +155,9 @@ function createPostMsUsername(app) {
});
if (usernameUsed) {
throw new Error('That username is already used');
return res
.status(403)
.json({ type: 'error', message: 'flash.ms.transcript.link-err-4' });
}
await MsUsername.destroyAll({ userId: req.user.id });
@@ -169,25 +170,27 @@ function createPostMsUsername(app) {
});
if (!newMsUsername?.id) {
res.status(500);
throw new Error(
'An error occured trying to link your Microsoft account'
);
return res
.status(500)
.json({ type: 'error', message: 'flash.ms.transcript.link-err-5' });
}
return res.json({ msUsername: userName });
} catch (e) {
log(e);
return res.send(e.message);
return res
.status(500)
.json({ type: 'error', message: 'flash.ms.transcript.link-err-6' });
}
};
}
function deleteMsUsernameResponse(req, res) {
if (!req.msUsernameDeleted) {
return res
.status(500)
.send('An error occurred trying to unlink your Microsoft username');
return res.status(500).json({
type: 'error',
message: 'flash.ms.transcript.unlink-err'
});
}
return res.send({ msUsername: null });