import { Product } from '../product/product'; import * as _ from 'lodash'; import * as moment from 'moment'; export class Offer { public discountRate: number = 0; // Discount percent in range (0,1), initialised when product is set public product: Product = undefined; // Optional assisting attribute to refer to related Product obj static createDummyOffer(): Offer { let dummyOffer = new Offer( '__DUMMY__' , '__DUMMY__' , '__DUMMY__' , '__DUMMY__' , '__DUMMY__' , '__DUMMY__' , '__DUMMY__' , -1 , '__DUMMY__' , moment('1970-01-01T00:00:00') ); return dummyOffer; } static fromJson(obj: any): Offer { let _id: string = (obj.id) ? obj.id : '__FALLBACK__'; let _tenantId: string = (obj.tenantId) ? obj.tenantId : '__FALLBACK__'; let _customerId: string = (obj.customerId) ? obj.customerId : '__FALLBACK__'; let _price: number = (obj.price) ? obj.price : -1; let _name: string = (obj.name) ? obj.name : '__FALLBACK__'; let _description: string = (obj.description) ? obj.description : '__FALLBACK__'; let _img_url: string = (obj.img_url) ? obj.img_url : '__FALLBACK__'; let _productId: string = (obj.productId) ? obj.productId : '__FALLBACK__'; let _storeId: string = (obj.storeId) ? obj.storeId : '__FALLBACK__'; let _created: moment.Moment = (obj.created) ? obj.created : moment('1970-01-01T00:00:00'); // TODO return error and handle it visually if api response not compatible return new Offer(_id, _tenantId, _customerId, _storeId, _productId, _name, _description, _price, _img_url, _created ); } // Pushes @offer in @offers only if it isn't already in @offers static pushOfferIfUnique(offerToPush: Offer, offers: Offer[]) { let foundOffer: Offer = _.find(offers, (o: Offer) => { return o.getOfferId() === offerToPush.getOfferId(); }); if (!foundOffer) { offers.push(offerToPush); } } static updateOrPushOffer(offerToUpdate: Offer, offers: Offer[]) { let indexOfItemToUpdate = _.findIndex(offers, (o: Offer) => { return o.getOfferId() === offerToUpdate.getOfferId(); }); if (indexOfItemToUpdate !== -1) { offers[indexOfItemToUpdate] = offerToUpdate; } else if (indexOfItemToUpdate === -1) { offers.push(offerToUpdate); console.warn('Warning: Former version of updated offer was not found in local cache, but cache was synced anyway.'); } } static deleteOffer(offerToDelete: Offer, offers: Offer[]) { let indexOfItemToDelete = _.findIndex(offers, (o: Offer) => { return o.getOfferId() === offerToDelete.getOfferId(); }); if (indexOfItemToDelete !== -1) { offers.splice(indexOfItemToDelete, 1); } else if (indexOfItemToDelete === -1) { console.warn('Warning: Former version of deleted offer not found in local cache.'); } } constructor ( public offerId: string , public tenantId: string , public customerId: string , public storeId: string , public productId: string , public name: string , public description: string , public discountedPrice: number // Must be in range (0,1) , public img_url: string , public created: moment.Moment ) {} getOfferId () { return this.offerId; } getTenantId () { return this.tenantId; } getCustomerId () { return this.customerId; } getStoreId () { return this.storeId; } getProductName () { return this.name; } getProductDescription () { return this.description; } getProductImgUrl () { return this.img_url; } getCreationDate () { return this.created; } getProduct () { return this.product; } getProductId () { return this.productId; } getDiscountRate () { return this.discountRate; } getDiscountedPrice () { return this.discountedPrice; } setProduct (prod: Product) { this.product = prod; this.discountRate = Math.abs( (this.discountedPrice - prod.getPrice()) / (prod.getPrice()) ); } }