From 3fdcbb9bf521f273c2c733c87a1b3b0ef3cb246c Mon Sep 17 00:00:00 2001 From: Aastha Bist Date: Fri, 28 Jun 2024 10:14:48 +0530 Subject: [PATCH] feat: Move suggest purpose to platform (#3103) --- .../v1/spender/reports.service.spec.ts | 13 +++++++ .../platform/v1/spender/reports.service.ts | 6 ++++ src/app/core/services/report.service.spec.ts | 36 ------------------- src/app/core/services/report.service.ts | 22 ------------ .../my-create-report.page.spec.ts | 10 +++--- .../my-create-report/my-create-report.page.ts | 2 +- .../create-new-report.component.spec.ts | 18 ++++++---- .../create-new-report.component.ts | 2 +- .../fy-add-to-report.component.spec.ts | 5 +-- .../fy-add-to-report.component.ts | 2 +- 10 files changed, 42 insertions(+), 74 deletions(-) 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 8301edb063..1c6087a104 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 @@ -82,6 +82,19 @@ describe('SpenderReportsService', () => { }); }); + it('suggestPurpose(): should get the purpose of the report', (done) => { + const reportData = { data: { purpose: ' #7: Jan 2023' } }; + spenderPlatformV1ApiService.post.and.returnValue(of(reportData)); + + spenderReportsService.suggestPurpose([]).subscribe((res) => { + expect(res).toEqual(reportData.data.purpose); + expect(spenderPlatformV1ApiService.post).toHaveBeenCalledOnceWith('/reports/suggest_purpose', { + data: { expense_ids: [] }, + }); + 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 aad71c1c84..705ac8ec4c 100644 --- a/src/app/core/services/platform/v1/spender/reports.service.ts +++ b/src/app/core/services/platform/v1/spender/reports.service.ts @@ -97,6 +97,12 @@ export class SpenderReportsService { .pipe(map((res) => res.data)); } + suggestPurpose(expenseIds: string[]): Observable { + return this.spenderPlatformV1ApiService + .post>('/reports/suggest_purpose', { data: { expense_ids: expenseIds } }) + .pipe(map((res) => res.data.purpose)); + } + 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 d1dad1dcbb..93af441660 100644 --- a/src/app/core/services/report.service.spec.ts +++ b/src/app/core/services/report.service.spec.ts @@ -354,20 +354,6 @@ describe('ReportService', () => { }); }); - it('updateReportDetails(): should update a report name', (done) => { - apiService.post.and.returnValue(of(apiReportUpdatedDetails)); - dataTransformService.unflatten.and.returnValue(dataErtpTransformed); - spyOn(reportService, 'clearTransactionCache').and.returnValue(of(null)); - - reportService.updateReportDetails(reportParam).subscribe((res) => { - expect(res).toEqual(apiReportUpdatedDetails); - expect(apiService.post).toHaveBeenCalledOnceWith('/reports', apiErptReporDataParam.rp); - expect(reportService.clearTransactionCache).toHaveBeenCalledTimes(1); - expect(dataTransformService.unflatten).toHaveBeenCalledOnceWith(reportParam); - done(); - }); - }); - it('updateReportPurpose(): should update the report purpose', (done) => { spenderPlatformV1ApiService.post.and.returnValue(of(platformReportData)); reportService.updateReportPurpose(platformReportData).subscribe((res) => { @@ -413,17 +399,6 @@ describe('ReportService', () => { }); }); - it('getApproversByReportId(): should get the approvers of a report', (done) => { - apiService.get.and.returnValue(of(apiApproverRes)); - const reportID = 'rphNNUiCISkD'; - - reportService.getApproversByReportId(reportID).subscribe((res) => { - expect(res).toEqual(apiApproverRes); - expect(apiService.get).toHaveBeenCalledOnceWith(`/reports/${reportID}/approvers`); - done(); - }); - }); - it('downloadSummaryPdfUrl(): allow a user to share the report', (done) => { const data = { report_ids: ['rp5eUkeNm9wB'], @@ -443,15 +418,4 @@ describe('ReportService', () => { done(); }); }); - - it('getReportPurpose(): should get the purpose of the report', (done) => { - const reportName = ' #7: Jan 2023'; - apiService.post.and.returnValue(of(apiEmptyReportRes)); - - reportService.getReportPurpose({ ids: [] }).subscribe((res) => { - expect(res).toEqual(reportName); - expect(apiService.post).toHaveBeenCalledOnceWith('/reports/purpose', { ids: [] }); - done(); - }); - }); }); diff --git a/src/app/core/services/report.service.ts b/src/app/core/services/report.service.ts index 09b2987184..30af802f90 100644 --- a/src/app/core/services/report.service.ts +++ b/src/app/core/services/report.service.ts @@ -10,11 +10,7 @@ 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 { UnflattenedReport } from '../models/report-unflattened.model'; -import { ReportV1 } from '../models/report-v1.model'; import { ApproverPlatformApiService } from './approver-platform-api.service'; -import { ExtendedReport } from '../models/report.model'; -import { Approver } from '../models/v1/approver.model'; import { ApiV2Service } from './api-v2.service'; import { ApiService } from './api.service'; import { AuthService } from './auth.service'; @@ -140,16 +136,6 @@ export class ReportService { return this.apiService.post('/reports/' + rptId + '/approvals', data); } - @CacheBuster({ - cacheBusterNotifier: reportsCacheBuster$, - }) - updateReportDetails(erpt: ExtendedReport): Observable { - const reportData = this.dataTransformService.unflatten(erpt); - return this.apiService - .post('/reports', reportData.rp) - .pipe(switchMap((res) => this.clearTransactionCache().pipe(map(() => res)))); - } - @CacheBuster({ cacheBusterNotifier: reportsCacheBuster$, }) @@ -206,10 +192,6 @@ export class ReportService { return this.apiService.get<{ results: PdfExport[] }>('/reports/' + rptId + '/exports'); } - getApproversByReportId(rptId: string): Observable { - return this.apiService.get('/reports/' + rptId + '/approvers'); - } - delete(rptId: string): Observable { return this.apiService .delete('/reports/' + rptId) @@ -220,10 +202,6 @@ export class ReportService { return this.apiService.post('/reports/summary/download', data); } - getReportPurpose(reportPurpose: { ids: string[] }): Observable { - return this.apiService.post('/reports/purpose', reportPurpose).pipe(map((res) => res.purpose)); - } - approverUpdateReportPurpose(report: Report): Observable { const params: { data: Pick } = { data: { 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 e28cb0f110..2931aa64f1 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 @@ -69,6 +69,7 @@ describe('MyCreateReportPage', () => { 'addExpenses', 'createDraft', 'getReportsCount', + 'suggestPurpose', ]); TestBed.configureTestingModule({ @@ -359,7 +360,7 @@ describe('MyCreateReportPage', () => { it('getReportTitle(): get report title', fakeAsync(() => { component.selectedElements = cloneDeep(readyToReportExpensesData); spyOn(component, 'getTotalSelectedExpensesAmount').and.returnValue(150); - reportService.getReportPurpose.and.returnValue(of('#Sept 24')); + spenderReportsService.suggestPurpose.and.returnValue(of('#Sept 24')); const el = getElementBySelector(fixture, "[data-testid='report-name']") as HTMLInputElement; el.value = 'New Report'; el.dispatchEvent(new Event('input')); @@ -373,9 +374,10 @@ describe('MyCreateReportPage', () => { component.getReportTitle(); - expect(reportService.getReportPurpose).toHaveBeenCalledOnceWith({ - ids: [readyToReportExpensesData[0].id, readyToReportExpensesData[1].id], - }); + expect(spenderReportsService.suggestPurpose).toHaveBeenCalledOnceWith([ + readyToReportExpensesData[0].id, + readyToReportExpensesData[1].id, + ]); expect(component.reportTitle).toEqual('#Sept 24'); expect(component.getTotalSelectedExpensesAmount).toHaveBeenCalledOnceWith(component.selectedElements); })); 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 6a7de043db..d7d0609b95 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 @@ -193,7 +193,7 @@ export class MyCreateReportPage implements OnInit { this.selectedTotalAmount = this.getTotalSelectedExpensesAmount(this.selectedElements); if (this.reportTitleInput && !this.reportTitleInput.dirty) { - return this.reportService.getReportPurpose({ ids: expenseIDs }).subscribe((res) => { + return this.spenderReportsService.suggestPurpose(expenseIDs).subscribe((res) => { this.reportTitle = res; }); } 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 f0f92390a2..3a2eef0935 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 @@ -45,7 +45,11 @@ describe('CreateNewReportComponent', () => { refinerService = jasmine.createSpyObj('RefinerService', ['startSurvey']); currencyService = jasmine.createSpyObj('CurrencyService', ['getHomeCurrency']); expenseFieldsService = jasmine.createSpyObj('ExpenseFieldsService', ['getAllMap']); - spenderReportsService = jasmine.createSpyObj('SpenderReportsService', ['addExpenses', 'createDraft']); + spenderReportsService = jasmine.createSpyObj('SpenderReportsService', [ + 'addExpenses', + 'createDraft', + 'suggestPurpose', + ]); const humanizeCurrencyPipeSpy = jasmine.createSpyObj('HumanizeCurrency', ['transform']); const fyCurrencyPipeSpy = jasmine.createSpyObj('FyCurrencyPipe', ['transform']); @@ -100,32 +104,32 @@ describe('CreateNewReportComponent', () => { it('should get the report title', () => { const reportName = '#1: Jul 2021'; component.selectedElements = apiExpenses1; - reportService.getReportPurpose.and.returnValue(of(reportName)); + spenderReportsService.suggestPurpose.and.returnValue(of(reportName)); component.getReportTitle(); fixture.detectChanges(); expect(component.reportTitle).toEqual(reportName); - expect(reportService.getReportPurpose).toHaveBeenCalledOnceWith({ ids: ['txDDLtRaflUW', 'tx5WDG9lxBDT'] }); + expect(spenderReportsService.suggestPurpose).toHaveBeenCalledOnceWith(['txDDLtRaflUW', 'tx5WDG9lxBDT']); }); it('should not get the report title when the element is not in the selectedElements array', () => { const reportName = '#1: Jul 2021'; component.selectedElements = [apiExpenses1[0]]; - reportService.getReportPurpose.and.returnValue(of(reportName)); + spenderReportsService.suggestPurpose.and.returnValue(of(reportName)); component.getReportTitle(); fixture.detectChanges(); expect(component.reportTitle).toEqual(reportName); - expect(reportService.getReportPurpose).toHaveBeenCalledOnceWith({ ids: ['txDDLtRaflUW'] }); + expect(spenderReportsService.suggestPurpose).toHaveBeenCalledOnceWith(['txDDLtRaflUW']); }); it('should get report title without reimbursable amount', () => { const reportName = '#1: Jul 2021'; component.selectedElements = [nonReimbursableExpense]; - reportService.getReportPurpose.and.returnValue(of(reportName)); + spenderReportsService.suggestPurpose.and.returnValue(of(reportName)); component.getReportTitle(); fixture.detectChanges(); expect(component.reportTitle).toEqual(reportName); - expect(reportService.getReportPurpose).toHaveBeenCalledOnceWith({ ids: [nonReimbursableExpense.id] }); + expect(spenderReportsService.suggestPurpose).toHaveBeenCalledOnceWith([nonReimbursableExpense.id]); }); }); 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 63f42bea70..22c775cc81 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 @@ -59,7 +59,7 @@ export class CreateNewReportComponent implements OnInit { ); if (this.reportTitleInput && !this.reportTitleInput.dirty && txnIds.length > 0) { - return this.reportService.getReportPurpose({ ids: txnIds }).subscribe((res) => { + return this.spenderReportsService.suggestPurpose(txnIds).subscribe((res) => { this.reportTitle = res; }); } diff --git a/src/app/shared/components/fy-add-to-report/fy-add-to-report.component.spec.ts b/src/app/shared/components/fy-add-to-report/fy-add-to-report.component.spec.ts index 3425cec426..7c2ae3b98c 100644 --- a/src/app/shared/components/fy-add-to-report/fy-add-to-report.component.spec.ts +++ b/src/app/shared/components/fy-add-to-report/fy-add-to-report.component.spec.ts @@ -41,6 +41,7 @@ describe('FyAddToReportComponent', () => { 'getReportsCount', 'getReportsByParams', 'createDraft', + 'suggestPurpose', ]); const modalControllerSpy = jasmine.createSpyObj('ModalController', ['create']); const modalPropertiesSpy = jasmine.createSpyObj('ModalPropertiesService', ['getModalDefaultProperties']); @@ -169,7 +170,7 @@ describe('FyAddToReportComponent', () => { component.options = reportOptionsData; component.value = expectedReportsPaginated[0]; modalProperties.getModalDefaultProperties.and.returnValue(properties); - reportService.getReportPurpose.and.returnValue(of('Client Meeting')); + platformSpenderReportsService.suggestPurpose.and.returnValue(of('Client Meeting')); const draftReportPopoverSpy = jasmine.createSpyObj('draftReportPopover', ['present', 'onWillDismiss']); draftReportPopoverSpy.onWillDismiss.and.resolveTo({ data: { @@ -224,7 +225,7 @@ describe('FyAddToReportComponent', () => { expect(selectionModalControllerSpy.onWillDismiss).toHaveBeenCalledTimes(1); expect(modalProperties.getModalDefaultProperties).toHaveBeenCalledTimes(1); expect(popoverController.create).toHaveBeenCalledOnceWith(popoverControllerParams3); - expect(reportService.getReportPurpose).toHaveBeenCalledOnceWith({ ids: null }); + expect(platformSpenderReportsService.suggestPurpose).toHaveBeenCalledOnceWith([]); expect(platformSpenderReportsService.getAllReportsByParams).toHaveBeenCalledOnceWith({ state: 'in.(DRAFT,APPROVER_PENDING,APPROVER_INQUIRY)', }); diff --git a/src/app/shared/components/fy-add-to-report/fy-add-to-report.component.ts b/src/app/shared/components/fy-add-to-report/fy-add-to-report.component.ts index 08b3d42357..1ba4cb5e7d 100644 --- a/src/app/shared/components/fy-add-to-report/fy-add-to-report.component.ts +++ b/src/app/shared/components/fy-add-to-report/fy-add-to-report.component.ts @@ -129,7 +129,7 @@ export class FyAddToReportComponent implements OnInit, OnChanges, ControlValueAc this.trackingService.addToReportFromExpense(); } if (data && data.createDraftReport) { - const reportTitle = await this.reportService.getReportPurpose({ ids: null }).toPromise(); + const reportTitle = await this.platformReportService.suggestPurpose([]).toPromise(); const draftReportPopover = await this.popoverController.create({ component: FyInputPopoverComponent,