Skip to content

Commit

Permalink
feat: Move suggest purpose to platform (#3103)
Browse files Browse the repository at this point in the history
  • Loading branch information
bistaastha authored Jun 28, 2024
1 parent eaed409 commit 3fdcbb9
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 74 deletions.
13 changes: 13 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 @@ -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 }));
Expand Down
6 changes: 6 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 @@ -97,6 +97,12 @@ export class SpenderReportsService {
.pipe(map((res) => res.data));
}

suggestPurpose(expenseIds: string[]): Observable<string> {
return this.spenderPlatformV1ApiService
.post<PlatformApiPayload<{ purpose: string }>>('/reports/suggest_purpose', { data: { expense_ids: expenseIds } })
.pipe(map((res) => res.data.purpose));
}

getAllReportsByParams(queryParams: ReportsQueryParams): Observable<Report[]> {
return this.getReportsCount(queryParams).pipe(
switchMap((count) => {
Expand Down
36 changes: 0 additions & 36 deletions src/app/core/services/report.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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'],
Expand All @@ -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();
});
});
});
22 changes: 0 additions & 22 deletions src/app/core/services/report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -140,16 +136,6 @@ export class ReportService {
return this.apiService.post('/reports/' + rptId + '/approvals', data);
}

@CacheBuster({
cacheBusterNotifier: reportsCacheBuster$,
})
updateReportDetails(erpt: ExtendedReport): Observable<ReportV1> {
const reportData = this.dataTransformService.unflatten<UnflattenedReport, ExtendedReport>(erpt);
return this.apiService
.post<ReportV1>('/reports', reportData.rp)
.pipe(switchMap((res) => this.clearTransactionCache().pipe(map(() => res))));
}

@CacheBuster({
cacheBusterNotifier: reportsCacheBuster$,
})
Expand Down Expand Up @@ -206,10 +192,6 @@ export class ReportService {
return this.apiService.get<{ results: PdfExport[] }>('/reports/' + rptId + '/exports');
}

getApproversByReportId(rptId: string): Observable<Approver[]> {
return this.apiService.get('/reports/' + rptId + '/approvers');
}

delete(rptId: string): Observable<void> {
return this.apiService
.delete<void>('/reports/' + rptId)
Expand All @@ -220,10 +202,6 @@ export class ReportService {
return this.apiService.post('/reports/summary/download', data);
}

getReportPurpose(reportPurpose: { ids: string[] }): Observable<string> {
return this.apiService.post<ReportV1>('/reports/purpose', reportPurpose).pipe(map((res) => res.purpose));
}

approverUpdateReportPurpose(report: Report): Observable<Report> {
const params: { data: Pick<Report, 'id' | 'source' | 'purpose'> } = {
data: {
Expand Down
10 changes: 6 additions & 4 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 @@ -69,6 +69,7 @@ describe('MyCreateReportPage', () => {
'addExpenses',
'createDraft',
'getReportsCount',
'suggestPurpose',
]);

TestBed.configureTestingModule({
Expand Down Expand Up @@ -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'));
Expand All @@ -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);
}));
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 @@ -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;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand Down Expand Up @@ -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]);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('FyAddToReportComponent', () => {
'getReportsCount',
'getReportsByParams',
'createDraft',
'suggestPurpose',
]);
const modalControllerSpy = jasmine.createSpyObj('ModalController', ['create']);
const modalPropertiesSpy = jasmine.createSpyObj('ModalPropertiesService', ['getModalDefaultProperties']);
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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)',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3fdcbb9

Please sign in to comment.