Skip to content

Commit 40beee7

Browse files
committed
ft(slack-channels): add table to store incident-slack channel relation
1 parent 27b7581 commit 40beee7

File tree

7 files changed

+113
-1
lines changed

7 files changed

+113
-1
lines changed

src/server/controllers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const categories = require('./categories');
55
const users = require('./users');
66
const roles = require('./roles');
77
const slackEvents = require('./chats/slackEvents');
8+
const slackChannels = require('./slackChannels');
89
const { catchErrors } = require('./errorLogs');
910

1011
module.exports = {
@@ -15,5 +16,6 @@ module.exports = {
1516
users,
1617
roles,
1718
slackEvents,
19+
slackChannels,
1820
catchErrors,
1921
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const SlackChannel = require('../models').SlackChannels
2+
3+
module.exports = {
4+
create: async (req, res) => {
5+
let { incidentId, channelId, channelName, channelMembers } = req.body
6+
const slackChannel = SlackChannel.create({
7+
incidentId,
8+
channelId,
9+
channelName,
10+
channelMembers
11+
})
12+
.then(response => {
13+
res.status(201).send({ status: "success", data: response })
14+
})
15+
.catch(error => {
16+
res.status(400).send({ status: "failure", error })
17+
})
18+
19+
},
20+
}

src/server/middlewares/schemas/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const incidentSchema = {
5151
statusId: Joi.number(),
5252
subject: Joi.string().required(),
5353
description: Joi.string().required(),
54-
dateOccurred: Joi.date().required(),
54+
dateOccurred: Joi.required(),
5555
levelId: Joi.string().required(),
5656
location: locationSchema,
5757
incidentReporter: incidentReporterSchema,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
module.exports = {
3+
up: (queryInterface, Sequelize) => {
4+
return queryInterface.createTable('SlackChannels', {
5+
id: {
6+
allowNull: false,
7+
autoIncrement: true,
8+
primaryKey: true,
9+
type: Sequelize.INTEGER
10+
},
11+
incidentId: {
12+
type: Sequelize.STRING
13+
},
14+
channelId: {
15+
type: Sequelize.STRING
16+
},
17+
channelName: {
18+
type: Sequelize.STRING
19+
},
20+
channelMembers: {
21+
type: Sequelize.STRING
22+
},
23+
createdAt: {
24+
allowNull: false,
25+
type: Sequelize.DATE
26+
},
27+
updatedAt: {
28+
allowNull: false,
29+
type: Sequelize.DATE
30+
}
31+
});
32+
},
33+
down: (queryInterface, Sequelize) => {
34+
return queryInterface.dropTable('SlackChannels');
35+
}
36+
};

src/server/models/slackchannels.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
module.exports = (sequelize, DataTypes) => {
3+
const SlackChannels = sequelize.define('SlackChannels', {
4+
incidentId: {
5+
type: DataTypes.STRING,
6+
allowNull: false
7+
},
8+
channelId: {
9+
type: DataTypes.STRING,
10+
allowNull: false
11+
},
12+
channelName: {
13+
type: DataTypes.STRING,
14+
allowNull: false
15+
},
16+
channelMembers: DataTypes.STRING
17+
}, {});
18+
SlackChannels.associate = function(models) {
19+
// associations can be defined here
20+
};
21+
return SlackChannels;
22+
};

src/server/routes/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const categoriesService = controllers.categories;
88
const usersService = controllers.users;
99
const rolesService = controllers.roles;
1010
const slackEventsService = controllers.slackEvents;
11+
const slackChannelsService = controllers.slackChannels;
1112
const { catchErrors } = controllers;
1213

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

4143
// slack chats endpoints
4244
app.post( '/api/slack/chats', slackEventsService.createSlackEvent);

src/tests/slackChannel.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { sendRequest } = require('./helpers/request');
2+
3+
const testPayload = {
4+
incidentId: "1",
5+
channelId: "1",
6+
channelName: "Channel name",
7+
channelMembers: "6678"
8+
}
9+
10+
const testPayloadBad = {
11+
channelId: "1",
12+
channelName: "Channel name",
13+
channelMembers: "6678"
14+
}
15+
16+
describe('Slack channel api test', () => {
17+
it('should save incident slack channel relationship', done => {
18+
sendRequest('post', '/api/slack/channel', testPayload, (err, res) => {
19+
expect(res.body.status).toEqual('success');
20+
done();
21+
});
22+
});
23+
24+
it('should return an error when data is not provided', done => {
25+
sendRequest('post', '/api/slack/channel', testPayloadBad, (err, res) => {
26+
expect(res.body.status).toEqual('failure');
27+
done();
28+
});
29+
});
30+
})

0 commit comments

Comments
 (0)