Skip to content

Commit

Permalink
feat: Move Advance request stats call to platform (#2843)
Browse files Browse the repository at this point in the history
  • Loading branch information
bistaastha authored Apr 3, 2024
1 parent bdd7b67 commit f76e865
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 108 deletions.
20 changes: 0 additions & 20 deletions src/app/core/mock-data/stats-dimension-response.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,3 @@ export const expectedReportRawStats: StatsDimensionResponse[] = [
name: 'scalar_stat',
},
];

export const apiAdvanceReqRes: Partial<ApiV2Response<StatsDimensionResponse>> = {
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',
};
4 changes: 4 additions & 0 deletions src/app/core/models/platform/v1/stats-response.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface StatsResponse {
count: number;
total_amount: number;
}
52 changes: 19 additions & 33 deletions src/app/core/services/advance-request.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -64,6 +64,7 @@ describe('AdvanceRequestService', () => {
let fileService: jasmine.SpyObj<FileService>;
let orgUserSettingsService: jasmine.SpyObj<OrgUserSettingsService>;
let timezoneService: jasmine.SpyObj<TimezoneService>;
let spenderService: jasmine.SpyObj<SpenderService>;

beforeEach(() => {
const apiServiceSpy = jasmine.createSpyObj('ApiService', ['get', 'post', 'delete']);
Expand All @@ -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: [
Expand Down Expand Up @@ -106,6 +108,10 @@ describe('AdvanceRequestService', () => {
provide: TimezoneService,
useValue: timezoneServiceSpy,
},
{
provide: SpenderService,
useValue: spenderServiceSpy,
},
],
});
advanceRequestService = TestBed.inject(AdvanceRequestService);
Expand All @@ -117,6 +123,7 @@ describe('AdvanceRequestService', () => {
fileService = TestBed.inject(FileService) as jasmine.SpyObj<FileService>;
orgUserSettingsService = TestBed.inject(OrgUserSettingsService) as jasmine.SpyObj<OrgUserSettingsService>;
timezoneService = TestBed.inject(TimezoneService) as jasmine.SpyObj<TimezoneService>;
spenderService = TestBed.inject(SpenderService) as jasmine.SpyObj<SpenderService>;
});

it('should be created', () => {
Expand Down Expand Up @@ -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();
Expand Down
36 changes: 14 additions & 22 deletions src/app/core/services/advance-request.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>();

Expand All @@ -45,10 +48,7 @@ type Config = Partial<{
}>;

type advanceRequestStat = {
aggregates: string;
areq_state: string;
areq_is_sent_back: string;
scalar: boolean;
state: string;
};

@Injectable({
Expand All @@ -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({
Expand Down Expand Up @@ -366,13 +367,6 @@ export class AdvanceRequestService {
);
}

getMyAdvanceRequestStats(params: advanceRequestStat): Observable<Partial<StatsDimensionResponse[]>> {
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) {
Expand All @@ -394,16 +388,14 @@ export class AdvanceRequestService {
return order;
}

private getAdvanceRequestStats(
eou: ExtendedOrgUser,
params: advanceRequestStat
): Observable<Partial<ApiV2Response<StatsDimensionResponse>>> {
return this.apiv2Service.get<StatsDimensionResponse, {}>('/advance_requests/stats', {
params: {
areq_org_user_id: 'eq.' + eou.ou.id,
...params,
},
});
getAdvanceRequestStats(params: advanceRequestStat): Observable<StatsResponse> {
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<Approval[]> {
Expand Down
16 changes: 8 additions & 8 deletions src/app/core/services/tasks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
);
Expand Down
20 changes: 11 additions & 9 deletions src/app/core/services/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -451,12 +452,9 @@ export class TasksService {
});
}

getSentBackAdvancesStats(): Observable<Datum[]> {
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<StatsResponse> {
return this.advancesRequestService.getAdvanceRequestStats({
state: 'eq.SENT_BACK',
});
}

Expand All @@ -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);
})
);
}

Expand Down
20 changes: 4 additions & 16 deletions src/app/core/test-data/tasks.service.spec.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit f76e865

Please sign in to comment.