diff --git a/src/app/core/mock-data/api-params.data.ts b/src/app/core/mock-data/api-params.data.ts index 4066d61b83..07a29a3872 100644 --- a/src/app/core/mock-data/api-params.data.ts +++ b/src/app/core/mock-data/api-params.data.ts @@ -25,7 +25,7 @@ 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({ @@ -33,5 +33,5 @@ export const getTeamReportsParams2: ReportApiParams = deepFreeze({ limit: 10, state: 'in.(APPROVER_PENDING)', next_approver_user_ids: 'cs.[usvKA4X8Ugcr]', - order: null, + order: 'created_at.desc,id.desc', }); diff --git a/src/app/core/mock-data/get-tasks-query-params-with-filters.data.ts b/src/app/core/mock-data/get-tasks-query-params-with-filters.data.ts index 65c4ef83a3..15af610722 100644 --- a/src/app/core/mock-data/get-tasks-query-params-with-filters.data.ts +++ b/src/app/core/mock-data/get-tasks-query-params-with-filters.data.ts @@ -15,7 +15,10 @@ export const tasksQueryParamsWithFiltersData: Partial = deepFreeze({ pageNumber: 1, sortDir: 'asc', - sortParam: 'approvalDate', + searchString: 'example', + queryParams: { + state: 'in.(APPROVER_PENDING)', + }, }); export const tasksQueryParamsWithFiltersData3: Partial = deepFreeze({ diff --git a/src/app/core/services/api-v2.service.ts b/src/app/core/services/api-v2.service.ts index 8be9fa45ad..03fe053d55 100644 --- a/src/app/core/services/api-v2.service.ts +++ b/src/app/core/services/api-v2.service.ts @@ -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', @@ -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(url: string, config: Partial = {}) { + get(url: string, config: Partial = {}): Observable>> { return this.httpClient.get>>(this.ROOT_ENDPOINT + '/v2' + url, config); } - getStats(url: string, config = {}) { + getStats(url: string, config = {}): Observable { return this.httpClient.get(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; } @@ -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 }); + } } } diff --git a/src/app/fyle/my-expenses/my-expenses.page.ts b/src/app/fyle/my-expenses/my-expenses.page.ts index 6ec5433b4b..46a835bde5 100644 --- a/src/app/fyle/my-expenses/my-expenses.page.ts +++ b/src/app/fyle/my-expenses/my-expenses.page.ts @@ -1511,7 +1511,6 @@ export class MyExpensesPage implements OnInit { if (params.searchString) { queryParams.q = params?.searchString + ':*'; } - return queryParams; }), switchMap((queryParams) => this.expenseService.getAllExpenses({ queryParams })) diff --git a/src/app/fyle/my-reports/my-reports.page.spec.ts b/src/app/fyle/my-reports/my-reports.page.spec.ts index 6951f21c0a..fdd7ed5ff2 100644 --- a/src/app/fyle/my-reports/my-reports.page.spec.ts +++ b/src/app/fyle/my-reports/my-reports.page.spec.ts @@ -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)); @@ -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); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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) => { @@ -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, @@ -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); diff --git a/src/app/fyle/my-reports/my-reports.page.ts b/src/app/fyle/my-reports/my-reports.page.ts index b808ee1d85..30bd6772a6 100644 --- a/src/app/fyle/my-reports/my-reports.page.ts +++ b/src/app/fyle/my-reports/my-reports.page.ts @@ -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; @@ -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); }), diff --git a/src/app/fyle/team-reports/team-reports-1.page.spec.ts b/src/app/fyle/team-reports/team-reports-1.page.spec.ts index acbd8fb463..5e7a5abd54 100644 --- a/src/app/fyle/team-reports/team-reports-1.page.spec.ts +++ b/src/app/fyle/team-reports/team-reports-1.page.spec.ts @@ -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); diff --git a/src/app/fyle/team-reports/team-reports.page.ts b/src/app/fyle/team-reports/team-reports.page.ts index ea261f602f..3f7fe52e47 100644 --- a/src/app/fyle/team-reports/team-reports.page.ts +++ b/src/app/fyle/team-reports/team-reports.page.ts @@ -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'; @@ -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, @@ -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) @@ -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);