]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-associate-users.component.ts
LP2061136 - Stamping 1405 DB upgrade script
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-associate-users.component.ts
1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {DialogComponent} from '@eg/share/dialog/dialog.component';
3 import {AuthService} from '@eg/core/auth.service';
4 import {NetService} from '@eg/core/net.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {Pager} from '@eg/share/util/pager';
7 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
8 import {GridDataSource} from '@eg/share/grid/grid';
9 import {GridComponent} from '@eg/share/grid/grid.component';
10 import {IdlObject} from '@eg/core/idl.service';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
13 import {PatronSearchDialogComponent} from '@eg/staff/share/patron/search-dialog.component';
14 import {ToastService} from '@eg/share/toast/toast.service';
15 import {CourseService} from '@eg/staff/share/course.service';
16 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
17
18 @Component({
19     selector: 'eg-course-associate-users-dialog',
20     templateUrl: './course-associate-users.component.html'
21 })
22
23 export class CourseAssociateUsersComponent extends DialogComponent implements OnInit {
24     @Input() currentCourse: IdlObject;
25     @Input() courseId: number;
26     @Input() courseIsArchived: String;
27     @Input() displayMode: String;
28     users: any[] = [];
29     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
30     @ViewChild('patronSearch') patronSearch: PatronSearchDialogComponent;
31     @ViewChild('usersGrid') usersGrid: GridComponent;
32     @ViewChild('userDeleteFailedString', { static: true })
33         userDeleteFailedString: StringComponent;
34     @ViewChild('userDeleteSuccessString', { static: true })
35         userDeleteSuccessString: StringComponent;
36     @ViewChild('userAddSuccessString', { static: true })
37         userAddSuccessString: StringComponent;
38     @ViewChild('userAddFailedString', { static: true })
39         userAddFailedString: StringComponent;
40     @ViewChild('userEditSuccessString', { static: true })
41         userEditSuccessString: StringComponent;
42     @ViewChild('userEditFailedString', { static: true })
43         userEditFailedString: StringComponent;
44     usersDataSource: GridDataSource;
45     userBarcode: String;
46     userRoleInput: ComboboxEntry;
47
48     constructor(
49         private auth: AuthService,
50         private course: CourseService,
51         private net: NetService,
52         private pcrud: PcrudService,
53         private toast: ToastService,
54         private modal: NgbModal
55     ) {
56         super(modal);
57         this.usersDataSource = new GridDataSource();
58     }
59
60     ngOnInit() {
61         this.usersDataSource.getRows = (pager: Pager, sort: any[]) => {
62             return this.course.getUsers([this.courseId]);
63         };
64     }
65
66     isDialog(): boolean {
67         return this.displayMode === 'dialog';
68     }
69
70     associateUser(barcode) {
71         if (barcode) {
72             const args = {
73                 currentCourse: this.currentCourse,
74                 barcode: barcode.trim(),
75             };
76
77             if (this.userRoleInput) {
78                 args['role'] = this.userRoleInput.id;
79             }
80
81             this.userBarcode = null;
82
83             this.net.request(
84                 'open-ils.actor',
85                 'open-ils.actor.user.retrieve_id_by_barcode_or_username',
86                 this.auth.token(), barcode.trim()
87             ).subscribe(patron => {
88                 this.course.associateUsers(patron, args)
89                     .then(() => this.usersGrid.reload());
90             }, (err: unknown) => {
91                 this.userAddFailedString.current().then(str => this.toast.danger(str));
92             }
93             );
94         }
95     }
96
97     editSelectedUsers(userFields: IdlObject[]) {
98         // Edit each IDL thing one at a time
99         const editOneThing = (user: IdlObject) => {
100             if (!user) { return; }
101
102             this.showEditDialog(user).then(
103                 () => editOneThing(userFields.shift()));
104         };
105
106         editOneThing(userFields.shift());
107     }
108
109     searchPatrons() {
110         this.patronSearch.open({size: 'xl'}).toPromise().then(
111             patrons => {
112                 if (!patrons || patrons.length === 0) { return; }
113                 const user = patrons[0];
114                 this.userBarcode = user.card().barcode();
115             }
116         );
117     }
118
119     showEditDialog(user: IdlObject): Promise<any> {
120         this.editDialog.mode = 'update';
121         this.editDialog.recordId = user.id();
122         return new Promise((resolve, reject) => {
123             this.editDialog.open({size: 'lg'}).subscribe(
124                 result => {
125                     this.userEditSuccessString.current()
126                         .then(str => this.toast.success(str));
127                     this.usersGrid.reload();
128                     resolve(result);
129                 },
130                 (error: unknown) => {
131                     this.userEditFailedString.current()
132                         .then(str => this.toast.danger(str));
133                     reject(error);
134                 }
135             );
136         });
137     }
138
139     deleteSelectedUsers(users) {
140         const acmcu_ids = users.map(u => u.id());
141         this.pcrud.search('acmcu', {course: this.courseId, id: acmcu_ids}).subscribe(user => {
142             user.isdeleted(true);
143             // eslint-disable-next-line rxjs/no-nested-subscribe
144             this.pcrud.autoApply(user).subscribe(
145                 val => {
146                     console.debug('deleted: ' + val);
147                     this.userDeleteSuccessString.current().then(str => this.toast.success(str));
148                     this.usersGrid.reload();
149                 },
150                 (err: unknown) => {
151                     this.userDeleteFailedString.current()
152                         .then(str => this.toast.danger(str));
153                 }
154             );
155         }).add(() => this.usersGrid.reload());
156     }
157
158 }