Skip to content

Commit

Permalink
feat: move submit POST call to platform (#3116)
Browse files Browse the repository at this point in the history
  • Loading branch information
bistaastha authored and suyashpatil78 committed Jun 28, 2024
1 parent a5223df commit 68127a3
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 85 deletions.
38 changes: 38 additions & 0 deletions src/app/core/services/platform/v1/spender/reports.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,44 @@ describe('SpenderReportsService', () => {
});
});

it('create(): should create a new report', (done) => {
spyOn(spenderReportsService, 'createDraft').and.returnValue(of(expectedReportsSinglePage[0]));
spenderPlatformV1ApiService.post.and.returnValue(of(null));
spyOn(spenderReportsService, 'submit').and.returnValue(of(null));

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

spenderReportsService.create(reportPurpose, expenseIds).subscribe((res) => {
expect(res).toEqual(expectedReportsSinglePage[0]);
expect(spenderReportsService.createDraft).toHaveBeenCalledOnceWith({ data: reportPurpose });
expect(spenderPlatformV1ApiService.post).toHaveBeenCalledOnceWith('/reports/add_expenses', payload);
expect(spenderReportsService.submit).toHaveBeenCalledOnceWith(reportID);
done();
});
});

it('submit(): should submit a report', (done) => {
spenderPlatformV1ApiService.post.and.returnValue(of(null));

const reportID = 'rpvcIMRMyM3A';

spenderReportsService.submit(reportID).subscribe(() => {
expect(spenderPlatformV1ApiService.post).toHaveBeenCalledOnceWith(`/reports/submit`, { data: { id: reportID } });
done();
});
});

it('postComment(): should add a comment', (done) => {
const expectedCommentData: Comment = platformReportData.comments[0];
spenderPlatformV1ApiService.post.and.returnValue(of({ data: expectedCommentData }));
Expand Down
24 changes: 24 additions & 0 deletions src/app/core/services/platform/v1/spender/reports.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { TransactionService } from '../../../transaction.service';
import { PlatformReportsStatsResponse } from 'src/app/core/models/platform/v1/report-stats-response.model';
import { ReportPermissions } from 'src/app/core/models/report-permissions.model';
import { Comment } from 'src/app/core/models/platform/v1/comment.model';
import { ReportPurpose } from 'src/app/core/models/report-purpose.model';

const reportsCacheBuster$ = new Subject<void>();

Expand All @@ -40,6 +41,25 @@ export class SpenderReportsService {
return this.transactionService.clearCache();
}

@CacheBuster({
cacheBusterNotifier: reportsCacheBuster$,
})
create(report: ReportPurpose, expenseIds: string[]): Observable<Report> {
return this.createDraft({ data: report }).pipe(
switchMap((newReport: Report) => {
const payload = {
data: {
id: newReport.id,
expense_ids: expenseIds,
},
};
return this.spenderPlatformV1ApiService
.post<Report>('/reports/add_expenses', payload)
.pipe(switchMap(() => this.submit(newReport.id).pipe(map(() => newReport))));
})
);
}

@CacheBuster({
cacheBusterNotifier: reportsCacheBuster$,
})
Expand Down Expand Up @@ -103,6 +123,10 @@ export class SpenderReportsService {
.pipe(map((res) => res.data.purpose));
}

submit(reportId: string): Observable<void> {
return this.spenderPlatformV1ApiService.post<void>('/reports/submit', { data: { id: reportId } });
}

getAllReportsByParams(queryParams: ReportsQueryParams): Observable<Report[]> {
return this.getReportsCount(queryParams).pipe(
switchMap((count) => {
Expand Down
40 changes: 0 additions & 40 deletions src/app/core/services/report.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,6 @@ describe('ReportService', () => {
});
});

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

const reportID = 'rpvcIMRMyM3A';

reportService.submit(reportID).subscribe(() => {
expect(apiService.post).toHaveBeenCalledOnceWith(`/reports/${reportID}/submit`);
expect(reportService.clearTransactionCache).toHaveBeenCalledTimes(1);
done();
});
});

it('getExports(): should get export actions for a report', (done) => {
apiService.get.and.returnValue(of([]));

Expand All @@ -222,33 +209,6 @@ describe('ReportService', () => {
});
});

it('create(): should create a new report', (done) => {
spyOn(spenderReportsService, 'createDraft').and.returnValue(of(expectedReportsSinglePage[0]));
spenderPlatformV1ApiService.post.and.returnValue(of(null));
spyOn(reportService, 'submit').and.returnValue(of(null));

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

reportService.create(reportPurpose, expenseIds).subscribe((res) => {
expect(res).toEqual(expectedReportsSinglePage[0]);
expect(spenderReportsService.createDraft).toHaveBeenCalledOnceWith({ data: reportPurpose });
expect(spenderPlatformV1ApiService.post).toHaveBeenCalledOnceWith('/reports/add_expenses', payload);
expect(reportService.submit).toHaveBeenCalledOnceWith(reportID);
done();
});
});

it('resubmit(): should resubmit a report', (done) => {
apiService.post.and.returnValue(of(null));

Expand Down
29 changes: 0 additions & 29 deletions src/app/core/services/report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { PdfExport } from '../models/pdf-exports.model';
import { Report } from '../models/platform/v1/report.model';
import { ReportAutoSubmissionDetails } from '../models/report-auto-submission-details.model';
import { ReportPermission } from '../models/report-permission.model';
import { ReportPurpose } from '../models/report-purpose.model';
import { ApproverPlatformApiService } from './approver-platform-api.service';
import { ApiV2Service } from './api-v2.service';
import { ApiService } from './api.service';
Expand Down Expand Up @@ -68,34 +67,6 @@ export class ReportService {
return this.transactionService.clearCache();
}

@CacheBuster({
cacheBusterNotifier: reportsCacheBuster$,
})
create(report: ReportPurpose, expenseIds: string[]): Observable<Report> {
return this.spenderReportsService.createDraft({ data: report }).pipe(
switchMap((newReport: Report) => {
const payload = {
data: {
id: newReport.id,
expense_ids: expenseIds,
},
};
return this.spenderPlatformV1ApiService
.post<Report>('/reports/add_expenses', payload)
.pipe(switchMap(() => this.submit(newReport.id).pipe(map(() => newReport))));
})
);
}

@CacheBuster({
cacheBusterNotifier: reportsCacheBuster$,
})
submit(rptId: string): Observable<void> {
return this.apiService
.post<void>('/reports/' + rptId + '/submit')
.pipe(switchMap((res) => this.clearTransactionCache().pipe(map(() => res))));
}

@CacheBuster({
cacheBusterNotifier: reportsCacheBuster$,
})
Expand Down
6 changes: 3 additions & 3 deletions src/app/fyle/my-create-report/my-create-report.page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ describe('MyCreateReportPage', () => {
const reportServiceSpy = jasmine.createSpyObj('ReportService', [
'createDraft',
'addTransactions',
'create',
'getReportPurpose',
]);
const currencyServiceSpy = jasmine.createSpyObj('CurrencyService', ['getHomeCurrency']);
Expand All @@ -70,6 +69,7 @@ describe('MyCreateReportPage', () => {
'createDraft',
'getReportsCount',
'suggestPurpose',
'create',
]);

TestBed.configureTestingModule({
Expand Down Expand Up @@ -270,14 +270,14 @@ describe('MyCreateReportPage', () => {
});

it('should create report', () => {
reportService.create.and.returnValue(of(expectedReportsSinglePage[0]));
spenderReportsService.create.and.returnValue(of(expectedReportsSinglePage[0]));
component.selectedElements = cloneDeep(readyToReportExpensesData);
fixture.detectChanges();

component.ctaClickedEvent('create_report');

expect(component.sendFirstReportCreated).toHaveBeenCalledTimes(1);
expect(reportService.create).toHaveBeenCalledOnceWith(
expect(spenderReportsService.create).toHaveBeenCalledOnceWith(
{
purpose: component.reportTitle,
source: 'MOBILE',
Expand Down
2 changes: 1 addition & 1 deletion src/app/fyle/my-create-report/my-create-report.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class MyCreateReportPage implements OnInit {
.subscribe(noop);
} else {
this.saveReportLoading = true;
this.reportService
this.spenderReportsService
.create(report, expenseIDs)
.pipe(
tap(() =>
Expand Down
5 changes: 3 additions & 2 deletions src/app/fyle/my-view-report/my-view-report.page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ describe('MyViewReportPage', () => {
'getReportById',
'permissions',
'postComment',
'submit',
]);

TestBed.configureTestingModule({
Expand Down Expand Up @@ -650,14 +651,14 @@ describe('MyViewReportPage', () => {
},
duration: 3000,
};
reportService.submit.and.returnValue(of(null));
spenderReportsService.submit.and.returnValue(of(null));
matSnackBar.openFromComponent.and.callThrough();
snackbarProperties.setSnackbarProperties.and.returnValue(properties);

const submitButton = getElementBySelector(fixture, '.fy-footer-cta--primary') as HTMLElement;
click(submitButton);

expect(reportService.submit).toHaveBeenCalledWith(component.reportId);
expect(spenderReportsService.submit).toHaveBeenCalledWith(component.reportId);
expect(refinerService.startSurvey).toHaveBeenCalledOnceWith({ actionName: 'Submit Report' });
expect(router.navigate).toHaveBeenCalledOnceWith(['/', 'enterprise', 'my_reports']);
expect(matSnackBar.openFromComponent).toHaveBeenCalledOnceWith(ToastMessageComponent, {
Expand Down
2 changes: 1 addition & 1 deletion src/app/fyle/my-view-report/my-view-report.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export class MyViewReportPage {
}

submitReport(): void {
this.reportService.submit(this.reportId).subscribe(() => {
this.spenderReportsService.submit(this.reportId).subscribe(() => {
this.refinerService.startSurvey({ actionName: 'Submit Report' });
this.router.navigate(['/', 'enterprise', 'my_reports']);
const message = `Report submitted successfully.`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ describe('CreateNewReportComponent', () => {

beforeEach(waitForAsync(() => {
modalController = jasmine.createSpyObj('ModalController', ['dismiss']);
reportService = jasmine.createSpyObj('ReportService', [
'getReportPurpose',
'createDraft',
'addTransactions',
'create',
]);
reportService = jasmine.createSpyObj('ReportService', ['getReportPurpose', 'createDraft', 'addTransactions']);
trackingService = jasmine.createSpyObj('TrackingService', ['createReport']);
refinerService = jasmine.createSpyObj('RefinerService', ['startSurvey']);
currencyService = jasmine.createSpyObj('CurrencyService', ['getHomeCurrency']);
Expand All @@ -49,6 +44,7 @@ describe('CreateNewReportComponent', () => {
'addExpenses',
'createDraft',
'suggestPurpose',
'create',
]);
const humanizeCurrencyPipeSpy = jasmine.createSpyObj('HumanizeCurrency', ['transform']);
const fyCurrencyPipeSpy = jasmine.createSpyObj('FyCurrencyPipe', ['transform']);
Expand Down Expand Up @@ -263,13 +259,13 @@ describe('CreateNewReportComponent', () => {

const txnIds = ['txDDLtRaflUW', 'tx5WDG9lxBDT'];
const report = expectedReportsSinglePage[0];
reportService.create.and.returnValue(of(expectedReportsSinglePage[0]));
spenderReportsService.create.and.returnValue(of(expectedReportsSinglePage[0]));
component.ctaClickedEvent('submit_report');
fixture.detectChanges();
tick(500);
expect(component.submitReportLoader).toBeFalse();
expect(component.showReportNameError).toBeFalse();
expect(reportService.create).toHaveBeenCalledOnceWith(reportPurpose, txnIds);
expect(spenderReportsService.create).toHaveBeenCalledOnceWith(reportPurpose, txnIds);
expect(refinerService.startSurvey).toHaveBeenCalledOnceWith({ actionName: 'Submit Newly Created Report' });
expect(component.submitReportLoader).toBeFalse();
expect(modalController.dismiss).toHaveBeenCalledOnceWith({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class CreateNewReportComponent implements OnInit {
});
} else {
this.submitReportLoader = true;
this.reportService
this.spenderReportsService
.create(report, txnIds)
.pipe(
tap(() => {
Expand Down

0 comments on commit 68127a3

Please sign in to comment.