src/services/SystemService.js
import Client from '../Client';
import {
hasAttribute,
hasToken,
isRequired,
validate,
} from '../utilities/ValidationUtility';
/**
* Service handling System Utility calls.
*
*/
class SystemService {
/**
* Tracks a new event in the log.
*
* @throws {Clinical6Error} - If missing token or missing required
* parameters
* @param {Object} [logEntry] - The new log entry to track. (optional)
* @param {String} [logEntry.message] - The message of the log entry. (optional)
* @param {String} [logEntry.tag] - The tag value of the log entry.
* (optional)
* @param {String} logEntry.level - Indicates the level of importance of
* the log entry. (required)
* @param {Object} [requestInformation] - The request information of the API call.
* (optional)
* @param {String} [requestInformation.method] - The method of the request. (optional)
* @param {String} [requestInformation.params] - The body or parameters that were sent on
* the failing/debugging request.
* (optional)
* @param {String} [requestInformationtry.status_code] - The response of the request. (optional)
* @param {String} [requestInformationtry.url] - The URL of the request. (optional)
* @param {Object} [deviceInformation] - The device information of the device
* that made the request. (optional)
* @param {String} [deviceInformation.app_version] - The version of the app that
* was running on the device. (optional)
* @param {String} [deviceInformation.brand] - The brand of the device. (optional)
* @param {String} [deviceInformation.carrier] - The carrier of the device . (optional)
* @param {String} [deviceInformation.gps_status] - The gps status of the device. (optional)
* @param {Number} [deviceInformation.latitude] - The latitude value where the device was.
* (optional)
* @param {Number} [deviceInformation.longitude] - The longitude value where the device
* was. (optional)
* @param {String} [deviceInformation.manufacturer] - The manufacturer of the device.
* (optional)
* @param {String} [deviceInformation.network_status] - The network status of the device.
* (optional)
* @param {String} [deviceInformation.os_version] - The OS version of the device. (optional)
* @param {String} [deviceInformation.push_id] - The push ID value of the device.
* (optional)
* @param {String} [deviceInformation.token] - The token value of the device.
* (optional)
* @param {String} [deviceInformation.udid] - The UDID value of the device. (optional)
* @param {String} [deviceInformation.technology] - The technology of the device. (optional)
* @return {Promise} - Promise object that returns success or
* failure
*
* @example <caption>Log</caption>
* import { systemService } from 'Clinical6';
* systemService.log({
* "message": "MyText",
* "tag": "MyString",
* "level": "Error"
* }, {
* "url": "MyString",
* "method": "MyString",
* "params": "MyText",
* "status_code": "MyString"
* }, {
* "udid": "MyString",
* "token": "MyString",
* "push_id": "MyString",
* "app_version": "MyString",
* "os_version": "MyString",
* "network_status": "MyString",
* "gps_status": "MyString",
* "latitude": "1.5",
* "longitude": "1.5",
* "brand": "MyString",
* "manufacturer": "MyString",
* "carrier": "MyString"
* });
*
* @example <caption>Success Response</caption>
* {
* "status": "ok",
* "message": "Debug Log was successfully created"
* }
*/
log(logEntry, requestInformation, deviceInformation) {
// Check to see if the optional entry object is passed in or not
if (logEntry) {
validate('Clinical6 log',
hasToken(),
hasAttribute({ logEntry }, 'level'));
} else {
validate('Clinical6 log',
hasToken());
}
const data = {
entry: logEntry,
request_information: requestInformation,
device_information: deviceInformation,
};
return new Promise((resolve, reject) => {
Client.instance.fetch(`/api/log`, 'post', data).then(
response => resolve(response),
err => reject(err)
);
});
}
/**
* Submits the provided JSON to the server. This is meant as a stub to submit any json to the
* server that will handle the xAuth according to the client. The examples are ways that the
* x_auth could be implemented.
*
* @throws {Clinical6Error} - If missing required parameters
* @param {Object} json - The JSON value with the attributes to validate.
* @return {Promise} - Promise object that returns success or failure
*
* @example <caption>xAuth</caption>
* import { systemService } from 'Clinical6';
* systemService.xAuth({
* site_id: '1234342',
* user_id: '2323423'
* });
*
* @example <caption>Success, Invited</caption>
* {"status":"active"}
*
* @example <caption>Success, Not Invited</caption>
* {"status":"not_invited"}
*
* @example <caption>Success, No Password</caption>
* {
* "status":"new",
* "token":"my-invitation-token",
* "site:":{
* "name":"La Jolla Clinic",
* "address_line_1":"5555 La Jolla Center Dr.",
* "address_line_2":"Suite 101",
* "address_line_3":"",
* "city":"San Diego",
* "state":"CA",
* "country":"USA",
* "zip_code":"920237"
* },
* "mobile_user":{
* "prefix":"Dr.",
* "first_name":"Pat",
* "last_name":"Cooper",
* "role":"Physician",
* "phone":"619-333-3456",
* "email":"pcooper@lajollaclinic.com"
* }
* }
*
* @example <caption>Success, Invalid</caption>
* {"status":"invalid"}
*
* @example <caption>Success, Participant example</caption>
* {
* "participant_id": "03929850"
* }
* 200
* {
* "status": "new",
* "token": "38762f553bf5abb1e318b5a5471b72394c82e0efa35b941506cbf834e0948827"
* }
*/
xAuth(json) {
validate('Clinical6 xAuth',
isRequired({ json }));
return new Promise((resolve, reject) => {
Client.instance.fetch(`/api/x_auth/auth`, 'post', json).then(
response => resolve(response),
err => reject(err)
);
});
}
}
export default SystemService;