Navigation Menu

Skip to content

Commit

Permalink
Rewrote get chats to be different for provider and patient
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom White committed Aug 29, 2018
1 parent db8f727 commit 83b3514
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Expand Up @@ -8,7 +8,7 @@
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\app.js",
"program": "${workspaceFolder}/app.js",
"outputCapture": "std",
"skipFiles": [
"${workspaceFolder}/node_modules/**/*.js",
Expand Down
45 changes: 35 additions & 10 deletions controllers/chat.js
Expand Up @@ -36,27 +36,37 @@ module.exports.ChatApiResource = class ChatApiResource {
permission_group: "user",
routes: [
{
"path": "chat/:uid",
"path": "chat/provider/:uid",
"get" : {
demand: security.PermissionType.READ,
method: this.getChatRoomsProviders
}
},
{
"path": "chat/patient/:uid",
"get" : {
demand: security.PermissionType.LIST,
method: this.getChatRooms
},
demand: security.PermissionType.READ,
method: this.getChatRoomsPatients
}
},
{
"path": "chat/:uid",
"post" : {
demand: security.PermissionType.WRITE,
demand: security.PermissionType.READ,
method: this.createChatRoom
}
},
{
"path": "chat/:cid/messages",
"get" : {
demand: security.PermissionType.LIST,
demand: security.PermissionType.READ,
method: this.getChatMessages
}
},
{
"path": "listen/:cid",
"get" : {
demand: security.PermissionType.LIST,
demand: security.PermissionType.READ,
method: this.initChatSocket
}
}
Expand Down Expand Up @@ -108,15 +118,30 @@ module.exports.ChatApiResource = class ChatApiResource {

/**
* @method
* @summary Get a list of chatrooms that a user is a part of
* @summary Get a list of chatrooms that a provider is a part of
* @param {Express.Request} req http req from the client
* @param {Express.Response} res The HTTP response going to the client
*/
async getChatRoomsProviders(req, res) {
console.log(req.params.uid);
if(!req.params.uid)
throw new exception.Exception("Missing chat user id parameter", exception.ErrorCodes.MISSING_PROPERTY);

res.status(200).json(await uhx.Repositories.chatRepository.getChatRoomsProviders(req.params.uid));
return true;
}

/**
* @method
* @summary Get a list of chatrooms that a patient is a part of
* @param {Express.Request} req http req from the client
* @param {Express.Response} res The HTTP response going to the client
*/
async getChatRooms(req, res) {
async getChatRoomsPatients(req, res) {
if(!req.params.uid)
throw new exception.Exception("Missing chat user id parameter", exception.ErrorCodes.MISSING_PROPERTY);

res.status(200).json(await uhx.Repositories.chatRepository.getChatRooms(req.params.uid));
res.status(200).json(await uhx.Repositories.chatRepository.getChatRoomsPatients(req.params.uid));
return true;
}

Expand Down
36 changes: 33 additions & 3 deletions repository/chatRepository.js
Expand Up @@ -39,7 +39,8 @@ module.exports = class ChatRepository {
constructor(connectionString) {
this._connectionString = connectionString;
this.createChatRoom = this.createChatRoom.bind(this);
this.getChatRooms = this.getChatRooms.bind(this);
this.getChatRoomsPatients = this.getChatRoomsPatients.bind(this);
this.getChatRoomsProviders = this.getChatRoomsProviders.bind(this);
this.createChatMessage = this.createChatMessage.bind(this);
this.getChatMessages = this.getChatMessages.bind(this);
}
Expand All @@ -66,10 +67,10 @@ module.exports = class ChatRepository {

/**
* @method
* @summary gets chatrooms associated with specific user
* @summary gets chatrooms associated with specific patient
* @param {string} userId The user associated with the chat rooms
*/
async getChatRooms(userId) {
async getChatRoomsPatients(userId) {
const dbc = new pg.Client(this._connectionString);
try {
let userChats = [];
Expand All @@ -93,6 +94,35 @@ module.exports = class ChatRepository {
}
}

/**
* @method
* @summary gets chatrooms associated with specific provider
* @param {string} userId The user associated with the chat rooms
*/
async getChatRoomsProviders(userId) {
const dbc = new pg.Client(this._connectionString);
try {
let userChats = [];
await dbc.connect();
let userChatsFromDB = await dbc.query(`SELECT cr.id, cr.title, cr.providerid, cr.patientid, p.name, pt.given_name, pt.family_name
FROM public.chat_room as cr
LEFT JOIN providers as p ON CAST(cr.providerid as text) = CAST(p.id as text)
LEFT JOIN patients as pt ON CAST(cr.patientid as text) = CAST(pt.id as text)
WHERE cr.providerid = $1`, [userId])


for(var r in userChatsFromDB.rows) {
userChats.push(new ChatRoom().fromData(userChatsFromDB.rows[r]));
}

return userChats;
}
catch(err){console.log(err)}
finally {
dbc.end();
}
}

/**
* @method
* @summary Creates a chat message
Expand Down

0 comments on commit 83b3514

Please sign in to comment.