Skip to content

Commit

Permalink
Merge pull request #205 from AndelaOSP/ft-incident-slack-channel-1663…
Browse files Browse the repository at this point in the history
…95417

#166395417 Incident-slack channel relation API endpoint
  • Loading branch information
brandeddavid authored Jul 9, 2019
2 parents 27b7581 + 40beee7 commit 7c63683
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/server/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const categories = require('./categories');
const users = require('./users');
const roles = require('./roles');
const slackEvents = require('./chats/slackEvents');
const slackChannels = require('./slackChannels');
const { catchErrors } = require('./errorLogs');

module.exports = {
Expand All @@ -15,5 +16,6 @@ module.exports = {
users,
roles,
slackEvents,
slackChannels,
catchErrors,
};
20 changes: 20 additions & 0 deletions src/server/controllers/slackChannels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const SlackChannel = require('../models').SlackChannels

module.exports = {
create: async (req, res) => {
let { incidentId, channelId, channelName, channelMembers } = req.body
const slackChannel = SlackChannel.create({
incidentId,
channelId,
channelName,
channelMembers
})
.then(response => {
res.status(201).send({ status: "success", data: response })
})
.catch(error => {
res.status(400).send({ status: "failure", error })
})

},
}
2 changes: 1 addition & 1 deletion src/server/middlewares/schemas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const incidentSchema = {
statusId: Joi.number(),
subject: Joi.string().required(),
description: Joi.string().required(),
dateOccurred: Joi.date().required(),
dateOccurred: Joi.required(),
levelId: Joi.string().required(),
location: locationSchema,
incidentReporter: incidentReporterSchema,
Expand Down
36 changes: 36 additions & 0 deletions src/server/migrations/20190601184111-create-slack-channels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('SlackChannels', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
incidentId: {
type: Sequelize.STRING
},
channelId: {
type: Sequelize.STRING
},
channelName: {
type: Sequelize.STRING
},
channelMembers: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('SlackChannels');
}
};
22 changes: 22 additions & 0 deletions src/server/models/slackchannels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const SlackChannels = sequelize.define('SlackChannels', {
incidentId: {
type: DataTypes.STRING,
allowNull: false
},
channelId: {
type: DataTypes.STRING,
allowNull: false
},
channelName: {
type: DataTypes.STRING,
allowNull: false
},
channelMembers: DataTypes.STRING
}, {});
SlackChannels.associate = function(models) {
// associations can be defined here
};
return SlackChannels;
};
2 changes: 2 additions & 0 deletions src/server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const categoriesService = controllers.categories;
const usersService = controllers.users;
const rolesService = controllers.roles;
const slackEventsService = controllers.slackEvents;
const slackChannelsService = controllers.slackChannels;
const { catchErrors } = controllers;

// authorise routes
Expand Down Expand Up @@ -37,6 +38,7 @@ module.exports = app => {
//no auth needed
app.post('/api/incidents', validateIncidentPayload, incidentsService.create);
app.post('/api/users/login', usersService.login);
app.post('/api/slack/channel', slackChannelsService.create);

// slack chats endpoints
app.post( '/api/slack/chats', slackEventsService.createSlackEvent);
Expand Down
30 changes: 30 additions & 0 deletions src/tests/slackChannel.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { sendRequest } = require('./helpers/request');

const testPayload = {
incidentId: "1",
channelId: "1",
channelName: "Channel name",
channelMembers: "6678"
}

const testPayloadBad = {
channelId: "1",
channelName: "Channel name",
channelMembers: "6678"
}

describe('Slack channel api test', () => {
it('should save incident slack channel relationship', done => {
sendRequest('post', '/api/slack/channel', testPayload, (err, res) => {
expect(res.body.status).toEqual('success');
done();
});
});

it('should return an error when data is not provided', done => {
sendRequest('post', '/api/slack/channel', testPayloadBad, (err, res) => {
expect(res.body.status).toEqual('failure');
done();
});
});
})

0 comments on commit 7c63683

Please sign in to comment.