import { UserDataState } from '../../common/state/UserDataState'; import { QuoteService } from '../../common/services/QuoteService'; import { AnalyticsService } from '../../common/services/AnalyticsService'; import { Quote, TariffWithId } from '../../common/entities/Quote'; import { Utility, TariffId, EligibilityStatus } from '../../common/entities/Enums'; import { LayoutState } from "../../common/state/LayoutState"; import { ModalService } from '../../common/services/ModalService'; import tilModalOptions from '../../common/modals/TILModal'; import { HostExtractor } from '../../common/util/HostExtractor'; import { EligibilityService } from "../../common/services/EligibilityService"; import { PropertyQuestionsService } from "../../common/services/PropertyQuestionsService"; import { Eligibility, Availability } from "../../common/entities/Eligibility"; import { isString } from 'lodash'; export class PlanPaygController implements ng.IController { loading = true; quote: Quote; tariff: TariffWithId; availability: Availability; eligibility: Eligibility; propertyQuestionsAnswered: boolean; emailLoading = false; error: string; email: string; saved = false; constructor(private $state: ng.ui.IStateService, private $window: ng.IWindowService, private layoutState: LayoutState, private userDataState: UserDataState, private quoteService: QuoteService, private analyticsService: AnalyticsService, private modal: ModalService, private eligibilityServiceNew: EligibilityService, private propertyQuestionsService: PropertyQuestionsService, private host: HostExtractor) { this.userDataState.tariffId = 'Variable'; this.layoutState.back = () => this.$window.history.back(); } $onInit() { return this.quoteService.getQuote() .then(this.quoteHandler, error => this.$state.go("^.get-quote")) .then(() => this.loading = false); } private quoteHandler = (quote: Quote) => { this.quote = quote; // For PAYG we assume that there is a Variable tariff this.tariff = { ...this.quote.tariffs['Variable'], ...{ id: 'Variable' } }; this.layoutState.title = this.tariff.name; this.analyticsService.push({ event: 'ViewPlan', tariff: 'Variable', cost: this.tariff.expectedAnnualSpend }); if (this.host.retailer === 'OVO') return this.eligibilityServiceNew.checkSupplyAddress().then(this.eligibilityHandler, () => { this.eligibility = { status: 'Eligible', smartMeterRequired: true, isIGT: false } }); }; private eligibilityHandler = (eligibility: Eligibility) => { if (eligibility.status === 'Eligible' && eligibility.smartMeterRequired) { const propertyQuestionsStatus = this.propertyQuestionsService.getEligibilityStatus(); this.propertyQuestionsAnswered = isString(propertyQuestionsStatus); if (this.propertyQuestionsAnswered) { this.eligibility = { ...eligibility, status: propertyQuestionsStatus }; // Only need to check the availability if they have answered the questions and are eligible if (propertyQuestionsStatus === 'Eligible') return this.eligibilityServiceNew.checkAvailability().then(availability => { this.availability = availability }, () => { this.availability = { appointmentAvailability: 'High' } }); } // They haven't answered the property questions yet else this.eligibility = eligibility; } // Ineligible for meter/area reasons, or they already have a smart meter else this.eligibility = eligibility; } submit() { if (this.eligibility && this.eligibility.status === 'Eligible' && this.eligibility.smartMeterRequired && !this.propertyQuestionsAnswered) this.$state.go('^.property-questions'); else this.$state.go('^.switch'); } saveQuote() { if (!this.emailLoading) { this.emailLoading = true; return this.quoteService.saveQuote({ email: this.email, quote: this.quote, tariffId: 'Variable', }).then(() => { this.emailLoading = false; this.saved = true; }).catch(() => { this.emailLoading = false; this.error = 'Sorry, we could not save your quote. Please try again later.'; }); } } openTilModal(utility: Utility) { this.modal.open(tilModalOptions({ tariff: this.tariff, til: this.tariff.tils[utility], serviceType: this.quote.serviceType, utility })); } }