From b9c050c19ccde38d047ccd3d081c41506edb7567 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Thu, 27 Feb 2020 13:45:54 +0100 Subject: [PATCH] 69115: ClaimedTaskDataService and components refactoring --- .../tasks/claimed-task-data.service.spec.ts | 25 +----------- .../core/tasks/claimed-task-data.service.ts | 28 ++----------- ...claimed-task-actions-abstract.component.ts | 39 ++++++++++++++++++- ...laimed-task-actions-approve.component.html | 2 +- ...med-task-actions-approve.component.spec.ts | 26 ++++++++++--- .../claimed-task-actions-approve.component.ts | 22 ++++------- .../claimed-task-actions.component.ts | 4 +- ...sk-actions-edit-metadata.component.spec.ts | 4 ++ ...ed-task-actions-edit-metadata.component.ts | 13 +++++-- ...claimed-task-actions-reject.component.html | 2 +- ...imed-task-actions-reject.component.spec.ts | 37 ++++++++++++------ .../claimed-task-actions-reject.component.ts | 32 ++++++++------- ...task-actions-return-to-pool.component.html | 2 +- ...k-actions-return-to-pool.component.spec.ts | 18 ++++++--- ...d-task-actions-return-to-pool.component.ts | 17 ++++---- ...imed-task-actions-loader.component.spec.ts | 6 ++- .../workflow-task-options.model.ts | 10 +++++ 17 files changed, 169 insertions(+), 118 deletions(-) create mode 100644 src/app/shared/mydspace-actions/claimed-task/workflow-task-options.model.ts diff --git a/src/app/core/tasks/claimed-task-data.service.spec.ts b/src/app/core/tasks/claimed-task-data.service.spec.ts index 90d449b22b..078fe1e63f 100644 --- a/src/app/core/tasks/claimed-task-data.service.spec.ts +++ b/src/app/core/tasks/claimed-task-data.service.spec.ts @@ -52,8 +52,7 @@ describe('ClaimedTaskDataService', () => { options.headers = headers; }); - describe('approveTask', () => { - + describe('submitTask', () => { it('should call postToEndpoint method', () => { const scopeId = '1234'; const body = { @@ -63,33 +62,13 @@ describe('ClaimedTaskDataService', () => { spyOn(service, 'postToEndpoint'); requestService.uriEncodeBody.and.returnValue(body); - service.approveTask(scopeId); - - expect(service.postToEndpoint).toHaveBeenCalledWith(linkPath, body, scopeId, options); - }); - }); - - describe('rejectTask', () => { - - it('should call postToEndpoint method', () => { - const scopeId = '1234'; - const reason = 'test reject'; - const body = { - submit_reject: 'true', - reason - }; - - spyOn(service, 'postToEndpoint'); - requestService.uriEncodeBody.and.returnValue(body); - - service.rejectTask(reason, scopeId); + service.submitTask(scopeId, body); expect(service.postToEndpoint).toHaveBeenCalledWith(linkPath, body, scopeId, options); }); }); describe('returnToPoolTask', () => { - it('should call deleteById method', () => { const scopeId = '1234'; diff --git a/src/app/core/tasks/claimed-task-data.service.ts b/src/app/core/tasks/claimed-task-data.service.ts index 0a9de20530..5815dad6e5 100644 --- a/src/app/core/tasks/claimed-task-data.service.ts +++ b/src/app/core/tasks/claimed-task-data.service.ts @@ -35,7 +35,6 @@ export class ClaimedTaskDataService extends TasksService { * * @param {RequestService} requestService * @param {RemoteDataBuildService} rdbService - * @param {NormalizedObjectBuildService} linkService * @param {Store} store * @param {ObjectCacheService} objectCache * @param {HALEndpointService} halService @@ -56,35 +55,16 @@ export class ClaimedTaskDataService extends TasksService { } /** - * Make a request to approve the given task + * Make a request for the given task * * @param scopeId * The task id + * @param body + * The request body * @return {Observable} * Emit the server response */ - public approveTask(scopeId: string): Observable { - const body = { - submit_approve: 'true' - }; - return this.postToEndpoint(this.linkPath, this.requestService.uriEncodeBody(body), scopeId, this.makeHttpOptions()); - } - - /** - * Make a request to reject the given task - * - * @param reason - * The reason of reject - * @param scopeId - * The task id - * @return {Observable} - * Emit the server response - */ - public rejectTask(reason: string, scopeId: string): Observable { - const body = { - submit_reject: 'true', - reason - }; + public submitTask(scopeId: string, body: any): Observable { return this.postToEndpoint(this.linkPath, this.requestService.uriEncodeBody(body), scopeId, this.makeHttpOptions()); } diff --git a/src/app/shared/mydspace-actions/claimed-task/abstract/claimed-task-actions-abstract.component.ts b/src/app/shared/mydspace-actions/claimed-task/abstract/claimed-task-actions-abstract.component.ts index 761f088389..bb08a6def3 100644 --- a/src/app/shared/mydspace-actions/claimed-task/abstract/claimed-task-actions-abstract.component.ts +++ b/src/app/shared/mydspace-actions/claimed-task/abstract/claimed-task-actions-abstract.component.ts @@ -1,11 +1,24 @@ import { EventEmitter, Input, Output } from '@angular/core'; import { ClaimedTask } from '../../../../core/tasks/models/claimed-task-object.model'; import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject'; +import { ClaimedTaskDataService } from '../../../../core/tasks/claimed-task-data.service'; +import { WorkflowTaskOptions } from '../workflow-task-options.model'; +import { ProcessTaskResponse } from '../../../../core/tasks/models/process-task-response'; /** * Abstract component for rendering a claimed task's action + * To create a child-component for a new option: + * - Make sure the option is defined in the WorkflowTaskOptions enum + * - Set the "option" of the component to the enum value + * - Add a @rendersWorkflowTaskOption annotation to your component providing the same enum value + * - Optionally overwrite createBody if the request body requires more than just the option */ export abstract class ClaimedTaskActionsAbstractComponent { + /** + * The workflow task option the child component represents + */ + abstract option: WorkflowTaskOptions; + /** * The Claimed Task to display an action for */ @@ -21,8 +34,30 @@ export abstract class ClaimedTaskActionsAbstractComponent { */ processing$ = new BehaviorSubject(false); + constructor(protected claimedTaskService: ClaimedTaskDataService) { + } + /** - * Method called when the action's button is clicked + * Create a request body for submitting the task + * Overwrite this method in the child component if the body requires more than just the option */ - abstract process(); + createbody(): any { + return { + [this.option]: 'true' + }; + } + + /** + * Submit the task for this option + * While the task is submitting, processing$ is set to true and processCompleted emits the response's status when + * completed + */ + submitTask() { + this.processing$.next(true); + this.claimedTaskService.submitTask(this.object.id, this.createbody()) + .subscribe((res: ProcessTaskResponse) => { + this.processing$.next(false); + this.processCompleted.emit(res.hasSucceeded); + }); + } } diff --git a/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.html b/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.html index 776b210e77..7944d24d96 100644 --- a/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.html +++ b/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.html @@ -2,7 +2,7 @@ [className]="'btn btn-success'" ngbTooltip="{{'submission.workflow.tasks.claimed.approve_help' | translate}}" [disabled]="processing$ | async" - (click)="process()"> + (click)="submitTask()"> {{'submission.workflow.tasks.generic.processing' | translate}} {{'submission.workflow.tasks.claimed.approve' | translate}} diff --git a/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.spec.ts b/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.spec.ts index e97caafc3c..1cbfdb7c46 100644 --- a/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.spec.ts +++ b/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.spec.ts @@ -16,7 +16,7 @@ let fixture: ComponentFixture; describe('ClaimedTaskActionsApproveComponent', () => { const object = Object.assign(new ClaimedTask(), { id: 'claimed-task-1' }); const claimedTaskService = jasmine.createSpyObj('claimedTaskService', { - approveTask: observableOf(new ProcessTaskResponse(true)) + submitTask: observableOf(new ProcessTaskResponse(true)) }); beforeEach(async(() => { @@ -61,13 +61,27 @@ describe('ClaimedTaskActionsApproveComponent', () => { expect(span).toBeDefined(); }); - it('should emit a successful processCompleted event', () => { - spyOn(component.processCompleted, 'emit'); + describe('submitTask', () => { + let expectedBody; - component.process(); - fixture.detectChanges(); + beforeEach(() => { + spyOn(component.processCompleted, 'emit'); - expect(component.processCompleted.emit).toHaveBeenCalled(); + expectedBody = { + [component.option]: 'true' + }; + + component.submitTask(); + fixture.detectChanges(); + }); + + it('should call claimedTaskService\'s submitTask with the expected body', () => { + expect(claimedTaskService.submitTask).toHaveBeenCalledWith(object.id, expectedBody) + }); + + it('should emit a successful processCompleted event', () => { + expect(component.processCompleted.emit).toHaveBeenCalledWith(true); + }); }); }); diff --git a/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.ts b/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.ts index 13cc50b6c0..22e8ec77f7 100644 --- a/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.ts +++ b/src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.ts @@ -1,10 +1,10 @@ import { Component } from '@angular/core'; import { ClaimedTaskActionsAbstractComponent } from '../abstract/claimed-task-actions-abstract.component'; -import { ProcessTaskResponse } from '../../../../core/tasks/models/process-task-response'; import { rendersWorkflowTaskOption } from '../switcher/claimed-task-actions-decorator'; +import { WorkflowTaskOptions } from '../workflow-task-options.model'; import { ClaimedTaskDataService } from '../../../../core/tasks/claimed-task-data.service'; -@rendersWorkflowTaskOption('submit_approve') +@rendersWorkflowTaskOption(WorkflowTaskOptions.Approve) @Component({ selector: 'ds-claimed-task-actions-approve', styleUrls: ['./claimed-task-actions-approve.component.scss'], @@ -14,20 +14,12 @@ import { ClaimedTaskDataService } from '../../../../core/tasks/claimed-task-data * Component for displaying and processing the approve action on a workflow task item */ export class ClaimedTaskActionsApproveComponent extends ClaimedTaskActionsAbstractComponent { + /** + * This component represents the approve option + */ + option = WorkflowTaskOptions.Approve; constructor(protected claimedTaskService: ClaimedTaskDataService) { - super(); - } - - /** - * Approve the task - */ - process() { - this.processing$.next(true); - this.claimedTaskService.approveTask(this.object.id) - .subscribe((res: ProcessTaskResponse) => { - this.processing$.next(false); - this.processCompleted.emit(res.hasSucceeded); - }); + super(claimedTaskService); } } diff --git a/src/app/shared/mydspace-actions/claimed-task/claimed-task-actions.component.ts b/src/app/shared/mydspace-actions/claimed-task/claimed-task-actions.component.ts index c3802a3846..3c8411b33a 100644 --- a/src/app/shared/mydspace-actions/claimed-task/claimed-task-actions.component.ts +++ b/src/app/shared/mydspace-actions/claimed-task/claimed-task-actions.component.ts @@ -16,7 +16,7 @@ import { RequestService } from '../../../core/data/request.service'; import { SearchService } from '../../../core/shared/search/search.service'; import { WorkflowAction } from '../../../core/tasks/models/workflow-action-object.model'; import { WorkflowActionDataService } from '../../../core/data/workflow-action-data.service'; -import { WORKFLOW_TASK_OPTION_RETURN } from './return-to-pool/claimed-task-actions-return-to-pool.component'; +import { WorkflowTaskOptions } from './workflow-task-options.model'; /** * This component represents actions related to ClaimedTask object. @@ -47,7 +47,7 @@ export class ClaimedTaskActionsComponent extends MyDSpaceActionsComponent; @@ -23,6 +24,9 @@ describe('ClaimedTaskActionsEditMetadataComponent', () => { } }) ], + providers: [ + { provide: ClaimedTaskDataService, useValue: {} } + ], declarations: [ClaimedTaskActionsEditMetadataComponent], schemas: [NO_ERRORS_SCHEMA] }).overrideComponent(ClaimedTaskActionsEditMetadataComponent, { diff --git a/src/app/shared/mydspace-actions/claimed-task/edit-metadata/claimed-task-actions-edit-metadata.component.ts b/src/app/shared/mydspace-actions/claimed-task/edit-metadata/claimed-task-actions-edit-metadata.component.ts index c260459e70..e74c954114 100644 --- a/src/app/shared/mydspace-actions/claimed-task/edit-metadata/claimed-task-actions-edit-metadata.component.ts +++ b/src/app/shared/mydspace-actions/claimed-task/edit-metadata/claimed-task-actions-edit-metadata.component.ts @@ -1,10 +1,10 @@ import { Component } from '@angular/core'; import { ClaimedTaskActionsAbstractComponent } from '../abstract/claimed-task-actions-abstract.component'; -import { ProcessTaskResponse } from '../../../../core/tasks/models/process-task-response'; import { rendersWorkflowTaskOption } from '../switcher/claimed-task-actions-decorator'; +import { WorkflowTaskOptions } from '../workflow-task-options.model'; import { ClaimedTaskDataService } from '../../../../core/tasks/claimed-task-data.service'; -@rendersWorkflowTaskOption('submit_edit_metadata') +@rendersWorkflowTaskOption(WorkflowTaskOptions.EditMetadata) @Component({ selector: 'ds-claimed-task-actions-edit-metadata', styleUrls: ['./claimed-task-actions-edit-metadata.component.scss'], @@ -14,7 +14,12 @@ import { ClaimedTaskDataService } from '../../../../core/tasks/claimed-task-data * Component for displaying the edit metadata action on a workflow task item */ export class ClaimedTaskActionsEditMetadataComponent extends ClaimedTaskActionsAbstractComponent { - process() { - // Nothing needs to happen for the edit-metadata button, it simply renders a link to another page + /** + * This component represents the edit metadata option + */ + option = WorkflowTaskOptions.EditMetadata; + + constructor(protected claimedTaskService: ClaimedTaskDataService) { + super(claimedTaskService); } } diff --git a/src/app/shared/mydspace-actions/claimed-task/reject/claimed-task-actions-reject.component.html b/src/app/shared/mydspace-actions/claimed-task/reject/claimed-task-actions-reject.component.html index 51be87b25c..7c7b83cd8a 100644 --- a/src/app/shared/mydspace-actions/claimed-task/reject/claimed-task-actions-reject.component.html +++ b/src/app/shared/mydspace-actions/claimed-task/reject/claimed-task-actions-reject.component.html @@ -21,7 +21,7 @@ -
+