Skip to content

Instantly share code, notes, and snippets.

@cloudscape-germany
Created October 11, 2016 00:43
Show Gist options
  • Select an option

  • Save cloudscape-germany/eac309590096cc6f1b910f40a1b2f0c3 to your computer and use it in GitHub Desktop.

Select an option

Save cloudscape-germany/eac309590096cc6f1b910f40a1b2f0c3 to your computer and use it in GitHub Desktop.
using name Observable but cannot be named
import { Injectable } from "@angular/core";
import { RequestMethod } from "@angular/http";
import { Store } from "@ngrx/store";
import { Observable } from "rxjs/Observable";
import {
AppState,
ActionCreator,
UserModel,
BackendService,
LoggerService,
PurchaseModel,
GoalModel
} from "../../../shared";
@Injectable()
export class PurchaseService {
constructor(private store: Store<AppState>, private backendService: BackendService, private log: LoggerService) {
}
public saveMonthlyPurchase(goal: GoalModel) {
return this.purchase(goal.monthlyPayment, "SEPA", 12, goal.id);
}
public saveCurrentPurchase() {
const purchase: PurchaseModel = this.getPurchase();
const goal : GoalModel = this.getGoal();
return this.purchase(purchase.amount, "SEPA", 1, goal.id);
}
public acceptPolicy() {
const user: UserModel = this.getUser();
const data = {
"userId": user.id,
"effectiveDate": new Date(),
"requestDate": new Date()
};
return this.backendService.request("/api/v1/policy", data, RequestMethod.Post)
.map(response => {
const policyId = response.json().policyId;
this.log.info("received policyId " + policyId, this);
user.policyId = policyId;
this.store.dispatch(ActionCreator.updateUser(user));
});
}
private purchase(amount: Number, paymentType: string, frequency: number, goalId: string) {
const user: UserModel = this.getUser();
this.log.info("purchase policy goalId:" + goalId, this);
const data = {
"userId": user.id,
"policyId": user.policyId,
"goalId": goalId,
"paymentAgreement": {
"accountNumber": user.payment.iban,
"accountName": user.payment.accountName,
"amount": amount,
"startDate": new Date(),
"paymentType": paymentType,
"currency": "EUR",
"frequency": frequency,
}
};
this.log.info("Purchase will be saved. " + JSON.stringify(data), this);
return this.backendService.request("/api/v1/purchase", data, RequestMethod.Post);
}
private getUser(): UserModel {
let user: UserModel;
this.store.select(state => state.user).subscribe(u => user = u).unsubscribe();
return user;
}
private getPurchase(): PurchaseModel {
let purchase: PurchaseModel;
this.store.select(state => state.currentPurchase).subscribe(p => purchase = p).unsubscribe();
return purchase;
}
private getGoal(): GoalModel {
let goal: GoalModel;
this.store.select(state => state.currentGoal).subscribe(g => goal = g).unsubscribe();
return goal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment