Skip to content

Commit

Permalink
feat: add approver platform API (#3131)
Browse files Browse the repository at this point in the history
  • Loading branch information
bistaastha authored Jul 11, 2024
1 parent 0ca1861 commit 91f3050
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 89 deletions.
1 change: 1 addition & 0 deletions src/app/core/models/employee-params.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface EmployeeParams {
ou_id: string;
order: string;
limit: number;
us_email?: string;
}
19 changes: 19 additions & 0 deletions src/app/core/services/platform/v1/approver/reports.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ describe('ApproverReportsService', () => {
});
});

it('addApprover(): should add approver to a report', (done) => {
approverPlatformApiService.post.and.returnValue(of(null));

const reportID = 'rprj1zHHpW2W';
const approverEmail = '[email protected]';
const comment = 'comment';

approverReportsService.addApprover(reportID, approverEmail, comment).subscribe(() => {
expect(approverPlatformApiService.post).toHaveBeenCalledOnceWith(`/reports/add_approver`, {
data: {
id: reportID,
approver_email: approverEmail,
comment,
},
});
done();
});
});

it('getReportById(): should get a report by id', () => {
spyOn(approverReportsService, 'getReportsByParams').and.returnValue(of(allReportsPaginated1));
const queryParams = {
Expand Down
10 changes: 10 additions & 0 deletions src/app/core/services/platform/v1/approver/reports.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ export class ApproverReportsService {
return this.approverPlatformApiService.post('/reports/send_back', { data: { id, comment } });
}

addApprover(rptId: string, approverEmail: string, comment: string): Observable<Report> {
const data = {
id: rptId,
approver_email: approverEmail,
comment,
};

return this.approverPlatformApiService.post('/reports/add_approver', { data });
}

permissions(id: string): Observable<ReportPermissions> {
return this.approverPlatformApiService
.post<PlatformApiPayload<ReportPermissions>>('/reports/permissions', { data: { id } })
Expand Down
16 changes: 0 additions & 16 deletions src/app/core/services/report.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,22 +260,6 @@ describe('ReportService', () => {
});
});

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

const reportID = 'rprj1zHHpW2W';
const approverEmail = '[email protected]';
const comment = 'comment';

reportService.addApprover(reportID, approverEmail, comment).subscribe(() => {
expect(apiService.post).toHaveBeenCalledOnceWith(`/reports/${reportID}/approvals`, {
approver_email: approverEmail,
comment,
});
done();
});
});

it('delete(): should delete a report', (done) => {
apiService.delete.and.returnValue(of(null));
spyOn(reportService, 'clearTransactionCache').and.returnValue(of(null));
Expand Down
11 changes: 0 additions & 11 deletions src/app/core/services/report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@ export class ReportService {
return this.apiService.post('/reports/' + rptId + '/approve');
}

@CacheBuster({
cacheBusterNotifier: reportsCacheBuster$,
})
addApprover(rptId: string, approverEmail: string, comment: string): Observable<void> {
const data = {
approver_email: approverEmail,
comment,
};
return this.apiService.post('/reports/' + rptId + '/approvals', data);
}

@CacheBuster({
cacheBusterNotifier: reportsCacheBuster$,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { FormsModule } from '@angular/forms';
import { pullBackAdvancedRequests } from 'src/app/core/mock-data/advance-requests.data';
import { getElementBySelector, getTextContent } from 'src/app/core/dom-helpers';
import { of } from 'rxjs';
import { ApproverReportsService } from 'src/app/core/services/platform/v1/approver/reports.service';

describe('AddApproversPopoverComponent', () => {
let component: AddApproversPopoverComponent;
Expand All @@ -20,15 +21,15 @@ describe('AddApproversPopoverComponent', () => {
let modalProperties: jasmine.SpyObj<ModalPropertiesService>;
let popoverController: jasmine.SpyObj<PopoverController>;
let advanceRequestService: jasmine.SpyObj<AdvanceRequestService>;
let reportService: jasmine.SpyObj<ReportService>;
let approverReportsService: jasmine.SpyObj<ApproverReportsService>;
let loaderService: jasmine.SpyObj<LoaderService>;

beforeEach(waitForAsync(() => {
const modalControllerSpy = jasmine.createSpyObj('ModalController', ['create']);
const modalPropertiesSpy = jasmine.createSpyObj('ModalPropertiesService', ['getModalDefaultProperties']);
const popoverControllerSpy = jasmine.createSpyObj('PopoverController', ['dismiss']);
const advanceRequestServiceSpy = jasmine.createSpyObj('AdvanceRequestService', ['addApprover']);
const reportServiceSpy = jasmine.createSpyObj('ReportService', ['addApprover']);
const approverReportsServiceSpy = jasmine.createSpyObj('ApproverReportsService', ['addApprover']);
const loaderServiceSpy = jasmine.createSpyObj('LoaderService', ['showLoader', 'hideLoader']);

TestBed.configureTestingModule({
Expand All @@ -52,8 +53,8 @@ describe('AddApproversPopoverComponent', () => {
useValue: advanceRequestServiceSpy,
},
{
provide: ReportService,
useValue: reportServiceSpy,
provide: ApproverReportsService,
useValue: approverReportsServiceSpy,
},
{
provide: LoaderService,
Expand All @@ -65,7 +66,7 @@ describe('AddApproversPopoverComponent', () => {
modalProperties = TestBed.inject(ModalPropertiesService) as jasmine.SpyObj<ModalPropertiesService>;
popoverController = TestBed.inject(PopoverController) as jasmine.SpyObj<PopoverController>;
advanceRequestService = TestBed.inject(AdvanceRequestService) as jasmine.SpyObj<AdvanceRequestService>;
reportService = TestBed.inject(ReportService) as jasmine.SpyObj<ReportService>;
approverReportsService = TestBed.inject(ApproverReportsService) as jasmine.SpyObj<ApproverReportsService>;
loaderService = TestBed.inject(LoaderService) as jasmine.SpyObj<LoaderService>;

fixture = TestBed.createComponent(AddApproversPopoverComponent);
Expand All @@ -84,17 +85,21 @@ describe('AddApproversPopoverComponent', () => {
component.type = 'report';
component.ownerEmail = '[email protected]';
const selectedApproversList = ['[email protected]', 'aiyush.dhar@fylein', '[email protected]', '[email protected]'];
component.selectedApproversList = selectedApproversList;
modalController.create.and.returnValue(Promise.resolve(modalSpy));
modalSpy.onWillDismiss.and.returnValue(Promise.resolve({ data: { selectedApproversList } } as any));
component.selectedApproversList = selectedApproversList.map((email) => {
return { email };
});
modalController.create.and.resolveTo(modalSpy);
modalSpy.onWillDismiss.and.resolveTo({ data: { selectedApproversList } } as any);

component.openModal();
tick();
expect(modalController.create).toHaveBeenCalledOnceWith({
component: ApproverDialogComponent,
componentProps: {
approverEmailsList: component.approverEmailsList,
initialApproverList: selectedApproversList,
initialApproverList: selectedApproversList.map((email) => {
return { email };
}),
id: component.id,
type: component.type,
ownerEmail: component.ownerEmail,
Expand All @@ -107,7 +112,7 @@ describe('AddApproversPopoverComponent', () => {
}));

it('closeAddApproversPopover(): should close popover', fakeAsync(() => {
popoverController.dismiss.and.returnValue(Promise.resolve(true));
popoverController.dismiss.and.resolveTo(true);

tick();
component.closeAddApproversPopover();
Expand All @@ -121,9 +126,9 @@ describe('AddApproversPopoverComponent', () => {
component.confirmationMessage = 'The request is approved';
component.selectedApproversList = [{ email: '[email protected]' }];
advanceRequestService.addApprover.and.returnValue(of(pullBackAdvancedRequests));
loaderService.showLoader.and.returnValue(Promise.resolve());
loaderService.hideLoader.and.returnValue(Promise.resolve());
popoverController.dismiss.and.returnValue(Promise.resolve(true));
loaderService.showLoader.and.resolveTo();
loaderService.hideLoader.and.resolveTo();
popoverController.dismiss.and.resolveTo(true);

component.saveUpdatedApproversList();

Expand All @@ -138,22 +143,22 @@ describe('AddApproversPopoverComponent', () => {
expect(popoverController.dismiss).toHaveBeenCalledOnceWith({ reload: true });
}));

it('should call reportService.addApprover() for other request types', fakeAsync(() => {
it('should call approverReportsService.addApprover() for other request types', fakeAsync(() => {
fixture.detectChanges();
component.type = 'report';
component.id = 'repP09oaYXAf';
component.confirmationMessage = 'The request is approved';
component.selectedApproversList = [{ email: '[email protected]' }];
reportService.addApprover.and.returnValue(of(null));
loaderService.showLoader.and.returnValue(Promise.resolve());
loaderService.hideLoader.and.returnValue(Promise.resolve());
popoverController.dismiss.and.returnValue(Promise.resolve(true));
approverReportsService.addApprover.and.returnValue(of(null));
loaderService.showLoader.and.resolveTo();
loaderService.hideLoader.and.resolveTo();
popoverController.dismiss.and.resolveTo(true);

component.saveUpdatedApproversList();

expect(loaderService.showLoader).toHaveBeenCalledTimes(1);
tick();
expect(reportService.addApprover).toHaveBeenCalledOnceWith(
expect(approverReportsService.addApprover).toHaveBeenCalledOnceWith(
'repP09oaYXAf',
'[email protected]',
'The request is approved'
Expand All @@ -168,7 +173,10 @@ describe('AddApproversPopoverComponent', () => {
});

it('should display the "+n more" chip when there are more than 3 selected approvers', () => {
component.selectedApproversList = ['[email protected]', '[email protected]', '[email protected]', '[email protected]'];
const selectedApproversList = ['[email protected]', '[email protected]', '[email protected]', '[email protected]'];
component.selectedApproversList = selectedApproversList.map((email) => {
return { email };
});
fixture.detectChanges();
const moreChip = getElementBySelector(fixture, '.add-approvers-popover--input-container__chip');
expect(moreChip).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ import { LoaderService } from 'src/app/core/services/loader.service';
import { ApproverDialogComponent } from './approver-dialog/approver-dialog.component';
import { ModalPropertiesService } from 'src/app/core/services/modal-properties.service';
import { AdvanceRequestService } from 'src/app/core/services/advance-request.service';
import { ReportService } from 'src/app/core/services/report.service';

import { ApproverReportsService } from 'src/app/core/services/platform/v1/approver/reports.service';
import { Approver } from './models/approver.model';
import { AdvanceRequests } from 'src/app/core/models/advance-requests.model';
import { Report } from 'src/app/core/models/platform/v1/report.model';
@Component({
selector: 'app-add-approvers-popover',
templateUrl: './add-approvers-popover.component.html',
styleUrls: ['./add-approvers-popover.component.scss'],
})
export class AddApproversPopoverComponent {
@Input() approverEmailsList;
@Input() approverEmailsList: string[];

@Input() id: string;

@Input() ownerEmail: string;

@Input() type;
@Input() type: string;

selectedApproversList = [];
selectedApproversList: Approver[] = [];

displayValue: string;

Expand All @@ -33,11 +35,11 @@ export class AddApproversPopoverComponent {
private modalProperties: ModalPropertiesService,
private popoverController: PopoverController,
private advanceRequestService: AdvanceRequestService,
private reportService: ReportService,
private loaderService: LoaderService
private loaderService: LoaderService,
private approverReportsService: ApproverReportsService
) {}

async openModal() {
async openModal(): Promise<void> {
const approversListModal = await this.modalController.create({
component: ApproverDialogComponent,
componentProps: {
Expand All @@ -53,7 +55,7 @@ export class AddApproversPopoverComponent {

await approversListModal.present();

const { data } = await approversListModal.onWillDismiss();
const { data } = await approversListModal.onWillDismiss<{ selectedApproversList: Approver[] }>();

if (data && data.selectedApproversList) {
this.selectedApproversList = data.selectedApproversList;
Expand All @@ -67,26 +69,32 @@ export class AddApproversPopoverComponent {
}
}

saveUpdatedApproversList() {
saveUpdatedApproversList(): void {
from(this.loaderService.showLoader())
.pipe(
switchMap(() => from(this.selectedApproversList.map((selectedApprover) => selectedApprover.email))),
concatMap((approver) => {
if (this.type === 'ADVANCE_REQUEST') {
return this.advanceRequestService.addApprover(this.id, approver, this.confirmationMessage);
} else {
return this.reportService.addApprover(this.id, approver, this.confirmationMessage);
return this.approverReportsService.addApprover(this.id, approver, this.confirmationMessage);
}
}),
reduce((acc, curr) => acc.concat(curr), []),
reduce((acc: AdvanceRequests[] | Report[], curr: AdvanceRequests | Report) => {
if (this.type === 'ADVANCE_REQUEST') {
(acc as AdvanceRequests[]).concat(curr as AdvanceRequests);
} else {
(acc as Report[]).concat(curr as Report);
}
}, []),
finalize(() => from(this.loaderService.hideLoader()))
)
.subscribe(() => {
this.popoverController.dismiss({ reload: true });
});
}

closeAddApproversPopover() {
closeAddApproversPopover(): void {
this.popoverController.dismiss();
}
}
Loading

0 comments on commit 91f3050

Please sign in to comment.