From 96ebb1162bca4b6d68258f9615f3a1dcf8f0242b Mon Sep 17 00:00:00 2001 From: Katya Kuzmenchuk Date: Mon, 31 Oct 2022 14:40:28 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20=20Improve=20notifications=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/notifications.js | 8 ++++---- tests/notifications.test.js | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/routes/notifications.js b/src/routes/notifications.js index d430ea8..defee52 100644 --- a/src/routes/notifications.js +++ b/src/routes/notifications.js @@ -28,18 +28,18 @@ router.use(auth) router.post('/', validate([ body('type').notEmpty().withMessage('Field \'type\' is required and cannot be empty.'), - body('recipients').isArray({ min: 1 }).withMessage('Field \'recipients\' is required and cannot be empty.'), + body('recipient').notEmpty().withMessage('Field \'recipient\' is required and cannot be empty.'), ]), async (req, res) => { - const { type, recipients, payload, action_url } = req.body + const { type, recipient, payload, action_url } = req.body const project = await fetchProjectByKey(req.headers['x-api-key']) - const values = recipients.map(recipient => ({ type, payload, user_id: recipient, project_id: project.id, action_url })); + const values = { type, payload, user_id: recipient, project_id: project.id, action_url }; try { const notifications = await db('notifications').insert(values).returning('id') return res.status(201).json({ notifications }) } catch(e) { - console.error('Failed to create notifications', e) + console.error('Failed to create notification', e) return res.status(500).json({ error: 'internal_server_error', message: `An error occurred while creating new notification: ${e.message}` }) } }) diff --git a/tests/notifications.test.js b/tests/notifications.test.js index a03dcbb..a952c6e 100644 --- a/tests/notifications.test.js +++ b/tests/notifications.test.js @@ -108,7 +108,7 @@ describe("testing /notifications route", () => { done() }) - it('should return 400 if notification recipients are not specified', async done => { + it('should return 400 if notification recipient is not specified', async done => { const data = { type: "test-notification" } @@ -122,15 +122,15 @@ describe("testing /notifications route", () => { expect(errorResult).toStrictEqual({ error: "bad_request", - message: "Field 'recipients' is required and cannot be empty.", + message: "Field 'recipient' is required and cannot be empty.", }) done() }) - it('should return 400 if notification recipients field is empty', async done => { + it('should return 400 if notification recipient field is empty', async done => { const data = { type: "test-notification", - recipients: [] + recipient: "" } const { body: errorResult } = await request(app) .post("/api/notifications") @@ -142,7 +142,7 @@ describe("testing /notifications route", () => { expect(errorResult).toStrictEqual({ error: "bad_request", - message: "Field 'recipients' is required and cannot be empty.", + message: "Field 'recipient' is required and cannot be empty.", }) done() }) @@ -150,7 +150,7 @@ describe("testing /notifications route", () => { it('should return 201 and create notification', async done => { const data = { type: "test-notification", - recipients: ["test"] + recipient: "test" } const { body: result } = await request(app) .post("/api/notifications")