From c7f5a4cf8b2e7e9df95ecd841ed8369fa2b20887 Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Tue, 9 May 2023 19:29:48 +0300 Subject: [PATCH] feat(api): add update IsHonest value endpoint (#50281) Co-authored-by: Oliver Eyton-Williams --- api/src/routes/settings.test.ts | 25 +++++++++++++++++++++ api/src/routes/settings.ts | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/api/src/routes/settings.test.ts b/api/src/routes/settings.test.ts index 10e2d86c1f5..ab7a0b37127 100644 --- a/api/src/routes/settings.test.ts +++ b/api/src/routes/settings.test.ts @@ -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', () => { diff --git a/api/src/routes/settings.ts b/api/src/routes/settings.ts index 2424fe3c3ff..6c046ab999f 100644 --- a/api/src/routes/settings.ts +++ b/api/src/routes/settings.ts @@ -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(); };