8534d2143787b233e2f387f72e478a9e44f15caf
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-associate-material.component.ts
1 import {Component, Input, ViewChild, OnInit, TemplateRef} from '@angular/core';
2 import {ActivatedRoute} from '@angular/router';
3 import {from, merge, Observable} from 'rxjs';
4 import {DialogComponent} from '@eg/share/dialog/dialog.component';
5 import {AuthService} from '@eg/core/auth.service';
6 import {NetService} from '@eg/core/net.service';
7 import {EventService} from '@eg/core/event.service';
8 import {OrgService} from '@eg/core/org.service';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {Pager} from '@eg/share/util/pager';
11 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
12 import {GridDataSource} from '@eg/share/grid/grid';
13 import {GridComponent} from '@eg/share/grid/grid.component';
14 import {IdlObject, IdlService} from '@eg/core/idl.service';
15 import {StringComponent} from '@eg/share/string/string.component';
16 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
17 import {ToastService} from '@eg/share/toast/toast.service';
18 import {CourseService} from '@eg/staff/share/course.service';
19
20 @Component({
21     selector: 'eg-course-associate-material-dialog',
22     templateUrl: './course-associate-material.component.html'
23 })
24
25 export class CourseAssociateMaterialComponent extends DialogComponent implements OnInit {
26     @Input() currentCourse: IdlObject;
27     @Input() courseId: any;
28     @Input() courseIsArchived: string;
29     @Input() displayMode: string;
30     materials: any[] = [];
31     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
32     @ViewChild('materialsGrid', {static: false}) materialsGrid: GridComponent;
33     @ViewChild('materialDeleteFailedString', { static: true })
34         materialDeleteFailedString: StringComponent;
35     @ViewChild('materialDeleteSuccessString', { static: true })
36         materialDeleteSuccessString: StringComponent;
37     @ViewChild('materialAddSuccessString', { static: true })
38         materialAddSuccessString: StringComponent;
39     @ViewChild('materialAddFailedString', { static: true })
40         materialAddFailedString: StringComponent;
41     @ViewChild('materialEditSuccessString', { static: true })
42         materialEditSuccessString: StringComponent;
43     @ViewChild('materialEditFailedString', { static: true })
44         materialEditFailedString: StringComponent;
45     @ViewChild('materialAddDifferentLibraryString', { static: true })
46         materialAddDifferentLibraryString: StringComponent;
47     materialsDataSource: GridDataSource;
48     @Input() barcodeInput: string;
49     @Input() relationshipInput: string;
50     @Input() tempCallNumber: string;
51     @Input() tempStatus: number;
52     @Input() tempLocation: number;
53     @Input() tempCircMod: string;
54     @Input() isModifyingStatus: boolean;
55     @Input() isModifyingCircMod: boolean;
56     @Input() isModifyingCallNumber: boolean;
57     @Input() isModifyingLocation: boolean;
58     bibId: number;
59
60     associateBriefRecord: (newRecord: string) => void;
61     associateElectronicBibRecord: () => void;
62
63     constructor(
64         private auth: AuthService,
65         private course: CourseService,
66         private event: EventService,
67         private idl: IdlService,
68         private net: NetService,
69         private org: OrgService,
70         private pcrud: PcrudService,
71         private route: ActivatedRoute,
72         private toast: ToastService,
73         private modal: NgbModal
74     ) {
75         super(modal);
76         this.materialsDataSource = new GridDataSource();
77
78         this.materialsDataSource.getRows = (pager: Pager, sort: any[]) => {
79             return this.net.request(
80                 'open-ils.courses',
81                 'open-ils.courses.course_materials.retrieve.fleshed',
82                 {course: this.courseId}
83             );
84         };
85     }
86
87     ngOnInit() {
88         this.associateBriefRecord = (newRecord: string) => {
89             return this.net.request(
90                 'open-ils.courses',
91                 'open-ils.courses.attach.biblio_record',
92                 this.auth.token(),
93                 newRecord,
94                 this.courseId,
95                 this.relationshipInput
96             ).subscribe(() => {
97                 this.materialsGrid.reload();
98                 this.materialAddSuccessString.current()
99                     .then(str => this.toast.success(str));
100             });
101         };
102
103         this.associateElectronicBibRecord = () => {
104             return this.net.request(
105                 'open-ils.courses',
106                 'open-ils.courses.attach.electronic_resource',
107                 this.auth.token(),
108                 this.bibId,
109                 this.courseId,
110                 this.relationshipInput
111             ).subscribe(() => {
112                 this.materialsGrid.reload();
113                 this.materialAddSuccessString.current()
114                     .then(str => this.toast.success(str));
115             });
116          };
117
118     }
119
120     isDialog(): boolean {
121         return this.displayMode === 'dialog';
122     }
123
124     editSelectedMaterials(itemFields: IdlObject[]) {
125         // Edit each IDL thing one at a time
126         const editOneThing = (item: IdlObject) => {
127             if (!item) { return; }
128
129             this.showEditDialog(item).then(
130                 () => editOneThing(itemFields.shift()));
131         };
132
133         editOneThing(itemFields.shift());
134     }
135
136     showEditDialog(courseMaterial: IdlObject): Promise<any> {
137         this.editDialog.mode = 'update';
138         this.editDialog.recordId = courseMaterial.id();
139         return new Promise((resolve, reject) => {
140             this.editDialog.open({size: 'lg'}).subscribe(
141                 result => {
142                     this.materialEditSuccessString.current()
143                         .then(str => this.toast.success(str));
144                     this.pcrud.retrieve('acmcm', result).subscribe(material => {
145                         if (material.course() !== this.courseId) {
146                             this.materialsGrid.reload();
147                         } else {
148                             courseMaterial.relationship = material.relationship();
149                         }
150                     });
151                     resolve(result);
152                 },
153                 error => {
154                     this.materialEditFailedString.current()
155                         .then(str => this.toast.danger(str));
156                     reject(error);
157                 }
158             );
159         });
160     }
161
162     associateItem(barcode, relationship) {
163         if (barcode) {
164             const args = {
165                 barcode: barcode.trim(),
166                 relationship: relationship,
167                 isModifyingCallNumber: this.isModifyingCallNumber,
168                 isModifyingCircMod: this.isModifyingCircMod,
169                 isModifyingLocation: this.isModifyingLocation,
170                 isModifyingStatus: this.isModifyingStatus,
171                 tempCircMod: this.tempCircMod,
172                 tempLocation: this.tempLocation,
173                 tempStatus: this.tempStatus,
174                 currentCourse: this.currentCourse
175             };
176             this.barcodeInput = null;
177
178             this.pcrud.search('acp', {barcode: args.barcode}, {
179                 flesh: 3, flesh_fields: {acp: ['call_number']}
180             }).subscribe(item => {
181                 const associatedMaterial = this.course.associateMaterials(item, args);
182                 associatedMaterial.material.then(res => {
183                     item = associatedMaterial.item;
184                     let new_cn = item.call_number().label();
185                     if (this.tempCallNumber) { new_cn = this.tempCallNumber; }
186                     this.course.updateItem(item, this.currentCourse.owning_lib(),
187                         new_cn, args.isModifyingCallNumber
188                     ).then(resp => {
189                         this.materialsGrid.reload();
190                         if (item.circ_lib() !== this.currentCourse.owning_lib()) {
191                             this.materialAddDifferentLibraryString.current()
192                             .then(str => this.toast.warning(str));
193                         } else {
194                             this.materialAddSuccessString.current()
195                             .then(str => this.toast.success(str));
196                         }
197                     });
198                 }, err => {
199                     this.materialAddFailedString.current()
200                     .then(str => this.toast.danger(str));
201                 });
202             });
203         }
204     }
205
206     deleteSelectedMaterials(items) {
207         let deleteRequest$ = this.course.detachMaterials(items);
208         merge(...deleteRequest$).subscribe(
209             val => {
210                 this.materialDeleteSuccessString.current().then(str => this.toast.success(str));
211             },
212             err => {
213                 this.materialDeleteFailedString.current()
214                     .then(str => this.toast.danger(str));
215             }
216         ).add(() => {
217             this.materialsGrid.reload();
218         });
219     }
220 }