From afd4402a49bd58c679d5f531cbeace6ce56f122d Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Tue, 9 May 2023 12:10:39 +0300 Subject: [PATCH] feat(api): add subscribe to quincy email endpoint (#50305) --- api/src/routes/settings.test.ts | 25 ++++++++++++++++++++ api/src/routes/settings.ts | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/api/src/routes/settings.test.ts b/api/src/routes/settings.test.ts index ecf1d3ae183..10e2d86c1f5 100644 --- a/api/src/routes/settings.test.ts +++ b/api/src/routes/settings.test.ts @@ -159,6 +159,31 @@ describe('settingRoutes', () => { expect(response?.statusCode).toEqual(400); }); }); + + describe('/update-my-quincy-email', () => { + test('PUT returns 200 status code with "success" message', async () => { + const response = await request(fastify?.server) + .put('/update-my-quincy-email') + .set('Cookie', cookies) + .send({ sendQuincyEmail: true }); + + expect(response?.statusCode).toEqual(200); + + expect(response?.body).toEqual({ + message: 'flash.subscribe-to-quincy-updated', + type: 'success' + }); + }); + + test('PUT returns 400 status code with invalid sendQuincyEmail', async () => { + const response = await request(fastify?.server) + .put('/update-my-quincy-email') + .set('Cookie', cookies) + .send({ sendQuincyEmail: 'invalid' }); + + expect(response?.statusCode).toEqual(400); + }); + }); }); describe('Unauthenticated User', () => { diff --git a/api/src/routes/settings.ts b/api/src/routes/settings.ts index 05bc50836ea..2424fe3c3ff 100644 --- a/api/src/routes/settings.ts +++ b/api/src/routes/settings.ts @@ -152,5 +152,46 @@ export const settingRoutes: FastifyPluginCallbackTypebox = ( } } ); + + fastify.put( + '/update-my-quincy-email', + { + schema: { + body: Type.Object({ + sendQuincyEmail: Type.Boolean() + }), + response: { + 200: Type.Object({ + message: Type.Literal('flash.subscribe-to-quincy-updated'), + 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: { + sendQuincyEmail: req.body.sendQuincyEmail + } + }); + + return { + message: 'flash.subscribe-to-quincy-updated', + type: 'success' + } as const; + } catch (err) { + fastify.log.error(err); + void reply.code(500); + return { message: 'flash.wrong-updating', type: 'danger' } as const; + } + } + ); + done(); };