consumerportal
Version:
mydna Custimised for you
227 lines (201 loc) • 8.78 kB
text/typescript
/// <reference path="../includes.ts" />
module services {
export interface IDietService {
details(type: string): angular.IPromise<any>
setGoal(key: string)
navigation(type: string): any[]
getWeightOverride(type: string): string
}
class DietService implements IDietService {
private goal: string = 'loss';
private gender: string;
private age: string;
private loss: any;
private maintenance: any;
private reportType: string;
private guidelinesRef: any = {
'Higher Protein': 'higherprotein',
'Higher Protein / Lower Fat': 'higherproteinlowerfat',
'Higher Protein / Lower Fat (Omega-3 rich)': 'higherproteinlowerfatomega3',
'Lower Fat': 'lowerfat',
'Lower Fat (Omega-3 rich)': 'lowerfatomega3',
'Mediterranean': 'mediterranean'
};
static $inject = [
'$q',
'apiSrvc',
'userService',
'patientService',
'mydnaApis',
'errorHandlerSrvc',
'$rootScope',
'$routeParams'
];
constructor(
private $q: angular.IQService,
private service: apiSrvc.IApiService,
private userService: IUserService,
private patientService: IPatientService,
private api: ImyDNAApis,
private errorService: errorHandlerSrvc.IErrorHandlerService,
private $rootScope: angular.IRootScopeService,
private $routeParams: angular.route.IRouteParamsService
) {
$rootScope.$on('changeUser', (ev: angular.IAngularEvent, user: IUser) => {
this.loss = this.maintenance = null;
this.detailsCount = -1;
});
}
navigation(type: string): any[] {
return [
{value: '#/diet/' + type, label: 'Dashboard', key: 'dashboard'},
{value: '#/diet/' + type + '/guidelines', label: 'Guidelines', key: 'guidelines'},
{value: '#/diet/' + type + '/servings', label: 'Servings', key: 'servings'},
{value: '#/diet/' + type + '/meals', label: 'Sample Menu', key: 'meals'}
]
}
getWeightOverride(type: string): string {
if(type === 'WeightManagementReport') {
return 'Weight Management';
}
}
setGoal(value: string): any {
this.goal = value;
this.$rootScope.$broadcast('dietMetricsChanged', this.state())
}
private state() {
return {
maintenance: this.maintenance,
loss: this.loss,
gender: this.gender,
age: this.age,
goal: this.goal
};
}
private deferred: angular.IDeferred<any>[] = [];
private detailsCount: number = -1;
private DETAILS_COMPLETE: number = 6;
private DETAILS_EMPTY: number = -1;
private detailsResolve(inc: boolean = true) { // removes hierarchy of data calls in details()
if (inc) {
this.detailsCount++;
}
if (this.detailsCount === this.DETAILS_COMPLETE) {
if (this.loss) {
this.loss.guidelines.Reference = this.guidelinesRef[this.loss.plan.Type];
}
this.maintenance.guidelines.Reference = this.guidelinesRef[this.maintenance.plan.Type];
this.deferred.forEach((defer: angular.IDeferred<any>) => {
defer.resolve(this.state());
});
this.deferred = [];
}
}
private formatEating(value: string) {
let hasSub: boolean = false;
let lines: string[] = value.split('\n').map((line: string) => {
let pre: string = '';
let fc: string = line.substr(0, 1);
if (fc === 'o' && !hasSub) {
hasSub = true;
pre = '<ul>'
} else if (fc !== 'o' && hasSub) {
pre = '</ul>';
hasSub = false;
}
return pre + (line.substring(2).length > 0 ? '<li>' + line.substring(2) + '</li>' : '');
});
let output = '<ul>' + lines.join('') + '</ul>';
return output;
}
private getJsonDetails(target: string, value: string) {
this.DETAILS_COMPLETE += 3;
this[target] = {};
this.data('plans', value).then((val: any) => {
this[target].plan = val;
this.detailsResolve();
});
this.data('servings', value).then((val: any) => {
this[target].servings = val;
this.detailsResolve();
});
this.data('guidelines', value).then((val: any) => {
this[target].guidelines = val;
this[target].guidelines.Eating = this.formatEating(this[target].guidelines.Eating);
this.detailsResolve();
});
}
details(type: string): angular.IPromise<any> {
if (type !== this.reportType) {
this.maintenance = null;
this.loss = null;
this.detailsCount = this.DETAILS_EMPTY;
}
let defer: angular.IDeferred<any> = this.$q.defer();
this.deferred.push(defer);
if ((!this.maintenance || !this.loss) && this.detailsCount === this.DETAILS_EMPTY) {
this.detailsCount = 0;
this.reportType = type;
this.userService.user().then((user: IUser) => {
this.age = user.Age.toString();
this.gender = this.gender = user.Gender !== 'O' ? user.Gender : null;
this.get(this.api.Diet + type , null, {}, false).then((value: any) => {
this.DETAILS_COMPLETE = 0;
if (this.$routeParams.diet1) {
value = {Loss: this.$routeParams.diet1, Maintenance: this.$routeParams.diet2}
}
this.getJsonDetails('maintenance', value.Maintenance);
if (value.Loss) {
this.goal = 'loss';
this.getJsonDetails('loss', value.Loss);
} else {
this.goal = 'maintenance';
}
});
});
} else {
if (this.detailsCount === this.DETAILS_COMPLETE) {
this.detailsResolve(false);
}
}
return defer.promise;
}
private data(type: string, key: string): angular.IPromise<any> {
let defer = this.$q.defer();
this.get_local('data/dietv2/' + type + '/' + key.replace(/[, +]/g, '').toLowerCase() + '.json').then((val: any) => {
defer.resolve(val);
});
return defer.promise;
}
private get(url: string, store: string, defaultValue: any, forceReload: boolean): angular.IPromise<any> {
let defer = this.$q.defer();
if (store && this[store] && !forceReload) {
defer.resolve(this[store]);
} else {
this.service.get_request_params(url).then((values: any[]) => {
if (store) {
this[store] = values || defaultValue;
}
defer.resolve(this[store] || values || defaultValue);
}, (err:any) => {
this.errorService.errorHandler(err, "debug", "");
defer.resolve([]);
});
}
return defer.promise;
}
private get_local(url: any) {
let defer = this.$q.defer();
this.service.get_request_params_otherResource(location.href.replace(location.hash, '') + url + '?<!--MYDNA-UUID-->').then((value: any) => {
defer.resolve(value);
}, (err:any) => {
this.errorService.errorHandler(err, "debug", "");
defer.resolve({});
});
return defer.promise;
}
}
angular
.module('app')
.service('dietService', DietService);
}