|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { Response } from '@angular/http'; |
| 3 | + |
| 4 | +import { DataService } from '../shared/services/data.service'; |
| 5 | +import { IOrder } from '../shared/models/order.model'; |
| 6 | +import { IOrderItem } from '../shared/models/orderItem.model'; |
| 7 | +import { IOrderDetail } from "../shared/models/order-detail.model"; |
| 8 | +import { SecurityService } from '../shared/services/security.service'; |
| 9 | +import { ConfigurationService } from '../shared/services/configuration.service'; |
| 10 | + |
| 11 | +import 'rxjs/Rx'; |
| 12 | +import { Observable } from 'rxjs/Observable'; |
| 13 | +import 'rxjs/add/observable/throw'; |
| 14 | +import { Observer } from 'rxjs/Observer'; |
| 15 | +import 'rxjs/add/operator/map'; |
| 16 | + |
| 17 | + |
| 18 | +@Injectable() |
| 19 | +export class CampaignService { |
| 20 | + private marketingUrl: string = ''; |
| 21 | + |
| 22 | + constructor(private service: DataService, private identityService: SecurityService, private configurationService: ConfigurationService) { |
| 23 | + if (this.configurationService.isReady) |
| 24 | + this.marketingUrl = this.configurationService.serverSettings.marketingUrl; |
| 25 | + else |
| 26 | + this.configurationService.settingsLoaded$.subscribe(x => this.marketingUrl = this.configurationService.serverSettings.marketingUrl); |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + getOrders(): Observable<IOrder[]> { |
| 31 | + let url = this.marketingUrl + '/api/v1/campaigns/'; |
| 32 | + |
| 33 | + return this.service.get(url).map((response: Response) => { |
| 34 | + return response.json(); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + getOrder(id: number): Observable<IOrderDetail> { |
| 39 | + let url = this.marketingUrl + '/api/v1/campaigns/' + id; |
| 40 | + |
| 41 | + return this.service.get(url).map((response: Response) => { |
| 42 | + return response.json(); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + mapOrderAndIdentityInfoNewOrder(): IOrder { |
| 47 | + let order = <IOrder>{}; |
| 48 | + let basket = this.basketService.basket; |
| 49 | + let identityInfo = this.identityService.UserData; |
| 50 | + |
| 51 | + console.log(basket); |
| 52 | + console.log(identityInfo); |
| 53 | + |
| 54 | + // Identity data mapping: |
| 55 | + order.street = identityInfo.address_street; |
| 56 | + order.city = identityInfo.address_city; |
| 57 | + order.country = identityInfo.address_country; |
| 58 | + order.state = identityInfo.address_state; |
| 59 | + order.zipcode = identityInfo.address_zip_code; |
| 60 | + order.cardexpiration = identityInfo.card_expiration; |
| 61 | + order.cardnumber = identityInfo.card_number; |
| 62 | + order.cardsecuritynumber = identityInfo.card_security_number; |
| 63 | + order.cardtypeid = identityInfo.card_type; |
| 64 | + order.cardholdername = identityInfo.card_holder; |
| 65 | + order.total = 0; |
| 66 | + order.expiration = identityInfo.card_expiration; |
| 67 | + |
| 68 | + // basket data mapping: |
| 69 | + order.orderItems = new Array<IOrderItem>(); |
| 70 | + basket.items.forEach(x => { |
| 71 | + let item: IOrderItem = <IOrderItem>{}; |
| 72 | + item.pictureurl = x.pictureUrl; |
| 73 | + item.productId = +x.productId; |
| 74 | + item.productname = x.productName; |
| 75 | + item.unitprice = x.unitPrice; |
| 76 | + item.units = x.quantity; |
| 77 | + |
| 78 | + order.total += (item.unitprice * item.units); |
| 79 | + |
| 80 | + order.orderItems.push(item); |
| 81 | + }); |
| 82 | + |
| 83 | + order.buyer = basket.buyerId; |
| 84 | + |
| 85 | + return order; |
| 86 | + } |
| 87 | + |
| 88 | +} |
| 89 | + |
0 commit comments