src/helpers/gamification/Badge.js
import BadgeModel from '../../models/gamification/Badge';
import { serviceFactory } from '../../utilities/factories/ServiceFactory';
import C6Image from '../Image';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';
/**
* Helper class representing an badge.
*
* @extends {BadgeModel}
* @extends {Helper}
*
* @example
* import { Badge, clinical6 } from 'clinical6';
*
* // Typically use clinical6.get()
* clinical6.get(Badge).then(badges => console.log(badges));
*
* const badge = new Badge({
* "data": {
* "id": "15",
* "type": "badges",
* "attributes": {
* "created_at": "2017-08-07T22:26:30Z",
* "updated_at": "2017-08-07T22:26:30Z",
* "title": "dummy_892",
* "description": "Demo Content info...",
* "redemption_points": 250,
* "image": {
* "image": {
* "url": "/uploads/test/reward/image/15/1.png",
* "small": {
* "url": "/uploads/test/reward/image/15/small_1.png"
* },
* "small_hd": {
* "url": "/uploads/test/reward/image/15/small_hd_1.png"
* },
* "fullscreen": {
* "url": "/uploads/test/reward/image/15/fullscreen_1.png"
* },
* "main": {
* "url": "/uploads/test/reward/image/15/main_1.png"
* },
* "fullscreen_hd": {
* "url": "/uploads/test/reward/image/15/fullscreen_hd_1.png"
* },
* "main_hd": {
* "url": "/uploads/test/reward/image/15/main_hd_1.png"
* }
* }
* },
* "position": 4,
* "enabled": true,
* "featured": null,
* "based_on": null,
* "threshold": null,
* "time_unit": null,
* "start_point": null,
* "start_at": null,
* "disabled_image": {
* "disabled_image": {
* "url": "/default/default.png",
* "small": {
* "url": "/default/small_default.png"
* },
* "small_hd": {
* "url": "/default/small_hd_default.png"
* },
* "fullscreen": {
* "url": "/default/fullscreen_default.png"
* },
* "main": {
* "url": "/default/main_default.png"
* },
* "fullscreen_hd": {
* "url": "/default/fullscreen_hd_default.png"
* },
* "main_hd": {
* "url": "/default/main_hd_default.png"
* }
* }
* },
* "cache_token": "BnSwoIJMt64"
* }
* }
*});
*/
class Badge extends aggregate(BadgeModel, Helper) {
/**
* Constructor for helper class representing an Badge
*
* @param {Object} json - json api response from server
*/
constructor(json = {}) {
super(json);
if (json.relationships) {
/** @type {Object} */
this._relationships = json.relationships;
this.syncRelationships();
}
const _response = json.data || json; // if json api is passed in directly
const attributes = _response.attributes || _response; // if json api is passed in directly
if (attributes.disabled_image) {
/** @type {C6Image} */
this.disabledImage = new C6Image(attributes.disabled_image);
}
if (attributes.image) {
/** @type {C6Image} */
this.image = new C6Image(attributes.image);
}
}
/** @type {String} - The type */
static get type() {
return 'badges';
}
/** @type {Object} */
get accessible() {
return this._accessible;
}
/** @type {Object} */
set accessible(accessible) {
/** @type {Object} */
this._accessible = accessible;
}
/** @type {Object} */
get brand() {
return this._brand;
}
/** @type {Object} */
set brand(brand) {
/** @type {Object} */
this._brand = brand;
}
/** @type {Object} */
get callback() {
return this._callback;
}
/** @type {Object} */
set callback(callback) {
/** @type {Object} */
this._callback = callback;
}
/** @type {Object} */
get pointsRecords() {
return this._points_records;
}
/** @type {Object} */
set pointsRecords(pointsRecords) {
/** @type {Object} */
this._points_records = pointsRecords;
}
/** @type {Object} */
get rewardEarnings() {
return this._reward_earnings;
}
/** @type {Object} */
set rewardEarnings(rewardEarnings) {
/** @type {Object} */
this._reward_earnings = rewardEarnings;
}
/** @type {Object} */
get segmentations() {
return this._segmentations;
}
/** @type {Object} */
set segmentations(segmentations) {
/** @type {Object} */
this._segmentations = segmentations;
}
/** @type {Object} */
get schedules() {
return this._schedules;
}
/** @type {Object} */
set schedules(schedules) {
/** @type {Object} */
this._schedules = schedules;
}
/** @type {Object} */
get tag() {
return this._tag;
}
/** @type {Object} */
set tag(tag) {
/** @type {Object} */
this._tag = tag;
}
/** @type {Object} */
get taggings() {
return this._taggings;
}
/** @type {Object} */
set taggings(taggings) {
/** @type {Object} */
this._taggings = taggings;
}
/**
* Saves a badge (insert if id doesn't exist, update if it does)
* @return {Promise} - Returns a promise via ajax call.
*
* @example
* import { Badge, Client } from 'clinical6';
*
* // Removes badge from server and local storage
* const badge = new Badge({
* "id": 1,
* "type": "badges",
* "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
* }
* });
* badge.delete();
*
* // No longer in storage
* Client.instance.storageUtility.has('badges', { id: 1 });
*/
delete() {
const service = serviceFactory.get(this.type);
return service.delete(this);
}
/**
* Saves a badge (insert if id doesn't exist, update if it does)
* @return {Promise<Badge>} - Returns a promise via ajax call.
*
* @example
* import { Badge, clinical6 } from 'clinical6';
*
* // Insert is different from other inserts. Uses mobile application key
* const badge = new Badge({
* "title": "You are a winner"
* });
* badge.save();
*
* // After you have the authtoken and then you've logged in
*
* // Updates existing badge (has existing id)
* clinical6.get(Badge).then(badges => badges[0].save());
*/
save() {
const service = serviceFactory.get(this.type);
return (this.id) ? service.update(this) : service.insert(this);
}
}
export default Badge;