Skip to content

Commit

Permalink
fix: Projects field fix for per diem and mileage page (#3066)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimple16 authored Jun 11, 2024
1 parent 3b4e114 commit b5e09d1
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 33 deletions.
26 changes: 26 additions & 0 deletions src/app/core/mock-data/org-category.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down
30 changes: 26 additions & 4 deletions src/app/fyle/add-edit-mileage/add-edit-mileage-1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
2 changes: 2 additions & 0 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 @@ -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();

Expand All @@ -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));
Expand Down
1 change: 1 addition & 0 deletions src/app/fyle/add-edit-mileage/add-edit-mileage-5.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
30 changes: 18 additions & 12 deletions src/app/fyle/add-edit-mileage/add-edit-mileage.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ export class AddEditMileagePage implements OnInit {

projectCategoryIds$: Observable<string[]>;

projectCategories$: Observable<OrgCategory[]>;

isConnected$: Observable<boolean>;

connectionStatus$: Observable<{ connected: boolean }>;
Expand Down Expand Up @@ -489,18 +491,20 @@ export class AddEditMileagePage implements OnInit {
});
}

getProjectCategoryIds(): Observable<string[]> {
getProjectCategories(): Observable<OrgCategory[]> {
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<string[]> {
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) => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
);
Expand Down Expand Up @@ -1701,14 +1707,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,
})
)
);
Expand Down
35 changes: 31 additions & 4 deletions src/app/fyle/add-edit-per-diem/add-edit-per-diem-1.page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -658,27 +663,49 @@ 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();
});
});
});

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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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');
Expand Down
31 changes: 18 additions & 13 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 @@ -182,6 +182,8 @@ export class AddEditPerDiemPage implements OnInit {

projectCategoryIds$: Observable<string[]>;

projectCategories$: Observable<OrgCategory[]>;

filteredCategories$: Observable<{ label: string; value: OrgCategory }[]>;

isConnected$: Observable<boolean>;
Expand Down Expand Up @@ -604,18 +606,20 @@ export class AddEditPerDiemPage implements OnInit {
);
}

getProjectCategoryIds(): Observable<string[]> {
getProjectCategories(): Observable<OrgCategory[]> {
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<string[]> {
return this.projectCategories$.pipe(map((categories) => categories.map((category) => category?.id?.toString())));
}

getPerDiemCategories(): Observable<{
defaultPerDiemCategory: OrgCategory;
perDiemCategories: OrgCategory[];
Expand Down Expand Up @@ -1005,13 +1009,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)
);
Expand Down Expand Up @@ -1309,8 +1314,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);
Expand All @@ -1337,15 +1342,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,
})
)
);
Expand Down

0 comments on commit b5e09d1

Please sign in to comment.