Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patron: allows librarians to update user password #411

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions projects/admin/src/app/circulation/checkin/checkin.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,21 @@ export class CheckinComponent implements OnInit {
if (barcode) {
this.isLoading = true;
this._recordService
.getRecords('patrons', `barcode:${barcode}`, 1, 1)
.getRecords('patrons', `${barcode}`, 1, 2, [], {simple: 1, roles: 'patron'})
.pipe(
map((response: any) => (this._recordService.totalHits(response.hits.total) === 0)
? null
: response.hits.hits[0].metadata
)
map((response: any) => {
const total = this._recordService.totalHits(response.hits.total);
if (total === 0) {
return null;
}
if (total > 1) {
this._toastService.warning(
this._translate.instant('Found more than one patron.'),
this._translate.instant('Checkin')
);
}
return response.hits.hits[0].metadata;
})
).subscribe(
patron => {
if (
Expand Down
17 changes: 10 additions & 7 deletions projects/admin/src/app/circulation/circulation.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { FormlyModule } from '@ngx-formly/core';
import { RecordModule } from '@rero/ng-core';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
import { CollapseModule } from 'ngx-bootstrap/collapse';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
import { SharedPipesModule } from '../shared/shared-pipes.module';
import { CheckinComponent } from './checkin/checkin.component';
import { CirculationRoutingModule } from './circulation-routing.module';
import { ItemComponent } from './item/item.component';
import { ItemsListComponent } from './items-list/items-list.component';
import { MainRequestComponent } from './main-request/main-request.component';
import { RequestedItemComponent } from './main-request/requested-item/requested-item.component';
import { RequestedItemsListComponent } from './main-request/requested-items-list/requested-items-list.component';
import { CardComponent } from './patron/card/card.component';
import { ChangePasswordFormComponent } from './patron/change-password-form/change-password-form.component';
import { HistoryItemComponent } from './patron/history/history-item/history-item.component';
import { HistoryComponent } from './patron/history/history.component';
import { LoanComponent } from './patron/loan/loan.component';
Expand All @@ -46,13 +49,11 @@ import {
} from './patron/patron-transactions/patron-transaction/overdue-transaction/overdue-transaction.component';
import { PatronTransactionComponent } from './patron/patron-transactions/patron-transaction/patron-transaction.component';
import { PatronTransactionsComponent } from './patron/patron-transactions/patron-transactions.component';
import { PendingItemComponent } from './patron/pending/pending-item/pending-item.component';
import { PendingComponent } from './patron/pending/pending.component';
import { PickupItemComponent } from './patron/pickup/pickup-item/pickup-item.component';
import { PickupComponent } from './patron/pickup/pickup.component';
import { ProfileComponent } from './patron/profile/profile.component';
import { PendingItemComponent } from './patron/pending/pending-item/pending-item.component';
import { PendingComponent } from './patron/pending/pending.component';
import { RequestedItemsListComponent } from './main-request/requested-items-list/requested-items-list.component';
import { RequestedItemComponent } from './main-request/requested-item/requested-item.component';


@NgModule({
Expand All @@ -79,7 +80,8 @@ import { RequestedItemComponent } from './main-request/requested-item/requested-
PatronTransactionEventFormComponent,
HistoryComponent,
HistoryItemComponent,
RequestedItemComponent
RequestedItemComponent,
ChangePasswordFormComponent
],
imports: [
CommonModule,
Expand All @@ -93,7 +95,8 @@ import { RequestedItemComponent } from './main-request/requested-item/requested-
SharedPipesModule
],
entryComponents: [
PatronTransactionEventFormComponent
PatronTransactionEventFormComponent,
ChangePasswordFormComponent
]
})
export class CirculationModule { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--
RERO ILS UI
Copyright (C) 2020 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/>.
-->
<div *ngIf="form" class="modal show d-block" role="dialog">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" translate>Update Patron Password</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" data-dismiss="modal" (click)="closeModal()">&times;</span>
</button>
</div>
<div class="modal-body">
<form [formGroup]="form" (ngSubmit)="submit(patron, model)">
<formly-form [form]="form" [fields]="formFields" [model]="model"></formly-form>
<div class="text-right">
<button id="change-password-submit-button" class="btn btn-primary" [disabled]="!form.valid" translate>Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* RERO ILS UI
* Copyright (C) 2020 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 { HttpClientTestingModule } from '@angular/common/http/testing';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { CirculationModule } from '../../circulation.module';
import { ChangePasswordFormComponent } from './change-password-form.component';


describe('ChangePasswordFormComponent', () => {
let component: ChangePasswordFormComponent;
let fixture: ComponentFixture<ChangePasswordFormComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot(),
HttpClientTestingModule,
CirculationModule
],
providers: [
BsModalRef
]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ChangePasswordFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* RERO ILS UI
* Copyright (C) 2020 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, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { TranslateService } from '@ngx-translate/core';
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
import { ToastrService } from 'ngx-toastr';
import { User } from '../../../class/user';
import { UserService } from '../../../service/user.service';

@Component({
selector: 'admin-change-password-form',
templateUrl: './change-password-form.component.html'
})
export class ChangePasswordFormComponent implements OnInit {

/** patron to change the password */
patron: User;

/** form */
form: FormGroup = new FormGroup({});

/** model */
model = {};

/** form fields */
formFields: FormlyFieldConfig[];

/**
* Constructor
* @param _modalService - BsModalService
* @param _bsModalRef - BsModalRef
* @param _translateService - TranslateService
* @param _toastr - ToastrService
* @param _userService - UserService
*/
constructor(
private _modalService: BsModalService,
private _bsModalRef: BsModalRef,
private _translateService: TranslateService,
private _toastr: ToastrService,
private _userService: UserService
) { }

/**
* Component initialization.
*/
ngOnInit() {
const initialState: any = this._modalService.config.initialState;
if (initialState.hasOwnProperty('patron')) {
this.closeModal();
}
this.patron = initialState.patron;
this._initForm();
}

/**
* Submit form
* @param model - Object
*/
submit(patron, model) {
this._userService.changePassword(patron.username, model.password).subscribe(
() => {
this._toastr.success(
this._translateService.instant('The patron password has been changed.'),
);
this.closeModal();
},
(resp) => {
console.log('Error: Update Patron Password', resp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console.log

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it is to leave at is as it is displayed only when an error occurs. This can be useful. But if you want I can remove it.

let error = this._translateService.instant('An error has occurred.');
if (resp.error && resp.error.message) {
error = `${error}: (${resp.error.message})`;
}
this._toastr.error(
error,
this._translateService.instant('Update Patron Password'),
{ disableTimeOut: true }
);
this.closeModal();
}
);
}

/**
* Initialize formly form.
*/
private _initForm() {
if (this.patron) {
this.formFields = [
{
key: 'password',
type: 'input',
focus: true,
templateOptions: {
type: 'password',
label: this._translateService.instant('New password'),
required: true,
// same as Invenio
minLength: 6,
maxLength: 128,
keydown: (field, event) => {
if (event.key === 'Enter') {
event.preventDefault();
}
}
}
}
];
}
}

/**
* Close modal dialog
* @param event - Event
*/
closeModal() {
this._bsModalRef.hide();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export class LoanComponent implements OnInit, OnDestroy {
/** Library PID of the logged user */
currentLibraryPid: string;

/** list of subscriptions */
private _subcription = new Subscription();
/** Observable subscription */
private _subscription = new Subscription();

/** checkout list sort criteria */
private _sortCriteria = '-transaction_date';
Expand All @@ -75,7 +75,7 @@ export class LoanComponent implements OnInit, OnDestroy {
) {}

ngOnInit() {
this._subcription.add(this._patronService.currentPatron$.subscribe(patron => {
this._subscription.add(this._patronService.currentPatron$.subscribe(patron => {
this.patron = patron;
if (patron) {
this.isLoading = true;
Expand All @@ -99,7 +99,7 @@ export class LoanComponent implements OnInit, OnDestroy {
}

ngOnDestroy() {
this._subcription.unsubscribe();
this._subscription.unsubscribe();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
<!--
RERO ILS UI
 Copyright (C) 2020 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="transactions">
<form [formGroup]="form" (ngSubmit)="onSubmitForm()">
<div class="modal-header">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
<!--
RERO ILS UI
 Copyright (C) 2020 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/>.
-->
<section *ngIf="!isLoading; else loading" class="content">
<ng-container *ngIf="transactions.length > 0; else noTransaction">
<!-- HEADER ~~~~~~~~~~~~~~~~~~~~~~~~ -->
Expand Down
Loading