src/services/StatusService.js
import { deprecate } from 'core-decorators';
import Section from '../helpers/Section';
import Status from '../helpers/Status';
import Client from '../Client';
import JsonApiService from './JsonApiService';
import {
hasAttribute,
hasToken,
isRequired,
validate,
} from '../utilities/ValidationUtility';
/**
* Service handling Status calls.
*
*/
class StatusService extends JsonApiService {
/**
* Update type to be statuses
*/
constructor() {
super('statuses',
{
title: 'StatusService',
obj: 'status',
tokenRequired: ['delete', 'get', 'getChildren', 'insert', 'update']
});
}
/**
* @override
* Call a POST request expecting JSON API information.
*
* @throws {Clinical6Error} - If missing token or missing required parameters
* @param {Helper} obj - Object to which get the status
* @param {Object} [options] - Override the options for this call
* @param {String} [options.cacheMode] - Override the caching method for this call
* @return {Promise<Status>} - Promise object that returns a status
*
* @example
* import { statusService } from 'clinical6';
*
* // You can retrieve an object's status with the `check(obj)` method
* statusService.check(user).then(status => console.log(status));
*/
async check(obj, options = {}) {
validate('StatusService.check',
hasToken(),
isRequired({ obj }),
hasAttribute({ obj }, 'id'),
hasAttribute({ obj }, 'type'));
options.url = `/v3/${Status.type}/check`;
return super.insert(obj, options);
}
/**
* Retrieves the status values for the sections.
*
* @param {String} resource - The dynamic content section resource having sections.
* @param {String} ownerType - The owner type.
* @param {String} owner - The owner resource or id.
*
* @return {Promise} - Promise object that returns success or failure
*/
@deprecate
getSections(resource, ownerType, owner = undefined) {
const required = { resource, ownerType };
if (ownerType !== 'mobile_user') {
required.owner = owner;
}
validate('StatusService.getSections',
hasToken(),
isRequired(required));
const params = {
status: {
owner_type: ownerType,
section: resource,
},
};
if (owner) {
params.status.owner = owner;
}
return new Promise((resolve, reject) => {
Client.instance.fetch(`/api/status/check_section`, 'post', params).then(
// (response) => resolve(response),
response => resolve(new Section(Object.assign({ },
{
object: resource,
ownerType,
owner,
},
response.section_status))),
err => reject(err)
);
});
}
/**
* Retrieves an individual status value for the sections.
*
* @param {String} type - The type of the section
* @param {Object} object - The object attribute of the section
* @param {String} ownerType - The owner type.
* @param {String} [owner] - The owner resource or id.
*
* @return {Promise} - Promise object that returns success or failure
*
* @deprecated
*/
@deprecate
async getStatus(type, object, ownerType, owner = undefined) {
const required = { type, object, ownerType };
if (ownerType !== 'mobile_user') {
required.owner = owner;
}
validate('StatusService.getStatus',
hasToken(),
isRequired(required));
const params = {
status: {
object_type: type,
object,
owner_type: ownerType,
},
};
if (owner) {
params.status.owner = owner;
}
return new Promise((resolve, reject) => {
Client.instance.fetch(`/api/status/check`, 'post', params).then(
response => resolve(response),
err => reject(err)
);
});
}
/**
* Explicitely applies an action to transition the object status from one status to another.
*
* @param {String} transition - The action to be applied to change the status of the object
* @param {String} type - The type of the section
* @param {Object} object - The object attribute of the section
* @param {String} ownerType - The owner type.
* @param {String} owner - The owner resource or id.
*
* @return {Promise} - Promise object that returns success or failure
*/
transition(transition, type, object, ownerType, owner = undefined) {
const required = {
transition, type, object, ownerType
};
if (ownerType !== 'mobile_user') {
required.owner = owner;
}
validate('StatusService.collect',
hasToken(),
isRequired(required));
const params = {
status: {
object_type: type,
object,
owner_type: ownerType,
transition,
},
};
if (owner) {
params.status.owner = owner;
}
return new Promise((resolve, reject) => {
Client.instance.fetch(`/api/status/transition`, 'post', params).then(
/**
* The response format is:
* {Object} response.status
* {String} response.status.object - The id of the object being transitioned
* {String} response.status.type - The type of the object being transitioned
* {String} response.status.value - The new status of the object being transitioned
*/
response => resolve(response),
/**
* The error format is:
* {Object} err.error_detail
* {String} err.error_detail.object - The error short_code
* {String} err.friendly_error - The friendly text of the error
* {Number} err.internal_code - The internal code of the error
* {String} err.more_info - More information, if available, of the error
* {String} err.status - The status of the error
* {String} err.message - The message of the error
*/
err => reject(err)
);
});
}
}
export default StatusService;