src/services/EventService.js
import { deprecate } from 'core-decorators';
import Client from '../Client';
import JsonApiService from './JsonApiService';
import {
hasToken,
hasAttribute,
isRequired,
validate,
} from '../utilities/ValidationUtility';
/**
* Service handling Event calls with specific endpoints.
*/
class EventService extends JsonApiService {
/**
* Update type to be events
*/
constructor() {
super('reminder__events',
{
title: 'EventService',
obj: 'event',
tokenRequired: ['delete', 'get', 'insert', 'update']
});
}
/**
* @override
* Call a PATCH request on the main obj.type expecting JSON API information.
*
* @param {Event} event - Event object in which to update
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise} - Message
*
* @example
* import { eventService } from 'clinical6';
*
* // You will be able to delete a event using `delete`.
* eventService.delete({ id: 23 });
* // This will also clear the local storage of this type and id
*/
delete(event, cacheMode = undefined) {
validate('EventService.delete',
hasToken(),
isRequired({ event }));
return super.delete(event, { cacheMode });
}
/**
* @override
* Call a GET request expecting JSON API information.
*
* @throws {Clinical6Error} - If missing token or missing required parameters
* @param {Object} [params] - Parameters used to get information from server (such as id)
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Event[] | Event>}
* - Promise object that returns one event or a key:event hashtable
*
* @example
* import { eventService } from 'clinical6';
*
* // You will be able to access these events using the `get` method.
* eventService.get().then(events => console.log(events));
*
* // Additionally, you can retrieve the subcategories for a specific event with the `get`
* // method, using the ID of the desired event as a parameter.
* eventService.get({ id: 23 }).then(event => console.log(event));
*/
get(params = {}, cacheMode = undefined) {
validate('EventService.get',
hasToken());
return super.get(params, { cacheMode });
}
/**
* @override
* Call a POST request on the main obj.type expecting JSON API information.
*
* @param {Event} event - Key from server
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Event>} - Inserted event
*
* @example
* import { Event, eventService } from 'clinical6';
* const event = new Event({...});
*
* // you can insert a event using the `insert` method.
* eventService.insert(mobileApplicationKey).then(event => console.log(event));
*
* // you could also just call `save` on the event if it doesn't have an id, which will also
* // invoke the `insert` method
* event.save();
*/
insert(event, cacheMode = undefined) {
validate('EventService.insert',
hasToken(),
isRequired({ event }));
return super.insert(event, { cacheMode });
}
/**
* @override
* Call a PATCH request on the main obj.type expecting JSON API information.
*
* @param {Event} event - Event object in which to update
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Event>} - Updated event
*
* @example
* import { Event, eventService } from 'clinical6';
* const event = new Event({...});
*
* // you can update a event using the `update` method.
* eventService.update(event).then(event => console.log(event));
*
* // you could also just call `save` on the event if it has an id, which will also
* // invoke the `update` method
* event.save();
*/
update(event, cacheMode = undefined) {
validate('EventService.update',
hasToken(),
isRequired({ event }),
hasAttribute({ event }, 'id'));
return super.update(event, { cacheMode });
}
// old v2 method
/**
* @deprecated
* Track an event done by a mobile user
*
* This controller allows the client to report actions that the user has taken in order to respond with the correct callbacks
*
* @throws {Clinical6Error} - If missing token or missing required parameters
* @param {!String} resource - The resource to be tracked
* @param {!String} action - The resource to be tracked
* @param {!Object} [context] - Hash of fields that provide context for the action
* @param {!String} [context.value] - The value (example: "approved")
* @return {Promise} - Promise object that returns success or failure
*
* @example
* import { eventService } from 'clinical6';
* eventService.trackEvents('callback', 'patient');
* eventService.trackEvents('callback', 'patient', { value: 'approved' });
*/
// TODO: make sure to account for parameters
@deprecate
trackEvents(resource, action, context) {
validate('EventService.trackEvents',
hasToken(),
isRequired({ resource, action, context }));
return new Promise((resolve, reject) => {
Client.instance.fetch(`/api/v2/event/reported_actions`, 'post', { resource, action, context }).then(
response => resolve(response),
err => reject(err)
);
});
}
}
export default EventService;