From e2d0d16d8a62ab7fe08c1b03117344be5c2b3176 Mon Sep 17 00:00:00 2001 From: Alicia Zangger Date: Tue, 20 Oct 2020 10:47:08 +0200 Subject: [PATCH] request: allow cancellation when item is at desk * Updates 'cancel request' permissions to allow the cancellation of an item 'at desk'. * Adds a method to update the item detailed view after a cancel request. * Adds a flash message to inform the user about the item status. * Moves 'cancel' and 'update' logic from the item-transaction component (list item) to the item-transactions (list) component for a better understanding and to clean the code. * Closes rero/rero-ils#1293. Co-Authored-by: Alicia Zangger --- .../item-detail-view.component.html | 5 +- .../item-detail-view.component.ts | 14 +++- .../item-transaction.component.html | 2 +- .../item-transaction.component.ts | 59 +++++--------- .../item-transactions.component.html | 3 +- .../item-transactions.component.ts | 76 ++++++++++++++++++- .../admin/src/app/service/items.service.ts | 2 +- 7 files changed, 115 insertions(+), 46 deletions(-) diff --git a/projects/admin/src/app/record/detail-view/item-detail-view/item-detail-view.component.html b/projects/admin/src/app/record/detail-view/item-detail-view/item-detail-view.component.html index fe484d580..29eea3b8b 100644 --- a/projects/admin/src/app/record/detail-view/item-detail-view/item-detail-view.component.html +++ b/projects/admin/src/app/record/detail-view/item-detail-view/item-detail-view.component.html @@ -157,5 +157,8 @@

Issue data

- + diff --git a/projects/admin/src/app/record/detail-view/item-detail-view/item-detail-view.component.ts b/projects/admin/src/app/record/detail-view/item-detail-view/item-detail-view.component.ts index d887e50ed..2b408a5bd 100644 --- a/projects/admin/src/app/record/detail-view/item-detail-view/item-detail-view.component.ts +++ b/projects/admin/src/app/record/detail-view/item-detail-view/item-detail-view.component.ts @@ -50,11 +50,11 @@ export class ItemDetailViewComponent implements DetailRecord, OnInit, OnDestroy * @param _recordService - RecordService */ constructor( - private _recordService: RecordService, + private _recordService: RecordService ) {} ngOnInit() { - this._recordObs = this.record$.subscribe( record => { + this._recordObs = this.record$.subscribe(record => { this.record = record; this._recordService.getRecord('locations', record.metadata.location.pid, 1).subscribe(data => this.location = data); }); @@ -67,4 +67,14 @@ export class ItemDetailViewComponent implements DetailRecord, OnInit, OnDestroy isPublicNote(note: ItemNote): boolean { return Item.PUBLIC_NOTE_TYPES.includes(note.type); } + + /** + * Update item status + */ + updateItemStatus() { + this._recordService.getRecord('items', this.record.metadata.pid).subscribe((item: any) => { + this.record.metadata.status = item.metadata.status; + this.record.metadata.available = item.metadata.available; + }); + } } diff --git a/projects/admin/src/app/record/detail-view/item-detail-view/item-transaction/item-transaction.component.html b/projects/admin/src/app/record/detail-view/item-detail-view/item-transaction/item-transaction.component.html index 1d3a5ad5b..540760100 100644 --- a/projects/admin/src/app/record/detail-view/item-detail-view/item-transaction/item-transaction.component.html +++ b/projects/admin/src/app/record/detail-view/item-detail-view/item-transaction/item-transaction.component.html @@ -43,7 +43,7 @@ diff --git a/projects/admin/src/app/record/detail-view/item-detail-view/item-transaction/item-transaction.component.ts b/projects/admin/src/app/record/detail-view/item-detail-view/item-transaction/item-transaction.component.ts index 9f4322351..93a76eace 100644 --- a/projects/admin/src/app/record/detail-view/item-detail-view/item-transaction/item-transaction.component.ts +++ b/projects/admin/src/app/record/detail-view/item-detail-view/item-transaction/item-transaction.component.ts @@ -16,12 +16,9 @@ */ import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; -import { DialogService, RecordService } from '@rero/ng-core'; -import { ToastrService } from 'ngx-toastr'; +import { DialogService } from '@rero/ng-core'; import { LoanState } from 'projects/admin/src/app/class/items'; -import { LoanService } from 'projects/admin/src/app/service/loan.service'; import { UserService } from 'projects/admin/src/app/service/user.service'; -import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { ItemsService } from 'projects/admin/src/app/service/items.service'; @@ -80,7 +77,12 @@ export class ItemTransactionComponent implements OnInit, OnDestroy { /** * Informs parent component to remove request when it is cancelled */ - @Output() removeRequest = new EventEmitter(); + @Output() cancelRequestEvent = new EventEmitter(); + + /** + * Informs parent component to update pickup location + */ + @Output() updatePickupLocationEvent = new EventEmitter(); /** * On init hook @@ -105,17 +107,13 @@ export class ItemTransactionComponent implements OnInit, OnDestroy { /** * constructor - * @param _loanService - LoanService * @param _userService - User service - * @param _toastrService - ToastrService * @param _translateService - TranslateService * @param _dialogService: DialogService * @param _itemService: ItemsService */ constructor( - private _loanService: LoanService, private _userService: UserService, - private _toastrService: ToastrService, private _translateService: TranslateService, private _dialogService: DialogService, private _itemService: ItemsService @@ -141,7 +139,8 @@ export class ItemTransactionComponent implements OnInit, OnDestroy { // No permission API in backend // Allowed when loan state is PENDING or ITEM_IN_TRANSIT_FOR_PICKUP according to actions.md return this.transaction.metadata.state === LoanState.PENDING - || this.transaction.metadata.state === LoanState.ITEM_IN_TRANSIT_FOR_PICKUP; + || this.transaction.metadata.state === LoanState.ITEM_IN_TRANSIT_FOR_PICKUP + || this.transaction.metadata.state === LoanState.ITEM_AT_DESK; } /** @@ -172,43 +171,27 @@ export class ItemTransactionComponent implements OnInit, OnDestroy { this._dialogService.show(config).subscribe((confirm: boolean) => { if (confirm) { - this.cancelRequest(); + this.emitCancelRequest(); } }); } /** - * Cancel request + * Inform parent to cancel the request */ - cancelRequest() { - this._loanService - .cancelLoan( - this.itemPid, - this.transaction.metadata.pid, - this._currentUser.currentLibrary - ) - .subscribe((itemData: any) => { - this.removeRequest.emit(true); - }); + emitCancelRequest() { + this.cancelRequestEvent.emit(this.transaction); } /** - * Update pickup location - * @param pickupLocationPid - pickup location pid to change - */ - updateRequest(pickupLocationPid: string) { - this._loanService - .updateLoanPickupLocation( - this.transaction.metadata.pid, - pickupLocationPid - ) - .subscribe((data: any) => { - this._toastrService.success( - this._translateService.instant('The pickup location has been changed.'), - this._translateService.instant('Request') - ); - this.transaction.metadata = data; - }); + * Inform parent to cancel the request + */ + emitUpdatePickupLocation(pickupLocationPid: string) { + const data = { + pickupLocationPid, + transaction: this.transaction + }; + this.updatePickupLocationEvent.emit(data); } /** diff --git a/projects/admin/src/app/record/detail-view/item-detail-view/item-transactions/item-transactions.component.html b/projects/admin/src/app/record/detail-view/item-detail-view/item-transactions/item-transactions.component.html index 3e500d183..1920ad203 100644 --- a/projects/admin/src/app/record/detail-view/item-detail-view/item-transactions/item-transactions.component.html +++ b/projects/admin/src/app/record/detail-view/item-detail-view/item-transactions/item-transactions.component.html @@ -50,7 +50,8 @@ type="loan_request" [transaction]="request" [itemPid]="item.metadata.pid" - (removeRequest)="deleteRequest(request)" + (cancelRequestEvent)="cancelRequest($event)" + (updatePickupLocationEvent)="updateRequestPickupLocation($event)" > diff --git a/projects/admin/src/app/record/detail-view/item-detail-view/item-transactions/item-transactions.component.ts b/projects/admin/src/app/record/detail-view/item-detail-view/item-transactions/item-transactions.component.ts index 15214a991..529be7f63 100644 --- a/projects/admin/src/app/record/detail-view/item-detail-view/item-transactions/item-transactions.component.ts +++ b/projects/admin/src/app/record/detail-view/item-detail-view/item-transactions/item-transactions.component.ts @@ -14,10 +14,14 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import { Component, Input, OnInit } from '@angular/core'; +import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; +import { TranslateService } from '@ngx-translate/core'; import { BsModalService } from 'ngx-bootstrap/modal'; +import { ToastrService } from 'ngx-toastr'; import { LoanService } from 'projects/admin/src/app/service/loan.service'; import { forkJoin } from 'rxjs'; +import { UserService } from 'projects/admin/src/app/service/user.service'; +import { Observable } from 'rxjs'; import { first } from 'rxjs/operators'; import { ItemRequestComponent } from '../../document-detail-view/item-request/item-request.component'; @@ -31,26 +35,43 @@ export class ItemTransactionsComponent implements OnInit { /** Item record */ @Input() item: any; + /** + * Informs parent component that a request has been cancelled + */ + @Output() cancelRequestEvent = new EventEmitter(); + /** Borrowed loan */ borrowedBy: Array = []; /** Requested loan(s) */ requestedBy: Array = []; + /** + * Current user + */ + private _currentUser: any; + /** * Constructor * @param _loanService - LoanService * @param _modalService - BsModalService + * @param _toastrService - ToastrService + * @param _translateService - TranslateService + * @param _userService - UserService */ constructor( private _loanService: LoanService, - private _modalService: BsModalService + private _modalService: BsModalService, + private _toastrService: ToastrService, + private _translateService: TranslateService, + private _userService: UserService ) { } /** * On init hook */ ngOnInit() { + this._currentUser = this._userService.getCurrentUser(); const borrowedBy$ = this._loanService.borrowedBy$(this.item.metadata.pid); const requestedBy$ = this._loanService.requestedBy$(this.item.metadata.pid); forkJoin([borrowedBy$, requestedBy$]).subscribe( @@ -80,4 +101,55 @@ export class ItemTransactionsComponent implements OnInit { this._loanService.requestedBy$(this.item.metadata.pid).subscribe(data => this.requestedBy = data); }); } + + /** + * Cancel request + * @param transaction - request to cancel + */ + cancelRequest(transaction: any) { + this._loanService + .cancelLoan( + this.item.pid, + transaction.metadata.pid, + this._currentUser.currentLibrary + ) + .subscribe((itemData: any) => { + const status = itemData.status; + this._toastrService.warning( + this._translateService.instant('The item is ' + status), + // this._translateService.instant('The item is {{ status }}', { status }), + this._translateService.instant('Request') + ); + this.cancelRequestEvent.emit(); + this.updateRequestList(); + }); + } + + /** + * Update request pickup location + * @param pickupLocationPid - pickup location pid to change + */ + updateRequestPickupLocation(data: any) { + this._loanService + .updateLoanPickupLocation( + data.transaction.metadata.pid, + data.pickupLocationPid + ) + .subscribe((response: any) => { + this._toastrService.success( + this._translateService.instant('The pickup location has been changed.'), + this._translateService.instant('Request') + ); + this.updateRequestList(); + }); + } + + /** + * Update request list + */ + updateRequestList() { + this._loanService.requestedBy$(this.item.metadata.pid).subscribe(requestedLoans => + this.requestedBy = requestedLoans + ); + } } diff --git a/projects/admin/src/app/service/items.service.ts b/projects/admin/src/app/service/items.service.ts index eb0986077..6c35ee08f 100644 --- a/projects/admin/src/app/service/items.service.ts +++ b/projects/admin/src/app/service/items.service.ts @@ -18,7 +18,7 @@ import { HttpClient, HttpParams } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { RecordService } from '@rero/ng-core'; -import { Observable, of } from 'rxjs'; +import { Observable, of, Subject } from 'rxjs'; import { catchError, map } from 'rxjs/operators'; import { Item, ItemAction, ItemNoteType, ItemStatus } from '../class/items'; import { UserService } from './user.service';