UNPKG

portal-www

Version:

Nova Portal Website. Based on Next starter by Ueno

690 lines (580 loc) 17.8 kB
import { isEmpty } from 'lodash'; import { action, computed, makeObservable, observable } from 'mobx'; import { IAutoRefill, IConnection, IProfile, IRateplan, IServicepack, IServicepackCategory, ISummary, IUsagePack, IVisitSlotInfo, ProfileStatusCode, } from 'typings'; import { FiberInfo, Profile as ProfileType, Subscription } from 'typings/graphql'; import { formatPhone, getMonth } from 'utils/helpers'; import { getAlltSamanNavItems, getCallForwardingServiceNavItems, getEnterpriseServiceNavItems, getFiberServiceNavItems, getHradleidNavItems, getLinks, getMobileSerivceNavItems, getNovaTVServiceNavItems, getPendingFiberServiceNavItems, getThjonustaSidePanelNavItems, getUnregisteredMobileServiceNavItems, NavItem, } from 'utils/links'; import AutoRefill from './AutoRefill'; import Connection from './Connection'; import Rateplan from './Rateplan'; import UsagePack from './UsageInfo'; export default class Profile { /* Temporarily allow construction of Profile from graphql type Profile as well as the the interface IProfile by assuming that a lot of ProfileType's properties are not defined. This is so we can more easily switch to using data from typed query hooks from using the manually created interfaces from IUserData without refactoring everything that user Profile. */ constructor(state: IProfile | ProfileType) { this.ssn = state.ssn; this.name = state.name; this.email = state.email; this.subscriptionId = state.subscriptionId; this.title = state.title!; this.rateplan = new Rateplan(state.rateplan as IRateplan); this.isLoggedInAs = state.isLoggedInAs!; this.isUser = state.isUser!; this.isPayer = state.isPayer!; this.statusCode = state.statusCode; this.selectedMonth = getMonth(new Date()); this.accountName = state.accountName; this.accountSsn = state.accountSsn; this.packs = state.packs as IUsagePack[]; this.autoRefills = state.autoRefills ? state.autoRefills.map((r) => new AutoRefill(r as IAutoRefill)) : []; this._connections = state.connections ? state.connections.map((connection) => new Connection(connection as IConnection)) : []; this.visitSlotInfo = state.visitSlotInfo as IVisitSlotInfo; this.activeUntil = state.activeUntil ? new Date(state.activeUntil) : null; this.summary = state.summary as ISummary; this.subscriptionGuid = state.subscriptionGuid!; this.created = state.created; this.subscriptions = state.subscriptions!; this.fiberInfo = state.fiberInfo; this.isRouted = state.isRouted || false; makeObservable(this, { ssn: observable, name: observable, subscriptionId: observable, subscriptionGuid: observable, title: observable, rateplan: observable, isUser: observable, isPayer: observable, isLoggedInAs: observable, statusCode: observable, autoRefills: observable, isLoadingSettings: observable, selectedMonth: observable, accountName: observable, accountSsn: observable, selected: observable, packs: observable, _connections: observable, visitSlotInfo: observable, summary: observable, updateSelectedMonth: action, updateVisitSlotInfo: action, updateAutoRefills: action, profileStatusCode: computed, allowedServicePacks: computed, hasOfferings: computed, onlyRateplanChangeAllowed: computed, displayRefillHistory: computed, isRegistered: computed, isBundle: computed, type: computed, b1w: computed, fiberSignupPage: computed, signup: computed, isPrepaid: computed, isNovaTV: computed, fiberNeedsVisit: computed, isChild: computed, panelSubNav: computed, }); } ssn: string; name: string; email: string; subscriptionId: string; subscriptionGuid: string; title: string; rateplan: IRateplan; isUser: boolean; isPayer: boolean; isLoggedInAs: boolean; statusCode: string; autoRefills: Array<AutoRefill>; isLoadingSettings = false; selectedMonth: string; accountName: string; accountSsn: string; selected = false; packs: Array<IUsagePack>; _connections: Array<IConnection>; visitSlotInfo: IVisitSlotInfo; summary: ISummary; activeUntil: Date | null; created: Date | undefined; subscriptions: Array<Subscription>; fiberInfo: FiberInfo; isRouted: boolean; get firstName() { return this.name ? this.name.split(' ')[0] : 'noname'; } get isActive() { return this.statusCode === 'active' || this.fiberSignupPage || this.b1w; } get hasConnections() { return !isEmpty(this.connections); } get mobileConnections() { if (this.hasConnections) { return this.connections.filter((c) => c.type === 'Mobile').length; } return 0; } get internetConnections() { if (this.hasConnections) { return this.connections.filter((c) => c.type === 'Internet').length; } return 0; } get numberForwardConnections() { if (this.hasConnections) { return this.connections.filter((c) => c.type === 'CallForwarding').length; } return 0; } get backupConnections() { if (this.hasConnections) { return this.connections.filter((c) => c.type === 'BackupConnection').length; } return 0; } get availableMobileConnections() { if (this?.rateplan?.mobileCount) { const availableConnections = this.rateplan.mobileCount - this.mobileConnections; if (availableConnections < 1) return 0; return availableConnections; } return 0; } get availableInternetConnections() { if (this?.rateplan?.internetCount) { const availableConnections = this.rateplan.internetCount - this.internetConnections; if (availableConnections < 1) return 0; return availableConnections; } return 0; } get availableNumberForwardConnections() { if (this?.rateplan?.numberForwardCount) { const availableConnections = this.rateplan.numberForwardCount - this.numberForwardConnections; if (availableConnections < 1) return 0; return availableConnections; } return 0; } get availableBackupConnections() { if (this?.rateplan?.backupCount) { const availableConnections = this.rateplan.backupCount - this.backupConnections; if (availableConnections < 1) return 0; return availableConnections; } return 0; } get displayTitle() { if (this.title.length === 7) { return formatPhone(this.title); } if (this.type === 'fiber') { return this.title.substring(0, this.title.indexOf(',')); } return this.title; } findAutoRefillForPack(packs: Array<UsagePack>, packIndex: number) { if (!this.autoRefills) return undefined; const pack = packs[packIndex]; return this.autoRefills.find((a) => pack.info && pack.info.id === a.refillId); } updateSelectedMonth(date: Date) { this.selectedMonth = getMonth(date); } updateVisitSlotInfo(visitSlotInfo: IVisitSlotInfo) { this.visitSlotInfo = visitSlotInfo; } updateAutoRefills(autoRefillList: Array<IAutoRefill>) { this.autoRefills = autoRefillList?.map((a) => new AutoRefill(a)); } get connections() { return ((connectionsToSort) => { return connectionsToSort.slice().sort((connection: IConnection) => { return connection.type === 'Internet' ? -1 : 1; }); })(this._connections); } get profileStatusCode(): ProfileStatusCode { return ProfileStatusCode[this.statusCode]; } get allowedServicePacks() { const currentPacks = this.packs || []; if (this.rateplan && this.rateplan.availableServicepacks) { const filteredList = this.rateplan.availableServicepacks.map( (category: IServicepackCategory) => { const { servicepacks, ...rest } = category; return { ...rest, servicepacks: servicepacks.filter( (s: IServicepack) => s.forSale && currentPacks.findIndex((p) => p.servicepackId === s.id) === -1, ), }; }, ); return filteredList.filter((cats) => cats.servicepacks.length > 0); } return []; } get hasOfferings() { return ( this.rateplan && !this.rateplan.isPrepaid && ((this.allowedServicePacks && this.allowedServicePacks.length > 0) || this.onlyRateplanChangeAllowed) ); } hasOfferingsByTypeId(typeId: string) { if (!typeId) { return false; } return ( this.allowedServicePacks.filter((p) => p.servicepackType === typeId).length > 0 || this.onlyRateplanChangeAllowed ); } isOfType(types: Array<string>) { return types.includes(this.type); } get onlyRateplanChangeAllowed() { return ( ['fiber', 'internet'].indexOf(this.type) > -1 && this.rateplan.availableRateplans.length > 0 ); } get displayRefillHistory() { return ['mobile', 'internet', 'unregistered'].indexOf(this.type) > -1; } get isRegistered() { return this.type !== 'unregistered'; } get isBundle() { return this.type === 'service_bundle' || this.type === 'vip_service_bundle'; } get type() { return this.rateplan?.typeId; } get b1w() { return this.statusCode === 'b1w' && !this.isPrepaid; } get fiberSignupPage() { return this.signup && this.fiberNeedsVisit; } get signup() { return this.type === 'fiber' && this.profileStatusCode === ProfileStatusCode.signup; } get isPrepaid() { return this.rateplan?.isPrepaid && !this.isBundle; } get isNovaTV() { // Better check needed return this.rateplan?.title === 'NovaTV'; } // 0: Engin GR pöntun // 1: Pöntun móttekin // 2: Uppsetning ekki bókuð // 3: Uppsetning bókuð // 4: Beðið eftir blæstri // 5: Pöntun hafnað // 6: Í viðskiptum get fiberNeedsVisit() { return [2, 3].includes(this.visitSlotInfo?.code); } get isChild() { let year = 0; switch (this.ssn.substring(9, 10)) { case '0': year = 2000; break; case '8': year = 1800; break; case '9': year = 1900; break; } year += parseInt(this.ssn.substring(4, 6), 10); const today = new Date(); const birthdate = new Date( year, parseInt(this.ssn.substring(2, 4), 10) - 1, parseInt(this.ssn.substring(0, 2), 10), ); let age = today.getFullYear() - birthdate.getFullYear(); today.setFullYear(year); if (birthdate > today) { age -= 1; } return age < 18; } get isContract() { // i know. console.log(this.email); // eslint-disable-line return this.email === 'na'; } getSubNav(ssn: string, isStaff: boolean): NavItem[] { const isAlltSaman = this.rateplan?.title?.toLowerCase().includes('alltsaman'); if (this.isNovaTV) { return getNovaTVServiceNavItems({ subscriptionId: this.subscriptionId, ssn, }); } if (this.signup) { return getPendingFiberServiceNavItems({ ssn, subscriptionId: this.subscriptionId, }); } if (this.isBundle && this.type === 'vip_service_bundle') { return getHradleidNavItems({ ssn, subscriptionId: this.subscriptionId, }); } if (!this.isRegistered) { return getUnregisteredMobileServiceNavItems(this.subscriptionId); } if (this.isBundle) { return getAlltSamanNavItems({ ssn, subscriptionId: this.subscriptionId, }); } if (this.type === 'fiber') { return getFiberServiceNavItems({ ssn, subscriptionId: this.subscriptionId }); } if (this.displayRefillHistory) { return getMobileSerivceNavItems({ ssn, subscriptionId: this.subscriptionId, isStaff, isAlltSaman, }); } if (this.type === 'landline' || this.type === 'continent') { return getEnterpriseServiceNavItems({ ssn, subscriptionId: this.subscriptionId, }); } if (this.type === 'forwarding') { return getCallForwardingServiceNavItems({ ssn, subscriptionId: this.subscriptionId, }); } return getMobileSerivceNavItems({ ssn, subscriptionId: this.subscriptionId, isStaff, isAlltSaman, }).filter((l) => l.id !== 'refillHistory'); } getHeroSubNavigation(ssn: string, isStaff: boolean): NavItem[] { // fetch links with isStaff is true since herosubNav is only for staff const { more, novatv } = getLinks(true); const list = this.getSubNav(ssn, isStaff); // What is this magic and why is it here? if (this.profileStatusCode < 3) { // eslint-disable-line if (this.isNovaTV) { list.push({ id: 'watch', className: 'tourWatch', title: 'common:hero.watchInBrowser', subPath: '', href: novatv.webplayer, addService: true, newTab: true, subLinks: [], hoverLinks: [], }); } else if (this.rateplan.isPrepaid && !this.isBundle) { list.push({ id: 'refill', className: 'tourRefill', title: 'common:hero.refillButton', subPath: '', href: `${more.fylltann.custom}/${this.subscriptionId}`, addService: true, newTab: true, subLinks: [], hoverLinks: [], }); } else if (this.hasOfferings && !this.onlyRateplanChangeAllowed) { list.push({ id: 'addService', className: 'tourAddService', title: 'common:hero.serviceButton', href: `/${ssn}/thjonusta/${this.subscriptionId}/kaupa`, subPath: '', addService: true, newTab: false, subLinks: [], hoverLinks: [], }); } } return list; } get panelSubNav() { const list: NavItem[] = getThjonustaSidePanelNavItems(this.subscriptionId); /* What in the world is happening below this comment? */ if (this.subscriptionGuid && !this.isBundle) { const leidrettingar = list.find((l) => l.id === 'leidrettingar'); leidrettingar?.hoverLinks?.push( { id: 'leidretting', className: '', title: 'Leiðrétting á núverandi', subPath: '', href: `https://portal.nova.is/${this.ssn}/thjonusta/${this.subscriptionId}/leidretting?step=1`, addService: false, newTab: true, subLinks: [], hoverLinks: [], }, { id: 'leidrettingReikning', title: 'Leiðrétting á útgefinn reikning', href: `https://innri.tankar.local/leidretting/velja/${this.ssn}`, newTab: true, subLinks: [], hoverLinks: [], }, { id: 'sarabaetur', title: 'Sárabætur', href: `/${this.ssn}/thjonusta/${this.subscriptionId}/sarabaetur`, newTab: true, subLinks: [], hoverLinks: [], }, ); list.push({ id: 'CRM', className: '', title: 'Gömul Saga', subPath: '', href: 'https://crm.nova.is/Nova/main.aspx?etc=10029&id=${this.subscriptionGuid}&pagetype=entityrecord', addService: false, newTab: true, subLinks: [], hoverLinks: [], }); } if (this.isChild) { list.push({ id: 'MinusAtjan', title: 'Mínus átján', className: '', subPath: '', href: `https://innri.nova.is/farsimi/frelsi/minus-atjan/${this.subscriptionId}`, addService: false, newTab: true, subLinks: [], hoverLinks: [], }); } if (this.type === 'fiber' || this.rateplan.id === 'R1707') { const msisdn = this.type === 'fiber' ? this.title : this.subscriptionId; list.push({ id: 'unlimitedWithFiber', className: '', title: 'Ótakmarkað net með Ljósleiðara', subPath: '', href: `https://innri.nova.is/farsimi/askrift/netnet/${this.accountSsn}/${this.ssn}/${msisdn}/komdu/1`, addService: false, newTab: true, subLinks: [], hoverLinks: [], }); } if (this.type === 'fiber') { list.push({ id: 'uppsogn', className: '', title: 'Segja upp', subPath: '', href: `https://fiberresign.nova.is/?msisdn=${this.title}`, addService: false, newTab: true, subLinks: [], hoverLinks: [], }); } const zendesk = list.find((l) => l.id === 'zendesk'); zendesk?.hoverLinks?.push( { id: 'newPayer', title: 'Nýtt mál á greiðanda', href: `https://portal.nova.is/skra-mal?&phoneNumber=${this.subscriptionId}&name=${this.accountName}&kt=${this.accountSsn}`, newTab: true, subLinks: [], hoverLinks: [], }, { id: 'newUser', title: 'Nýtt mál á notanda', href: `https://portal.nova.is/skra-mal?&email=${this.email}&phoneNumber=${this.subscriptionId}&name=${this.name}&kt=${this.ssn}`, newTab: true, subLinks: [], hoverLinks: [], }, ); if (!this.isRegistered) { list.push({ id: 'registerUnregistered', className: '', title: 'Skrá óskráð frelsi', href: `/skra?subscriptionId=${this.subscriptionId}`, subPath: '', addService: false, newTab: true, subLinks: [], hoverLinks: [], }); } return list; } }