Skip to content

Commit

Permalink
permissions: refactoring permissions usage
Browse files Browse the repository at this point in the history
Updates way how permissions are used. Instead to use permissions
returned by a search query, use permissions provided by the backend
permission API (`/api/permissions/<type>/<pid>`).
After these changes, we could refactor the backend SearchSerializer to
remove permissions and speed up search query results.

This commit also updates button and functionalities according to the UX
chart.

* Closes rero/rero-ils#819
* Closes rero/rero-ils#932

Co-authored-by : Renaud Michotte <[email protected]>
  • Loading branch information
zannkukai committed May 27, 2020
1 parent 33ae600 commit 7120284
Show file tree
Hide file tree
Showing 20 changed files with 349 additions and 451 deletions.
4 changes: 3 additions & 1 deletion projects/admin/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import { TabsModule } from 'ngx-bootstrap/tabs';
import { TooltipModule } from 'ngx-bootstrap/tooltip';
import { PopoverModule } from 'ngx-bootstrap/popover';
import { BsLocaleService } from 'ngx-bootstrap/datepicker';
import { OrderLineComponent } from './record/detail-view/acquisition-order-detail-view/order-lines/order-line/order-line.component';

@NgModule({
declarations: [
Expand Down Expand Up @@ -172,7 +173,8 @@ import { BsLocaleService } from 'ngx-bootstrap/datepicker';
RelatedResourceComponent,
ResourceComponent,
ItemRequestComponent,
ErrorPageComponent
ErrorPageComponent,
OrderLineComponent
],
imports: [
AppRoutingModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ <h1 class="mb-3">{{ record.metadata.order_number }}</h1>
</article>

<!-- Order lines -->
<admin-acquisition-order-lines [order]="record" [orderLines$]="orderLines$" (deleteOrderLine)="deleteOrderLine($event)">
<admin-acquisition-order-lines [order]="record" [orderLines$]="orderLines$" (deleteOrderLineEmitter)="deleteOrderLine($event)">
</admin-acquisition-order-lines>
</ng-container>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export class AcquisitionOrderDetailViewComponent implements OnInit, DetailRecord

/**
* Constructor
* @param _recordService - RecordService
* @param _recordUiService - RecordUiService
*/
constructor(
private _recordService: RecordService,
Expand All @@ -55,7 +57,7 @@ export class AcquisitionOrderDetailViewComponent implements OnInit, DetailRecord
map(record => record.metadata.total_amount)
);
// retrieve all order lines linked
this.orderLines$ = this.record$.pipe(
this.orderLines$ = this.record$.pipe(
switchMap(record => {
const query = `acq_order.pid:${record.metadata.pid}`;
return this._recordService.getRecords(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,81 +14,26 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<h4 class="mt-4" translate>Order lines</h4>

<div class="mb-2">
<a *ngIf="!order.permissions.cannot_update"
class="btn btn-sm btn-primary"
<h4 class="mt-4" translate>
{{ 'Order lines' | translate }}
<a *ngIf="permissions && permissions.update.can"
class="btn btn-sm btn-outline-primary"
[routerLink]="['/', 'records', 'acq_order_lines', 'new']"
[queryParams]="{ order: order.metadata.pid }"
>
<i class="fa fa-plus-square-o"></i> {{ 'Add' | translate }}
<i class="fa fa-plus-square-o"></i> {{ 'Add' | translate }} ...
</a>
</div>
<div class="container" *ngIf="orderLines$ | async as orderLines; else noOrderLines">
<!-- HEADER -->
</h4>
<div class="container mt-2" *ngIf="orderLines$ | async as orderLines; else noOrderLines">
<div class="row p-2 mb-1 bg-dark rounded text-light">
<div class="col-sm-1">#</div>
<div class="col-sm-3" translate>Title</div>
<div class="col-sm-2" translate>Amount</div>
<div class="col-sm-1" translate>Quantity</div>
<div class="col-sm-2" translate>Discount amount</div>
<div class="col-sm-2" translate>Total amount</div>
</div>
<div *ngFor="let orderLine of orderLines; let i = index">
<ng-container *ngIf="orderLine">
<div class="row p-2 mb-1 border rounded align-middle">
<!-- # -->
<div class="col-sm-1">
<a [routerLink]="['/records', 'acq_order_lines', 'detail', orderLine.metadata.pid]">
{{ i + 1 }}
</a>
</div>
<!-- DOCUMENT -->
<ng-container *ngIf="orderLine.metadata.document.pid | getRecord: 'documents' | async as document">
<div class="col-sm-3">
<a [routerLink]="['/records', 'documents', 'detail', document.metadata.pid]"
*ngIf="document.metadata.title | mainTitle as title">
{{ title }}
</a>
</div>
</ng-container>
<!-- AMOUNT -->
<div class="col-sm-2">
{{ orderLine.metadata.amount | currency:order.metadata.currency:'symbol' }}
</div>
<!-- QUANTITY -->
<div class="col-sm-1">
{{ orderLine.metadata.quantity }}
</div>
<!-- DISCOUNT_AMOUNT -->
<div class="col-sm-2">
{{ orderLine.metadata.discount_amount | currency:order.metadata.currency:'symbol' }}
</div>
<!-- TOTAL_AMOUNT -->
<div class="col-sm-2">
{{ orderLine.metadata.total_amount | currency:order.metadata.currency:'symbol' }}
</div>
<!-- ACTIONS -->
<div class="col-sm-1">
<button *ngIf="!orderLine.permissions.cannot_update" type="button" class="btn btn-link p-0"
[routerLink]="['/records', 'acq_order_lines', 'edit', orderLine.metadata.pid]">
<i class="fa fa-pencil"></i>
</button>

<button *ngIf="!orderLine.permissions.cannot_delete" type="button" class="btn btn-link p-0 ml-1"
(click)="delete(orderLine.metadata.pid)">
<i class="fa fa-trash"></i>
</button>
<ng-template #notDelete>
<button type="button" class="btn btn-dark btn-sm ml-1" (click)="showDeleteMessage(orderLine)">
<i class="fa fa-trash"></i>
</button>
</ng-template>
</div>
</div>
</ng-container>
</div>
<admin-order-line [orderLine]="orderLine" [order]="order" (deleteOrderLine)="deleteOrderLine($event)"
*ngFor="let orderLine of orderLines"></admin-order-line>
</div>

<ng-template #noOrderLines>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,56 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { RecordService, RecordUiService } from '@rero/ng-core';
import { RecordPermissionService } from 'projects/admin/src/app/service/record-permission.service';
import { Observable } from 'rxjs';
import { RecordPermissionMessageService } from '../../../../service/record-permission-message.service';

@Component({
selector: 'admin-acquisition-order-lines',
templateUrl: './acquisition-order-lines.component.html'
})
export class AcquisitionOrderLinesComponent {
export class AcquisitionOrderLinesComponent implements OnInit {

/** Acquisition order pid */
@Input() order: any;

/** Event for delete order line */
@Output()
deleteOrderLine = new EventEmitter();

/** Acquisition order Line observable */
@Input()
orderLines$: Observable<Array<any>>;

/** record permissions */
permissions: any;

/** Event for delete order line */
@Output() deleteOrderLineEmitter = new EventEmitter();

/**
* Constructor
* @param recordService - RecordService
* @param recordUiService - RecordUiService
* @param recordPermissionMessage - RecordPermissionMessageService
* @param _recordService - RecordService
* @param _recordUiService - RecordUiService
* @param _recordPermissionService - RecordPermissionService
*/
constructor(
private _recordService: RecordService,
private _recordUiService: RecordUiService,
private _recordPermissionMessage: RecordPermissionMessageService
private _recordPermissionService: RecordPermissionService
) { }

/**
* Delete order line
* @param orderLinePid - AcqOrderLine pid
* On init hook
*/
delete(orderLinePid: string) {
this.deleteOrderLine.emit(orderLinePid);
ngOnInit(): void {
this._recordPermissionService.getPermission('acq_orders', this.order.metadata.pid).subscribe(
(permissions) => this.permissions = permissions
);
}

/**
* Display message if the record cannot be deleted
* @param orderLine - AcqOrderLine record
* Delete order line
* @param orderLinePid - AcqOrderLine pid
*/
public showDeleteMessage(orderLine: object) {
const message = this._recordPermissionMessage.generateMessage(orderLine);
this._recordUiService.showDeleteMessage(message);
deleteOrderLine(orderLinePid: string) {
this.deleteOrderLineEmitter.emit(orderLinePid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!--
RERO ILS UI
Copyright (C) 2019 RERO
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<ng-container *ngIf="orderLine && permissions else loading">
<div class="row p-2 mb-1 border rounded align-middle">
<!-- DOCUMENT -->
<ng-container *ngIf="orderLine.metadata.document.pid | getRecord: 'documents' | async as document">
<div class="col-sm-3">
<a [routerLink]="['/records', 'documents', 'detail', document.metadata.pid]"
*ngIf="document.metadata.title | mainTitle as title">
{{ title | truncateText }}
</a>
</div>
</ng-container>
<!-- AMOUNT -->
<div class="col-sm-2">
{{ orderLine.metadata.amount | currency:order.metadata.currency:'symbol' }}
</div>
<!-- QUANTITY -->
<div class="col-sm-1">
{{ orderLine.metadata.quantity }}
</div>
<!-- DISCOUNT_AMOUNT -->
<div class="col-sm-2">
{{ orderLine.metadata.discount_amount | currency:order.metadata.currency:'symbol' }}
</div>
<!-- TOTAL_AMOUNT -->
<div class="col-sm-2">
{{ orderLine.metadata.total_amount | currency:order.metadata.currency:'symbol' }}
</div>
<!-- ACTIONS -->
<div class="col-sm-2 p-0 text-right">
<button type="button" class="btn btn-sm btn-outline-primary mr-1"
[routerLink]="['/records', 'acq_order_lines', 'detail', orderLine.metadata.pid]">
<i class="fa fa-eye"></i>
</button>
<button *ngIf="permissions.update.can" type="button" class="btn btn-outline-primary btn-sm mr-1"
[routerLink]="['/records', 'acq_order_lines', 'edit', orderLine.metadata.pid]">
<i class="fa fa-pencil"></i>
</button>

<button *ngIf="this.permissions.delete.can; else deleteInfo"
type="button" class="btn btn-outline-danger btn-sm"
title="{{ 'Delete' | translate}}"
(click)="delete(orderLine.metadata.pid)">
<i class="fa fa-trash" ></i>
</button>
<ng-template #deleteInfo>
<button type="button" class="btn btn-sm btn-outline-danger disabled"
title="{{ 'Delete' | translate}}"
[popover]="tolTemplate" triggers="mouseenter:mouseleave">
<i class="fa fa-trash"></i>
</button>
<ng-template #tolTemplate><div [innerHtml]="deleteInfoMessage | nl2br"></div></ng-template>
</ng-template>
</div>
</div>
</ng-container>
<ng-template #loading>
<i class="fa fa-spin fa-spinner"></i>
</ng-template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* RERO ILS UI
* Copyright (C) 2019 RERO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { RecordPermissionService } from 'projects/admin/src/app/service/record-permission.service';

@Component({
selector: 'admin-order-line',
templateUrl: './order-line.component.html',
styles: []
})
export class OrderLineComponent implements OnInit {

/** order line */
@Input() orderLine: any;

/** parent order */
@Input() order: any;

/** order line permission */
permissions: any;

/** Event for delete order line */
@Output() deleteOrderLine = new EventEmitter();

/**
* Constructor
* @param _recordPermissionService - RecordPermissionService
*/
constructor(
private _recordPermissionService: RecordPermissionService
) { }

/**
* On init hook
*/
ngOnInit() {
this._recordPermissionService.getPermission('acq_order_lines', this.orderLine.metadata.pid).subscribe(
(permissions) => this.permissions = permissions
);
}

/**
* Delete order line
* @param orderLinePid - AcqOrderLine pid
*/
delete(orderLinePid: string) {
this.deleteOrderLine.emit(orderLinePid);
}

/**
* Return a message containing the reasons wht the item cannot be requested
* @return the message to display into the tooltip box
*/
get deleteInfoMessage(): string {
return this._recordPermissionService.generateDeleteMessage(this.permissions.delete.reasons);
}

}
Loading

0 comments on commit 7120284

Please sign in to comment.