]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-associate-material.component.ts
LP1849212: Users can attach brief bib records and e-resources to courses
[Evergreen.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, Observable} from 'rxjs';
4 import {switchMap} from 'rxjs/operators';
5 import {DialogComponent} from '@eg/share/dialog/dialog.component';
6 import {AuthService} from '@eg/core/auth.service';
7 import {NetService} from '@eg/core/net.service';
8 import {EventService} from '@eg/core/event.service';
9 import {OrgService} from '@eg/core/org.service';
10 import {PcrudService} from '@eg/core/pcrud.service';
11 import {Pager} from '@eg/share/util/pager';
12 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
13 import {GridDataSource} from '@eg/share/grid/grid';
14 import {GridComponent} from '@eg/share/grid/grid.component';
15 import {IdlObject, IdlService} from '@eg/core/idl.service';
16 import {StringComponent} from '@eg/share/string/string.component';
17 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
18 import {ToastService} from '@eg/share/toast/toast.service';
19 import {CourseService} from '@eg/staff/share/course.service';
20
21 @Component({
22     selector: 'eg-course-associate-material-dialog',
23     templateUrl: './course-associate-material.component.html'
24 })
25
26 export class CourseAssociateMaterialComponent extends DialogComponent implements OnInit {
27     @Input() currentCourse: IdlObject;
28     @Input() courseId: any;
29     @Input() displayMode: String;
30     materials: any[] = [];
31     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
32     @ViewChild('materialsGrid', {static: true}) 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(course_material: IdlObject): Promise<any> {
137         this.editDialog.mode = 'update';
138         this.editDialog.recordId = course_material._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.materialsDataSource.data.splice(
147                                 this.materialsDataSource.data.indexOf(course_material, 0), 1
148                             );
149                         } else {
150                             course_material._relationship = material.relationship();
151                         }
152                     });
153                     resolve(result);
154                 },
155                 error => {
156                     this.materialEditFailedString.current()
157                         .then(str => this.toast.danger(str));
158                     reject(error);
159                 }
160             );
161         });
162     }
163
164     associateItem(barcode, relationship) {
165         if (barcode) {
166             const args = {
167                 barcode: barcode,
168                 relationship: relationship,
169                 isModifyingCallNumber: this.isModifyingCallNumber,
170                 isModifyingCircMod: this.isModifyingCircMod,
171                 isModifyingLocation: this.isModifyingLocation,
172                 isModifyingStatus: this.isModifyingStatus,
173                 tempCircMod: this.tempCircMod,
174                 tempLocation: this.tempLocation,
175                 tempStatus: this.tempStatus,
176                 currentCourse: this.currentCourse
177             };
178             this.barcodeInput = null;
179
180             this.pcrud.search('acp', {barcode: args.barcode}, {
181                 flesh: 3, flesh_fields: {acp: ['call_number']}
182             }).subscribe(item => {
183                 const associatedMaterial = this.course.associateMaterials(item, args);
184                 associatedMaterial.material.then(res => {
185                     item = associatedMaterial.item;
186                     let new_cn = item.call_number().label();
187                     if (this.tempCallNumber) { new_cn = this.tempCallNumber; }
188                     this.course.updateItem(item, this.currentCourse.owning_lib(),
189                         new_cn, args.isModifyingCallNumber
190                     ).then(resp => {
191                         this.materialsGrid.reload();
192                         if (item.circ_lib() !== this.currentCourse.owning_lib()) {
193                             this.materialAddDifferentLibraryString.current()
194                             .then(str => this.toast.warning(str));
195                         } else {
196                             this.materialAddSuccessString.current()
197                             .then(str => this.toast.success(str));
198                         }
199                     });
200                 }, err => {
201                     this.materialAddFailedString.current()
202                     .then(str => this.toast.danger(str));
203                 });
204             });
205         }
206     }
207
208     deleteSelectedMaterials(items) {
209         const item_ids = [];
210         items.forEach(item => {
211             this.materialsDataSource.data.splice(this.materialsDataSource.data.indexOf(item, 0), 1);
212             item_ids.push(item.id());
213         });
214         this.pcrud.search('acmcm', {course: this.courseId, item: item_ids}).subscribe(material => {
215             material.isdeleted(true);
216             this.pcrud.autoApply(material).subscribe(
217                 val => {
218                     this.course.resetItemFields(material, this.currentCourse.owning_lib());
219                     console.debug('deleted: ' + val);
220                     this.materialDeleteSuccessString.current().then(str => this.toast.success(str));
221                 },
222                 err => {
223                     this.materialDeleteFailedString.current()
224                         .then(str => this.toast.danger(str));
225                 }
226             );
227         });
228     }
229 }