src/helpers/cohort/CohortAssignment.js
import CohortAssignmentModel from '../../models/cohort/CohortAssignment';
import { serviceFactory } from '../../utilities/factories/ServiceFactory';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';
/**
* Helper class representing an cohortAssignment.
*
* @extends {CohortAssignmentModel}
* @extends {Helper}
*
*
* @example
* // To get a list of Cohort Assignments you must have the cohort first, then get it based on this cohort
* import { clinical6, CohortAssignment } from 'clinical6';
* const cohort = new Cohort({...}); // get the cohort in some way
* cohort.getAssignments().then(a => console.log(a)); // use getAssignments
* clinical6.getChildren(cohort, CohortAssignment).then(a => console.log(a)); // or using get children
*
* // To get a single cohort, you can also use clinical6
* clinical6.get({ id: 5, type: 'cohort_assignments'});
*
* // To save or insert, you can either use the .save() capability or clinical6
* myCohortAssignment.save(); // insert if no id, save if id
* clinical6.insert(new CohortAssignment({...}));
* clinical6.update(myCohortAssignment);
*
* // To delete, use clinical6 or .delete();
* myCohortAssignment.delete();
* clinical6.delete(myCohortAssignment);
*/
class CohortAssignment extends aggregate(CohortAssignmentModel, Helper) {
/**
* Constructor for helper class representing an CohortAssignment
*
* @param {Object} json - json api response from server
*/
constructor(json = {}) {
super(json);
this.deserializeRelationshipStubs(json);
this.syncRelationships(json);
}
/** @type {String} - The type */
static get type() {
return 'cohort_assignments';
}
/** @type {Cohort[]} */
get cohort() {
return this._relationships.cohort;
}
/** @type {Cohort[]} */
set cohort(cohort) {
/** @type {Cohort[]} */
this._relationships.cohort = cohort;
}
/** @type {User} */
get user() {
return this._relationships.user;
}
/** @type {User} */
set user(user) {
/** @type {User} */
this._relationships.user = user;
}
/**
* Deletes a cohortAssignment
* @return {Promise} - Returns a promise via ajax call.
*
* @example
* import { CohortAssignment, Client } from 'clinical6';
*
* // Removes cohortAssignment from server and local storage
* const cohortAssignment = new CohortAssignment({...});
* cohortAssignment.delete();
*
* // No longer in storage
* Client.instance.storageUtility.has('cohort_assignments', { id: 1 });
*/
delete() {
const service = serviceFactory.get(this.type);
return service.delete(this);
}
/**
* Saves a cohortAssignment (insert if id doesn't exist, update if it does)
* @return {Promise<CohortAssignment>} - Returns a promise via ajax call.
*
* @example
* import { CohortAssignment, clinical6 } from 'clinical6';
*
* // Insert is different from other inserts. Uses mobile application key
* const cohortAssignment = new CohortAssignment({...});
* cohortAssignment.save();
*
* // After you have the authtoken and then you've logged in
*
* // Updates existing cohortAssignment (has existing id)
* clinical6.get(CohortAssignment).then(cohortAssignments => cohortAssignments[0].save());
*/
save() {
const service = serviceFactory.get(this.type);
return (this.id) ? service.update(this) : service.insert(this);
}
}
export default CohortAssignment;