Skip to content

Commit

Permalink
fix: remove orgUserSettingsService.getAllowedCostCenters - use cost c…
Browse files Browse the repository at this point in the history
…enters service (#3237)

* fix: remove orgUserSettingsService.getAllowedCostCenters - use cost centers service

* fix tests

* fix tests

* fix split expense
  • Loading branch information
Rushour0 authored Oct 23, 2024
1 parent 081d859 commit a3d8fad
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 52 deletions.
9 changes: 6 additions & 3 deletions src/app/fyle/add-edit-expense/add-edit-expense-1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -122,6 +123,7 @@ export function TestCases1(getTestBed) {
let titleCasePipe: jasmine.SpyObj<TitleCasePipe>;
let paymentModesService: jasmine.SpyObj<PaymentModesService>;
let taxGroupService: jasmine.SpyObj<TaxGroupService>;
let costCentersService: jasmine.SpyObj<CostCentersService>;
let orgUserSettingsService: jasmine.SpyObj<OrgUserSettingsService>;
let storageService: jasmine.SpyObj<StorageService>;
let launchDarklyService: jasmine.SpyObj<LaunchDarklyService>;
Expand Down Expand Up @@ -178,6 +180,7 @@ export function TestCases1(getTestBed) {
titleCasePipe = TestBed.inject(TitleCasePipe) as jasmine.SpyObj<TitleCasePipe>;
paymentModesService = TestBed.inject(PaymentModesService) as jasmine.SpyObj<PaymentModesService>;
taxGroupService = TestBed.inject(TaxGroupService) as jasmine.SpyObj<TaxGroupService>;
costCentersService = TestBed.inject(CostCentersService) as jasmine.SpyObj<CostCentersService>;
orgUserSettingsService = TestBed.inject(OrgUserSettingsService) as jasmine.SpyObj<OrgUserSettingsService>;
storageService = TestBed.inject(StorageService) as jasmine.SpyObj<StorageService>;
launchDarklyService = TestBed.inject(LaunchDarklyService) as jasmine.SpyObj<LaunchDarklyService>;
Expand Down Expand Up @@ -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();
Expand All @@ -1514,7 +1517,7 @@ export function TestCases1(getTestBed) {
});

expect(orgSettingsService.get).toHaveBeenCalledTimes(1);
expect(orgUserSettingsService.getAllowedCostCenters).toHaveBeenCalledOnceWith(orgUserSettingsData);
expect(costCentersService.getAllActive).toHaveBeenCalledTimes(1);
done();
});

Expand All @@ -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();
Expand Down
11 changes: 5 additions & 6 deletions src/app/fyle/add-edit-expense/add-edit-expense.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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([]);
}
Expand Down
12 changes: 7 additions & 5 deletions src/app/fyle/add-edit-expense/add-edit-expense.setup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -402,6 +400,10 @@ describe('AddEditExpensePage', () => {
provide: TaxGroupService,
useValue: taxGroupServiceSpy,
},
{
provide: CostCentersService,
useValue: costCentersServiceSpy,
},
{
provide: OrgUserSettingsService,
useValue: orgUserSettingsServiceSpy,
Expand Down
11 changes: 7 additions & 4 deletions src/app/fyle/add-edit-mileage/add-edit-mileage-4.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -140,6 +141,7 @@ export function TestCases4(getTestBed) {
let titleCasePipe: jasmine.SpyObj<TitleCasePipe>;
let paymentModesService: jasmine.SpyObj<PaymentModesService>;
let taxGroupService: jasmine.SpyObj<TaxGroupService>;
let costCentersService: jasmine.SpyObj<CostCentersService>;
let orgUserSettingsService: jasmine.SpyObj<OrgUserSettingsService>;
let storageService: jasmine.SpyObj<StorageService>;
let launchDarklyService: jasmine.SpyObj<LaunchDarklyService>;
Expand Down Expand Up @@ -198,6 +200,7 @@ export function TestCases4(getTestBed) {
titleCasePipe = TestBed.inject(TitleCasePipe) as jasmine.SpyObj<TitleCasePipe>;
paymentModesService = TestBed.inject(PaymentModesService) as jasmine.SpyObj<PaymentModesService>;
taxGroupService = TestBed.inject(TaxGroupService) as jasmine.SpyObj<TaxGroupService>;
costCentersService = TestBed.inject(CostCentersService) as jasmine.SpyObj<CostCentersService>;
orgUserSettingsService = TestBed.inject(OrgUserSettingsService) as jasmine.SpyObj<OrgUserSettingsService>;
storageService = TestBed.inject(StorageService) as jasmine.SpyObj<StorageService>;
launchDarklyService = TestBed.inject(LaunchDarklyService) as jasmine.SpyObj<LaunchDarklyService>;
Expand Down Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/fyle/add-edit-mileage/add-edit-mileage-5.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -418,6 +420,10 @@ describe('AddEditMileagePage', () => {
provide: TaxGroupService,
useValue: taxGroupServiceSpy,
},
{
provide: CostCentersService,
useValue: costCentersServiceSpy,
},
{
provide: OrgUserSettingsService,
useValue: orgUserSettingsServiceSpy,
Expand Down
18 changes: 7 additions & 11 deletions src/app/fyle/add-edit-mileage/add-edit-mileage.page.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1227,17 +1229,11 @@ export class AddEditMileagePage implements OnInit {
);
}

getCostCenters(
orgSettings$: Observable<OrgSettings>,
orgUserSettings$: Observable<OrgUserSettings>
): Observable<CostCenterOptions[]> {
return forkJoin({
orgSettings: orgSettings$,
orgUserSettings: orgUserSettings$,
}).pipe(
switchMap(({ orgSettings, orgUserSettings }) => {
getCostCenters(orgSettings$: Observable<OrgSettings>): Observable<CostCenterOptions[]> {
return orgSettings$.pipe(
switchMap((orgSettings) => {
if (orgSettings.cost_centers.enabled) {
return this.orgUserSettingsService.getAllowedCostCenters(orgUserSettings);
return this.costCentersService.getAllActive();
} else {
return of([]);
}
Expand Down Expand Up @@ -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$,
Expand Down
11 changes: 7 additions & 4 deletions src/app/fyle/add-edit-per-diem/add-edit-per-diem-2.page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -134,6 +135,7 @@ export function TestCases2(getTestBed) {
let snackbarProperties: jasmine.SpyObj<SnackbarPropertiesService>;
let platform: Platform;
let paymentModesService: jasmine.SpyObj<PaymentModesService>;
let costCentersService: jasmine.SpyObj<CostCentersService>;
let orgUserSettingsService: jasmine.SpyObj<OrgUserSettingsService>;
let storageService: jasmine.SpyObj<StorageService>;
let perDiemService: jasmine.SpyObj<PerDiemService>;
Expand Down Expand Up @@ -177,6 +179,7 @@ export function TestCases2(getTestBed) {
snackbarProperties = TestBed.inject(SnackbarPropertiesService) as jasmine.SpyObj<SnackbarPropertiesService>;
platform = TestBed.inject(Platform);
paymentModesService = TestBed.inject(PaymentModesService) as jasmine.SpyObj<PaymentModesService>;
costCentersService = TestBed.inject(CostCentersService) as jasmine.SpyObj<CostCentersService>;
orgUserSettingsService = TestBed.inject(OrgUserSettingsService) as jasmine.SpyObj<OrgUserSettingsService>;
storageService = TestBed.inject(StorageService) as jasmine.SpyObj<StorageService>;
perDiemService = TestBed.inject(PerDiemService) as jasmine.SpyObj<PerDiemService>;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -250,6 +252,10 @@ describe('AddEditPerDiemPage', () => {
provide: CategoriesService,
useValue: categoriesServiceSpy,
},
{
provide: CostCentersService,
useValue: costCentersServiceSpy,
},
{
provide: OrgUserSettingsService,
useValue: orgUserSettingsServiceSpy,
Expand Down
11 changes: 5 additions & 6 deletions src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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([]);
}
Expand Down
Loading

0 comments on commit a3d8fad

Please sign in to comment.