src/services/DeviceService.js
import Client from '../Client';
import JsonApiService from './JsonApiService';
import {
hasToken,
isA,
isRequired,
validate,
} from '../utilities/ValidationUtility';
/**
* Service handling Device calls with specific endpoints.
*/
class DeviceService extends JsonApiService {
/**
* Update type to be devices
*/
constructor() {
super('devices');
/** @type {String} */
this.cacheMode = 'networkOnly';
}
/**
* @override
* Call a PATCH request on the main obj.type expecting JSON API information.
*
* @param {Device} device - Device object in which to update
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise} - Message
*
* @example
* import { deviceService } from 'clinical6';
*
* // You will be able to delete a device using `delete`.
* deviceService.delete({ id: 23 });
* // This will also clear the local storage of this type and id
*/
delete(device, cacheMode = undefined) {
validate('DeviceService.delete',
hasToken(),
isRequired({ device }));
return super.delete(device, { 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<Device[] | Device>}
* - Promise object that returns one device or a key:device hashtable
*
* @example
* import { deviceService } from 'clinical6';
*
* // You will be able to access these devices using the `get` method.
* deviceService.get().then(devices => console.log(devices));
*
* // Additionally, you can retrieve a specific device with the `get`
* // method, using the ID of the desired device as a parameter.
* deviceService.get(23).then(device => console.log(device));
*/
get(params = {}, cacheMode = undefined) {
validate('DeviceService.get',
hasToken());
return super.get(params, { cacheMode });
}
/**
* @override
* Call a POST request on the main obj.type expecting JSON API information.
*
* @param {String} mobileApplicationKey - Key from server
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Device>} - Inserted device
*
* @example
* import { Device, deviceService } from 'clinical6';
* const device = new Device({...});
*
* // you can insert a device using the `insert` method.
* deviceService.insert(mobileApplicationKey).then(device => console.log(device));
*
* // you could also just call `save` on the device if it doesn't have an id, which will also
* // invoke the `insert` method
* device.save();
*/
insert(mobileApplicationKey = undefined, cacheMode = undefined) {
const key = mobileApplicationKey || Client.instance.mobileApplicationKey;
validate('DeviceService.insert',
isRequired({ key }),
isA({ key }, 'string'));
const params = Client.instance.device.toJSON();
params.attributes.mobile_application_key = key;
return super.insert(params, { cacheMode }).then((d) => {
Client.instance.device.id = d.id;
Client.instance.device.createdAt = d.createdAt;
Client.instance.device.updatedAt = d.updatedAt;
Client.instance.device.accessToken = d.accessToken;
Client.instance.authToken = d.accessToken;
return d;
});
}
/**
* @override
* Call a PATCH request on the main obj.type expecting JSON API information.
*
* @param {Device} device - Device object in which to update
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Device>} - Updated device
*
* @example
* import { Device, deviceService } from 'clinical6';
* const device = new Device({...});
*
* // you can update a device using the `update` method.
* deviceService.update(device).then(device => console.log(device));
*
* // you could also just call `save` on the device if it has an id, which will also
* // invoke the `update` method
* device.save();
*/
update(device, cacheMode = undefined) {
validate('DeviceService.update',
hasToken(),
isRequired({ device }));
return super.update(device, { cacheMode });
}
}
export default DeviceService;