From 885cf86cd6889c8884bb9314fa78546e08dce06d Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Thu, 18 May 2023 16:29:43 +0300 Subject: [PATCH] feat(api): add socials links endpoint (#50332) * feat(api): add socials links endpoint * Sort the typo in body call Co-authored-by: Naomi Carrigan * Revert "Sort the typo in body call" This reverts commit 0588d3d70de708d4d54f0b86d99250aef292c245. --------- Co-authored-by: Naomi Carrigan --- api/src/routes/settings.test.ts | 35 +++++++++++++++++++++++++ api/src/routes/settings.ts | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/api/src/routes/settings.test.ts b/api/src/routes/settings.test.ts index 24bd2a670d2..d8f3d214542 100644 --- a/api/src/routes/settings.test.ts +++ b/api/src/routes/settings.test.ts @@ -211,6 +211,41 @@ describe('settingRoutes', () => { }); }); + describe('/update-my-socials', () => { + test('PUT returns 200 status code with "success" message', async () => { + const response = await request(fastify?.server) + .put('/update-my-socials') + .set('Cookie', cookies) + .send({ + website: 'https://www.freecodecamp.org/', + twitter: 'https://twitter.com/ossia', + linkedin: 'https://www.linkedin.com/in/quincylarson', + githubProfile: 'https://github.com/QuincyLarson' + }); + + expect(response?.statusCode).toEqual(200); + + expect(response?.body).toEqual({ + message: 'flash.updated-socials', + type: 'success' + }); + }); + + test('PUT returns 400 status code with invalid socials setting', async () => { + const response = await request(fastify?.server) + .put('/update-my-socials') + .set('Cookie', cookies) + .send({ + website: 'invalid', + twitter: 'invalid', + linkedin: 'invalid', + githubProfile: 'invalid' + }); + + expect(response?.statusCode).toEqual(400); + }); + }); + describe('/update-my-quincy-email', () => { test('PUT returns 200 status code with "success" message', async () => { const response = await superRequest('/update-my-quincy-email', { diff --git a/api/src/routes/settings.ts b/api/src/routes/settings.ts index 3ccc8784bcf..9a5f9a3be92 100644 --- a/api/src/routes/settings.ts +++ b/api/src/routes/settings.ts @@ -117,6 +117,52 @@ export const settingRoutes: FastifyPluginCallbackTypebox = ( } ); + fastify.put( + '/update-my-socials', + { + schema: { + body: Type.Object({ + website: Type.Optional(Type.String({ format: 'url' })), + twitter: Type.Optional(Type.String({ format: 'url' })), + githubProfile: Type.Optional(Type.String({ format: 'url' })), + linkedin: Type.Optional(Type.String({ format: 'url' })) + }), + response: { + 200: Type.Object({ + message: Type.Literal('flash.updated-socials'), + 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: { + website: req.body.website, + twitter: req.body.twitter, + githubProfile: req.body.githubProfile, + linkedin: req.body.linkedin + } + }); + + return { + message: 'flash.updated-socials', + type: 'success' + } as const; + } catch (err) { + fastify.log.error(err); + void reply.code(500); + return { message: 'flash.wrong-updating', type: 'danger' } as const; + } + } + ); + fastify.put( '/update-my-keyboard-shortcuts', {