-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
399 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import models from '../models'; | ||
import response from '../utils/response'; | ||
import DbServices from '../services/dbServices'; | ||
import messages from '../utils/messages'; | ||
import pushChatToFront from '../services/chatServices'; | ||
|
||
const { Chat, User } = models; | ||
const { serverError, chatAdded } = messages; | ||
const { getAllRecord, create, getById } = DbServices; | ||
|
||
/** | ||
* get all chat message in controller | ||
* @param {Object} req - server request | ||
* @param {Object} res - server response | ||
* @returns {Object} - custom response | ||
*/ | ||
const getAllChat = async (req, res) => { | ||
try { | ||
const options = { | ||
order: [ | ||
['createdAt', 'ASC'] | ||
], | ||
include: [{ | ||
model: User, | ||
as: 'sender', | ||
attributes: ['id', 'firstName', 'lastName', 'email', 'phoneNo'] | ||
}], | ||
attributes: { exclude: ['userId'] } | ||
}; | ||
const chats = await getAllRecord(Chat, options); | ||
return response(res, 200, 'success', chats); | ||
} catch (error) { | ||
return response(res, 500, 'error', serverError); | ||
} | ||
}; | ||
|
||
/** | ||
* insert chat message in controller | ||
* @param {Object} req - server request | ||
* @param {Object} res - server response | ||
* @returns {Object} - custom response | ||
*/ | ||
const postChat = async (req, res) => { | ||
try { | ||
const { id: userId } = req.decoded; | ||
const { message } = req.body; | ||
const chat = await create(Chat, { userId, message }); | ||
const userDetails = await getById(User, userId, { attributes: ['id', 'firstName', 'lastName', 'email', 'phoneNo'] }); | ||
const sender = { | ||
firstName: userDetails.firstName, | ||
lastName: userDetails.lastName, | ||
email: userDetails.email, | ||
phoneNo: userDetails.phoneNo | ||
}; | ||
pushChatToFront(message, sender, chat.createdAt); | ||
return response(res, 200, 'success', { message: chatAdded }); | ||
} catch (error) { | ||
return response(res, 500, 'error', serverError); | ||
} | ||
}; | ||
|
||
export default { | ||
getAllChat, postChat | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Chats', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.UUID, | ||
}, | ||
userId: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
}, | ||
}, | ||
message: { | ||
type: Sequelize.TEXT, | ||
allowNull: false | ||
}, | ||
createdAt: { | ||
allowNull: true, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('NOW') | ||
}, | ||
updatedAt: { | ||
allowNull: true, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('NOW') | ||
} | ||
}); | ||
}, | ||
|
||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Chats'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Chat = sequelize.define('Chat', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: DataTypes.UUID, | ||
defaultValue: DataTypes.UUIDV4, | ||
}, | ||
message: { | ||
type: DataTypes.TEXT, | ||
allowNull: false, | ||
}, | ||
}, {}); | ||
|
||
Chat.associate = (models) => { | ||
Chat.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
as: 'sender' | ||
}); | ||
}; | ||
return Chat; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import chatController from '../../controllers/chatController'; | ||
import { checkToken } from '../../middlewares/userMiddlewares'; | ||
import validate from '../../middlewares/validator'; | ||
import chatSchema from '../../validation/chatSchema'; | ||
|
||
const { getAllChat, postChat } = chatController; | ||
const { chatPostSchema } = chatSchema; | ||
|
||
const chatRoutes = (router) => { | ||
router.route('/chat') | ||
/** | ||
* @swagger | ||
* components: | ||
* schemas: | ||
* ChatUser: | ||
* properties: | ||
* email: | ||
* type: string | ||
* firstName: | ||
* type: string | ||
* lastName: | ||
* type: string | ||
* phoneNo: | ||
* type: string | ||
* ChatResponse: | ||
* properties: | ||
* id: | ||
* type: string | ||
* readOnly: true | ||
* sender: | ||
* type: string | ||
* formal: uuid | ||
* message: | ||
* type: string | ||
* createdAt: | ||
* type: string | ||
* format: date-time | ||
* readOnly: true | ||
* updateAt: | ||
* type: string | ||
* format: date-time | ||
* readOnly: true | ||
* User: | ||
* type: array | ||
* items: | ||
* $ref: '#/components/schemas/ChatUser' | ||
* ErrorResponse: | ||
* properties: | ||
* status: | ||
* type: string | ||
* example: error | ||
* data: | ||
* type: object | ||
*/ | ||
/** | ||
* @swagger | ||
* /api/v1/chat: | ||
* get: | ||
* tags: | ||
* - Chats | ||
* description: Get all chats | ||
* produces: | ||
* - application/json | ||
* responses: | ||
* 200: | ||
* description: Get all chats was successful | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* status: | ||
* type: string | ||
* example: success | ||
* data: | ||
* type: array | ||
* description: array of chats | ||
* items: | ||
* $ref: '#/components/schemas/ChatResponse' | ||
* 403: | ||
* description: Unauthorized | ||
* 500: | ||
* description: Internal Server error | ||
* content: | ||
* application/json: | ||
* schema: | ||
* $ref: '#/components/schemas/ErrorResponse' | ||
* security: | ||
* - bearerAuth: [] | ||
*/ | ||
.get(checkToken, getAllChat); | ||
|
||
router.route('/chat') | ||
/** | ||
* @swagger | ||
* components: | ||
* schemas: | ||
* Chat: | ||
* properties: | ||
* message: | ||
* type: string | ||
*/ | ||
|
||
/** | ||
* @swagger | ||
* /api/v1/chat: | ||
* post: | ||
* tags: | ||
* - Chats | ||
* description: Create a new chat message | ||
* produces: | ||
* - application/json | ||
* requestBody: | ||
* description: Chat data object | ||
* required: true | ||
* content: | ||
* application/json: | ||
* schema: | ||
* $ref: '#/components/schemas/Chat' | ||
* responses: | ||
* 200: | ||
* description: Your message was pushed successfully | ||
* 500: | ||
* description: Internal Server error | ||
* security: | ||
* - bearerAuth: [] | ||
*/ | ||
.post(checkToken, validate(chatPostSchema), postChat); | ||
}; | ||
|
||
export default chatRoutes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import client from '../config/pusher'; | ||
|
||
/** | ||
* Helper function to send chat update to browser through pusher | ||
* @param {String} message - message | ||
* @param {String} sender - sender's details | ||
* @param {Date} createdAt - notification body | ||
* @returns {VoidFunction} - pusher | ||
*/ | ||
const pushChatToFront = (message, sender, createdAt) => client.trigger('chatroom', 'message', { | ||
message, sender, createdAt | ||
}); | ||
|
||
export default pushChatToFront; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default { | ||
validChat: { | ||
message: 'my trip to abuja was awesome. Thanks to Sharaton Hotel Abuja', | ||
}, | ||
badChatMessage: { | ||
wrongmessage: 'my trip to abuja was awesome. Thanks to Sharaton Hotel Abuja', | ||
}, | ||
emptyChatMessage: { | ||
message: '', | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import userMock from './userMock'; | ||
import requestMock from './requestMock'; | ||
import roleMock from './roleMock'; | ||
import chatMock from './chatMock'; | ||
|
||
export default { | ||
userMock, | ||
requestMock, | ||
roleMock, | ||
chatMock | ||
}; |
Oops, something went wrong.