Skip to content

Commit

Permalink
fix: revert eject expenses changes (#2932)
Browse files Browse the repository at this point in the history
* Revert "fix: remove /txns from create report flow (#2906)"

This reverts commit 11064fa.

* Revert "feat: Add eject and add expenses from platform (#2875)"

This reverts commit e7c6d25.

* Update reports.service.ts

* Update report.service.ts

* Update reports.service.ts
  • Loading branch information
bistaastha committed Apr 30, 2024
1 parent 53c36e8 commit 7a4e7ad
Show file tree
Hide file tree
Showing 44 changed files with 251 additions and 431 deletions.
42 changes: 0 additions & 42 deletions src/app/core/services/platform/v1/approver/reports.service.spec.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/app/core/services/platform/v1/approver/reports.service.ts

This file was deleted.

85 changes: 22 additions & 63 deletions src/app/core/services/platform/v1/spender/reports.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing';
import { SpenderReportsService } from './reports.service';
import { ReportsService } from './reports.service';
import { SpenderPlatformV1ApiService } from '../../../spender-platform-v1-api.service';
import { of } from 'rxjs';
import { PAGINATION_SIZE } from 'src/app/constants';
Expand All @@ -13,50 +13,46 @@ import {
} from 'src/app/core/mock-data/platform-report.data';
import { ReportsQueryParams } from 'src/app/core/models/platform/v1/reports-query-params.model';

describe('SpenderReportsService', () => {
let spenderReportsService: SpenderReportsService;
let spenderPlatformV1ApiService: jasmine.SpyObj<SpenderPlatformV1ApiService>;
describe('ReportsService', () => {
let reportsService: ReportsService;
const spenderPlatformV1ApiServiceMock = jasmine.createSpyObj('SpenderPlatformV1ApiService', ['get']);

beforeEach(() => {
const spenderPlatformV1ApiServiceSpy = jasmine.createSpyObj('SpenderPlatformV1ApiService', ['get', 'post']);
TestBed.configureTestingModule({
providers: [
SpenderReportsService,
ReportsService,
{ provide: PAGINATION_SIZE, useValue: 2 },
{ provide: SpenderPlatformV1ApiService, useValue: spenderPlatformV1ApiServiceSpy },
{ provide: SpenderPlatformV1ApiService, useValue: spenderPlatformV1ApiServiceMock },
],
});
spenderReportsService = TestBed.inject(SpenderReportsService);
spenderPlatformV1ApiService = TestBed.inject(
SpenderPlatformV1ApiService
) as jasmine.SpyObj<SpenderPlatformV1ApiService>;
reportsService = TestBed.inject(ReportsService);
});

it('should be created', () => {
expect(spenderReportsService).toBeTruthy();
expect(reportsService).toBeTruthy();
});

it('getReportsCount(): should get a count of reports', (done) => {
// Mock the response of getReportsByParams
spyOn(spenderReportsService, 'getReportsByParams').and.returnValue(of(platformReportCountData));
spyOn(reportsService, 'getReportsByParams').and.returnValue(of(platformReportCountData));

const expectedParams: ReportsQueryParams = {
...mockQueryParams,
limit: 1,
offset: 0,
};

spenderReportsService.getReportsCount(mockQueryParams).subscribe((res) => {
reportsService.getReportsCount(mockQueryParams).subscribe((res) => {
// Verify
expect(res).toEqual(4); // Check if the count is as expected
expect(spenderReportsService.getReportsByParams).toHaveBeenCalledWith(expectedParams); // Check if the method is called with the expected params
expect(reportsService.getReportsByParams).toHaveBeenCalledWith(expectedParams); // Check if the method is called with the expected params
done(); // Call 'done' to indicate the end of the asynchronous test
});
});

it('getAllReportsByParams(): should get all reports multiple pages', (done) => {
const getReportsByParams = spyOn(spenderReportsService, 'getReportsByParams');
spyOn(spenderReportsService, 'getReportsCount').and.returnValue(of(4));
const getReportsByParams = spyOn(reportsService, 'getReportsByParams');
spyOn(reportsService, 'getReportsCount').and.returnValue(of(4));

const expectedParams1: ReportsQueryParams = {
...mockQueryParams,
Expand All @@ -72,9 +68,9 @@ describe('SpenderReportsService', () => {
getReportsByParams.withArgs(expectedParams1).and.returnValue(of(allReportsPaginated1));
getReportsByParams.withArgs(expectedParams2).and.returnValue(of(allReportsPaginated2));

spenderReportsService.getAllReportsByParams(mockQueryParams).subscribe((res) => {
reportsService.getAllReportsByParams(mockQueryParams).subscribe((res) => {
expect(res).toEqual(expectedReportsPaginated);
expect(spenderReportsService.getReportsCount).toHaveBeenCalledOnceWith({
expect(reportsService.getReportsCount).toHaveBeenCalledOnceWith({
state: 'in.(DRAFT,APPROVER_PENDING,APPROVER_INQUIRY)',
});
expect(getReportsByParams).toHaveBeenCalledWith(expectedParams1);
Expand All @@ -85,8 +81,8 @@ describe('SpenderReportsService', () => {
});

it('getAllReportsByParams(): should get all reports single page', (done) => {
const getReportsByParams = spyOn(spenderReportsService, 'getReportsByParams');
spyOn(spenderReportsService, 'getReportsCount').and.returnValue(of(1));
const getReportsByParams = spyOn(reportsService, 'getReportsByParams');
spyOn(reportsService, 'getReportsCount').and.returnValue(of(1));

const expectedParams: ReportsQueryParams = {
...mockQueryParams,
Expand All @@ -96,9 +92,9 @@ describe('SpenderReportsService', () => {

getReportsByParams.withArgs(expectedParams).and.returnValue(of(allReportsPaginated1));

spenderReportsService.getAllReportsByParams(mockQueryParams).subscribe((res) => {
reportsService.getAllReportsByParams(mockQueryParams).subscribe((res) => {
expect(res).toEqual(expectedReportsSinglePage);
expect(spenderReportsService.getReportsCount).toHaveBeenCalledOnceWith(mockQueryParams);
expect(reportsService.getReportsCount).toHaveBeenCalledOnceWith(mockQueryParams);
expect(getReportsByParams).toHaveBeenCalledOnceWith(expectedParams);
done();
});
Expand All @@ -115,47 +111,10 @@ describe('SpenderReportsService', () => {
...queryParams,
},
};
spenderPlatformV1ApiService.get.and.returnValue(of(allReportsPaginated1));
spenderReportsService.getReportsByParams(queryParams).subscribe((response) => {
spenderPlatformV1ApiServiceMock.get.and.returnValue(of(allReportsPaginated1));
reportsService.getReportsByParams(queryParams).subscribe((response) => {
expect(response).toEqual(allReportsPaginated1);
expect(spenderPlatformV1ApiService.get).toHaveBeenCalledOnceWith('/reports', expectedConfig);
done();
});
});

it('addExpenses(): should add an expense to a report', (done) => {
spenderPlatformV1ApiService.post.and.returnValue(of(null));

const reportID = 'rpvcIMRMyM3A';
const txns = ['txTQVBx7W8EO'];

const payload = {
data: {
id: reportID,
expense_ids: txns,
},
};
spenderReportsService.addExpenses(reportID, txns).subscribe(() => {
expect(spenderPlatformV1ApiService.post).toHaveBeenCalledOnceWith('/reports/add_expenses', payload);
done();
});
});

it('ejectExpenses(): should remove an expense from a report', (done) => {
spenderPlatformV1ApiService.post.and.returnValue(of(null));

const reportID = 'rpvcIMRMyM3A';
const txns = ['txTQVBx7W8EO'];

const payload = {
data: {
id: reportID,
expense_ids: txns,
},
reason: undefined,
};
spenderReportsService.ejectExpenses(reportID, txns[0]).subscribe(() => {
expect(spenderPlatformV1ApiService.post).toHaveBeenCalledOnceWith('/reports/eject_expenses', payload);
expect(spenderPlatformV1ApiServiceMock.get).toHaveBeenCalledOnceWith('/reports', expectedConfig);
done();
});
});
Expand Down
31 changes: 5 additions & 26 deletions src/app/core/services/platform/v1/spender/reports.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Inject, Injectable } from '@angular/core';
import { Observable, Subject, from, of, range } from 'rxjs';
import { catchError, concatMap, map, mergeMap, reduce, switchMap, tap } from 'rxjs/operators';
import { Observable, range } from 'rxjs';
import { concatMap, map, reduce, switchMap } from 'rxjs/operators';
import { Report } from 'src/app/core/models/platform/v1/report.model';
import { SpenderPlatformV1ApiService } from '../../../spender-platform-v1-api.service';
import { PlatformApiResponse } from 'src/app/core/models/platform/platform-api-response.model';
Expand All @@ -12,7 +12,7 @@ import { PlatformApiPayload } from 'src/app/core/models/platform/platform-api-pa
@Injectable({
providedIn: 'root',
})
export class SpenderReportsService {
export class ReportsService {
constructor(
@Inject(PAGINATION_SIZE) private paginationSize: number,
private spenderPlatformV1ApiService: SpenderPlatformV1ApiService
Expand All @@ -25,7 +25,7 @@ export class SpenderReportsService {
return range(0, count);
}),
concatMap((page) => {
let params = {
const params = {
state: queryParams.state,
offset: this.paginationSize * page,
limit: this.paginationSize,
Expand All @@ -37,7 +37,7 @@ export class SpenderReportsService {
}

getReportsCount(queryParams: ReportsQueryParams): Observable<number> {
let params = {
const params = {
state: queryParams.state,
limit: 1,
offset: 0,
Expand All @@ -59,25 +59,4 @@ export class SpenderReportsService {
.post<PlatformApiPayload<Report>>('/reports', data)
.pipe(map((res) => res.data));
}

ejectExpenses(rptId: string, txnId: string, comment?: string[]): Observable<void> {
const payload = {
data: {
id: rptId,
expense_ids: [txnId],
},
reason: comment,
};
return this.spenderPlatformV1ApiService.post<void>('/reports/eject_expenses', payload);
}

addExpenses(rptId: string, expenseIds: string[]): Observable<void> {
const payload = {
data: {
id: rptId,
expense_ids: expenseIds,
},
};
return this.spenderPlatformV1ApiService.post<void>('/reports/add_expenses', payload);
}
}
49 changes: 40 additions & 9 deletions src/app/core/services/report.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,26 @@ describe('ReportService', () => {
});
});

it('removeTransaction(): should remove a transaction from report', (done) => {
apiService.post.and.returnValue(of(null));
spyOn(reportService, 'clearTransactionCache').and.returnValue(of(null));

const reportID = 'rpvcIMRMyM3A';
const txnID = 'txTQVBx7W8EO';

const params = {
status: {
comment: null,
},
};

reportService.removeTransaction(reportID, txnID, null).subscribe(() => {
expect(apiService.post).toHaveBeenCalledOnceWith(`/reports/${reportID}/txns/${txnID}/remove`, params);
expect(reportService.clearTransactionCache).toHaveBeenCalledTimes(1);
done();
});
});

describe('getMyReports()', () => {
it('should get reports from API as specified by params', (done) => {
authService.getEou.and.returnValue(Promise.resolve(apiEouRes));
Expand Down Expand Up @@ -496,6 +516,20 @@ describe('ReportService', () => {
});
});

it('addTransactions(): should add a transaction to a report', (done) => {
apiService.post.and.returnValue(of(null));
spyOn(reportService, 'clearTransactionCache').and.returnValue(of(null));

const reportID = 'rpvcIMRMyM3A';
const tnxs = ['txTQVBx7W8EO'];

reportService.addTransactions(reportID, tnxs).subscribe(() => {
expect(apiService.post).toHaveBeenCalledOnceWith(`/reports/${reportID}/txns`, { ids: tnxs });
expect(reportService.clearTransactionCache).toHaveBeenCalledTimes(1);
done();
});
});

it('actions(): should get report actions', (done) => {
apiService.get.and.returnValue(of(apiReportActions));

Expand All @@ -521,26 +555,23 @@ describe('ReportService', () => {

it('create(): should create a new report', (done) => {
spyOn(reportService, 'createDraft').and.returnValue(of(reportUnflattenedData2));
spenderPlatformV1ApiService.post.and.returnValue(of(null));
apiService.post.and.returnValue(of(null));
spyOn(reportService, 'submit').and.returnValue(of(null));

const reportPurpose = {
purpose: 'A new report',
source: 'MOBILE',
};
const expenseIds = ['tx6Oe6FaYDZl'];
const txnIds = ['tx6Oe6FaYDZl'];
const reportID = 'rp5eUkeNm9wB';
const payload = {
data: {
id: reportID,
expense_ids: expenseIds,
},
const txnParam = {
ids: txnIds,
};

reportService.create(reportPurpose, expenseIds).subscribe((res) => {
reportService.create(reportPurpose, txnIds).subscribe((res) => {
expect(res).toEqual(reportUnflattenedData2);
expect(reportService.createDraft).toHaveBeenCalledOnceWith(reportPurpose);
expect(spenderPlatformV1ApiService.post).toHaveBeenCalledOnceWith('/reports/add_expenses', payload);
expect(apiService.post).toHaveBeenCalledOnceWith(`/reports/${reportID}/txns`, txnParam);
expect(reportService.submit).toHaveBeenCalledOnceWith(reportID);
done();
});
Expand Down
Loading

0 comments on commit 7a4e7ad

Please sign in to comment.