consumerportal
Version:
mydna Custimised for you
159 lines (133 loc) • 5.16 kB
text/typescript
module services {
import IAppStorage = app.IAppStorage;
export interface ICartService {
getReports(): angular.IPromise<any>
addItem(item: CartItem)
removeItem(item: CartItem)
getItems(): CartItem[]
checkout(): void
}
export class CartItem {
public key: string;
public name: string;
public type: string;
public cost: number;
public currency: string;
public state: string;
}
class CartService implements ICartService {
static $inject = [
'$q',
'apiSrvc',
'userService',
'mydnaApis',
'errorHandlerSrvc',
'$rootScope',
'appLocalStorage'
];
private user: IUser;
private items: CartItem[] = [];
constructor(
private $q: angular.IQService,
private service: apiSrvc.IApiService,
private userService: IUserService,
private api: ImyDNAApis,
private errorService: errorHandlerSrvc.IErrorHandlerService,
private $rootScope: angular.IRootScopeService,
private storage: IAppStorage
) {
$rootScope.$on('changeUser', (ev: any, user: IUser) => {
this.user = user;
this.items = [];
});
/*userService.user().then((user: IUser) => {
this.user = user;
});
*/
}
getReports() {
let defer = this.$q.defer();
this.userService.user().then((user: IUser) => {
this.user = user;
this.items = [];
let ls: string = this.storage.getItem(this.user.Username + 'cart');
let temp: string[] = ls ? JSON.parse(ls) : [];
let diet = new CartItem();
diet.key = 'diet';
diet.name = 'Diet Report';
let fitness = new CartItem();
fitness.key = 'fitness';
fitness.name = 'Fitness Report';
let alcohol = new CartItem();
alcohol.key = 'alcohol';
alcohol.name = 'Alcohol Report';
let list: CartItem[] = [
diet,
fitness,
alcohol
];
list.forEach((item: CartItem) => {
if (!item.state) {
item.state = temp.indexOf(item.key) > -1 ? 'cart' : null;
if (item.state === 'cart') {
this.items.push(item);
}
}
});
defer.resolve(list);
this.$rootScope.$broadcast('cartUpdated', this.items);
});
return defer.promise;
}
getItems() {
return this.items;
}
private storeInMemory() {
this.storage.setItem(this.user.Username + 'cart', JSON.stringify(this.items.map((i: CartItem) => { return i.key })));
let ls: string = this.storage.getItem(this.user.Username + 'cart');
let temp: string[] = ls ? JSON.parse(ls) : [];
}
addItem(item: CartItem) {
this.items.push(item);
this.storeInMemory();
this.$rootScope.$broadcast('cartUpdated', this.items);
}
removeItem(item: CartItem) {
this.items = this.items.filter((i: CartItem) => {return i.key !== item.key});
this.storeInMemory();
this.$rootScope.$broadcast('cartUpdated', this.items);
}
checkout() {
}
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).then((value: any) => {
defer.resolve(value);
}, (err:any) => {
this.errorService.errorHandler(err, "debug", "");
defer.resolve({});
});
return defer.promise;
}
}
angular
.module('app')
.service('cartService', CartService);
}