diff --git a/src/app/core/mock-data/org-category.data.ts b/src/app/core/mock-data/org-category.data.ts index 379f7728e6..169f834606 100644 --- a/src/app/core/mock-data/org-category.data.ts +++ b/src/app/core/mock-data/org-category.data.ts @@ -1031,6 +1031,32 @@ export const perDiemCategory: OrgCategory = deepFreeze({ sub_category: 'Per Diem', updated_at: new Date('2022-09-13T17:16:56.232081+00:00'), }); +export const perDiemCategories2: OrgCategory[] = deepFreeze([ + { + code: null, + created_at: new Date('2018-07-27T08:52:38.938006+00:00'), + displayName: 'Per Diem', + enabled: true, + fyle_category: 'Per Diem', + id: 38912, + name: 'Per Diem', + org_id: 'orrb8EW1zZsy', + sub_category: 'Per Diem', + updated_at: new Date('2022-09-13T17:16:56.232081+00:00'), + }, + { + code: null, + created_at: new Date('2018-07-27T08:52:38.938006+00:00'), + displayName: 'Per Diem', + enabled: true, + fyle_category: 'Per Diem', + id: 38912, + name: 'Per Diem', + org_id: 'orrb8EW1zZsy', + sub_category: 'Per Diem', + updated_at: new Date('2022-09-13T17:16:56.232081+00:00'), + }, +]); export const mileageCategories: OrgCategory[] = deepFreeze([ { diff --git a/src/app/fyle/add-edit-mileage/add-edit-mileage-1.spec.ts b/src/app/fyle/add-edit-mileage/add-edit-mileage-1.spec.ts index e5104ac79e..22ae615f6e 100644 --- a/src/app/fyle/add-edit-mileage/add-edit-mileage-1.spec.ts +++ b/src/app/fyle/add-edit-mileage/add-edit-mileage-1.spec.ts @@ -395,33 +395,55 @@ export function TestCases1(getTestBed) { describe('getProjectCategoryIds():', () => { it('should return MILEAGE category IDs', (done) => { + component.projectCategories$ = of(mileageCategories); categoriesService.getAll.and.returnValue(of(mileageCategories)); component.getProjectCategoryIds().subscribe((res) => { - expect(res).toEqual(['141295', '141300']); - expect(categoriesService.getAll).toHaveBeenCalledTimes(1); + expect(res).toEqual(['141295', '141300', '226659']); done(); }); }); it('should return an empty array if there are no MILEAGE categories', (done) => { + component.projectCategories$ = of([]); categoriesService.getAll.and.returnValue(of(transformedOrgCategoryById)); component.getProjectCategoryIds().subscribe((res) => { expect(res).toEqual([]); - expect(categoriesService.getAll).toHaveBeenCalledTimes(1); done(); }); }); it('should return undefined if category id is not defined', (done) => { + component.projectCategories$ = of(mileageCategoryWithoutId); categoriesService.getAll.and.returnValue(of(mileageCategoryWithoutId)); const projectCategoryIds = component.getProjectCategoryIds(); projectCategoryIds.subscribe((expectedProjectCategoryIds) => { expect(expectedProjectCategoryIds).toEqual([undefined]); - expect(categoriesService.getAll).toHaveBeenCalledTimes(1); + done(); + }); + }); + }); + + describe('getProjectCategories():', () => { + it('should return MILEAGE category IDs', (done) => { + component.projectCategories$ = of(mileageCategories); + categoriesService.getAll.and.returnValue(of(mileageCategories)); + + component.getProjectCategories().subscribe((res) => { + expect(res).toEqual([mileageCategories[0], mileageCategories[1]]); + done(); + }); + }); + + it('should return an empty array if there are no MILEAGE categories', (done) => { + component.projectCategories$ = of([]); + categoriesService.getAll.and.returnValue(of(transformedOrgCategoryById)); + + component.getProjectCategories().subscribe((res) => { + expect(res).toEqual([]); done(); }); }); 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 a8a3d40f4c..b8e76d7ade 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 @@ -496,6 +496,7 @@ export function TestCases4(getTestBed) { it('should return project from ID specified in the expense', (done) => { component.etxn$ = of(unflattenedTxnData); component.subCategories$ = of(sortedCategory); + component.projectCategories$ = of(sortedCategory); projectsService.getbyId.and.returnValue(of(expectedProjectsResponse[0])); fixture.detectChanges(); @@ -508,6 +509,7 @@ export function TestCases4(getTestBed) { it('should get default project ID and return the project if not provided in the expense', (done) => { component.etxn$ = of(newUnflattenedTxn); + component.projectCategories$ = of(sortedCategory); component.subCategories$ = of(sortedCategory); orgSettingsService.get.and.returnValue(of(orgSettingsRes)); orgUserSettingsService.get.and.returnValue(of(orgUserSettingsData)); 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 61acb73b43..8d1054acd3 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 @@ -244,6 +244,7 @@ export function TestCases5(getTestBed) { function getClassValues() { spyOn(component, 'getTransactionFields').and.returnValue(of(expenseFieldObjData)); spyOn(component, 'getSubCategories').and.returnValue(of(mileageCategories2)); + spyOn(component, 'getProjectCategories').and.returnValue(of(mileageCategories2)); spyOn(component, 'getProjectCategoryIds').and.returnValue(of(['141295', '141300'])); spyOn(component, 'getNewExpense').and.returnValue(of(newExpenseMileageData1)); spyOn(component, 'getCustomInputs').and.returnValue(of(null)); 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 1b6b1b1317..3d5737fd31 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 @@ -190,6 +190,8 @@ export class AddEditMileagePage implements OnInit { projectCategoryIds$: Observable; + projectCategories$: Observable; + isConnected$: Observable; connectionStatus$: Observable<{ connected: boolean }>; @@ -489,18 +491,20 @@ export class AddEditMileagePage implements OnInit { }); } - getProjectCategoryIds(): Observable { + getProjectCategories(): Observable { return this.categoriesService.getAll().pipe( map((categories) => { - const mileageCategories = categories - .filter((category) => ['Mileage'].indexOf(category.fyle_category) > -1) - .map((category) => category.id?.toString()); + const mileageCategories = categories.filter((category) => category.fyle_category === 'Mileage'); return mileageCategories; }) ); } + getProjectCategoryIds(): Observable { + return this.projectCategories$.pipe(map((categories) => categories.map((category) => category?.id?.toString()))); + } + getMileageCategories(): Observable<{ defaultMileageCategory: OrgCategory; mileageCategories: OrgCategory[] }> { return this.categoriesService.getAll().pipe( map((categories) => { @@ -1160,8 +1164,8 @@ export class AddEditMileagePage implements OnInit { }), switchMap((projectId) => { if (projectId) { - return this.subCategories$.pipe( - switchMap((allActiveSubCategories) => this.projectsService.getbyId(projectId, allActiveSubCategories)) + return this.projectCategories$.pipe( + switchMap((projectCategories) => this.projectsService.getbyId(projectId, projectCategories)) ); } else { return of(null); @@ -1571,10 +1575,12 @@ export class AddEditMileagePage implements OnInit { this.homeCurrency$ = this.currencyService.getHomeCurrency(); this.setupFilteredCategories(); + this.projectCategories$ = this.getProjectCategories(); this.projectCategoryIds$ = this.getProjectCategoryIds(); - this.isProjectVisible$ = combineLatest([this.projectCategoryIds$, this.subCategories$]).pipe( - switchMap(([projectCategoryIds, allActiveSubCategories]) => - this.projectsService.getProjectCount({ categoryIds: projectCategoryIds }, allActiveSubCategories) + + this.isProjectVisible$ = combineLatest([this.projectCategoryIds$, this.projectCategories$]).pipe( + switchMap(([projectCategoryIds, projectCategories]) => + this.projectsService.getProjectCount({ categoryIds: projectCategoryIds }, projectCategories) ), map((projectCount) => projectCount > 0) ); @@ -1705,14 +1711,14 @@ export class AddEditMileagePage implements OnInit { recentValues: this.recentlyUsedValues$, mileageCategoryIds: this.projectCategoryIds$, eou: eou$, - activeSubCategories: this.subCategories$, + projectCategories: this.projectCategories$, }).pipe( - switchMap(({ recentValues, mileageCategoryIds, eou, activeSubCategories }) => + switchMap(({ recentValues, mileageCategoryIds, eou, projectCategories }) => this.recentlyUsedItemsService.getRecentlyUsedProjects({ recentValues, eou, categoryIds: mileageCategoryIds, - activeCategoryList: activeSubCategories, + activeCategoryList: projectCategories, }) ) ); diff --git a/src/app/fyle/add-edit-per-diem/add-edit-per-diem-1.page.spec.ts b/src/app/fyle/add-edit-per-diem/add-edit-per-diem-1.page.spec.ts index eadb12a5a1..6f2ec075ec 100644 --- a/src/app/fyle/add-edit-per-diem/add-edit-per-diem-1.page.spec.ts +++ b/src/app/fyle/add-edit-per-diem/add-edit-per-diem-1.page.spec.ts @@ -48,7 +48,12 @@ import { import { AccountType } from 'src/app/core/enums/account-type.enum'; import { cloneDeep } from 'lodash'; import { expenseFieldsMapResponse } from 'src/app/core/mock-data/expense-fields-map.data'; -import { expectedAllOrgCategories, perDiemCategory } from 'src/app/core/mock-data/org-category.data'; +import { + expectedAllOrgCategories, + perDiemCategories2, + perDiemCategory, + transformedOrgCategoryById, +} from 'src/app/core/mock-data/org-category.data'; import { txnFieldsData2 } from 'src/app/core/mock-data/expense-field-obj.data'; import { defaultTxnFieldValuesData2 } from 'src/app/core/mock-data/default-txn-field-values.data'; import { orgSettingsCCCDisabled } from 'src/app/core/mock-data/org-settings.data'; @@ -591,7 +596,7 @@ export function TestCases1(getTestBed) { expect(expenseFieldsService.getDefaultTxnFieldValues).toHaveBeenCalledOnceWith(mockTxnFieldData); expect(component.fg.controls.costCenter.value).toEqual(15818); expect(component.fg.controls.purpose.value).toEqual('test_term'); - expect(component.fg.controls.billable.value).toEqual(true); + expect(component.fg.controls.billable.value).toBeTrue(); }); it('getPaymentModes(): should get payment modes', (done) => { @@ -658,20 +663,20 @@ export function TestCases1(getTestBed) { describe('getProjectCategoryIds():', () => { it('should return project category ids', (done) => { + component.projectCategories$ = of([perDiemCategory]); categoriesService.getAll.and.returnValue(of([...expectedAllOrgCategories, perDiemCategory])); component.getProjectCategoryIds().subscribe((res) => { - expect(categoriesService.getAll).toHaveBeenCalledTimes(1); expect(res).toEqual(['38912']); done(); }); }); it('should return undefined if category id is undefined', (done) => { + component.projectCategories$ = of([undefined]); const mockPerDiemCategory = cloneDeep(perDiemCategory); mockPerDiemCategory.id = undefined; categoriesService.getAll.and.returnValue(of([...expectedAllOrgCategories, mockPerDiemCategory])); component.getProjectCategoryIds().subscribe((res) => { - expect(categoriesService.getAll).toHaveBeenCalledTimes(1); // If category id is undefined, it will return undefined due to default behaviour of map function expect(res).toEqual([undefined]); done(); @@ -679,6 +684,28 @@ export function TestCases1(getTestBed) { }); }); + describe('getProjectCategories():', () => { + it('should return MILEAGE category IDs', (done) => { + component.projectCategories$ = of(perDiemCategories2); + categoriesService.getAll.and.returnValue(of(perDiemCategories2)); + + component.getProjectCategories().subscribe((res) => { + expect(res).toEqual(perDiemCategories2); + done(); + }); + }); + + it('should return an empty array if there are no MILEAGE categories', (done) => { + component.projectCategories$ = of([]); + categoriesService.getAll.and.returnValue(of(transformedOrgCategoryById)); + + component.getProjectCategories().subscribe((res) => { + expect(res).toEqual([]); + done(); + }); + }); + }); + it('getPerDiemCategories(): should return defaultPerDiemCategory and perDiemCategories', (done) => { categoriesService.getAll.and.returnValue(of([...expectedAllOrgCategories, perDiemCategory])); component.getPerDiemCategories().subscribe((res) => { 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 bd961c5266..327b6c403e 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 @@ -421,6 +421,7 @@ export function TestCases2(getTestBed) { perDiemService.getRates.and.returnValue(of(expectedPerDiems)); spyOn(component, 'getSubCategories').and.returnValue(of(orgCategoryData1)); spyOn(component, 'setupFilteredCategories'); + spyOn(component, 'getProjectCategories').and.returnValue(of(orgCategoryData1)); spyOn(component, 'getProjectCategoryIds').and.returnValue(of(['129140', '129112', '16582', '201952'])); spyOn(component, 'getPerDiemCategories').and.returnValue( of({ @@ -442,6 +443,7 @@ export function TestCases2(getTestBed) { it('should initialize all the variables correctly', fakeAsync(() => { const dependentFieldSpy = jasmine.createSpyObj('DependentFieldComponent', ['ngOnInit']); + component.projectCategories$ = of(orgCategoryData1); component.projectDependentFieldsRef = dependentFieldSpy; component.costCenterDependentFieldsRef = dependentFieldSpy; const tomorrow = new Date('2023-08-18'); 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 a42a5559c3..b9f2804919 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 @@ -183,6 +183,8 @@ export class AddEditPerDiemPage implements OnInit { projectCategoryIds$: Observable; + projectCategories$: Observable; + filteredCategories$: Observable<{ label: string; value: OrgCategory }[]>; isConnected$: Observable; @@ -605,18 +607,20 @@ export class AddEditPerDiemPage implements OnInit { ); } - getProjectCategoryIds(): Observable { + getProjectCategories(): Observable { return this.categoriesService.getAll().pipe( map((categories) => { - const perDiemCategories = categories - .filter((category) => ['Per Diem'].indexOf(category.fyle_category) > -1) - .map((category) => category?.id?.toString()); + const perDiemCategories = categories.filter((category) => category.fyle_category === 'Per Diem'); return perDiemCategories; }) ); } + getProjectCategoryIds(): Observable { + return this.projectCategories$.pipe(map((categories) => categories.map((category) => category?.id?.toString()))); + } + getPerDiemCategories(): Observable<{ defaultPerDiemCategory: OrgCategory; perDiemCategories: OrgCategory[]; @@ -1006,13 +1010,14 @@ export class AddEditPerDiemPage implements OnInit { this.txnFields$ = this.getTransactionFields(); this.homeCurrency$ = this.currencyService.getHomeCurrency(); - this.setupFilteredCategories(); + this.projectCategories$ = this.getProjectCategories(); this.projectCategoryIds$ = this.getProjectCategoryIds(); - this.isProjectVisible$ = combineLatest([this.projectCategoryIds$, this.subCategories$]).pipe( - switchMap(([projectCategoryIds, allActiveSubCategories]) => - this.projectsService.getProjectCount({ categoryIds: projectCategoryIds }, allActiveSubCategories) + + this.isProjectVisible$ = combineLatest([this.projectCategoryIds$, this.projectCategories$]).pipe( + switchMap(([projectCategoryIds, projectCategories]) => + this.projectsService.getProjectCount({ categoryIds: projectCategoryIds }, projectCategories) ), map((projectCount) => projectCount > 0) ); @@ -1314,8 +1319,8 @@ export class AddEditPerDiemPage implements OnInit { }), switchMap((projectId) => { if (projectId) { - return this.subCategories$.pipe( - switchMap((allActiveSubCategories) => this.projectsService.getbyId(projectId, allActiveSubCategories)) + return this.projectCategories$.pipe( + switchMap((projectCategories) => this.projectsService.getbyId(projectId, projectCategories)) ); } else { return of(null); @@ -1342,15 +1347,15 @@ export class AddEditPerDiemPage implements OnInit { this.recentlyUsedProjects$ = forkJoin({ recentValues: this.recentlyUsedValues$, perDiemCategoryIds: this.projectCategoryIds$, + perDiemCategories: this.projectCategories$, eou: this.authService.getEou(), - activeSubCategories: this.subCategories$, }).pipe( - switchMap(({ recentValues, perDiemCategoryIds, eou, activeSubCategories }) => + switchMap(({ recentValues, perDiemCategoryIds, perDiemCategories, eou }) => this.recentlyUsedItemsService.getRecentlyUsedProjects({ recentValues, eou, categoryIds: perDiemCategoryIds, - activeCategoryList: activeSubCategories, + activeCategoryList: perDiemCategories, }) ) );