Skip to content

Commit

Permalink
fix(api): send msUsername in get session user endpoint (freeCodeCamp#…
Browse files Browse the repository at this point in the history
…54050)

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
  • Loading branch information
Nirajn2311 and ShaunSHamilton authored Mar 28, 2024
1 parent c333a74 commit f6ae52f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
17 changes: 16 additions & 1 deletion api/src/routes/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ describe('userRoutes', () => {
});
});

describe('user/get-user-session', () => {
describe('/user/get-user-session', () => {
beforeEach(async () => {
await fastifyTestInstance.prisma.user.updateMany({
where: { email: testUserData.email },
Expand Down Expand Up @@ -590,6 +590,21 @@ describe('userRoutes', () => {
expect(tokenData.id).toBe(userToken);
});

test('GET returns the msUsername if it exists', async () => {
await fastifyTestInstance.prisma.msUsername.create({
data: msUsernameData[0] as (typeof msUsernameData)[0]
});

const msUsernames = await fastifyTestInstance.prisma.msUsername.count();
expect(msUsernames).toBe(1);

const response = await superGet('/user/get-session-user');

const { msUsername } = response.body.user.foobar;

expect(msUsername).toBe(msUsernameData[0]?.msUsername);
});

test('GET returns a minimal user when all optional properties are missing', async () => {
// To get a minimal test user we first delete the existing one...
await fastifyTestInstance.prisma.user.deleteMany({
Expand Down
19 changes: 13 additions & 6 deletions api/src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,17 @@ export const userGetRoutes: FastifyPluginCallbackTypebox = (
where: { userId: req.user!.id }
});

const [userToken, user, completedSurveys] = await Promise.all([
userTokenP,
userP,
completedSurveysP
]);
const msUsernameP = fastify.prisma.msUsername.findFirst({
where: { userId: req.user?.id }
});

const [userToken, user, completedSurveys, msUsername] =
await Promise.all([
userTokenP,
userP,
completedSurveysP,
msUsernameP
]);

if (!user?.username) {
void res.code(500);
Expand Down Expand Up @@ -556,7 +562,8 @@ export const userGetRoutes: FastifyPluginCallbackTypebox = (
twitter: normalizeTwitter(twitter),
username: usernameDisplay || username,
userToken: encodedToken,
completedSurveys: normalizeSurveys(completedSurveys)
completedSurveys: normalizeSurveys(completedSurveys),
msUsername: msUsername?.msUsername
}
},
result: user.username
Expand Down
3 changes: 2 additions & 1 deletion api/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ export const schemas = {
})
)
})
)
),
msUsername: Type.Optional(Type.String())
})
),
result: Type.String()
Expand Down

0 comments on commit f6ae52f

Please sign in to comment.