diff --git a/src/app/core/services/platform/v1/spender/reports.service.spec.ts b/src/app/core/services/platform/v1/spender/reports.service.spec.ts index 1c6087a104..d554a85f36 100644 --- a/src/app/core/services/platform/v1/spender/reports.service.spec.ts +++ b/src/app/core/services/platform/v1/spender/reports.service.spec.ts @@ -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 })); diff --git a/src/app/core/services/platform/v1/spender/reports.service.ts b/src/app/core/services/platform/v1/spender/reports.service.ts index 705ac8ec4c..b76d8240a7 100644 --- a/src/app/core/services/platform/v1/spender/reports.service.ts +++ b/src/app/core/services/platform/v1/spender/reports.service.ts @@ -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(); @@ -40,6 +41,25 @@ export class SpenderReportsService { return this.transactionService.clearCache(); } + @CacheBuster({ + cacheBusterNotifier: reportsCacheBuster$, + }) + create(report: ReportPurpose, expenseIds: string[]): Observable { + return this.createDraft({ data: report }).pipe( + switchMap((newReport: Report) => { + const payload = { + data: { + id: newReport.id, + expense_ids: expenseIds, + }, + }; + return this.spenderPlatformV1ApiService + .post('/reports/add_expenses', payload) + .pipe(switchMap(() => this.submit(newReport.id).pipe(map(() => newReport)))); + }) + ); + } + @CacheBuster({ cacheBusterNotifier: reportsCacheBuster$, }) @@ -103,6 +123,10 @@ export class SpenderReportsService { .pipe(map((res) => res.data.purpose)); } + submit(reportId: string): Observable { + return this.spenderPlatformV1ApiService.post('/reports/submit', { data: { id: reportId } }); + } + getAllReportsByParams(queryParams: ReportsQueryParams): Observable { return this.getReportsCount(queryParams).pipe( switchMap((count) => { diff --git a/src/app/core/services/report.service.spec.ts b/src/app/core/services/report.service.spec.ts index 93af441660..5d64ad15db 100644 --- a/src/app/core/services/report.service.spec.ts +++ b/src/app/core/services/report.service.spec.ts @@ -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([])); @@ -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)); diff --git a/src/app/core/services/report.service.ts b/src/app/core/services/report.service.ts index 30af802f90..12a499a50b 100644 --- a/src/app/core/services/report.service.ts +++ b/src/app/core/services/report.service.ts @@ -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'; @@ -68,34 +67,6 @@ export class ReportService { return this.transactionService.clearCache(); } - @CacheBuster({ - cacheBusterNotifier: reportsCacheBuster$, - }) - create(report: ReportPurpose, expenseIds: string[]): Observable { - return this.spenderReportsService.createDraft({ data: report }).pipe( - switchMap((newReport: Report) => { - const payload = { - data: { - id: newReport.id, - expense_ids: expenseIds, - }, - }; - return this.spenderPlatformV1ApiService - .post('/reports/add_expenses', payload) - .pipe(switchMap(() => this.submit(newReport.id).pipe(map(() => newReport)))); - }) - ); - } - - @CacheBuster({ - cacheBusterNotifier: reportsCacheBuster$, - }) - submit(rptId: string): Observable { - return this.apiService - .post('/reports/' + rptId + '/submit') - .pipe(switchMap((res) => this.clearTransactionCache().pipe(map(() => res)))); - } - @CacheBuster({ cacheBusterNotifier: reportsCacheBuster$, }) diff --git a/src/app/fyle/my-create-report/my-create-report.page.spec.ts b/src/app/fyle/my-create-report/my-create-report.page.spec.ts index 2931aa64f1..b1a537be2b 100644 --- a/src/app/fyle/my-create-report/my-create-report.page.spec.ts +++ b/src/app/fyle/my-create-report/my-create-report.page.spec.ts @@ -55,7 +55,6 @@ describe('MyCreateReportPage', () => { const reportServiceSpy = jasmine.createSpyObj('ReportService', [ 'createDraft', 'addTransactions', - 'create', 'getReportPurpose', ]); const currencyServiceSpy = jasmine.createSpyObj('CurrencyService', ['getHomeCurrency']); @@ -70,6 +69,7 @@ describe('MyCreateReportPage', () => { 'createDraft', 'getReportsCount', 'suggestPurpose', + 'create', ]); TestBed.configureTestingModule({ @@ -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', diff --git a/src/app/fyle/my-create-report/my-create-report.page.ts b/src/app/fyle/my-create-report/my-create-report.page.ts index d7d0609b95..bcbbd1d30c 100644 --- a/src/app/fyle/my-create-report/my-create-report.page.ts +++ b/src/app/fyle/my-create-report/my-create-report.page.ts @@ -147,7 +147,7 @@ export class MyCreateReportPage implements OnInit { .subscribe(noop); } else { this.saveReportLoading = true; - this.reportService + this.spenderReportsService .create(report, expenseIDs) .pipe( tap(() => diff --git a/src/app/fyle/my-view-report/my-view-report.page.spec.ts b/src/app/fyle/my-view-report/my-view-report.page.spec.ts index 58500dd9da..6583aaf7a4 100644 --- a/src/app/fyle/my-view-report/my-view-report.page.spec.ts +++ b/src/app/fyle/my-view-report/my-view-report.page.spec.ts @@ -131,6 +131,7 @@ describe('MyViewReportPage', () => { 'getReportById', 'permissions', 'postComment', + 'submit', ]); TestBed.configureTestingModule({ @@ -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, { diff --git a/src/app/fyle/my-view-report/my-view-report.page.ts b/src/app/fyle/my-view-report/my-view-report.page.ts index 135f2f5427..0b0f88ff15 100644 --- a/src/app/fyle/my-view-report/my-view-report.page.ts +++ b/src/app/fyle/my-view-report/my-view-report.page.ts @@ -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.`; diff --git a/src/app/shared/components/create-new-report-v2/create-new-report.component.spec.ts b/src/app/shared/components/create-new-report-v2/create-new-report.component.spec.ts index 3a2eef0935..39be5e2f55 100644 --- a/src/app/shared/components/create-new-report-v2/create-new-report.component.spec.ts +++ b/src/app/shared/components/create-new-report-v2/create-new-report.component.spec.ts @@ -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']); @@ -49,6 +44,7 @@ describe('CreateNewReportComponent', () => { 'addExpenses', 'createDraft', 'suggestPurpose', + 'create', ]); const humanizeCurrencyPipeSpy = jasmine.createSpyObj('HumanizeCurrency', ['transform']); const fyCurrencyPipeSpy = jasmine.createSpyObj('FyCurrencyPipe', ['transform']); @@ -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({ diff --git a/src/app/shared/components/create-new-report-v2/create-new-report.component.ts b/src/app/shared/components/create-new-report-v2/create-new-report.component.ts index 22c775cc81..c7a94f7779 100644 --- a/src/app/shared/components/create-new-report-v2/create-new-report.component.ts +++ b/src/app/shared/components/create-new-report-v2/create-new-report.component.ts @@ -148,7 +148,7 @@ export class CreateNewReportComponent implements OnInit { }); } else { this.submitReportLoader = true; - this.reportService + this.spenderReportsService .create(report, txnIds) .pipe( tap(() => {