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")