diff --git a/src/app/core/mock-data/org-settings.data.ts b/src/app/core/mock-data/org-settings.data.ts index 7244a5def6..fd5cf5176c 100644 --- a/src/app/core/mock-data/org-settings.data.ts +++ b/src/app/core/mock-data/org-settings.data.ts @@ -1,5 +1,5 @@ import { AllowedPaymentModes } from '../models/allowed-payment-modes.enum'; -import { OrgSettings, TaxSettings } from '../models/org-settings.model'; +import { EmailEvents, OrgSettings, TaxSettings } from '../models/org-settings.model'; import { orgSettingsData } from '../test-data/accounts.service.spec.data'; export const orgSettingsRes: OrgSettings = { @@ -1346,3 +1346,11 @@ export const orgSettingsWoMileage: OrgSettings = { ...orgSettingsParams2, mileage: null, }; + +export const orgSettingsWithUnsubscribeEvent: OrgSettings = { + ...orgSettingsRes, + admin_email_settings: { + ...orgSettingsRes.admin_email_settings, + unsubscribed_events: [EmailEvents.DELEGATOR_SUBSCRIPTION, EmailEvents.EADVANCES_CREATED], + }, +}; diff --git a/src/app/core/mock-data/org-user-settings.data.ts b/src/app/core/mock-data/org-user-settings.data.ts index 5b9c456c68..605d165484 100644 --- a/src/app/core/mock-data/org-user-settings.data.ts +++ b/src/app/core/mock-data/org-user-settings.data.ts @@ -683,3 +683,30 @@ export const orgUserSettingsWoProjects: OrgUserSettings = { ...orgUserSettingsData, project_ids: null, }; + +export const notificationDelegateeSettings1: OrgUserSettings = { + ...orgUserSettingsData, + notification_settings: { + ...orgUserSettingsData.notification_settings, + notify_delegatee: true, + notify_user: false, + }, +}; + +export const notificationDelegateeSettings2: OrgUserSettings = { + ...orgUserSettingsData, + notification_settings: { + ...orgUserSettingsData.notification_settings, + notify_delegatee: false, + notify_user: true, + }, +}; + +export const notificationDelegateeSettings3: OrgUserSettings = { + ...orgUserSettingsData, + notification_settings: { + ...orgUserSettingsData.notification_settings, + notify_delegatee: true, + notify_user: true, + }, +}; diff --git a/src/app/core/models/notification-events.model.ts b/src/app/core/models/notification-events.model.ts index 1561a035ab..fdf5ba8dfb 100644 --- a/src/app/core/models/notification-events.model.ts +++ b/src/app/core/models/notification-events.model.ts @@ -1,8 +1,10 @@ +import { EmailEvents as EE } from './org-settings.model'; + export type EmailEvents = { email: { selected: boolean; }; - eventType: string; + eventType: string | EE; feature: string; push: { selected: boolean; @@ -14,7 +16,7 @@ export type EmailEvents = { export interface NotificationEvents { events: EmailEvents[]; features: { - advances: { + advances?: { selected: boolean; textLabel: string; }; @@ -24,3 +26,14 @@ export interface NotificationEvents { }; }; } + +export interface NotificationEventFeatures { + advances: { + selected: boolean; + textLabel: string; + }; + expensesAndReports: { + selected: boolean; + textLabel: string; + }; +} diff --git a/src/app/fyle/notifications/notifications.page.spec.ts b/src/app/fyle/notifications/notifications.page.spec.ts index e9bacfa4c8..82e8a21746 100644 --- a/src/app/fyle/notifications/notifications.page.spec.ts +++ b/src/app/fyle/notifications/notifications.page.spec.ts @@ -1,26 +1,360 @@ -import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; -import { IonicModule } from '@ionic/angular'; +import { ComponentFixture, TestBed, fakeAsync, tick, waitForAsync } from '@angular/core/testing'; +import { NavController } from '@ionic/angular'; +import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; +import { FormArray, FormBuilder, ReactiveFormsModule } from '@angular/forms'; +import { Router } from '@angular/router'; +import { RouterTestingModule } from '@angular/router/testing'; +import { cloneDeep } from 'lodash'; +import { of } from 'rxjs'; +import { apiEouRes } from 'src/app/core/mock-data/extended-org-user.data'; +import { notificationEventsData } from 'src/app/core/mock-data/notification-events.data'; +import { orgSettingsWithUnsubscribeEvent } from 'src/app/core/mock-data/org-settings.data'; +import { + notificationDelegateeSettings1, + notificationDelegateeSettings2, + notificationDelegateeSettings3, + orgUserSettingsData, +} from 'src/app/core/mock-data/org-user-settings.data'; +import { AuthService } from 'src/app/core/services/auth.service'; +import { OrgSettingsService } from 'src/app/core/services/org-settings.service'; +import { OrgUserSettingsService } from 'src/app/core/services/org-user-settings.service'; +import { orgSettingsData } from 'src/app/core/test-data/accounts.service.spec.data'; import { NotificationsPage } from './notifications.page'; -xdescribe('NotificationsPage', () => { +export class NavMock { + public navigateBack: Function = (url: string | any[], options: any) => {}; + public navigateForward: Function = (url: string | any[], options: any) => {}; + public navigateRoot: Function = (url: string | any[], options: any) => {}; +} + +describe('NotificationsPage', () => { let component: NotificationsPage; let fixture: ComponentFixture; + let authService: jasmine.SpyObj; + let orgUserSettingsService: jasmine.SpyObj; + let fb: FormBuilder; + let orgSettingsService: jasmine.SpyObj; + let router: jasmine.SpyObj; + let navController: jasmine.SpyObj; + + beforeEach(waitForAsync(() => { + const authServiceSpy = jasmine.createSpyObj('AuthService', ['getEou']); + const orgUserSettingsServiceSpy = jasmine.createSpyObj('OrgUserSettingsService', [ + 'post', + 'clearOrgUserSettings', + 'get', + 'getNotificationEvents', + ]); + const orgSettingsServiceSpy = jasmine.createSpyObj('OrgSettingsService', ['get']); + const routerSpy = jasmine.createSpyObj('Router', ['navigate']); + + TestBed.configureTestingModule({ + declarations: [NotificationsPage], + imports: [RouterTestingModule, ReactiveFormsModule], + providers: [ + FormBuilder, + { + provide: AuthService, + useValue: authServiceSpy, + }, + { + provide: OrgUserSettingsService, + useValue: orgUserSettingsServiceSpy, + }, + { + provide: OrgSettingsService, + useValue: orgSettingsServiceSpy, + }, + { + provide: Router, + useValue: routerSpy, + }, + { + provide: NavController, + useClass: NavMock, + }, + ], + schemas: [CUSTOM_ELEMENTS_SCHEMA], + }).compileComponents(); + + fixture = TestBed.createComponent(NotificationsPage); + component = fixture.componentInstance; + + authService = TestBed.inject(AuthService) as jasmine.SpyObj; + orgUserSettingsService = TestBed.inject(OrgUserSettingsService) as jasmine.SpyObj; + orgSettingsService = TestBed.inject(OrgSettingsService) as jasmine.SpyObj; + router = TestBed.inject(Router) as jasmine.SpyObj; + navController = TestBed.inject(NavController) as jasmine.SpyObj; + fb = TestBed.inject(FormBuilder); - beforeEach( - waitForAsync(() => { - TestBed.configureTestingModule({ - declarations: [NotificationsPage], - imports: [IonicModule.forRoot()], - }).compileComponents(); + component.notificationForm = fb.group({ + notifyOption: [], + pushEvents: new FormArray([]), + emailEvents: new FormArray([]), + }); - fixture = TestBed.createComponent(NotificationsPage); - component = fixture.componentInstance; - fixture.detectChanges(); - }) - ); + orgUserSettingsService.get.and.returnValue(of(orgUserSettingsData)); + authService.getEou.and.resolveTo(apiEouRes); + orgSettingsService.get.and.returnValue(of(orgSettingsData)); + orgUserSettingsService.getNotificationEvents.and.returnValue(of(notificationEventsData)); + + component.delegationOptions = ['Notify me and my delegate', 'Notify my delegate', 'Notify me only']; + + component.isAllSelected = { + emailEvents: false, + pushEvents: false, + }; + })); it('should create', () => { expect(component).toBeTruthy(); }); + + describe('getDelegateeSubscription():', () => { + it('should only notify the delegatee', (done) => { + component.orgUserSettings$ = of(notificationDelegateeSettings1); + + component.getDelegateeSubscription().subscribe((res) => { + expect(res).toEqual('Notify my delegate'); + done(); + }); + }); + + it('should only notify the user', (done) => { + component.orgUserSettings$ = of(notificationDelegateeSettings2); + + component.getDelegateeSubscription().subscribe((res) => { + expect(res).toEqual('Notify me only'); + done(); + }); + }); + + it('should notify both the user and delegatee', (done) => { + component.orgUserSettings$ = of(notificationDelegateeSettings3); + + component.getDelegateeSubscription().subscribe((res) => { + expect(res).toEqual('Notify me and my delegate'); + done(); + }); + }); + }); + + it('setEvents(): should set events', () => { + component.setEvents(cloneDeep(notificationEventsData), cloneDeep(orgUserSettingsData)); + + expect(component.emailEvents.length).not.toEqual(0); + expect(component.pushEvents.length).not.toEqual(0); + }); + + it('goBack(): should go back to the profile page', () => { + component.goBack(); + + expect(router.navigate).toHaveBeenCalledOnceWith(['/', 'enterprise', 'my_profile']); + }); + + it('saveNotificationSettings(): should save notification settings', () => { + component.setEvents(notificationEventsData, orgUserSettingsData); + component.notificationEvents = cloneDeep(notificationEventsData); + component.orgUserSettings = cloneDeep(orgUserSettingsData); + orgUserSettingsService.post.and.returnValue(of(null)); + orgUserSettingsService.clearOrgUserSettings.and.returnValue(of(null)); + spyOn(component, 'navBack'); + + component.saveNotificationSettings(); + + expect(orgUserSettingsService.post).toHaveBeenCalledOnceWith(component.orgUserSettings); + expect(orgUserSettingsService.clearOrgUserSettings).toHaveBeenCalledTimes(1); + expect(component.navBack).toHaveBeenCalledTimes(1); + }); + + it('isAllEventsSubscribed(): should check if all events are subscribed', () => { + component.orgUserSettings = orgUserSettingsData; + + component.isAllEventsSubscribed(); + + expect(component.isAllSelected).toEqual({ + emailEvents: false, + pushEvents: false, + }); + }); + + it('removeAdminUnsbscribedEvents(): should remove admin unsubscribe events', fakeAsync(() => { + component.orgSettings$ = of(orgSettingsWithUnsubscribeEvent); + component.orgSettings = orgSettingsWithUnsubscribeEvent; + component.notificationEvents = cloneDeep(notificationEventsData); + + component.removeAdminUnsbscribedEvents(); + tick(500); + + expect(Object.keys(component.notificationEvents.features).includes('advances')).toBeFalse(); + })); + + it('updateAdvanceRequestFeatures(): should update advance request features', () => { + component.notificationEvents = notificationEventsData; + component.orgSettings$ = of(orgSettingsData); + + component.updateAdvanceRequestFeatures(); + + expect(Object.keys(notificationEventsData.features).includes('advances')).toBeFalse(); + }); + + describe('updateDelegateeNotifyPreference():', () => { + it('should set settings to notify delegatee', () => { + component.orgUserSettings = orgUserSettingsData; + + component.updateDelegateeNotifyPreference({ + value: 'Notify my delegate', + }); + + expect(component.orgUserSettings.notification_settings.notify_delegatee).toBeTrue(); + expect(component.orgUserSettings.notification_settings.notify_user).toBeFalse(); + }); + + it('should set settings to notify delegatee and user', () => { + component.orgUserSettings = orgUserSettingsData; + + component.updateDelegateeNotifyPreference({ + value: 'Notify me and my delegate', + }); + + expect(component.orgUserSettings.notification_settings.notify_delegatee).toBeTrue(); + expect(component.orgUserSettings.notification_settings.notify_user).toBeTrue(); + }); + + it('should set settings to notify user', () => { + component.orgUserSettings = orgUserSettingsData; + + component.updateDelegateeNotifyPreference({ + value: 'Notify me only', + }); + + expect(component.orgUserSettings.notification_settings.notify_delegatee).toBeFalse(); + expect(component.orgUserSettings.notification_settings.notify_user).toBeTrue(); + }); + }); + + it('removeDisabledFeatures(): should remove disabled features', () => { + spyOn(component, 'updateAdvanceRequestFeatures'); + component.notificationEvents = cloneDeep(notificationEventsData); + + component.removeDisabledFeatures(); + + expect(component.updateAdvanceRequestFeatures).toHaveBeenCalledTimes(1); + }); + + it('updateNotificationEvents(): should update notification events', () => { + spyOn(component, 'removeAdminUnsbscribedEvents'); + spyOn(component, 'removeDisabledFeatures'); + + component.updateNotificationEvents(); + + expect(component.removeAdminUnsbscribedEvents).toHaveBeenCalledTimes(1); + expect(component.removeDisabledFeatures).toHaveBeenCalledTimes(1); + }); + + describe('toggleAllSelected():', () => { + beforeEach(() => { + component.setEvents(notificationEventsData, orgUserSettingsData); + }); + + it('should set value if email event exists', fakeAsync(() => { + component.isAllSelected = { + emailEvents: true, + }; + + component.toggleAllSelected('email'); + tick(500); + + expect(component.emailEvents.getRawValue().length).not.toBe(0); + })); + + it('should set value if email event does not exist', fakeAsync(() => { + component.isAllSelected = {}; + + component.toggleAllSelected('email'); + tick(500); + + expect(component.emailEvents.getRawValue().length).not.toBe(0); + })); + + it('should set value if push event exists', fakeAsync(() => { + component.isAllSelected = { + pushEvents: true, + }; + + component.toggleAllSelected('push'); + tick(500); + + expect(component.emailEvents.getRawValue().length).not.toBe(0); + })); + + it('should set value if push event does not exist', fakeAsync(() => { + component.isAllSelected = {}; + + component.toggleAllSelected('push'); + tick(500); + + expect(component.emailEvents.getRawValue().length).not.toBe(0); + })); + }); + + it('ngOnInit(): should initialize the form and observables', (done) => { + orgUserSettingsService.get.and.returnValue(of(orgUserSettingsData)); + spyOn(component, 'getDelegateeSubscription').and.returnValue(of('Notify my delegate')); + authService.getEou.and.resolveTo(apiEouRes); + orgSettingsService.get.and.returnValue(of(orgSettingsData)); + orgUserSettingsService.getNotificationEvents.and.returnValue(of(notificationEventsData)); + + spyOn(component, 'isAllEventsSubscribed'); + spyOn(component, 'updateNotificationEvents'); + spyOn(component, 'setEvents'); + spyOn(component, 'toggleEvents'); + + component.ngOnInit(); + + component.orgUserSettings$.subscribe((res) => { + expect(res).toEqual(orgUserSettingsData); + expect(orgUserSettingsService.get).toHaveBeenCalledTimes(1); + }); + + component.isDelegateePresent$.subscribe((res) => { + expect(res).toBeFalse(); + expect(authService.getEou).toHaveBeenCalledTimes(1); + }); + + component.orgSettings$.subscribe((res) => { + expect(res).toEqual(orgSettingsData); + expect(orgSettingsService.get).toHaveBeenCalledTimes(1); + }); + + component.notificationEvents$.subscribe((res) => { + expect(res).toEqual(notificationEventsData); + expect(orgUserSettingsService.getNotificationEvents).toHaveBeenCalledTimes(1); + }); + + expect(component.isAllEventsSubscribed).toHaveBeenCalledTimes(1); + expect(component.updateNotificationEvents).toHaveBeenCalledTimes(1); + expect(component.toggleEvents).toHaveBeenCalledTimes(1); + expect(component.setEvents).toHaveBeenCalledTimes(1); + done(); + }); + + it('toggleEvents(): should toggle events on value change', fakeAsync(() => { + component.isAllSelected = { + emailEvents: true, + pushEvents: false, + }; + + component.toggleEvents(); + tick(500); + + component.setEvents(notificationEventsData, orgUserSettingsData); + tick(500); + + expect(component.isAllSelected).toEqual({ + emailEvents: false, + pushEvents: false, + }); + })); }); diff --git a/src/app/fyle/notifications/notifications.page.ts b/src/app/fyle/notifications/notifications.page.ts index fb6d04a41e..9ca4c64c3a 100644 --- a/src/app/fyle/notifications/notifications.page.ts +++ b/src/app/fyle/notifications/notifications.page.ts @@ -1,14 +1,19 @@ import { Component, OnInit } from '@angular/core'; -import { Observable, from, forkJoin, merge, zip, noop } from 'rxjs'; -import { AuthService } from 'src/app/core/services/auth.service'; -import { map, tap, switchMap, mergeMap, finalize } from 'rxjs/operators'; -import { OrgUserSettingsService } from 'src/app/core/services/org-user-settings.service'; -import { OrgUserSettings } from 'src/app/core/models/org_user_settings.model'; -import { FormGroup, FormControl, FormBuilder, FormArray } from '@angular/forms'; -import { LoaderService } from 'src/app/core/services/loader.service'; +import { FormArray, FormBuilder, FormControl, FormGroup } from '@angular/forms'; import { Router } from '@angular/router'; import { NavController } from '@ionic/angular'; +import { Observable, from, noop, zip } from 'rxjs'; +import { finalize, map } from 'rxjs/operators'; +import { + EmailEvents, + NotificationEventFeatures, + NotificationEvents, +} from 'src/app/core/models/notification-events.model'; +import { OrgSettings } from 'src/app/core/models/org-settings.model'; +import { OrgUserSettings } from 'src/app/core/models/org_user_settings.model'; +import { AuthService } from 'src/app/core/services/auth.service'; import { OrgSettingsService } from 'src/app/core/services/org-settings.service'; +import { OrgUserSettingsService } from 'src/app/core/services/org-user-settings.service'; @Component({ selector: 'app-notifications', @@ -20,23 +25,21 @@ export class NotificationsPage implements OnInit { orgUserSettings$: Observable; - notificationEvents$: Observable; - - orgSettings$: Observable; + notificationEvents$: Observable; - features$: Observable; + orgSettings$: Observable; - delegationOptions; + delegationOptions: string[]; - notificationEvents; + notificationEvents: NotificationEvents; - orgUserSettings; + orgUserSettings: OrgUserSettings; - orgSettings; + orgSettings: OrgSettings; isAllSelected: { - emailEvents: boolean; - pushEvents: boolean; + emailEvents?: boolean; + pushEvents?: boolean; }; notifEvents = []; @@ -62,7 +65,7 @@ export class NotificationsPage implements OnInit { return this.notificationForm.controls.emailEvents as FormArray; } - getDelegateeSubscription() { + getDelegateeSubscription(): Observable { return this.orgUserSettings$.pipe( map((ouSetting) => { if ( @@ -85,15 +88,7 @@ export class NotificationsPage implements OnInit { ); } - trackByFeatureKey(index, item) { - return item.key; - } - - trackByEventType(index, item) { - return item.eventType; - } - - setEvents(notificationEvents, orgUserSettings) { + setEvents(notificationEvents: NotificationEvents, orgUserSettings: OrgUserSettings): void { const unSubscribedPushNotifications = orgUserSettings.notification_settings.push.unsubscribed_events; const unSubscribedEmailNotifications = orgUserSettings.notification_settings.email.unsubscribed_events; @@ -121,23 +116,25 @@ export class NotificationsPage implements OnInit { }); } - goBack() { + goBack(): void { this.router.navigate(['/', 'enterprise', 'my_profile']); } - saveNotificationSettings() { + saveNotificationSettings(): void { this.saveNotifLoading = true; - const unsubscribedPushEvents = []; - const unsubscribedEmailEvents = []; + const unsubscribedPushEvents: string[] = []; + const unsubscribedEmailEvents: string[] = []; - this.notificationEvents.events.forEach((event, index) => { - event.email.selected = this.emailEvents.value[index]; - event.push.selected = this.pushEvents.value[index]; + this.notificationEvents.events.forEach((event: EmailEvents, index: number) => { + const emailEvent = this.emailEvents.value as { value: { email: { selected: boolean } } }; + const pushEvent = this.pushEvents.value as { value: { push: { selected: boolean } } }; + event.email.selected = emailEvent[index] as boolean; + event.push.selected = pushEvent[index] as boolean; - if (this.emailEvents.value[index] === false) { + if (emailEvent[index] === false) { unsubscribedEmailEvents.push(event.eventType.toUpperCase()); } - if (this.pushEvents.value[index] === false) { + if (pushEvent[index] === false) { unsubscribedPushEvents.push(event.eventType.toUpperCase()); } }); @@ -153,31 +150,35 @@ export class NotificationsPage implements OnInit { .pipe(() => this.orgUserSettingsService.clearOrgUserSettings()) .pipe(finalize(() => (this.saveNotifLoading = false))) .subscribe(() => { - this.navController.back(); + this.navBack(); }); } - isAllEventsSubscribed() { + navBack(): void { + this.navController.back(); + } + + isAllEventsSubscribed(): void { this.isAllSelected.emailEvents = this.orgUserSettings.notification_settings.email.unsubscribed_events.length === 0; this.isAllSelected.pushEvents = this.orgUserSettings.notification_settings.push.unsubscribed_events.length === 0; } - removeAdminUnsbscribedEvents() { - this.orgSettings$.pipe( - map((setting) => { - if (setting.admin_email_settings.unsubscribed_events.length) { - this.notificationEvents.events = this.notificationEvents.events.filter( - (notificationEvent) => - this.orgSettings.admin_email_settings.unsubscribed_events.indexOf( - notificationEvent.eventType.toUpperCase() - ) === -1 - ); - } - }) - ); + removeAdminUnsbscribedEvents(): void { + this.orgSettings$ + .pipe( + map((setting) => { + if (setting.admin_email_settings.unsubscribed_events.length) { + this.notificationEvents.events = this.notificationEvents.events.filter((notificationEvent) => { + const emailEvents = this.orgSettings.admin_email_settings.unsubscribed_events as string[]; + return emailEvents.indexOf(notificationEvent.eventType.toUpperCase()) === -1; + }); + } + }) + ) + .subscribe(noop); } - updateAdvanceRequestFeatures() { + updateAdvanceRequestFeatures(): void { this.orgSettings$ .pipe( map((setting) => { @@ -192,7 +193,7 @@ export class NotificationsPage implements OnInit { .subscribe(noop); } - updateDelegateeNotifyPreference(event) { + updateDelegateeNotifyPreference(event: { value: string }): void { if (event) { if (event.value === 'Notify my delegate') { this.orgUserSettings.notification_settings.notify_delegatee = true; @@ -207,54 +208,60 @@ export class NotificationsPage implements OnInit { } } - removeDisabledFeatures() { + removeDisabledFeatures(): void { this.updateAdvanceRequestFeatures(); const activeFeatures = this.notificationEvents.events.reduce((accumulator, notificationEvent) => { if (accumulator.indexOf(notificationEvent.feature) === -1) { accumulator.push(notificationEvent.feature); } - return accumulator; + return accumulator as string[]; }, []); - const newFeatures = {}; - activeFeatures.forEach((featureKey) => { - newFeatures[featureKey] = this.notificationEvents.features[featureKey]; + const newFeatures: NotificationEventFeatures = { + advances: { + selected: false, + textLabel: '', + }, + expensesAndReports: { + selected: false, + textLabel: '', + }, + }; + activeFeatures.forEach((featureKey: string) => { + newFeatures[featureKey] = this.notificationEvents.features[featureKey] as { + selected: boolean; + textLabel: string; + }; }); this.notificationEvents.features = newFeatures; } - updateNotificationEvents() { + updateNotificationEvents(): void { this.removeAdminUnsbscribedEvents(); this.removeDisabledFeatures(); } - toggleAllSelected(eventType) { + toggleAllSelected(eventType: string): void { if (eventType === 'email') { + const emailEventsValue = this.notificationForm.controls.emailEvents.value as string[]; if (this.isAllSelected.emailEvents) { - this.notificationForm.controls.emailEvents.setValue( - this.notificationForm.controls.emailEvents.value.map(() => false) - ); + this.emailEvents.setValue(emailEventsValue.map(() => false)); } else { - this.notificationForm.controls.emailEvents.setValue( - this.notificationForm.controls.emailEvents.value.map(() => true) - ); + this.emailEvents.setValue(emailEventsValue.map(() => true)); } } if (eventType === 'push') { + const pushEventsValue = this.notificationForm.controls.pushEvents.value as string[]; if (this.isAllSelected.pushEvents) { - this.notificationForm.controls.pushEvents.setValue( - this.notificationForm.controls.pushEvents.value.map(() => false) - ); + this.pushEvents.setValue(pushEventsValue.map(() => false)); } else { - this.notificationForm.controls.pushEvents.setValue( - this.notificationForm.controls.pushEvents.value.map(() => true) - ); + this.pushEvents.setValue(pushEventsValue.map(() => true)); } } } - ngOnInit() { + ngOnInit(): void { this.delegationOptions = ['Notify me and my delegate', 'Notify my delegate', 'Notify me only']; this.isAllSelected = { @@ -281,7 +288,7 @@ export class NotificationsPage implements OnInit { this.orgSettings$ = this.orgSettingsService.get(); this.notificationEvents$ = this.orgUserSettingsService.getNotificationEvents(); - const mergedData$ = zip(this.notificationEvents$, this.orgUserSettings$, this.orgSettings$) + zip(this.notificationEvents$, this.orgUserSettings$, this.orgSettings$) .pipe( finalize(() => { this.isAllEventsSubscribed(); @@ -298,9 +305,16 @@ export class NotificationsPage implements OnInit { * on valueChange of any check box, checking for all box selected or not * if selected will toggle all select box */ - this.notificationForm.valueChanges.subscribe((change) => { - this.isAllSelected.emailEvents = change.emailEvents.every((selected) => selected === true); - this.isAllSelected.pushEvents = change.pushEvents.every((selected) => selected === true); + + this.toggleEvents(); + } + + toggleEvents(): void { + this.notificationForm.valueChanges.subscribe((change: { emailEvents: boolean[]; pushEvents: boolean[] }) => { + const emailEvents = change.emailEvents; + const pushEvents = change.pushEvents; + this.isAllSelected.emailEvents = emailEvents.every((selected) => selected === true); + this.isAllSelected.pushEvents = pushEvents.every((selected) => selected === true); }); } }