import * as _ from 'lodash'; import { CartItem } from '../cart/cart'; import { Product } from'../product/product'; export class WishList { public wishCost: number; public static fromJson (obj: any) { return new WishList(obj.tenantId, _.map((_.values(obj.items)), WishItem.fromJson), obj.type, obj.name, obj.userId, obj.id); } public toJson() { let wishlistJson: any = { id: this.id, userId: this.userId, name: this.name, tenantId: '', __v: '', created: '', numItems: this.getItemsCount(), items: [] , // carItems to json type: this.type }; return wishlistJson; } constructor( public tenantId: string // TODO should be a Store instance when we have that , public wishItems: WishItem[] , public type: string , public name: string , public userId: string , public id?: string ) { this.calculateWishlistCost(); if (this.id === undefined) { this.id = ''; } } public calculateWishlistCost() { this.wishCost = 0; this.wishItems.forEach( (cItem: WishItem) => { this.wishCost += cItem.getCost(); }); } private findItemInWishlistItems(keyItem: WishItem) { return _.find(this.wishItems, (cItem: WishItem) => { return cItem.getProduct().getId() === keyItem.getProduct().getId(); }); } private findProductInWishlistItems(keyId: string) { return _.find(this.wishItems, (cItem: WishItem) => { return cItem.getProduct().getId() === keyId; }); } deleteWishlist() { // this.wishItems.splice(0, this.wishItems.length); // this.calculateWishlistCost(); } addWishlistItem(newItem: WishItem) { let foundItem: WishItem = this.findItemInWishlistItems(newItem); if (foundItem) { foundItem.updateItemQuantity(foundItem.getQuantity() + newItem.getQuantity()); } else { this.wishItems.push(newItem); } this.calculateWishlistCost(); } removeWishlistItem(idOfProductToRemove: string) { let indexOfItemToRemove = _.findIndex(this.wishItems, (cItem: WishItem) => { return cItem.getProduct().getId() === idOfProductToRemove; }); if (indexOfItemToRemove === -1) { // Case of failure console.error('Failed to remove product with id "' + idOfProductToRemove + '" from cart of "'); } else { // Case of proceeding as normal this.wishItems.splice(indexOfItemToRemove, 1); this.calculateWishlistCost(); } } getItem(prodId: string) { const indexOfItem = _.findIndex(this.wishItems, (cItem: WishItem) => { return cItem.getProduct().getId() === prodId; }); return this.wishItems[indexOfItem]; } incrementQuantity(prodId: string) { let foundItem: WishItem = this.findProductInWishlistItems(prodId); foundItem.updateItemQuantity(foundItem.getQuantity() + 1); this.calculateWishlistCost(); } decrementQuantity(prodId: string) { let foundItem: WishItem = this.findProductInWishlistItems(prodId); foundItem.updateItemQuantity(foundItem.getQuantity() - 1); this.calculateWishlistCost(); } getWishlistCost() { return this.wishCost; } getItemsCount() { return this.wishItems.length; } getTenantId() { return this.tenantId; } getWishlistItems() { return this.wishItems; } } export class WishItem { public cost: number; public static fromJson (obj: any) { return new WishItem( CartItem.fromJson(obj), obj.storeId); } public toJson() { let wishItem: any = this.cartItem.toJson(); wishItem.storeId = this.storeId; return wishItem; } constructor( // TODO have a unique itemId for each CartItem private cartItem: CartItem, public storeId: string ) { this.cost = this.getProduct().getPrice() * this.getQuantity() ; } updateItemQuantity(updatedQuantity: number) { this.getCartItem().quantity = updatedQuantity; this.cost = this.getCartItem().product.getPrice() * this.getQuantity(); } getCost() { return this.cost; } getProduct() { return this.getCartItem().getProduct(); } getQuantity() { return this.getCartItem().getQuantity() ; } getSize() { return this.getCartItem().getSize(); } getColour() { return this.getCartItem().getColour(); } getStoreId() { return this.storeId; } getCartItem() { return this.cartItem; } }