import { Injectable } from '@angular/core'; import { ApiProvider} from '../api/api.provider'; import { CartItem } from '../cart/cart'; import { Subject } from 'rxjs/Subject'; import {WishList, WishItem} from './wishlist'; import 'rxjs/add/operator/map'; import * as _ from 'lodash'; import { Product } from '../product/product'; // let wishlists: WishList[] = [ // new WishList('56ea50cb8f535eecb185917f', []), // new WishList('56ea50cb8f535eecb185823', []) // ]; @Injectable() export class WishListsProvider { wishlists: WishList[] = []; currentWishlist: WishList; // "broadcasting" currentCart wishListObservable: Subject; wishListsObservable: Subject; constructor( public apiProvider: ApiProvider) { this.wishlists = []; this.currentWishlist = WishList.fromJson(this.emptyWishlist()); this.wishListObservable = > new Subject(); this.wishListsObservable = > new Subject(); } wishlistState(result: any) { this.currentWishlist = result; this.wishListObservable.next(this.currentWishlist); // // TODO REMOVE need to check if a cart actually exists this.wishListsObservable.next(this.wishlists); } emptyWishlist(tenantId?: any, id?: string, name?: string, user?: string, type?: string) { let empty: any[]; empty = []; return { 'id': id, 'userId': user, 'name': name, 'tenantId': tenantId, '__v': '', 'created': '', 'items': empty, 'numItems': 0, 'type': type }; } // create(tenantId, storeId, customerId) { create(tenantId: string, name: string, type: string) { let payload: any = {}; payload = { name: name, type: type, tenantId: tenantId }; let promise = new Promise( (resolve, reject) => { this.apiProvider.api('POST', '/v1/list', payload) // new ApiProvider(http).api('POST', '/v1/cart/', payload) .then((response: any) => { if (response.id) { const list = WishList.fromJson(response); this.wishlists.push(list); this.wishlistState(list); console.log('resolving'); resolve(list); } else { reject('Error wish#1'); } }, (err) => { // @todo: We should check for errors ?!? reject(err); }); }); return promise; } // (list wishlist Wishlist, userId string) // wishlist |-> id(appId) |-> type |-> name /* retrieve by userId [] createList().then (list => updatelist(list,userId )) */ // updateList(wishlistId: string, type: string, name: string, userId: string) { // let payload: any = { // type: type, // name: name, // }; // if (userId) {payload.userId = userId; } // // payload.tenantId = tenantId; // console.log('updating...'); // let promise = new Promise((resolve, reject) => { // this.apiProvider.api('PUT', `/v1/list/${wishlistId}`, payload) // .then((response: any) => { // if (response) { // // this.wishlists[response._id] = response; // // this.wishlistState(response); // // this.currentWishlist = response; // console.log('vvvvvvvvvvv'); // console.log(response); // resolve(response); // ?? // } else { // reject(); // } // }, (err) => { // catch error // // @todo: We should check for errors ?!? // reject(err); // }); // }); // return promise; // } updateList(wishlist: WishList, userId: string) { let payload: any = { type: wishlist.type, name: userId, tenantId: wishlist.tenantId }; // if (userId) {payload.userId = userId; } // payload.tenantId = tenantId; console.log('updating...'); let promise = new Promise((resolve, reject) => { this.apiProvider.api('PUT', `/v1/list/${wishlist.id}`, payload) .then((response: any) => { if (response.id) { const list = WishList.fromJson(response); const position = this.wishlists.findIndex((item) => { return item.id === list.id; }); this.wishlists.splice(position, 1, list); this.wishlistState(list); resolve(list); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } delete(cartId: string) { let payload = {}; let promise = new Promise((resolve, reject) => { this.apiProvider.api('DELETE', `/v1/list/${cartId}`, payload) .then((response: any) => { if (response.id) { const list = WishList.fromJson(response); const position = this.wishlists.findIndex((item) => { return item.id === list.id; }); this.wishlists.splice(position, 1); this.wishListsObservable.next(this.wishlists); // this.wishlistState(response); resolve(response); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; // this.getWishlistOfStore(cartId).addWishlistItem(product); } addItem(cartId: string, product: WishItem) { let payload: any = {}; payload = { productId: product.getProduct().id, quantity: product.getQuantity(), finalPrice: product.getCost(), title: product.getProduct().getName(), storeId: product.getStoreId() }; // let payload = product.toJson(); let promise = new Promise((resolve, reject) => { this.apiProvider.api('POST', `/v1/list/${cartId}`, payload) .then((response: any) => { if (response.id) { const list = WishList.fromJson(response); // this.wishlists.push(list); const position = this.wishlists.findIndex((item) => { return item.id === list.id; }); this.wishlists.splice(position, 1, list); this.wishlistState(list); resolve(list); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; // this.getWishlistOfStore(cartId).addWishlistItem(product); } deleteItem(cartId: string, product: WishItem, batch?: boolean) { let payload: any = {}; let promise = new Promise((resolve, reject) => { this.apiProvider.api('DELETE', `/v1/list/${cartId}/${product.getProduct().id}`, payload) .then((response: any) => { if (response.id) { const list = WishList.fromJson(response); if (batch !== true) { // this.wishlists[response._id] = response; // this.wishlists = _.map(this.wishlists, (el: any) => { // if (el._id === cartId) { // return el = response; // } else { return el; } // }); const position = this.wishlists.findIndex((item) => { return item.id === list.id; }); this.wishlists.splice(position, 1, list); this.wishlistState(list); } resolve(list); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } emptyList(id: string) { const wish = _.find(this.wishlists, function(o: WishList) { return o.id === id; }); let promiseStack = []; wish.getWishlistItems().forEach((cItem: WishItem) => { promiseStack.push(this.deleteItem(id, cItem, true)); }); Promise.all(promiseStack).then( ()=> { console.log('all ok'); } ) // wish.getWishlistItems().reduce(function(p, val) { // return p.then(function() { // this.deleteItem(id, val, true); // }); // }, // Promise.resolve()).then(function(finalResult) { // // done here // wish.wishItems.splice(0, wish.wishItems.length); // const position = this.wishlists.findIndex((item) => { return item.id === wish.id; }); // this.wishlists.splice(position, 1, wish); // this.wishlistState(wish); // }, function(err) { // // error here` // }); // wish.wishItems.splice(0, wish.wishItems.length); // // this.wishlists = _.map(this.wishlists, (el: WishList) => { // // if (el.id === wish.id) { // // el.wishItems.splice(0, el.wishItems.length); // // } // // return el; // // }); // const position = this.wishlists.findIndex((item) => { return item.id === wish.id; }); // this.wishlists.splice(position, 1, wish); // this.wishlistState(wish); } // TODO: CHECK retrieveByUser(tenantId: string, userId: string) { let payload: any = { 'tenantId': tenantId, }; // payload.tenantId = tenantId; let promise = new Promise((resolve, reject) => { if (this.wishlists.length > 0 && !_.isUndefined(this.wishlists[0].name) ) resolve(this.wishlists); else { this.apiProvider.api('GET', '/v1/lists', payload) .then((response: any) => { if (response) { let lists: WishList[] = []; response.forEach((item: any) => { lists.push(WishList.fromJson(item)); }); // this.wishlists = response; // this.wishlistState(response); this.wishlists = lists.filter((item: WishList) => { return (item.name === userId && item.getTenantId() === tenantId); }); this.wishListsObservable.next(this.wishlists); console.log('resolving'); resolve(this.wishlists); // ?? } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); } }); return promise; } retrieve() { let payload: any = {}; payload.userId = 'randomID'; let promise = new Promise((resolve, reject) => { this.apiProvider.api('GET', '/v1/lists', payload) .then((response: any) => { if (response) { let lists: WishList[] = []; response.forEach((item: any) => { lists.push(WishList.fromJson(item)); }); this.wishlists = lists; this.wishListsObservable.next(this.wishlists); // this.wishlistState(this.wishlists); console.log('resolving'); resolve(lists); // ?? } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } retrieveListDetails(cartId: string, tenantId: string) { let payload: any = { 'tenantId': tenantId }; // payload.tenantId = tenantId; let promise = new Promise((resolve, reject) => { let wishlist_temp = this.wishlists.filter((item: WishList) => { return cartId === item.id; }); if (wishlist_temp.length >0 ) resolve(wishlist_temp[0]); else { this.apiProvider.api('GET', `/v1/list${cartId}`, payload) .then((response: any) => { if (response.id) { console.log('resolving'); const list = WishList.fromJson(response); this.currentWishlist = list; this.wishListObservable.next(this.currentWishlist); resolve(list); // ?? } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); } }); return promise; } retrieveListItem(cartId: string, productId: string) { let payload: any = {}; // payload.tenantId = tenantId; let promise = new Promise((resolve, reject) => { this.apiProvider.api('GET', `/v1/list/${cartId}/${productId}`, payload) .then((response: any) => { if (response) { console.log('resolving'); let product = Product.fromJson(response); resolve(product); // ?? } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } // updateList(cartId: string, tenantId: string, userId: string) { // let payload: any = { // 'tenantId': tenantId, // 'userId': userId // }; // // payload.tenantId = tenantId; // let promise = new Promise((resolve, reject) => { // this.apiProvider.api('PUT', `/v1/lists${cartId}`, payload) // .then((response: any) => { // if (response) { // this.wishlists[response._id] = response; // this.wishlistState(response); // this.currentWishlist = response; // console.log('resolving'); // resolve(response); // ?? // } else { // reject(); // } // }, (err) => { // catch error // // @todo: We should check for errors ?!? // reject(err); // }); // }); // return promise; // } // TODO CHANGE SINCE WE DON'T KNOW WHAT UPDATE ITEM RETURNS updateListItem(cartId: string, cItem: WishItem) { let payload: any = { productId: cItem.getProduct().id, quantity: cItem.getQuantity(), finalPrice: cItem.getCost(), title: cItem.getProduct().getName(), storeId: cItem.getStoreId() }; // payload.tenantId = tenantId; let promise = new Promise((resolve, reject) => { this.apiProvider.api('PUT', `/v1/list/${cartId}/${cItem.getProduct().id}`, payload) .then((response: any) => { if (response) { // const list = response // const position = this.wishlists.findIndex((item) => { return item.id === list.id; }); // this.wishlists.splice(position, 1, list); // this.wishlistState(list); console.log('resolving'); resolve(response); // ?? } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } // getDetails (cartId: string) { // let wishlist = {}; // if (!this.wishlists[cartId]) { // we don't have an active cart stored // // So, let's create a new one and store it in our cache // wishlist = this.emptyWishlist(); // this.wishlists[cartId] = wishlist; // }else { // wishlist = this.wishlists[cartId]; // } // return wishlist; // } // setCurrentWishlist (id: string) { // let wishlist: any = this.getDetails(id); // this.currentWishlist = {}; // for (let item in wishlist) { // if (wishlist.hasOwnProperty(item)) { // this.currentWishlist[item] = wishlist[item]; // } // } // // this.wishListObservable.next(this.currentWishlist); // console.log(this.wishlists); // } // testing deserialize retrieveDez(cartId: string) { let payload = {}; let promise = new Promise((resolve, reject) => { this.apiProvider.api('GET', '/v1/cart/' + cartId, payload) .then((response: any) => { if (response._id) { this.wishlists[response.id] = response; // this.wishlistState(response); this.currentWishlist = response; console.log('resolving'); resolve(this.deserializeWishlist(this.currentWishlist)); } else { reject(); } }, (err) => { // @todo: We should check for errors ?!? reject(err); }); }); return promise; } deserializeWishlist(incoming: any) { let items: WishItem[]; items = []; for (let item of incoming.items) { // let tempCartItem = let tempCartItem = new WishItem( CartItem.fromJson(item) , item.storeId); items.push(tempCartItem); } return new WishList(undefined, items, incoming.type, incoming.name, incoming._id); } deserializeWishlists(incoming: any) { let wishlistsN: WishList[] = _.map((_.values(incoming)), WishList.fromJson); // this.bind(wishlistsN); return wishlistsN; } // TODO Remove for demo // public bind(wishlistsN: WishList[]) { // let demoWishlists = JSON.parse(localStorage.getItem('apiWishLists')); // for (let wishList of wishlistsN){ // if (demoWishlists.typicodeId === wishList.apiId) { // wishList.tenantId = '56ea50cb8f535eecb185917f'; // } // if (demoWishlists.store_3TenantId === wishList.apiId) { // wishList.tenantId = '56ea50cb8f535eecb185823'; // } // } // } }