diff --git a/src/app/core/mock-data/stats-dimension-response.data.ts b/src/app/core/mock-data/stats-dimension-response.data.ts index 52cfa1f8f9..07f9451de4 100644 --- a/src/app/core/mock-data/stats-dimension-response.data.ts +++ b/src/app/core/mock-data/stats-dimension-response.data.ts @@ -80,23 +80,3 @@ export const expectedReportRawStats: StatsDimensionResponse[] = [ name: 'scalar_stat', }, ]; - -export const apiAdvanceReqRes: Partial> = { - data: [ - { - aggregates: [ - { - function_name: 'count(areq_id)', - function_value: 0, - }, - { - function_name: 'sum(areq_amount)', - function_value: null, - }, - ], - dimensions: [], - name: 'scalar_stat', - }, - ], - url: '/v2/advance_requests/stats', -}; diff --git a/src/app/core/models/platform/v1/stats-response.model.ts b/src/app/core/models/platform/v1/stats-response.model.ts new file mode 100644 index 0000000000..551a395c6a --- /dev/null +++ b/src/app/core/models/platform/v1/stats-response.model.ts @@ -0,0 +1,4 @@ +export interface StatsResponse { + count: number; + total_amount: number; +} diff --git a/src/app/core/services/advance-request.service.spec.ts b/src/app/core/services/advance-request.service.spec.ts index ac8f30d17e..b88e2dcab7 100644 --- a/src/app/core/services/advance-request.service.spec.ts +++ b/src/app/core/services/advance-request.service.spec.ts @@ -40,7 +40,6 @@ import { import { apiEouRes } from '../mock-data/extended-org-user.data'; import { fileObjectData3, fileObjectData4 } from '../mock-data/file-object.data'; import { fileData1, fileData2 } from '../mock-data/file.data'; -import { apiAdvanceReqRes } from '../mock-data/stats-dimension-response.data'; import { AdvancesStates } from '../models/advances-states.model'; import { SortingDirection } from '../models/sorting-direction.model'; import { SortingParam } from '../models/sorting-param.model'; @@ -53,6 +52,7 @@ import { DateService } from './date.service'; import { FileService } from './file.service'; import { OrgUserSettingsService } from './org-user-settings.service'; import { TimezoneService } from './timezone.service'; +import { SpenderService } from './platform/v1/spender/spender.service'; describe('AdvanceRequestService', () => { let advanceRequestService: AdvanceRequestService; @@ -64,6 +64,7 @@ describe('AdvanceRequestService', () => { let fileService: jasmine.SpyObj; let orgUserSettingsService: jasmine.SpyObj; let timezoneService: jasmine.SpyObj; + let spenderService: jasmine.SpyObj; beforeEach(() => { const apiServiceSpy = jasmine.createSpyObj('ApiService', ['get', 'post', 'delete']); @@ -73,6 +74,7 @@ describe('AdvanceRequestService', () => { const fileServiceSpy = jasmine.createSpyObj('FileService', ['post']); const orgUserSettingsServiceSpy = jasmine.createSpyObj('OrgUserSettingsService', ['get']); const timezoneServiceSpy = jasmine.createSpyObj('TimezoneService', ['convertToUtc']); + const spenderServiceSpy = jasmine.createSpyObj('SpenderService', ['post']); TestBed.configureTestingModule({ providers: [ @@ -106,6 +108,10 @@ describe('AdvanceRequestService', () => { provide: TimezoneService, useValue: timezoneServiceSpy, }, + { + provide: SpenderService, + useValue: spenderServiceSpy, + }, ], }); advanceRequestService = TestBed.inject(AdvanceRequestService); @@ -117,6 +123,7 @@ describe('AdvanceRequestService', () => { fileService = TestBed.inject(FileService) as jasmine.SpyObj; orgUserSettingsService = TestBed.inject(OrgUserSettingsService) as jasmine.SpyObj; timezoneService = TestBed.inject(TimezoneService) as jasmine.SpyObj; + spenderService = TestBed.inject(SpenderService) as jasmine.SpyObj; }); it('should be created', () => { @@ -237,43 +244,22 @@ describe('AdvanceRequestService', () => { }); }); - it('getMyAdvanceRequestStats(): should get advance request stats by params provided', (done) => { - authService.getEou.and.returnValue(Promise.resolve(apiEouRes)); - //@ts-ignore - spyOn(advanceRequestService, 'getAdvanceRequestStats').and.returnValue(of(apiAdvanceReqRes)); - const params = { - aggregates: 'count(areq_id),sum(areq_amount)', - areq_state: 'in.(DRAFT)', - areq_is_sent_back: 'is.true', - scalar: true, - }; - - advanceRequestService.getMyAdvanceRequestStats(params).subscribe((res) => { - expect(res).toEqual(apiAdvanceReqRes.data); - //@ts-ignore - expect(advanceRequestService.getAdvanceRequestStats).toHaveBeenCalledOnceWith(apiEouRes, params); - expect(authService.getEou).toHaveBeenCalledTimes(1); - done(); - }); - }); - it('getAdvanceRequestStats(): should get advance request stats', (done) => { - apiv2Service.get.and.returnValue(of(apiAdvanceReqRes)); + const statsResponse = { + count: 2, + total_amount: 12322, + }; + spenderService.post.and.returnValue(of({ data: statsResponse })); const params = { - aggregates: 'count(areq_id),sum(areq_amount)', - areq_state: 'in.(DRAFT)', - areq_is_sent_back: 'is.true', - scalar: true, + state: 'eq.SENT_BACK', }; - //@ts-ignore - advanceRequestService.getAdvanceRequestStats(apiEouRes, params).subscribe((res) => { - expect(res).toEqual(apiAdvanceReqRes); - expect(apiv2Service.get).toHaveBeenCalledOnceWith('/advance_requests/stats', { - params: { - areq_org_user_id: 'eq.' + apiEouRes.ou.id, - ...params, + advanceRequestService.getAdvanceRequestStats(params).subscribe((res) => { + expect(res).toEqual(statsResponse); + expect(spenderService.post).toHaveBeenCalledOnceWith('/advance_requests/stats', { + data: { + query_params: `state=${params.state}`, }, }); done(); diff --git a/src/app/core/services/advance-request.service.ts b/src/app/core/services/advance-request.service.ts index 3be591c268..9295c75253 100644 --- a/src/app/core/services/advance-request.service.ts +++ b/src/app/core/services/advance-request.service.ts @@ -27,6 +27,9 @@ import { StatsDimensionResponse } from '../models/stats-dimension-response.model import { AdvanceRequestActions } from '../models/advance-request-actions.model'; import { AdvanceRequestFile } from '../models/advance-request-file.model'; import { UnflattenedAdvanceRequest } from '../models/unflattened-advance-request.model'; +import { SpenderService } from './platform/v1/spender/spender.service'; +import { PlatformApiResponse } from '../models/platform/platform-api-response.model'; +import { StatsResponse } from '../models/platform/v1/stats-response.model'; const advanceRequestsCacheBuster$ = new Subject(); @@ -45,10 +48,7 @@ type Config = Partial<{ }>; type advanceRequestStat = { - aggregates: string; - areq_state: string; - areq_is_sent_back: string; - scalar: boolean; + state: string; }; @Injectable({ @@ -63,7 +63,8 @@ export class AdvanceRequestService { private timezoneService: TimezoneService, private dataTransformService: DataTransformService, private dateService: DateService, - private fileService: FileService + private fileService: FileService, + private spenderService: SpenderService ) {} @Cacheable({ @@ -366,13 +367,6 @@ export class AdvanceRequestService { ); } - getMyAdvanceRequestStats(params: advanceRequestStat): Observable> { - return from(this.authService.getEou()).pipe( - switchMap((eou) => this.getAdvanceRequestStats(eou, params)), - map((res) => res.data) - ); - } - private getSortOrder(sortParam: SortingParam, sortDir: SortingDirection): string { let order: string; if (sortParam === SortingParam.creationDate) { @@ -394,16 +388,14 @@ export class AdvanceRequestService { return order; } - private getAdvanceRequestStats( - eou: ExtendedOrgUser, - params: advanceRequestStat - ): Observable>> { - return this.apiv2Service.get('/advance_requests/stats', { - params: { - areq_org_user_id: 'eq.' + eou.ou.id, - ...params, - }, - }); + getAdvanceRequestStats(params: advanceRequestStat): Observable { + return this.spenderService + .post<{ data: StatsResponse }>('/advance_requests/stats', { + data: { + query_params: `state=${params.state}`, + }, + }) + .pipe(map((res) => res.data)); } private getApproversByAdvanceRequestId(advanceRequestId: string): Observable { diff --git a/src/app/core/services/tasks.service.spec.ts b/src/app/core/services/tasks.service.spec.ts index 98d55f8f1d..c01832b8bc 100644 --- a/src/app/core/services/tasks.service.spec.ts +++ b/src/app/core/services/tasks.service.spec.ts @@ -80,7 +80,7 @@ describe('TasksService', () => { const expensesServiceSpy = jasmine.createSpyObj('ExpensesService', ['getExpenseStats', 'getDuplicateSets']); const userEventServiceSpy = jasmine.createSpyObj('UserEventService', ['onTaskCacheClear']); const authServiceSpy = jasmine.createSpyObj('AuthService', ['getEou']); - const advanceRequestServiceSpy = jasmine.createSpyObj('AdvanceRequestService', ['getMyAdvanceRequestStats']); + const advanceRequestServiceSpy = jasmine.createSpyObj('AdvanceRequestService', ['getAdvanceRequestStats']); const corporateCreditCardExpenseServiceSpy = jasmine.createSpyObj('CorporateCreditCardExpenseService', [ 'getCorporateCards', ]); @@ -157,12 +157,12 @@ describe('TasksService', () => { it('should be able to fetch tasks related to sent back advances', (done) => { currencyService.getHomeCurrency.and.returnValue(of(homeCurrency)); - advanceRequestService.getMyAdvanceRequestStats.and.returnValue(of(sentBackAdvancesResponse)); + advanceRequestService.getAdvanceRequestStats.and.returnValue(of(sentBackAdvancesResponse)); humanizeCurrencyPipe.transform - .withArgs(sentBackAdvancesResponse[0].aggregates[1].function_value, homeCurrency, true) + .withArgs(sentBackAdvancesResponse.total_amount, homeCurrency, true) .and.returnValue('123.37M'); humanizeCurrencyPipe.transform - .withArgs(sentBackAdvancesResponse[0].aggregates[1].function_value, homeCurrency) + .withArgs(sentBackAdvancesResponse.total_amount, homeCurrency) .and.returnValue('₹123.37M'); tasksService.getSentBackAdvanceTasks().subscribe((sentBackAdvancesData) => { @@ -689,7 +689,7 @@ describe('TasksService', () => { function setupData() { currencyService.getHomeCurrency.and.returnValue(of(homeCurrency)); - advanceRequestService.getMyAdvanceRequestStats.and.returnValue(of(sentBackAdvancesResponse)); + advanceRequestService.getAdvanceRequestStats.and.returnValue(of(sentBackAdvancesResponse)); setupUnsibmittedReportsResponse(); getUnreportedExpenses(); reportService.getReportStatsData @@ -818,16 +818,16 @@ describe('TasksService', () => { it('should generate proper content in all cases for sent back advances', () => { humanizeCurrencyPipe.transform - .withArgs(sentBackAdvancesResponse[0].aggregates[1].function_value, homeCurrency, true) + .withArgs(sentBackAdvancesResponse.total_amount, homeCurrency, true) .and.returnValue('123.37M'); humanizeCurrencyPipe.transform - .withArgs(sentBackAdvancesResponse[0].aggregates[1].function_value, homeCurrency) + .withArgs(sentBackAdvancesResponse.total_amount, homeCurrency) .and.returnValue('₹123.37M'); const sentBackAdvanceTask = tasksService.mapSentBackAdvancesToTasks( { totalCount: 1, - totalAmount: sentBackAdvancesResponse[0].aggregates[1].function_value, + totalAmount: sentBackAdvancesResponse.total_amount, }, homeCurrency ); diff --git a/src/app/core/services/tasks.service.ts b/src/app/core/services/tasks.service.ts index 8f65df6700..194d4ccfe8 100644 --- a/src/app/core/services/tasks.service.ts +++ b/src/app/core/services/tasks.service.ts @@ -19,6 +19,7 @@ import { Datum } from '../models/v2/stats-response.model'; import { ExpensesService } from './platform/v1/spender/expenses.service'; import { OrgSettingsService } from './org-settings.service'; import { EmployeesService } from './platform/v1/spender/employees.service'; +import { StatsResponse } from '../models/platform/v1/stats-response.model'; @Injectable({ providedIn: 'root', @@ -451,12 +452,9 @@ export class TasksService { }); } - getSentBackAdvancesStats(): Observable { - return this.advancesRequestService.getMyAdvanceRequestStats({ - aggregates: 'count(areq_id),sum(areq_amount)', - areq_state: 'in.(DRAFT)', - areq_is_sent_back: 'is.true', - scalar: true, + getSentBackAdvancesStats(): Observable { + return this.advancesRequestService.getAdvanceRequestStats({ + state: 'eq.SENT_BACK', }); } @@ -465,9 +463,13 @@ export class TasksService { advancesStats: this.getSentBackAdvancesStats(), homeCurrency: this.currencyService.getHomeCurrency(), }).pipe( - map(({ advancesStats, homeCurrency }: { advancesStats: Datum[]; homeCurrency: string }) => - this.mapSentBackAdvancesToTasks(this.mapScalarAdvanceStatsResponse(advancesStats), homeCurrency) - ) + map(({ advancesStats, homeCurrency }: { advancesStats: StatsResponse; homeCurrency: string }) => { + const aggregate = { + totalAmount: advancesStats.total_amount, + totalCount: advancesStats.count, + }; + return this.mapSentBackAdvancesToTasks(aggregate, homeCurrency); + }) ); } diff --git a/src/app/core/test-data/tasks.service.spec.data.ts b/src/app/core/test-data/tasks.service.spec.data.ts index 47e6280071..a8b59de734 100644 --- a/src/app/core/test-data/tasks.service.spec.data.ts +++ b/src/app/core/test-data/tasks.service.spec.data.ts @@ -480,19 +480,7 @@ export const incompleteExpensesResponse = [ }, ]; -export const sentBackAdvancesResponse = [ - { - aggregates: [ - { - function_name: 'count(areq_id)', - function_value: 5, - }, - { - function_name: 'sum(areq_amount)', - function_value: 123371160, - }, - ], - dimensions: [], - name: 'scalar_stat', - }, -]; +export const sentBackAdvancesResponse = { + count: 5, + total_amount: 123370000, +};