From 032f9d95ae2b331e2b693959b5b3d76543964af1 Mon Sep 17 00:00:00 2001 From: Niraj Nandish Date: Tue, 16 May 2023 00:21:32 +0400 Subject: [PATCH] feat(api): add update privacy terms endpoint (#50300) * feat: move update privacy terms route * test: update privacy terms route tests * feat: add /settings prefix to settings route * fix: updated routes in tests * Revert "fix: updated routes in tests" This reverts commit 4a1305e13519aa1ac48d78cf8ec9d2a7efa3d82b. * Revert "feat: add /settings prefix to settings route" This reverts commit 2a0d4566feb8658c59961ae1095cfd83477e9e36. --- api/src/app.ts | 2 -- api/src/routes/settings.test.ts | 38 +++++++++++++++++++++++++++++ api/src/routes/settings.ts | 42 +++++++++++++++++++++++++++++++++ api/src/routes/test.ts | 37 ----------------------------- 4 files changed, 80 insertions(+), 39 deletions(-) delete mode 100644 api/src/routes/test.ts diff --git a/api/src/app.ts b/api/src/app.ts index 0b94fce4357..9b4a05f7d44 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -20,7 +20,6 @@ import cors from './plugins/cors'; import jwtAuthz from './plugins/fastify-jwt-authz'; import security from './plugins/security'; import sessionAuth from './plugins/session-auth'; -import { testRoutes } from './routes/test'; import { settingRoutes } from './routes/settings'; import { auth0Routes, devLoginCallback } from './routes/auth'; import { testMiddleware } from './middleware'; @@ -117,7 +116,6 @@ export const build = async ( void fastify.register(prismaPlugin); - void fastify.register(testRoutes); void fastify.register(auth0Routes, { prefix: '/auth' }); if (FCC_ENABLE_DEV_LOGIN_MODE) { void fastify.register(devLoginCallback, { prefix: '/auth' }); diff --git a/api/src/routes/settings.test.ts b/api/src/routes/settings.test.ts index c3e8afffb76..986b672f31d 100644 --- a/api/src/routes/settings.test.ts +++ b/api/src/routes/settings.test.ts @@ -210,6 +210,36 @@ describe('settingRoutes', () => { expect(response?.statusCode).toEqual(400); }); }); + + describe('/update-privacy-terms', () => { + test('PUT returns 200 status code with "success" message', async () => { + const response = await request(fastify?.server) + .put('/update-privacy-terms') + .set('Cookie', cookies) + .send({ quincyEmails: true }); + + expect(response?.statusCode).toEqual(200); + + expect(response?.body).toEqual({ + message: 'flash.privacy-updated', + type: 'success' + }); + }); + + test('PUT returns 400 status code with non-boolean data', async () => { + const response = await request(fastify?.server) + .put('/update-privacy-terms') + .set('Cookie', cookies) + .send({ quincyEmails: '123' }); + + expect(response?.statusCode).toEqual(400); + expect(response?.body).toEqual({ + error: 'Bad Request', + message: 'body/quincyEmails must be boolean', + statusCode: 400 + }); + }); + }); }); describe('Unauthenticated User', () => { @@ -226,5 +256,13 @@ describe('settingRoutes', () => { expect(response?.statusCode).toEqual(401); }); + + test('PUT /update-privacy-terms returns 401 status code for un-authenticated users', async () => { + const response = await request(fastify?.server).put( + '/update-privacy-terms' + ); + + expect(response?.statusCode).toEqual(401); + }); }); }); diff --git a/api/src/routes/settings.ts b/api/src/routes/settings.ts index 6c046ab999f..1f24230308b 100644 --- a/api/src/routes/settings.ts +++ b/api/src/routes/settings.ts @@ -232,5 +232,47 @@ export const settingRoutes: FastifyPluginCallbackTypebox = ( } } ); + + fastify.put( + '/update-privacy-terms', + { + schema: { + body: Type.Object({ + quincyEmails: Type.Boolean() + }), + response: { + 200: Type.Object({ + message: Type.Literal('flash.privacy-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: { + acceptedPrivacyTerms: true, + sendQuincyEmail: req.body.quincyEmails + } + }); + + return { + message: 'flash.privacy-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(); }; diff --git a/api/src/routes/test.ts b/api/src/routes/test.ts deleted file mode 100644 index 294645bc0bd..00000000000 --- a/api/src/routes/test.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { FastifyPluginCallback } from 'fastify'; - -export const testRoutes: FastifyPluginCallback = (fastify, _options, done) => { - fastify.addHook('onRequest', fastify.authenticateSession); - - fastify.put<{ Body: { quincyEmails: boolean } }>( - '/update-privacy-terms', - { - schema: { - body: { - type: 'object', - required: ['quincyEmails'], - properties: { - quincyEmails: { type: 'boolean' } - } - } - } - }, - async req => { - const { - body: { quincyEmails } - } = req; - - try { - await fastify.prisma.user.update({ - where: { id: req.session.user.id }, - data: { acceptedPrivacyTerms: true, sendQuincyEmail: quincyEmails } - }); - return { msg: 'Successfully updated' }; - } catch (err) { - fastify.log.error(err); - throw { msg: 'Something went wrong' }; - } - } - ); - done(); -};