Skip to content

Commit

Permalink
Merge pull request #43 from andela/ft-chat-feature-167727342
Browse files Browse the repository at this point in the history
#167727342 Chat feature
  • Loading branch information
Hector101 committed Sep 12, 2019
2 parents 4d685c4 + 35852fa commit 00a45ef
Show file tree
Hide file tree
Showing 22 changed files with 884 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .coveralls.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
service_name: travis-pro
repo_token: XNKnyASxAKlbHCdtkGZtyK9fQCHum5Rho
repo_token: XNKnyASxAKlbHCdtkGZtyK9fQCHum5Rho
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@
}
]
}
}
}
29 changes: 29 additions & 0 deletions src/controllers/chatController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { chatEmitter, getChatsEmitter } from '../helpers/notificationHandler';
import { getChats, postChat } from '../services/chatServices';
import { respondWithSuccess, respondWithWarning } from '../helpers/responseHandler';
import statusCode from '../helpers/statusCode';

export const createChat = async (req, res) => {
const payload = { ...req.body, userId: req.auth.id };
try {
const chat = await postChat(payload);
chatEmitter(chat.toJSON());
return respondWithSuccess(res, statusCode.created, 'Chat created successfully', chat.toJSON());
} catch (error) {
return respondWithWarning(res, statusCode.internalServerError, 'Oops something bad happened');
}
};

export const getChatMessages = async (req, res) => {
try {
const chats = await getChats(req.query.sender, req.query.recipient);
if (!chats.length) {
return respondWithWarning(res, statusCode.resourceNotFound, 'Chats not found');
}
const payload = { ...chats, sender: req.query.sender, recipient: req.query.recipient };
getChatsEmitter(payload);
return respondWithSuccess(res, statusCode.success, 'Chat was retrieved successfully', chats);
} catch (error) {
return respondWithWarning(res, statusCode.internalServerError, 'Oops something bad happened');
}
};
3 changes: 1 addition & 2 deletions src/database/migrations/20190821123649-create-users-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ export default {
},
isVerified: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
},
password: {
type: Sequelize.STRING,
allowNull: false,
},
profileImage: {
type: Sequelize.STRING,
},
roleId: {
type: Sequelize.INTEGER,
allowNull: false,
},
createdAt: {
allowNull: false,
Expand Down
37 changes: 37 additions & 0 deletions src/database/migrations/20190911020614-create-chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('Chats', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
},
recipient: {
type: Sequelize.STRING,
allowNull: false,
},
sender: {
type: Sequelize.STRING,
allowNull: false,
},
message: {
type: Sequelize.TEXT,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
}
}),
down: queryInterface => queryInterface.dropTable('Chats')
};
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export default {
{
actionName: 'VIEW_TRIP_COMMENT',
},
{
actionName: 'VIEW_CHAT_MESSAGE',
},
], {}),

down: (queryInterface, Sequelize) => queryInterface.bulkDelete('Permissions', null, {})
Expand Down
8 changes: 8 additions & 0 deletions src/database/seeders/20190825190012-create-users-seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export default {
password: await passwordHash('123456'),
roleId: 5
},
{
firstName: 'John',
lastName: 'Jammy',
username: 'jammjones',
email: 'jammjones@nomad.com',
password: await passwordHash('123456'),
roleId: 6
},
], {}),

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,10 @@ export default {
roleId: 6,
permissionId: 29
},
{
roleId: 1,
permissionId: 30
},
{
roleId: 2,
permissionId: 30
},
{
roleId: 3,
permissionId: 30
},
{
roleId: 4,
permissionId: 30
Expand Down Expand Up @@ -480,6 +472,18 @@ export default {
{
roleId: 6,
permissionId: 42
},
{
roleId: 2,
permissionId: 43
},
{
roleId: 4,
permissionId: 43
},
{
roleId: 6,
permissionId: 43
}
], {}),

Expand Down
78 changes: 78 additions & 0 deletions src/database/seeders/20190911192401-create-chat-seeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
export default {
up: (queryInterface) => queryInterface.bulkInsert('Chats', [
{
userId: 3,
recipient: 'alexiwobi@nomad.com',
sender: 'lionelmessi@nomad.com',
message: 'Hello there ...',
},
{
userId: 4,
recipient: 'lionelmessi@nomad.com',
sender: 'alexiwobi@nomad.com',
message: 'Hello messi, how are you doing?',
},
{
userId: 4,
recipient: 'lionelmessi@nomad.com',
sender: 'alexiwobi@nomad.com',
message: 'I just saw your trip request, i think i need to give you some information on how this whole process goes.',
},
{
userId: 3,
recipient: 'alexiwobi@nomad.com',
sender: 'lionelmessi@nomad.com',
message: 'Ok. I would love you to clarify me on the whole process. What are the things involved for me to have a smooth trip?',
},
{
userId: 4,
recipient: 'lionelmessi@nomad.com',
sender: 'alexiwobi@nomad.com',
message: 'I will be sending you a link. study the information there in and let me know if there are any other things you are not clear about.',
},
{
userId: 3,
recipient: 'alexiwobi@nomad.com',
sender: 'lionelmessi@nomad.com',
message: 'Ok. Thanks. I will look in to it and get back to you.',
},
{
userId: 3,
recipient: 'johndoe@nomad.com',
sender: 'lionelmessi@nomad.com',
message: 'Hello there ...',
},
{
userId: 2,
recipient: 'lionelmessi@nomad.com',
sender: 'johndoe@nomad.com',
message: 'Hello messi, how are you doing?',
},
{
userId: 2,
recipient: 'lionelmessi@nomad.com',
sender: 'johndoe@nomad.com',
message: 'I just saw your trip request, i think i need to give you some information on how this whole process goes.',
},
{
userId: 3,
recipient: 'johndoe@nomad.com',
sender: 'lionelmessi@nomad.com',
message: 'Ok. I would love you to clarify me on the whole process. What are the things involved for me to have a smooth trip?',
},
{
userId: 2,
recipient: 'lionelmessi@nomad.com',
sender: 'johndoe@nomad.com',
message: 'I will be sending you a link. study the information there in and let me know if there are any other things you are not clear about.',
},
{
userId: 3,
recipient: 'alexiwobi@nomad.com',
sender: 'lionelmessi@nomad.com',
message: 'Ok. Thanks. I will look in to it and get back to you.',
},
]),

down: queryInterface => queryInterface.bulkDelete('Chats', null, {})
};
19 changes: 19 additions & 0 deletions src/docs/swagger/definitions/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const createChat = {
type: 'object',
properties: {
sender: {
type: 'string',
format: 'email',
example: 'jammjones@nomad.com',
},
recipient: {
type: 'string',
format: 'email',
example: 'alexiwobi@nomad.com',
},
message: {
type: 'string',
example: 'Hello, i go your comment on my trip request'
}
}
};
128 changes: 128 additions & 0 deletions src/docs/swagger/paths/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
export const postChatPath = {
post: {
tags: [
'chats'
],
security: [
{
BearerToken: []
}
],
summary: 'Manager, Requester or anyone with the permission should be able to chat on the platform',
description: 'A manager, requester or any other person with the permission should be able to chat on the platform',
parameters: [
{
name: 'body',
in: 'body',
description: 'Chat request object',
required: true,
schema: {
$ref: '#/definitions/createChat'
}
}
],
responses: {
201: {
description: 'Chat has been created successfully',
schema: {
$ref: '#/definitions/created'
}
},
400: {
description: 'Invalid request details',
schema: {
$ref: '#/definitions/badRequest'
}
},
401: {
description: 'Unauthorized',
schema: {
$ref: '#/definitions/notAuthorized'
}
},
403: {
description: 'Access forbidden',
schema: {
$ref: '#/definitions/accessForbidden'
}
},
500: {
description: 'Server error',
schema: {
$ref: '#/definitions/serverError'
}
}
}
}
};

export const getChatsPath = {
get: {
tags: [
'chats'
],
security: [
{
BearerToken: []
}
],
summary: 'Manager, Requester or anyone with the permission should be able to get private chats on the platform',
description: 'Manager, Requester or anyone with the permission should be able to get private chats on the platform',
parameters: [
{
in: 'query',
name: 'sender',
description: 'Chat sender query string',
required: true,
type: 'string',
example: 'jammjones@nomad.com'
},
{
in: 'query',
name: 'recipient',
description: 'Chat recipient query string',
required: true,
type: 'string',
example: 'alexiwobi@nomad.com'
}
],
responses: {
200: {
description: 'Chat was retrieved successfully',
schema: {
$ref: '#/definitions/successful'
}
},
400: {
description: 'Invalid request details',
schema: {
$ref: '#/definitions/badRequest'
}
},
401: {
description: 'Unauthorized',
schema: {
$ref: '#/definitions/notAuthorized'
}
},
403: {
description: 'Access forbidden',
schema: {
$ref: '#/definitions/accessForbidden'
}
},
404: {
description: 'Chats not found',
schema: {
$ref: '#/definitions/notFound'
}
},
500: {
description: 'Server error',
schema: {
$ref: '#/definitions/serverError'
}
}
}
}
};
Loading

0 comments on commit 00a45ef

Please sign in to comment.