src/services/ProjectContentService.js
import Client from '../Client';
import {
hasToken,
isRequired,
validate,
} from '../utilities/ValidationUtility';
/**
* Service that includes generic functions to pull, update, and create data.
*
* This will only work with v2 content such as:
* Api | V2 | Agreements
* Api | V2 | Reminder | Rules
* Api | V2 | SiteStart | Addresses
* Api | V2 | SiteStart | LabAccreditations
* Api | V2 | SiteStart | LabDirectors
* Api | V2 | SiteStart | Labs
* Api | V2 | SiteStart | PrimaryLicenses
* Api | V2 | SiteStart | PrimaryMedicalBoards
* Api | V2 | SiteStart | SiteLabs
* Api | V2 | SiteStart | Sites
* Api | V2 | SiteStart | Studies
* Api | V2 | SiteStart | StudyAddress
* Api | V2 | SiteStart | StudyIrb
* Api | V2 | SiteStart | StudyLab
*/
class ProjectContentService {
/**
* Generic function that deletes data on the server.
*
* This should not be called directly but rather be called using the
* {@link ProjectContentService#deleteProject} method. For this reason, no example is
* provided.
*
* @throws {Clinical6Error} - If missing token or missing required parameters
*
* @param {String} version - Version of the API of the API call to make
* @param {String} ownerType - The owner type
* @param {Number} [owner=null] - The owner
* @param {String} [type=''] - The content type
* @param {Number} [id] - The id of the object to delete
*
* @return {Promise} - Promise object that returns success or failure
*/
delete(version, ownerType, owner = null, type = '', id = null) {
validate('ProjectContentService.get',
hasToken(),
isRequired({ version }),
isRequired({ ownerType }));
let ownerVal = '';
if (owner !== null) {
ownerVal = `/${owner}`;
}
let typeVal = '';
if (type !== '') {
typeVal = `/${type}`;
}
let idVal = '';
if (id !== null) {
idVal = `/${id}`;
}
return new Promise((resolve, reject) => {
Client.instance
.fetch(`/api/${version}/${ownerType}${ownerVal}${typeVal}${idVal}`, 'delete')
.then(
response => resolve(response),
err => reject(err)
);
});
}
/**
* Delete a sibling or child based on the ProjectContent type and/or child type
*
* @param {Object} project - ProjectContent options
* @param {!Object} project.version - ProjectContent version
* @param {!Object} project.ownerType - ProjectContent owner type
* @param {Object} project.owner - ProjectContent owner
* @param {String} [type=''] - Child type
* @param {Number} id - id to delete
*
* @return {Promise}
*/
deleteProject(project, type = '', id) {
return this.delete(project.version, project.ownerType, project.owner, type, id);
}
/**
* Generic function that retrieves data.
*
* Handles the following URL patterns:
*
* List:
* -api/version/ownerType
* -api/version/ownerType/owner/contentType
*
* Show:
* -api/version/ownerType/owner
* -api/version/ownerType/owner/contentType/objectId
*
* This should not be called directly but rather be called using the
* {@link ProjectContentService#getProject} method. For this reason, no example is
* provided.
*
* @throws {Clinical6Error} - If missing token or missing required parameters
*
* @param {String} version - Version of the API of the API call to make
* @param {String} ownerType - The owner type
* @param {Number} [owner=null] - The owner
* @param {String} [contentType=''] - The content type
* @param {Number} [contentId=null] - The ID value of object to show
*
* @return {Promise} - Promise object that returns success or failure
*/
get(version, ownerType, owner = null, contentType = '', contentId = null) {
validate('ProjectContentService.get',
hasToken(),
isRequired({ version }),
isRequired({ ownerType }));
let ownerVal = '';
if (owner !== null) {
ownerVal = `/${owner}`;
}
let contentTypeVal = '';
if (contentType !== '') {
contentTypeVal = `/${contentType}`;
}
let contentIdVal = '';
if (contentId !== null) {
contentIdVal = `/${contentId}`;
}
return new Promise((resolve, reject) => {
Client.instance
.fetch(`/api/${version}/${ownerType}${ownerVal}${contentTypeVal}${contentIdVal}`)
.then(
response => resolve(response),
err => reject(err)
);
});
}
/**
* Get Data via ProjectContent helper class
*
* This should not be called directly but rather be called using the
* {@link ProjectContent#getAll}, {@link ProjectContent#getChild},
* {@link ProjectContent#getChildren}, {@link ProjectContent#get}, and
* {@link ProjectContent#getSibling} method. For this reason, no examples are provided.
*
* @param {Object} project - ProjectContent options
* @param {!Object} project.version - ProjectContent version
* @param {!Object} project.ownerType - ProjectContent owner type
* @param {Object} project.owner - ProjectContent owner
* @param {String} [contentType=''] - Content type
* @param {Number} [contentId=null] - Content id
*
* @return {Promise} - Promise object that returns success or failure
*/
getProject(project, contentType = '', contentId = null) {
return this.get(project.version, project.ownerType, project.owner, contentType, contentId);
}
/**
* Generic function that updates data on the server.
*
* This should not be called directly but rather be called using the
* {@link ProjectContentService#insertProject} method. For this reason, no example is
* provided.
*
* @throws {Clinical6Error} - If missing token or missing required parameters
*
* @param {String} version - Version of the API of the API call to make
* @param {String} ownerType - The owner type
* @param {Number} [owner=null] - The owner
* @param {String} [contentType=''] - The content type
* @param {Object} data - The object to create on the server
*
* @return {Promise} - Promise object that returns success or failure
*/
insert(version, ownerType, owner = null, contentType = '', data) {
validate('ProjectContentService.insert',
hasToken(),
isRequired({ version }),
isRequired({ ownerType }),
isRequired({ data }));
let ownerVal = '';
if (owner !== null) {
ownerVal = `/${owner}/`;
}
return new Promise((resolve, reject) => {
Client.instance
.fetch(`/api/${version}/${ownerType}${ownerVal}${contentType}`, 'post', { data }).then(
response => resolve(response.data),
err => reject(err)
);
});
}
/**
* Insert a new sibling or child based on the ProjectContent type and/or child type
*
* This should not be called directly but rather be called using the {@link ProjectContent#save}
* method and {@link ProjectContent#addChild}. For this reason, no examples are provided.
*
* @param {Object} project - ProjectContent options
* @param {!Object} project.version - ProjectContent version
* @param {!Object} project.ownerType - ProjectContent owner type
* @param {Object} project.owner - ProjectContent owner
* @param {String} [type=''] - Child type
* @param {Object} data - data to insert
*
* @return {Promise} - Promise object that returns success or failure
*/
insertProject(project, type = '', data) {
return this.insert(project.version, project.ownerType, project.owner, type, data);
}
/**
* Generic function that updates data on the server.
*
* This should not be called directly but rather be called using the
* {@link ProjectContentService#updateProject} method. For this reason, no example is
* provided.
*
* @throws {Clinical6Error} - If missing token or missing required parameters
*
* @param {String} version - Version of the API of the API call to make
* @param {String} ownerType - The owner type
* @param {Number} owner - The owner
* @param {String} [contentType=''] - The content type
* @param {Number} [contentId=null] - The ID value of the object to update
* @param {Object} data - The updated object to pass to the server
*
* @return {Promise} - Promise object that returns success or failure
*/
update(version, ownerType, owner, contentType = '', contentId = null, data) {
validate('ProjectContentService.update',
hasToken(),
isRequired({ version }),
isRequired({ ownerType }),
isRequired({ owner }),
isRequired({ data }));
let contentTypeVal = '';
if (contentType !== '') {
contentTypeVal = `/${contentType}`;
}
let contentIdVal = '';
if (contentId !== null) {
contentIdVal = `/${contentId}`;
}
return new Promise((resolve, reject) => {
Client.instance
.fetch(`/api/${version}/${ownerType}/${owner}${contentTypeVal}${contentIdVal}`, 'put',
{ data }).then(
response => resolve(response.data),
err => reject(err)
);
});
}
/**
* Update ProjectContent information based on the ProjectContent Helper class.
*
* This should not be called directly but rather be called using the {@link ProjectContent#save}
* method and {@link ProjectContent#updateChild}. For this reason, no examples are provided.
*
* @param {Object} project - The ProjectContent.options object
* @param {String} [type=''] - The child type to update
* @param {Number} [id=null] - The child id to update
* @param {Object} data - The data being used for the update
*
* @return {Promise} - Promise object that returns success or failure
*/
updateProject(project, type = '', id = null, data) {
return this.update(project.version, project.ownerType, project.owner, type, id, data);
}
}
export default ProjectContentService;