From a3d8fadd86e208e721252e64ec89c45745d59be4 Mon Sep 17 00:00:00 2001 From: Rushikesh Patade <72869428+Rushour0@users.noreply.github.com> Date: Wed, 23 Oct 2024 10:55:12 +0530 Subject: [PATCH] fix: remove orgUserSettingsService.getAllowedCostCenters - use cost centers service (#3237) * fix: remove orgUserSettingsService.getAllowedCostCenters - use cost centers service * fix tests * fix tests * fix split expense --- .../add-edit-expense-1.spec.ts | 9 ++++++--- .../add-edit-expense/add-edit-expense.page.ts | 11 +++++------ .../add-edit-expense.setup.spec.ts | 12 +++++++----- .../add-edit-mileage-4.spec.ts | 11 +++++++---- .../add-edit-mileage-5.spec.ts | 2 +- .../add-edit-mileage.page.setup.spec.ts | 6 ++++++ .../add-edit-mileage/add-edit-mileage.page.ts | 18 +++++++----------- .../add-edit-per-diem-2.page.spec.ts | 11 +++++++---- .../add-edit-per-diem.page.setup.spec.ts | 6 ++++++ .../add-edit-per-diem.page.ts | 11 +++++------ .../split-expense/split-expense.page.spec.ts | 13 ++++++++----- .../fyle/split-expense/split-expense.page.ts | 12 +++++------- 12 files changed, 70 insertions(+), 52 deletions(-) diff --git a/src/app/fyle/add-edit-expense/add-edit-expense-1.spec.ts b/src/app/fyle/add-edit-expense/add-edit-expense-1.spec.ts index 8a44cfac55..effaa79263 100644 --- a/src/app/fyle/add-edit-expense/add-edit-expense-1.spec.ts +++ b/src/app/fyle/add-edit-expense/add-edit-expense-1.spec.ts @@ -77,6 +77,7 @@ import { expenseFieldResponse } from 'src/app/core/mock-data/expense-field.data' import { expectedProjects4 } from 'src/app/core/mock-data/extended-projects.data'; import { reportData1 } from 'src/app/core/mock-data/report.data'; import { sortedCategory } from 'src/app/core/mock-data/org-category.data'; +import { CostCentersService } from 'src/app/core/services/cost-centers.service'; export function TestCases1(getTestBed) { return describe('AddEditExpensePage-1', () => { @@ -122,6 +123,7 @@ export function TestCases1(getTestBed) { let titleCasePipe: jasmine.SpyObj; let paymentModesService: jasmine.SpyObj; let taxGroupService: jasmine.SpyObj; + let costCentersService: jasmine.SpyObj; let orgUserSettingsService: jasmine.SpyObj; let storageService: jasmine.SpyObj; let launchDarklyService: jasmine.SpyObj; @@ -178,6 +180,7 @@ export function TestCases1(getTestBed) { titleCasePipe = TestBed.inject(TitleCasePipe) as jasmine.SpyObj; paymentModesService = TestBed.inject(PaymentModesService) as jasmine.SpyObj; taxGroupService = TestBed.inject(TaxGroupService) as jasmine.SpyObj; + costCentersService = TestBed.inject(CostCentersService) as jasmine.SpyObj; orgUserSettingsService = TestBed.inject(OrgUserSettingsService) as jasmine.SpyObj; storageService = TestBed.inject(StorageService) as jasmine.SpyObj; launchDarklyService = TestBed.inject(LaunchDarklyService) as jasmine.SpyObj; @@ -1500,7 +1503,7 @@ export function TestCases1(getTestBed) { it('should return list of cost centers if enabled', (done) => { component.orgUserSettings$ = of(orgUserSettingsData); orgSettingsService.get.and.returnValue(of(orgSettingsRes)); - orgUserSettingsService.getAllowedCostCenters.and.returnValue(of(costCenterApiRes1)); + costCentersService.getAllActive.and.returnValue(of(costCenterApiRes1)); fixture.detectChanges(); component.setupCostCenters(); @@ -1514,7 +1517,7 @@ export function TestCases1(getTestBed) { }); expect(orgSettingsService.get).toHaveBeenCalledTimes(1); - expect(orgUserSettingsService.getAllowedCostCenters).toHaveBeenCalledOnceWith(orgUserSettingsData); + expect(costCentersService.getAllActive).toHaveBeenCalledTimes(1); done(); }); @@ -1523,7 +1526,7 @@ export function TestCases1(getTestBed) { orgSettingsService.get.and.returnValue( of({ ...orgSettingsRes, cost_centers: { ...orgSettingsRes.cost_centers, enabled: false } }) ); - orgUserSettingsService.getAllowedCostCenters.and.returnValue(of(costCenterApiRes1)); + costCentersService.getAllActive.and.returnValue(of(costCenterApiRes1)); fixture.detectChanges(); component.setupCostCenters(); diff --git a/src/app/fyle/add-edit-expense/add-edit-expense.page.ts b/src/app/fyle/add-edit-expense/add-edit-expense.page.ts index b278216a1c..2163c65240 100644 --- a/src/app/fyle/add-edit-expense/add-edit-expense.page.ts +++ b/src/app/fyle/add-edit-expense/add-edit-expense.page.ts @@ -138,6 +138,7 @@ import { corporateCardTransaction } from 'src/app/core/models/platform/v1/cc-tra import { PlatformFileGenerateUrlsResponse } from 'src/app/core/models/platform/platform-file-generate-urls-response.model'; import { SpenderFileService } from 'src/app/core/services/platform/v1/spender/file.service'; import { ExpenseTransactionStatus } from 'src/app/core/enums/platform/v1/expense-transaction-status.enum'; +import { CostCentersService } from 'src/app/core/services/cost-centers.service'; // eslint-disable-next-line type FormValue = { @@ -484,6 +485,7 @@ export class AddEditExpensePage implements OnInit { private titleCasePipe: TitleCasePipe, private paymentModesService: PaymentModesService, private taxGroupService: TaxGroupService, + private costCentersService: CostCentersService, private orgUserSettingsService: OrgUserSettingsService, private storageService: StorageService, private launchDarklyService: LaunchDarklyService, @@ -1140,13 +1142,10 @@ export class AddEditExpensePage implements OnInit { this.isCostCentersEnabled$ = orgSettings$.pipe(map((orgSettings) => orgSettings.cost_centers.enabled)); - this.costCenters$ = forkJoin({ - orgSettings: orgSettings$, - orgUserSettings: this.orgUserSettings$, - }).pipe( - switchMap(({ orgSettings, orgUserSettings }) => { + this.costCenters$ = orgSettings$.pipe( + switchMap((orgSettings) => { if (orgSettings.cost_centers.enabled) { - return this.orgUserSettingsService.getAllowedCostCenters(orgUserSettings); + return this.costCentersService.getAllActive(); } else { return of([]); } diff --git a/src/app/fyle/add-edit-expense/add-edit-expense.setup.spec.ts b/src/app/fyle/add-edit-expense/add-edit-expense.setup.spec.ts index f346dbae3a..53e9e3ab34 100644 --- a/src/app/fyle/add-edit-expense/add-edit-expense.setup.spec.ts +++ b/src/app/fyle/add-edit-expense/add-edit-expense.setup.spec.ts @@ -63,6 +63,7 @@ import { SpenderFileService } from 'src/app/core/services/platform/v1/spender/fi import { AdvanceWalletsService } from 'src/app/core/services/platform/v1/spender/advance-wallets.service'; import { PAGINATION_SIZE } from 'src/app/constants'; import { SpenderService } from 'src/app/core/services/platform/v1/spender/spender.service'; +import { CostCentersService } from 'src/app/core/services/cost-centers.service'; export function setFormValid(component) { Object.defineProperty(component.fg, 'valid', { @@ -208,11 +209,8 @@ describe('AddEditExpensePage', () => { 'checkIfPaymentModeConfigurationsIsEnabled', ]); const taxGroupServiceSpy = jasmine.createSpyObj('TaxGroupService', ['get']); - const orgUserSettingsServiceSpy = jasmine.createSpyObj('OrgUserSettingsService', [ - 'getAllowedCostCenters', - 'getAllowedPaymentModes', - 'get', - ]); + const costCentersServiceSpy = jasmine.createSpyObj('CostCentersService', ['getAllActive']); + const orgUserSettingsServiceSpy = jasmine.createSpyObj('OrgUserSettingsService', ['getAllowedPaymentModes', 'get']); const storageServiceSpy = jasmine.createSpyObj('StorageService', ['set', 'get']); const launchDarklyServiceSpy = jasmine.createSpyObj('LaunchDarklyService', ['getVariation']); const platformSpy = jasmine.createSpyObj('Platform', ['is']); @@ -402,6 +400,10 @@ describe('AddEditExpensePage', () => { provide: TaxGroupService, useValue: taxGroupServiceSpy, }, + { + provide: CostCentersService, + useValue: costCentersServiceSpy, + }, { provide: OrgUserSettingsService, useValue: orgUserSettingsServiceSpy, diff --git a/src/app/fyle/add-edit-mileage/add-edit-mileage-4.spec.ts b/src/app/fyle/add-edit-mileage/add-edit-mileage-4.spec.ts index f3019831a3..f8b6acff1c 100644 --- a/src/app/fyle/add-edit-mileage/add-edit-mileage-4.spec.ts +++ b/src/app/fyle/add-edit-mileage/add-edit-mileage-4.spec.ts @@ -95,6 +95,7 @@ import { AddEditMileagePage } from './add-edit-mileage.page'; import { commuteDetailsData } from 'src/app/core/mock-data/commute-details.data'; import { CommuteDeduction } from 'src/app/core/enums/commute-deduction.enum'; import { cloneDeep } from 'lodash'; +import { CostCentersService } from 'src/app/core/services/cost-centers.service'; export function TestCases4(getTestBed) { return describe('AddEditMileage-4', () => { @@ -140,6 +141,7 @@ export function TestCases4(getTestBed) { let titleCasePipe: jasmine.SpyObj; let paymentModesService: jasmine.SpyObj; let taxGroupService: jasmine.SpyObj; + let costCentersService: jasmine.SpyObj; let orgUserSettingsService: jasmine.SpyObj; let storageService: jasmine.SpyObj; let launchDarklyService: jasmine.SpyObj; @@ -198,6 +200,7 @@ export function TestCases4(getTestBed) { titleCasePipe = TestBed.inject(TitleCasePipe) as jasmine.SpyObj; paymentModesService = TestBed.inject(PaymentModesService) as jasmine.SpyObj; taxGroupService = TestBed.inject(TaxGroupService) as jasmine.SpyObj; + costCentersService = TestBed.inject(CostCentersService) as jasmine.SpyObj; orgUserSettingsService = TestBed.inject(OrgUserSettingsService) as jasmine.SpyObj; storageService = TestBed.inject(StorageService) as jasmine.SpyObj; launchDarklyService = TestBed.inject(LaunchDarklyService) as jasmine.SpyObj; @@ -611,16 +614,16 @@ export function TestCases4(getTestBed) { describe('getCostCenters():', () => { it('should get cost center if enabled', (done) => { - orgUserSettingsService.getAllowedCostCenters.and.returnValue(of(costCentersData)); - component.getCostCenters(of(orgSettingsData), of(orgUserSettingsData)).subscribe((res) => { + costCentersService.getAllActive.and.returnValue(of(costCentersData)); + component.getCostCenters(of(orgSettingsData)).subscribe((res) => { expect(res).toEqual(costCenterOptions2); - expect(orgUserSettingsService.getAllowedCostCenters).toHaveBeenCalledOnceWith(orgUserSettingsData); + expect(costCentersService.getAllActive).toHaveBeenCalledTimes(1); done(); }); }); it('should return empty array if cost centers are disabled', (done) => { - component.getCostCenters(of(orgSettingsCCDisabled), of(orgUserSettingsData)).subscribe((res) => { + component.getCostCenters(of(orgSettingsCCDisabled)).subscribe((res) => { expect(res).toEqual([]); done(); }); diff --git a/src/app/fyle/add-edit-mileage/add-edit-mileage-5.spec.ts b/src/app/fyle/add-edit-mileage/add-edit-mileage-5.spec.ts index ac741c06a3..3cffe1bf9b 100644 --- a/src/app/fyle/add-edit-mileage/add-edit-mileage-5.spec.ts +++ b/src/app/fyle/add-edit-mileage/add-edit-mileage-5.spec.ts @@ -415,7 +415,7 @@ export function TestCases5(getTestBed) { expect(res).toBeTrue(); }); - expect(component.getCostCenters).toHaveBeenCalledOnceWith(jasmine.any(Observable), jasmine.any(Observable)); + expect(component.getCostCenters).toHaveBeenCalledOnceWith(jasmine.any(Observable)); component.reports$.subscribe((res) => { expect(res).toEqual(reportOptionsData3); diff --git a/src/app/fyle/add-edit-mileage/add-edit-mileage.page.setup.spec.ts b/src/app/fyle/add-edit-mileage/add-edit-mileage.page.setup.spec.ts index 6ec4fa1ec4..176123e784 100644 --- a/src/app/fyle/add-edit-mileage/add-edit-mileage.page.setup.spec.ts +++ b/src/app/fyle/add-edit-mileage/add-edit-mileage.page.setup.spec.ts @@ -66,6 +66,7 @@ import { EmployeesService } from 'src/app/core/services/platform/v1/spender/empl import { AdvanceWalletsService } from 'src/app/core/services/platform/v1/spender/advance-wallets.service'; import { PAGINATION_SIZE } from 'src/app/constants'; import { SpenderService } from 'src/app/core/services/platform/v1/spender/spender.service'; +import { CostCentersService } from 'src/app/core/services/cost-centers.service'; export function setFormValid(component) { Object.defineProperty(component.fg, 'valid', { @@ -210,6 +211,7 @@ describe('AddEditMileagePage', () => { 'checkIfPaymentModeConfigurationsIsEnabled', ]); const taxGroupServiceSpy = jasmine.createSpyObj('TaxGroupService', ['get']); + const costCentersServiceSpy = jasmine.createSpyObj('CostCentersService', ['getAllActive']); const orgUserSettingsServiceSpy = jasmine.createSpyObj('OrgUserSettingsService', [ 'getAllowedCostCenters', 'getAllowedPaymentModes', @@ -418,6 +420,10 @@ describe('AddEditMileagePage', () => { provide: TaxGroupService, useValue: taxGroupServiceSpy, }, + { + provide: CostCentersService, + useValue: costCentersServiceSpy, + }, { provide: OrgUserSettingsService, useValue: orgUserSettingsServiceSpy, diff --git a/src/app/fyle/add-edit-mileage/add-edit-mileage.page.ts b/src/app/fyle/add-edit-mileage/add-edit-mileage.page.ts index e3d796d55d..e4fae0cda2 100644 --- a/src/app/fyle/add-edit-mileage/add-edit-mileage.page.ts +++ b/src/app/fyle/add-edit-mileage/add-edit-mileage.page.ts @@ -1,3 +1,4 @@ +import { CostCentersService } from 'src/app/core/services/cost-centers.service'; // TODO: Very hard to fix this file without making massive changes /* eslint-disable complexity */ import { ChangeDetectorRef, Component, ElementRef, EventEmitter, HostListener, OnInit, ViewChild } from '@angular/core'; @@ -324,6 +325,7 @@ export class AddEditMileagePage implements OnInit { private currencyService: CurrencyService, private mileageRateService: MileageRatesService, private orgUserSettingsService: OrgUserSettingsService, + private costCentersService: CostCentersService, private categoriesService: CategoriesService, private orgSettingsService: OrgSettingsService, private platformHandlerService: PlatformHandlerService, @@ -1227,17 +1229,11 @@ export class AddEditMileagePage implements OnInit { ); } - getCostCenters( - orgSettings$: Observable, - orgUserSettings$: Observable - ): Observable { - return forkJoin({ - orgSettings: orgSettings$, - orgUserSettings: orgUserSettings$, - }).pipe( - switchMap(({ orgSettings, orgUserSettings }) => { + getCostCenters(orgSettings$: Observable): Observable { + return orgSettings$.pipe( + switchMap((orgSettings) => { if (orgSettings.cost_centers.enabled) { - return this.orgUserSettingsService.getAllowedCostCenters(orgUserSettings); + return this.costCentersService.getAllActive(); } else { return of([]); } @@ -1673,7 +1669,7 @@ export class AddEditMileagePage implements OnInit { this.paymentModes$ = this.getPaymentModes(); - this.costCenters$ = this.getCostCenters(orgSettings$, orgUserSettings$); + this.costCenters$ = this.getCostCenters(orgSettings$); this.recentlyUsedCostCenters$ = forkJoin({ costCenters: this.costCenters$, diff --git a/src/app/fyle/add-edit-per-diem/add-edit-per-diem-2.page.spec.ts b/src/app/fyle/add-edit-per-diem/add-edit-per-diem-2.page.spec.ts index ae0b20013e..e08be02561 100644 --- a/src/app/fyle/add-edit-per-diem/add-edit-per-diem-2.page.spec.ts +++ b/src/app/fyle/add-edit-per-diem/add-edit-per-diem-2.page.spec.ts @@ -96,6 +96,7 @@ import { } from 'src/app/core/mock-data/per-diem-form-value.data'; import { platformExpenseData } from 'src/app/core/mock-data/platform/v1/expense.data'; import { transformedExpenseData } from 'src/app/core/mock-data/transformed-expense.data'; +import { CostCentersService } from 'src/app/core/services/cost-centers.service'; export function TestCases2(getTestBed) { return describe('add-edit-per-diem test cases set 2', () => { @@ -134,6 +135,7 @@ export function TestCases2(getTestBed) { let snackbarProperties: jasmine.SpyObj; let platform: Platform; let paymentModesService: jasmine.SpyObj; + let costCentersService: jasmine.SpyObj; let orgUserSettingsService: jasmine.SpyObj; let storageService: jasmine.SpyObj; let perDiemService: jasmine.SpyObj; @@ -177,6 +179,7 @@ export function TestCases2(getTestBed) { snackbarProperties = TestBed.inject(SnackbarPropertiesService) as jasmine.SpyObj; platform = TestBed.inject(Platform); paymentModesService = TestBed.inject(PaymentModesService) as jasmine.SpyObj; + costCentersService = TestBed.inject(CostCentersService) as jasmine.SpyObj; orgUserSettingsService = TestBed.inject(OrgUserSettingsService) as jasmine.SpyObj; storageService = TestBed.inject(StorageService) as jasmine.SpyObj; perDiemService = TestBed.inject(PerDiemService) as jasmine.SpyObj; @@ -534,7 +537,7 @@ export function TestCases2(getTestBed) { })); perDiemService.getAllowedPerDiems.and.returnValue(of(mockPerDiem)); component.isConnected$ = of(true); - orgUserSettingsService.getAllowedCostCenters.and.returnValue(of(costCentersData)); + costCentersService.getAllActive.and.returnValue(of(costCentersData)); reportService.getAutoSubmissionReportName.and.returnValue(of('#1: Aug 2023')); accountsService.getAccountTypeFromPaymentMode.and.returnValue(AccountType.CCC); recentlyUsedItemsService.getRecentlyUsed.and.returnValue(of(recentlyUsedRes)); @@ -829,7 +832,7 @@ export function TestCases2(getTestBed) { it('should update the costCenters$, recentlyUsedCostCenters$ and reports$ correctly', (done) => { component.ionViewWillEnter(); component.costCenters$.subscribe((res) => { - expect(orgUserSettingsService.getAllowedCostCenters).toHaveBeenCalledOnceWith(orgUserSettingsData); + expect(costCentersService.getAllActive).toHaveBeenCalledTimes(1); expect(res).toEqual(expectedCCdata3); }); @@ -858,7 +861,7 @@ export function TestCases2(getTestBed) { orgSettingsService.get.and.returnValue(of(mockOrgSettings)); component.ionViewWillEnter(); component.costCenters$.subscribe((res) => { - expect(orgUserSettingsService.getAllowedCostCenters).not.toHaveBeenCalled(); + expect(costCentersService.getAllActive).not.toHaveBeenCalled(); expect(res).toEqual([]); }); done(); @@ -1001,7 +1004,7 @@ export function TestCases2(getTestBed) { it('should set costCenter if costCenter length is 1 and mode is add', fakeAsync(() => { component.getNewExpense = jasmine.createSpy().and.returnValue(of(unflattenedExpWoCostCenter)); - orgUserSettingsService.getAllowedCostCenters.and.returnValue(of([costCentersData[0]])); + costCentersService.getAllActive.and.returnValue(of([costCentersData[0]])); activatedRoute.snapshot.params.id = undefined; component.ionViewWillEnter(); tick(1000); diff --git a/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.setup.spec.ts b/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.setup.spec.ts index 531d70bc4f..185bebd754 100644 --- a/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.setup.spec.ts +++ b/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.setup.spec.ts @@ -44,6 +44,7 @@ import { TestCases2 } from './add-edit-per-diem-2.page.spec'; import { TestCases3 } from './add-edit-per-diem-3.page.spec'; import { TestCases4 } from './add-edit-per-diem-4.page.spec'; import { TestCases5 } from './add-edit-per-diem-5.page.spec'; +import { CostCentersService } from 'src/app/core/services/cost-centers.service'; describe('AddEditPerDiemPage', () => { const getTestBed = () => { @@ -132,6 +133,7 @@ describe('AddEditPerDiemPage', () => { 'checkIfPaymentModeConfigurationsIsEnabled', ]); const categoriesServiceSpy = jasmine.createSpyObj('CategoriesService', ['getAll']); + const costCentersServiceSpy = jasmine.createSpyObj('CostCentersService', ['getAllActive']); const orgUserSettingsServiceSpy = jasmine.createSpyObj('OrgUserSettingsService', [ 'getAllowedPaymentModes', 'get', @@ -250,6 +252,10 @@ describe('AddEditPerDiemPage', () => { provide: CategoriesService, useValue: categoriesServiceSpy, }, + { + provide: CostCentersService, + useValue: costCentersServiceSpy, + }, { provide: OrgUserSettingsService, useValue: orgUserSettingsServiceSpy, diff --git a/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.ts b/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.ts index c2116142e0..bb4fec365a 100644 --- a/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.ts +++ b/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.ts @@ -111,6 +111,7 @@ import { PerDiemRedirectedFrom } from 'src/app/core/models/per-diem-redirected-f import { ExpensesService } from 'src/app/core/services/platform/v1/spender/expenses.service'; import { AdvanceWallet } from 'src/app/core/models/platform/v1/advance-wallet.model'; import { AdvanceWalletsService } from 'src/app/core/services/platform/v1/spender/advance-wallets.service'; +import { CostCentersService } from 'src/app/core/services/cost-centers.service'; @Component({ selector: 'app-add-edit-per-diem', @@ -287,6 +288,7 @@ export class AddEditPerDiemPage implements OnInit { private paymentModesService: PaymentModesService, private perDiemService: PerDiemService, private categoriesService: CategoriesService, + private costCentersService: CostCentersService, private orgUserSettingsService: OrgUserSettingsService, private orgSettingsService: OrgSettingsService, private platform: Platform, @@ -1139,13 +1141,10 @@ export class AddEditPerDiemPage implements OnInit { this.paymentModes$ = this.getPaymentModes(); - this.costCenters$ = forkJoin({ - orgSettings: orgSettings$, - orgUserSettings: orgUserSettings$, - }).pipe( - switchMap(({ orgSettings, orgUserSettings }) => { + this.costCenters$ = orgSettings$.pipe( + switchMap((orgSettings) => { if (orgSettings.cost_centers.enabled) { - return this.orgUserSettingsService.getAllowedCostCenters(orgUserSettings); + return this.costCentersService.getAllActive(); } else { return of([]); } diff --git a/src/app/fyle/split-expense/split-expense.page.spec.ts b/src/app/fyle/split-expense/split-expense.page.spec.ts index 42c6b4cb1b..4c8cb66a54 100644 --- a/src/app/fyle/split-expense/split-expense.page.spec.ts +++ b/src/app/fyle/split-expense/split-expense.page.spec.ts @@ -130,6 +130,7 @@ import { SplitExpenseMissingFieldsData } from 'src/app/core/mock-data/split-expe import { splitPayloadData1 } from 'src/app/core/mock-data/split-payload.data'; import { platformExpenseWithExtractedData } from 'src/app/core/mock-data/platform/v1/expense.data'; import { orgSettingsWithProjectCategoryRestrictions } from 'src/app/core/mock-data/org-settings.data'; +import { CostCentersService } from 'src/app/core/services/cost-centers.service'; describe('SplitExpensePage', () => { let component: SplitExpensePage; @@ -149,6 +150,7 @@ describe('SplitExpensePage', () => { let policyService: jasmine.SpyObj; let modalController: jasmine.SpyObj; let modalProperties: jasmine.SpyObj; + let costCentersService: jasmine.SpyObj; let orgUserSettingsService: jasmine.SpyObj; let orgSettingsService: jasmine.SpyObj; let dependentFieldsService: jasmine.SpyObj; @@ -192,6 +194,7 @@ describe('SplitExpensePage', () => { const policyServiceSpy = jasmine.createSpyObj('PolicyService', ['checkIfViolationsExist']); const modalControllerSpy = jasmine.createSpyObj('ModalController', ['create', 'getTop']); const modalPropertiesSpy = jasmine.createSpyObj('ModalPropertiesService', ['getModalDefaultProperties']); + const costCentersServiceSpy = jasmine.createSpyObj('CostCentersService', ['getAllActive']); const orgUserSettingsServiceSpy = jasmine.createSpyObj('OrgUserSettingsService', ['getAllowedCostCenters', 'get']); const orgSettingsServiceSpy = jasmine.createSpyObj('OrgSettingsService', ['get']); const dependentFieldsServiceSpy = jasmine.createSpyObj('DependentFieldsService', [ @@ -239,6 +242,7 @@ describe('SplitExpensePage', () => { { provide: PolicyService, useValue: policyServiceSpy }, { provide: ModalController, useValue: modalControllerSpy }, { provide: ModalPropertiesService, useValue: modalPropertiesSpy }, + { provide: CostCentersService, useValue: costCentersServiceSpy }, { provide: OrgUserSettingsService, useValue: orgUserSettingsServiceSpy }, { provide: OrgSettingsService, useValue: orgSettingsServiceSpy }, { provide: DependentFieldsService, useValue: dependentFieldsServiceSpy }, @@ -290,6 +294,7 @@ describe('SplitExpensePage', () => { policyService = TestBed.inject(PolicyService) as jasmine.SpyObj; modalController = TestBed.inject(ModalController) as jasmine.SpyObj; modalProperties = TestBed.inject(ModalPropertiesService) as jasmine.SpyObj; + costCentersService = TestBed.inject(CostCentersService) as jasmine.SpyObj; orgUserSettingsService = TestBed.inject(OrgUserSettingsService) as jasmine.SpyObj; orgSettingsService = TestBed.inject(OrgSettingsService) as jasmine.SpyObj; dependentFieldsService = TestBed.inject(DependentFieldsService) as jasmine.SpyObj; @@ -868,15 +873,14 @@ describe('SplitExpensePage', () => { it('should set costCenters$ correctly if splitType is cost centers', () => { const mockCostCenters = costCentersData3.slice(0, 2); - orgUserSettingsService.getAllowedCostCenters.and.returnValue(of(mockCostCenters)); + costCentersService.getAllActive.and.returnValue(of(mockCostCenters)); activateRouteMock.snapshot.params.splitType = 'cost centers'; component.ionViewWillEnter(); component.costCenters$.subscribe((costCenters) => { expect(orgSettingsService.get).toHaveBeenCalledTimes(1); - expect(orgUserSettingsService.get).toHaveBeenCalledTimes(1); - expect(orgUserSettingsService.getAllowedCostCenters).toHaveBeenCalledOnceWith(orgUserSettingsData); + expect(costCentersService.getAllActive).toHaveBeenCalledTimes(1); expect(costCenters).toEqual(expectedCCdata); }); }); @@ -891,8 +895,7 @@ describe('SplitExpensePage', () => { component.costCenters$.subscribe((costCenters) => { expect(orgSettingsService.get).toHaveBeenCalledTimes(1); - expect(orgUserSettingsService.get).toHaveBeenCalledTimes(1); - expect(orgUserSettingsService.getAllowedCostCenters).not.toHaveBeenCalled(); + expect(costCentersService.getAllActive).not.toHaveBeenCalledTimes(1); expect(costCenters).toEqual([]); }); }); diff --git a/src/app/fyle/split-expense/split-expense.page.ts b/src/app/fyle/split-expense/split-expense.page.ts index c8f3394134..8b940917aa 100644 --- a/src/app/fyle/split-expense/split-expense.page.ts +++ b/src/app/fyle/split-expense/split-expense.page.ts @@ -1,3 +1,4 @@ +import { CostCentersService } from 'src/app/core/services/cost-centers.service'; import { Component } from '@angular/core'; import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; @@ -133,6 +134,7 @@ export class SplitExpensePage { private policyService: PolicyService, private modalController: ModalController, private modalProperties: ModalPropertiesService, + private costCentersService: CostCentersService, private orgUserSettingsService: OrgUserSettingsService, private orgSettingsService: OrgSettingsService, private dependentFieldsService: DependentFieldsService, @@ -927,14 +929,10 @@ export class SplitExpensePage { ); if (this.splitType === 'cost centers') { - const orgUserSettings$ = this.orgUserSettingsService.get(); - this.costCenters$ = forkJoin({ - orgSettings: orgSettings$, - orgUserSettings: orgUserSettings$, - }).pipe( - switchMap(({ orgSettings, orgUserSettings }) => { + this.costCenters$ = orgSettings$.pipe( + switchMap((orgSettings) => { if (orgSettings.cost_centers.enabled) { - return this.orgUserSettingsService.getAllowedCostCenters(orgUserSettings); + return this.costCentersService.getAllActive(); } else { return of([]); }