Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/routes/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}` })
}
})
Expand Down
12 changes: 6 additions & 6 deletions tests/notifications.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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")
Expand All @@ -142,15 +142,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 201 and create notification', async done => {
const data = {
type: "test-notification",
recipients: ["test"]
recipient: "test"
}
const { body: result } = await request(app)
.post("/api/notifications")
Expand Down