src/helpers/reminder/Schedule.js
// import Client from '../../Client';
import ScheduleModel from '../../models/reminder/Schedule';
import UserService from '../../services/UserService';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';
/**
* Helper class representing a schedule.
*
* @extends {ScheduleModel}
* @extends {Helper}
*
* @example
* import { Schedule, scheduleService } from 'clinical6';
*
* // Typically use ScheduleService.get()
* scheduleService.get().then(schedules => console.log(schedules));
*
* const schedule = new Schedule({
* "data": {
* "id": "1",
* "type": "schedules",
* "attributes": {
* "udid": "this-is-a-udid-string",
* "technology": "ios",
* "access_token": "cd68fa04e458d6d1a9d29faec6a329d3",
* "push_id": null,
* "created_at": "2017-05-19T17:21:26.311Z",
* "updated_at": "2017-05-19T17:21:26.311Z",
* "app_version": null
* }
* }
* });
*/
class Schedule extends aggregate(ScheduleModel, Helper) {
/**
* Constructor for helper class representing an Schedule
*
* @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 'scheduler__personalized_rule_schedules';
}
/** @type {User} */
get mobileUser() {
return this._relationships.mobile_user;
}
/** @type {User} */
set mobileUser(mobileUser) {
/** @type {User} */
this._relationships.mobile_user = mobileUser;
}
/** @type {(EntryGroup|Rule)} */
get scheduleable() {
return this._relationships.scheduleable;
}
/** @type {(EntryGroup|Rule)} */
set scheduleable(scheduleable) {
/** @type {(EntryGroup|Rule)} */
this._relationships.scheduleable = scheduleable;
}
/**
* Saves a schedule (insert if id doesn't exist, update if it does)
* @return {Promise} - Returns a promise via ajax call.
*
* @example
* import { Schedule, Client } from 'clinical6';
*
* // Removes schedule from server and local storage
* const schedule = new Schedule({
* "id": 1,
* "type": "schedules",
* "attributes": {
* "udid": "this-is-a-udid-string",
* "technology": "ios",
* "access_token": "cd68fa04e458d6d1a9d29faec6a329d3",
* "push_id": null,
* "created_at": "2017-05-19T17:21:26.311Z",
* "updated_at": "2017-05-19T17:21:26.311Z",
* "app_version": null
* }
* });
* schedule.delete();
*
* // No longer in storage
* Client.instance.storageUtility.has('schedules', { id: 1 });
*/
delete() {
return (new UserService()).deleteSchedule(this);
}
/**
* Saves a schedule (insert if id doesn't exist, update if it does)
* @return {Promise<Schedule>} - Returns a promise via ajax call.
*
* @example
* import { Schedule, scheduleService } from 'clinical6';
*
* // Insert is different from other inserts. Uses mobile application key
* const schedule = new Schedule({
* "type": "schedules",
* "attributes": {
* "udid": "this-is-a-udid-string",
* "technology": "ios",
* "access_token": "cd68fa04e458d6d1a9d29faec6a329d3",
* "push_id": null,
* "created_at": "2017-05-19T17:21:26.311Z",
* "updated_at": "2017-05-19T17:21:26.311Z",
* "app_version": null
* }
* });
* schedule.save();
*
* // After you have the authtoken and then you've logged in
*
* // Updates existing schedule (has existing id)
* scheduleService.get().then(schedules => schedules[0].save());
*/
save() {
return (this.id)
? (new UserService()).updateSchedule(this)
: (new UserService()).addSchedule(this);
}
}
export default Schedule;