src/services/MessageService.js
import Client from '../Client';
import Message from '../helpers/Message';
import {
hasAttribute,
hasToken,
isRequired,
validate,
} from '../utilities/ValidationUtility';
/**
* Service handling Message calls.
*
* @deprecated
*/
class MessageService {
/**
* Retrieves the list of all conversations.
*
* @throws {Clinical6Error} - If missing token
* @return {Promise<Message[]>} - Promise object that returns success or failure
*
* @example
* import { messageService } from 'clinical6';
* messageService.getConversations().then(messages => console.log(messages));
*/
getConversations() {
validate('Clinical6 getConversations',
hasToken());
return new Promise((resolve, reject) => {
Client.instance.fetch(`/api/conversations`).then(
response => resolve(response.conversations.map(obj => new Message(obj))),
err => reject(err)
);
});
}
/**
* Fetches all messages for a conversation and marks the messages as read on next call.
*
* @throws {Clinical6Error} - If missing token or missing required parameters
* @param {String|Number} id - ID of the conversation
* @return {Promise<Message[]>} - Promise object that returns success or failure
*
* @example
* import { messageService } from 'clinical6';
* messageService.getMessages(20).then(messages => console.log(messages));
*
* @example <caption>Failure, User Not Found</caption>
* {
* "error_detail": {
* "conversation": "invalid_value"
* },
* "friendly_error": "Mobile User not found using the value:conversation",
* "internal_code": 50364,
* "more_info": "",
* "status": "fail",
* "message": "Mobile User not found using the value:conversation"
* }
*/
getMessages(id) {
validate('Clinical6 getMessages',
hasToken(),
isRequired({ id }));
return new Promise((resolve, reject) => {
Client.instance.fetch(`/api/conversations/${id}/messages`).then(
response => resolve(response.content.messages.map(obj => new Message(obj))),
err => reject(err)
);
});
}
/**
* Sends a message to recipients.
*
* @throws {Clinical6Error} - If missing token or missing required
* parameters
* @param {!Object} message - Object including the following
* message-based fields:
* @param {!String} message.subject - The subject of the message.
* @param {!String} message.body - The body of the message.
* @param {!Array<String>} message.recipients - Recipients of the message.
* @param {Object} [message.attachment] - Attachment included in the message.
* @param {String} [message.attachment.file_data] - File data for the attachment.
* @param {String} [message.attachment.content_type] - Content type of the attachment.
* @param {String} [message.attachment.file_name] - File name of hte attachment.
* @return {Promise} - Promise object that returns success or
* failure
* @example
* import { messageService } from 'clinical6';
* const message = {
* subject: 'blanditiis',
* body: '0i03jrgt8tinkt4tok5k3oc1w3y40auvi4g5fucxczu0plk6sz4olys2b4o9',
* recipients: ['67']
* };
* messageService.sendMessage(message);
*
* // Returns
* {
* "status": "ok",
* "conversation_id": 1
* }
*
* @example <caption>Failure, Missing Body</caption>
* {
* "error_detail": {
* "body": [
* "missing_field"
* ]
* },
* "friendly_error": "No value found for body on the parameters list",
* "internal_code": 50362,
* "more_info": "",
* "status": "fail",
* "message": "No value found for body on the parameters list"
* }
*
* @example <caption>Failure, Missing Subject</caption>
* {
* "error_detail": {
* "subject": [
* "missing_field"
* ]
* },
* "friendly_error": "No value found for subject on the parameters list",
* "internal_code": 50362,
* "more_info": "",
* "status": "fail",
* "message": "No value found for subject on the parameters list"
* }
*
* @example <caption>Failure, Missing Message</caption>
* {
* "error_detail": {
* "message": [
* "missing_field"
* ]
* },
* "friendly_error": "No value found for message on the parameters list",
* "internal_code": 50362,
* "more_info": "",
* "status": "fail",
* "message": "No value found for message on the parameters list"
* }
*
* @example <caption>Failure, User Not Found</caption>
* {
* "error_detail": {
* "[999]": "invalid_value"
* },
* "friendly_error": "Mobile User not found using the value:[999]",
* "internal_code": 50364,
* "more_info": "",
* "status": "fail",
* "message": "Mobile User not found using the value:[999]"
* }
*/
sendMessage(message) {
validate('Clinical6 sendMessage',
hasToken(),
isRequired({ message }));
// If message exists then check its attributes
if (message) {
validate('Clinical6 sendMessage',
hasAttribute({ message }, 'subject'),
hasAttribute({ message }, 'body'),
hasAttribute({ message }, 'recipients'));
}
const data = new Message(message);
return new Promise((resolve, reject) => {
Client.instance.fetch('/api/messages', 'post', { message: data }).then(
response => resolve(response),
err => reject(err)
);
});
}
}
export default MessageService;