mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-06-28 21:00:56 +08:00
feat(api): add update IsHonest value endpoint (#50281)
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
parent
77769db9af
commit
c7f5a4cf8b
@ -184,6 +184,31 @@ describe('settingRoutes', () => {
|
||||
expect(response?.statusCode).toEqual(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('/update-my-honesty', () => {
|
||||
test('PUT returns 200 status code with "success" message', async () => {
|
||||
const response = await request(fastify?.server)
|
||||
.put('/update-my-honesty')
|
||||
.set('Cookie', cookies)
|
||||
.send({ isHonest: true });
|
||||
|
||||
expect(response?.statusCode).toEqual(200);
|
||||
|
||||
expect(response?.body).toEqual({
|
||||
message: 'buttons.accepted-honesty',
|
||||
type: 'success'
|
||||
});
|
||||
});
|
||||
|
||||
test('PUT returns 400 status code with invalid honesty', async () => {
|
||||
const response = await request(fastify?.server)
|
||||
.put('/update-my-honesty')
|
||||
.set('Cookie', cookies)
|
||||
.send({ isHonest: false });
|
||||
|
||||
expect(response?.statusCode).toEqual(400);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Unauthenticated User', () => {
|
||||
|
||||
@ -193,5 +193,44 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
|
||||
}
|
||||
);
|
||||
|
||||
fastify.put(
|
||||
'/update-my-honesty',
|
||||
{
|
||||
schema: {
|
||||
body: Type.Object({
|
||||
isHonest: Type.Literal(true)
|
||||
}),
|
||||
response: {
|
||||
200: Type.Object({
|
||||
message: Type.Literal('buttons.accepted-honesty'),
|
||||
type: Type.Literal('success')
|
||||
}),
|
||||
500: Type.Object({
|
||||
message: Type.Literal('flash.wrong-updating'),
|
||||
type: Type.Literal('danger')
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
async (req, reply) => {
|
||||
try {
|
||||
await fastify.prisma.user.update({
|
||||
where: { id: req.session.user.id },
|
||||
data: {
|
||||
isHonest: req.body.isHonest
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
message: 'buttons.accepted-honesty',
|
||||
type: 'success'
|
||||
} as const;
|
||||
} catch (err) {
|
||||
fastify.log.error(err);
|
||||
void reply.code(500);
|
||||
return { message: 'flash.wrong-updating', type: 'danger' } as const;
|
||||
}
|
||||
}
|
||||
);
|
||||
done();
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user