Skip to content

Commit e8ca474

Browse files
authored
Merge pull request #209 from AndelaOSP/ch-merge-slackevents-appchats-tables-167781111
#167781111 Merge slackevents and wire web chats tables
2 parents c799317 + 0bb2c58 commit e8ca474

22 files changed

+327
-32
lines changed

src/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"start:dev": "nodemon ./bin/www",
1111
"debug": "node --inspect ./bin/www",
12-
"seed": "sequelize db:seed:undo:all && sequelize db:seed --seed levels && sequelize db:seed --seed categories && sequelize db:seed --seed status && sequelize db:seed --seed locations && sequelize db:seed --seed roles && sequelize db:seed --seed incidents && sequelize db:seed --seed users && sequelize db:seed --seed reporters && sequelize db:seed --seed witnesses && sequelize db:seed --seed notificationGroups && sequelize db:seed --seed notificationTypes && sequelize db:seed --seed notifications && sequelize db:seed --seed slackUsers && sequelize db:seed --seed slackEvents",
12+
"seed": "sequelize db:seed:undo:all && sequelize db:seed --seed levels && sequelize db:seed --seed categories && sequelize db:seed --seed status && sequelize db:seed --seed locations && sequelize db:seed --seed roles && sequelize db:seed --seed incidents && sequelize db:seed --seed users && sequelize db:seed --seed reporters && sequelize db:seed --seed witnesses && sequelize db:seed --seed notificationGroups && sequelize db:seed --seed notificationTypes && sequelize db:seed --seed notifications && sequelize db:seed --seed slackUsers && sequelize db:seed --seed slackEvents && sequelize db:seed --seed slackChannels",
1313
"migrate": "sequelize db:migrate:undo:all && sequelize db:migrate",
1414
"lint": "./node_modules/.bin/eslint --fix ./server ./test",
1515
"migrate-seed": "npm run migrate && npm run seed",
@@ -26,7 +26,7 @@
2626
"axios": "0.19.0",
2727
"body-parser": "^1.18.2",
2828
"cors": "^2.8.4",
29-
"cuid": "^2.1.0",
29+
"cuid": "^2.1.6",
3030
"dotenv": "^4.0.0",
3131
"express": "^4.16.2",
3232
"joi": "14.3.0",

src/server/controllers/chats/slackEvents.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
const slackEvent = require('../../models/index').slackEvents;
2-
const {slackUserRequest} = require('../../middlewares/index');
1+
/* eslint-disable indent */
2+
const slackEvent = require('../../models/index').SlackWireEvents;
3+
const { slackUserRequest } = require('../../middlewares/index');
4+
const slackUser = require('../../models/index').slackUsers;
35

46
const createSlackEvents = payload => ({
57
id: payload.event_id,
@@ -23,24 +25,25 @@ module.exports = {
2325
createSlackEvent: async (req, res) => {
2426
if (req.body.challenge) {
2527
const data = urlVerification(req);
26-
return res.status(201).send({data, status: 'success'});
28+
return res.status(201).send({ data, status: 'success' });
2729
}
2830
// Get event payload
2931
let payload = req.body;
30-
3132
await slackUserRequest.getSlackUsers(payload.event.user);
32-
33-
slackEvent.create(createSlackEvents(payload))
33+
slackEvent
34+
.create(createSlackEvents(payload))
3435
.then(() => {
35-
slackUserRequest.getSlackChannel(payload.event.channel, payload.event.user);
36-
36+
slackUserRequest.getSlackChannel(
37+
payload.event.channel,
38+
payload.event.user
39+
);
3740
// slack response should be returned within 3 seconds
38-
res.status(201).send({status: 'success'});
41+
res.status(201).send({ status: 'success' });
3942
})
4043
.catch(err => {
4144
res.status(400).send({
4245
status: 'failure',
43-
message: err.errors ? err.errors[0].message : err.message
46+
message: err.errors ? err.errors[0].message : err.message,
4447
});
4548
});
4649
},
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const models = require('../../models/index');
2+
const { postChat } = require('../../middlewares/index');
3+
4+
require('dotenv').config();
5+
6+
const message = {
7+
message: 'there was an error posting your message to slack, try again',
8+
status: 'failure',
9+
};
10+
11+
module.exports = {
12+
// eslint-disable-next-line max-lines-per-function
13+
createAppChat: async (req, res) => {
14+
if (req.body.text.length === 0) {
15+
return res
16+
.status(400)
17+
.send({ message: 'Text is required', status: 'failure' });
18+
}
19+
let channelID;
20+
const { incidentId } = req.body;
21+
try {
22+
const SlackChannel = await models.SlackChannels.findAll({
23+
attributes: ['channelId'],
24+
where: { incidentId: incidentId },
25+
});
26+
const { channelId } = SlackChannel[0];
27+
channelID = channelId;
28+
} catch (err) {
29+
return res
30+
.status(400)
31+
.send({ message: 'incident not found', status: 'failure' });
32+
}
33+
try {
34+
const chat = await models.SlackWireEvents.create({
35+
text: req.body.text,
36+
type: 'web_message',
37+
userId: res.locals.currentUser.id,
38+
channelId: channelID,
39+
});
40+
// post chat to specific slack channel
41+
postChat
42+
.postChat(chat.text, channelID)
43+
.then(response => {
44+
if (response.ok) {
45+
return res.status(201).send({ chat, status: 'success' });
46+
}
47+
return res.status(400).send(message);
48+
})
49+
.catch(() => {
50+
return res.status(400).send(message);
51+
});
52+
} catch (err) {
53+
res.status(500).send({
54+
message:
55+
'sorry an error occured. We could not complete your action, try again',
56+
status: 'failure',
57+
});
58+
}
59+
},
60+
};

src/server/controllers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const roles = require('./roles');
77
const slackEvents = require('./chats/slackEvents');
88
const slackChannels = require('./slackChannels');
99
const { catchErrors } = require('./errorLogs');
10+
const appChats = require('./chats/webChats');
1011

1112
module.exports = {
1213
incidents,
@@ -18,4 +19,5 @@ module.exports = {
1819
slackEvents,
1920
slackChannels,
2021
catchErrors,
22+
appChats,
2123
};

src/server/middlewares/authentication.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const Auth = (req, res, next) => {
2020
message: 'Invalid token provided',
2121
});
2222
}
23-
2423
res.locals.currentUser = decoded;
2524

2625
return next();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const axios = require('axios');
2+
3+
// Read a token from the environment variables
4+
const token = process.env.SLACK_TOKEN;
5+
6+
const postChat = async (text, channel, authToken = token) => {
7+
try {
8+
const res = await axios.post(
9+
`https://slack.com/api/chat.postMessage?token=${authToken}&channel=${channel}&text=${text}`
10+
);
11+
return res.data;
12+
} catch (err) {
13+
throw err;
14+
}
15+
};
16+
17+
module.exports = { postChat };

src/server/middlewares/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const validateUserId = require('./users/validateUserId');
1111
const validateNotePayload = require('./notes/validateNotePayload');
1212
const validateNoteId = require('./notes/validateNoteId');
1313
const slackUserRequest = require('./slack/getSlackInfo');
14-
14+
const postChat = require('./chats/postChat');
1515

1616
module.exports = {
1717
validateIncidentPayload,
@@ -22,5 +22,6 @@ module.exports = {
2222
validateUserId,
2323
validateNotePayload,
2424
validateNoteId,
25-
slackUserRequest
25+
slackUserRequest,
26+
postChat,
2627
};

src/server/middlewares/slack/getSlackInfo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ async function createSlackUser({user: {id, profile}}) {
88
models.slackUsers.create({
99
id,
1010
username: profile.real_name,
11+
email: profile.email,
1112
});
1213
}
1314

src/server/migrations/20190328134329-create-slack-users.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
module.exports = {
4+
// eslint-disable-next-line max-lines-per-function
45
up: (queryInterface, Sequelize) => {
56
return queryInterface.createTable('slackUsers', {
67
id: {
@@ -11,6 +12,13 @@ module.exports = {
1112
username: {
1213
type: Sequelize.STRING
1314
},
15+
email: {
16+
type: Sequelize.STRING,
17+
validate: {
18+
isEmail: true
19+
},
20+
unique: true,
21+
},
1422
channelName: {
1523
type: Sequelize.STRING
1624
},
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
const cuid = require('cuid');
3+
4+
module.exports = {
5+
// eslint-disable-next-line max-lines-per-function
6+
up: (queryInterface, Sequelize) => {
7+
return queryInterface.createTable('SlackWireEvents', {
8+
id: {
9+
allowNull: false,
10+
primaryKey: true,
11+
type: Sequelize.STRING,
12+
defaultValue: () => cuid(),
13+
},
14+
type: {
15+
type: Sequelize.STRING,
16+
},
17+
eventTs: {
18+
type: Sequelize.STRING,
19+
},
20+
userId: {
21+
type: Sequelize.STRING,
22+
},
23+
ts: {
24+
type: Sequelize.STRING,
25+
},
26+
text: {
27+
type: Sequelize.TEXT,
28+
},
29+
channelId: {
30+
type: Sequelize.STRING,
31+
},
32+
channelType: {
33+
type: Sequelize.STRING,
34+
},
35+
createdAt: {
36+
allowNull: false,
37+
type: Sequelize.DATE,
38+
},
39+
updatedAt: {
40+
allowNull: false,
41+
type: Sequelize.DATE,
42+
},
43+
});
44+
},
45+
down: (queryInterface, Sequelize) => {
46+
return queryInterface.dropTable('SlackWireEvents');
47+
},
48+
};

0 commit comments

Comments
 (0)