Skip to content

Commit

Permalink
feat: fix simple search issues (#3032)
Browse files Browse the repository at this point in the history
  • Loading branch information
bistaastha authored Jun 3, 2024
1 parent 17aa321 commit fe27eca
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/app/core/mock-data/api-params.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ export const getTeamReportsParams1: ReportApiParams = deepFreeze({
limit: 10,
state: 'in.(APPROVER_PENDING)',
next_approver_user_ids: 'cs.[usvKA4X8Ugcr]',
order: null,
order: 'created_at.desc,id.desc',
});

export const getTeamReportsParams2: ReportApiParams = deepFreeze({
offset: 0,
limit: 10,
state: 'in.(APPROVER_PENDING)',
next_approver_user_ids: 'cs.[usvKA4X8Ugcr]',
order: null,
order: 'created_at.desc,id.desc',
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export const tasksQueryParamsWithFiltersData: Partial<GetTasksQueryParamsWithFil
export const tasksQueryParamsWithFiltersData2: Partial<GetTasksQueryParamsWithFilters> = deepFreeze({
pageNumber: 1,
sortDir: 'asc',
sortParam: 'approvalDate',
searchString: 'example',
queryParams: {
state: 'in.(APPROVER_PENDING)',
},
});

export const tasksQueryParamsWithFiltersData3: Partial<GetTasksQueryParamsWithFilters> = deepFreeze({
Expand Down
16 changes: 11 additions & 5 deletions src/app/core/services/api-v2.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { ApiV2Response } from '../models/v2/api-v2-response.model';
import { Observable } from 'rxjs';

@Injectable({
providedIn: 'root',
Expand All @@ -13,19 +14,20 @@ export class ApiV2Service {
this.ROOT_ENDPOINT = environment.ROOT_URL;
}

setRoot(rootUrl: string) {
setRoot(rootUrl: string): void {
this.ROOT_ENDPOINT = rootUrl;
}

get<T, K>(url: string, config: Partial<K> = {}) {
get<T, K>(url: string, config: Partial<K> = {}): Observable<Partial<ApiV2Response<T>>> {
return this.httpClient.get<Partial<ApiV2Response<T>>>(this.ROOT_ENDPOINT + '/v2' + url, config);
}

getStats<T>(url: string, config = {}) {
getStats<T>(url: string, config = {}): Observable<T> {
return this.httpClient.get<T>(this.ROOT_ENDPOINT + '/v2' + url, config);
}

extendQueryParamsForTextSearch(queryParams = {}, simpleSearchText: string) {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
extendQueryParamsForTextSearch(queryParams = {}, simpleSearchText: string, usingPlatformApi?: boolean) {
if (simpleSearchText === undefined || simpleSearchText.length < 1) {
return queryParams;
}
Expand All @@ -39,6 +41,10 @@ export class ApiV2Service {
.concat(lastElement)
.concat(':*');

return Object.assign({}, queryParams, { _search_document: 'fts.' + searchQuery });
if (usingPlatformApi) {
return Object.assign({}, queryParams, { q: searchQuery });
} else {
return Object.assign({}, queryParams, { _search_document: 'fts.' + searchQuery });
}
}
}
1 change: 0 additions & 1 deletion src/app/fyle/my-expenses/my-expenses.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,6 @@ export class MyExpensesPage implements OnInit {
if (params.searchString) {
queryParams.q = params?.searchString + ':*';
}

return queryParams;
}),
switchMap((queryParams) => this.expenseService.getAllExpenses({ queryParams }))
Expand Down
27 changes: 27 additions & 0 deletions src/app/fyle/my-reports/my-reports.page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ describe('MyReportsPage', () => {
describe('ionViewWillEnter(): ', () => {
it('should initialize component properties and load data', fakeAsync(() => {
tasksService.getReportsTaskCount.and.returnValue(of(5));
apiV2Service.extendQueryParamsForTextSearch.and.returnValue({
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
q: 'example:*',
});
const homeCurrency = 'USD';
currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));

Expand Down Expand Up @@ -324,6 +328,7 @@ describe('MyReportsPage', () => {
limit: 10,
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
order: 'created_at.desc,id.desc',
q: 'example:*',
});

expect(component.acc).toEqual(expectedReportsSinglePage);
Expand Down Expand Up @@ -371,6 +376,9 @@ describe('MyReportsPage', () => {

it('should initialize component properties and load data when search string is empty', fakeAsync(() => {
tasksService.getReportsTaskCount.and.returnValue(of(5));
apiV2Service.extendQueryParamsForTextSearch.and.returnValue({
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
});
const homeCurrency = 'USD';
currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));

Expand Down Expand Up @@ -495,6 +503,9 @@ describe('MyReportsPage', () => {

it('should initialize component properties and set simplifyReportsSetting$ to undefined if orgSetting$ is undefined', fakeAsync(() => {
tasksService.getReportsTaskCount.and.returnValue(of(5));
apiV2Service.extendQueryParamsForTextSearch.and.returnValue({
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
});
const homeCurrency = 'USD';
currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));

Expand Down Expand Up @@ -619,6 +630,9 @@ describe('MyReportsPage', () => {

it('should initialize component properties and set simplifyReportsSetting$ to false if orgSetting$.payment_mode_setting.payment_modes_order is not defined', fakeAsync(() => {
tasksService.getReportsTaskCount.and.returnValue(of(5));
apiV2Service.extendQueryParamsForTextSearch.and.returnValue({
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
});
const homeCurrency = 'USD';
currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));

Expand Down Expand Up @@ -742,6 +756,9 @@ describe('MyReportsPage', () => {

it('should initialize component properties and get report by order if sortParam and sortDir is defined, aggregates is empty array and simplified_report is enabled', fakeAsync(() => {
tasksService.getReportsTaskCount.and.returnValue(of(5));
apiV2Service.extendQueryParamsForTextSearch.and.returnValue({
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
});
const homeCurrency = 'USD';
currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));

Expand Down Expand Up @@ -858,6 +875,9 @@ describe('MyReportsPage', () => {

it('should initialize component properties and load data if filters is defined in activatedRoute.snapshot', fakeAsync(() => {
tasksService.getReportsTaskCount.and.returnValue(of(5));
apiV2Service.extendQueryParamsForTextSearch.and.returnValue({
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
});
const homeCurrency = 'USD';
currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));

Expand Down Expand Up @@ -995,6 +1015,10 @@ describe('MyReportsPage', () => {

it('should initialize component properties and load data if state is defined in activatedRoute.snapshot', fakeAsync(() => {
tasksService.getReportsTaskCount.and.returnValue(of(5));
apiV2Service.extendQueryParamsForTextSearch.and.returnValue({
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
q: 'example:*',
});
const homeCurrency = 'USD';
currencyService.getHomeCurrency.and.returnValue(of(homeCurrency));

Expand Down Expand Up @@ -1049,6 +1073,7 @@ describe('MyReportsPage', () => {
// It is called 6 times because loadData$ is behaviorSubject and next() is called 1 times
expect(spenderReportsService.getReportsCount).toHaveBeenCalledWith({
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
q: 'example:*',
});

component.expensesAmountStats$.subscribe((expenseAmountStates) => {
Expand All @@ -1075,6 +1100,7 @@ describe('MyReportsPage', () => {
limit: 10,
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
order: 'created_at.desc,id.desc',
q: 'example:*',
});
expect(spenderReportsService.getReportsByParams).toHaveBeenCalledWith({
offset: 0,
Expand All @@ -1088,6 +1114,7 @@ describe('MyReportsPage', () => {
limit: 10,
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
order: 'created_at.desc,id.desc',
q: 'example:*',
});

expect(component.acc).toEqual(expectedReportsSinglePage);
Expand Down
16 changes: 4 additions & 12 deletions src/app/fyle/my-reports/my-reports.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,10 @@ export class MyReportsPage {

const paginatedPipe = this.loadData$.pipe(
switchMap((params) => {
const queryParams = params.queryParams || {
let queryParams = params.queryParams || {
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
};
if (params.searchString) {
queryParams.q = params.searchString + ':*';
} else {
delete queryParams.q;
}
queryParams = this.apiV2Service.extendQueryParamsForTextSearch(queryParams, params.searchString, true);
const orderByParams =
params.sortParam && params.sortDir ? `${params.sortParam}.${params.sortDir}` : 'created_at.desc,id.desc';
this.isLoadingDataInInfiniteScroll = true;
Expand Down Expand Up @@ -222,14 +218,10 @@ export class MyReportsPage {

this.count$ = this.loadData$.pipe(
switchMap((params) => {
const queryParams = params.queryParams || {
let queryParams = params.queryParams || {
state: 'in.(DRAFT,APPROVED,APPROVER_PENDING,APPROVER_INQUIRY,PAYMENT_PENDING,PAYMENT_PROCESSING,PAID)',
};
if (params.searchString) {
queryParams.q = params.searchString + ':*';
} else {
delete queryParams.q;
}
queryParams = this.apiV2Service.extendQueryParamsForTextSearch(queryParams, params.searchString, true);
this.isLoadingDataInInfiniteScroll = true;
return this.spenderReportsService.getReportsCount(queryParams);
}),
Expand Down
6 changes: 5 additions & 1 deletion src/app/fyle/team-reports/team-reports-1.page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,17 @@ export function TestCases1(getTestBed) {

it('should call approverReporsService.getReportsByParams and update acc', (done) => {
mockAddNewFiltersToParams.and.returnValue(tasksQueryParamsWithFiltersData2);
apiV2Service.extendQueryParamsForTextSearch.and.returnValue({
state: 'in.(APPROVER_PENDING)',
next_approver_user_ids: 'cs.[usvKA4X8Ugcr]',
});
component.ionViewWillEnter();
component.eou$.subscribe((eou) => {
expect(eou).toEqual(apiEouRes);
expect(approverReportsService.getReportsByParams).toHaveBeenCalledTimes(2);
expect(approverReportsService.getReportsByParams).toHaveBeenCalledWith(getTeamReportsParams1);
expect(approverReportsService.getReportsByParams).toHaveBeenCalledWith(getTeamReportsParams2);
expect(component.isLoadingDataInInfiniteScroll).toBeFalse();
expect(component.isLoadingDataInInfiniteScroll).toBeTrue();
expect(component.acc).toEqual(expectedReportsSinglePage);
component.teamReports$.subscribe((teamReports) => {
expect(teamReports).toEqual(expectedReportsSinglePage);
Expand Down
30 changes: 9 additions & 21 deletions src/app/fyle/team-reports/team-reports.page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit, ViewChild, ElementRef, EventEmitter } from '@angular/core';
import { Observable, BehaviorSubject, fromEvent, iif, of, noop, concat, Subject, from } from 'rxjs';
import { Observable, BehaviorSubject, fromEvent, noop, concat, Subject, from } from 'rxjs';
import { NetworkService } from 'src/app/core/services/network.service';
import { LoaderService } from 'src/app/core/services/loader.service';
import { ReportService } from 'src/app/core/services/report.service';
Expand Down Expand Up @@ -176,15 +176,10 @@ export class TeamReportsPage implements OnInit {

const paginatedPipe = this.loadData$.pipe(
switchMap((params) => {
const queryParams = params.queryParams;
const orderByParams = params.sortParam && params.sortDir ? `${params.sortParam}.${params.sortDir}` : null;
if (params.searchString) {
queryParams.q = params.searchString + ':*';
} else {
if (queryParams && queryParams.q) {
delete queryParams.q;
}
}
let queryParams = params.queryParams;
queryParams = this.apiV2Service.extendQueryParamsForTextSearch(queryParams, params.searchString, true);
const orderByParams =
params.sortParam && params.sortDir ? `${params.sortParam}.${params.sortDir}` : 'created_at.desc,id.desc';
this.isLoadingDataInInfiniteScroll = true;
return this.approverReportsService.getReportsByParams({
offset: (params.pageNumber - 1) * 10,
Expand All @@ -207,14 +202,9 @@ export class TeamReportsPage implements OnInit {

this.count$ = this.loadData$.pipe(
switchMap((params) => {
const queryParams = params.queryParams;
if (params.searchString) {
queryParams.q = params.searchString + ':*';
} else {
if (queryParams && queryParams.q) {
delete queryParams.q;
}
}
let queryParams = params.queryParams;
queryParams = this.apiV2Service.extendQueryParamsForTextSearch(queryParams, params.searchString, true);
this.isLoadingDataInInfiniteScroll = true;
return this.approverReportsService.getReportsCount(queryParams);
}),
shareReplay(1)
Expand All @@ -224,9 +214,7 @@ export class TeamReportsPage implements OnInit {
switchMap((reports) => this.count$.pipe(map((count) => count > reports.length)))
);

this.isInfiniteScrollRequired$ = this.loadData$.pipe(
switchMap((params) => iif(() => params.searchString && params.searchString !== '', of(false), paginatedScroll$))
);
this.isInfiniteScrollRequired$ = this.loadData$.pipe(switchMap(() => paginatedScroll$));

this.teamReports$.subscribe(noop);
this.count$.subscribe(noop);
Expand Down

0 comments on commit fe27eca

Please sign in to comment.