src/helpers/ediary/Entry.js
import Helper from '../Helper';
import EntryModel from '../../models/ediary/Entry';
import { serviceFactory } from '../../utilities/factories/ServiceFactory';
import { aggregate } from '../../utilities/ClassUtility';
/**
* Helper class representing an eDiary Entry.
*
* @extends {EntryModel}
* @extends {Helper}
*/
class Entry extends aggregate(EntryModel, Helper) {
/**
* Constructor for helper class representing an eDiary Entry
*
* @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 'ediary__entries';
}
/** @type {FlowDataGroup} */
get flowDataGroup() {
return this._relationships.captured_value_group;
}
/** @type {FlowDataGroup} */
set flowDataGroup(flowDataGroup) {
/** @type {FlowDataGroup} */
this._relationships.captured_value_group = flowDataGroup;
}
/** @type {EntryGroup} */
get entryGroup() {
return this._relationships.entry_group;
}
/** @type {EntryGroup} */
set entryGroup(entryGroup) {
/** @type {EntryGroup} */
this._relationships.entry_group = entryGroup;
}
/** @type {Flow} */
get flow() {
let f = this._relationships.flow_process;
if (this.template && this.template.flow) {
f = this.template.flow;
}
return f;
}
/** @type {EntryTemplate} */
get template() {
return this._relationships.template;
}
/** @type {EntryTemplate} */
set template(template) {
/** @type {EntryTemplate} */
this._relationships.template = template;
}
/** @type {User} */
get owner() {
return this._relationships.owner;
}
/** @type {User} */
set owner(owner) {
/** @type {User} */
this._relationships.owner = owner;
}
/** @type {Status} */
get status() {
return this._relationships.status;
}
/** @type {Status} */
set status(status) {
/** @type {Status} */
this._relationships.status = status;
}
/**
* Saves a entry (insert if id doesn't exist, update if it does)
* @return {Promise<Entry>} - Returns a promise via ajax call.
*
* @example
* import { Entry, clinical6 } from 'clinical6';
*
* // Insert is different from other inserts.
* const entry = new Entry({...});
* await entry.save();
*
* // After you have the authtoken and then you've logged in
*
* // Updates existing entry (has existing id)
* clinical6.get(Entry).then(entries => entries[0].save());
*/
async save() {
const service = serviceFactory.get(this.type);
return (this.id) ? service.update(this) : service.insert(this);
}
}
export default Entry;