]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/course.service.ts
LP1849212: Improvements to course materials admin UI
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / course.service.ts
1 import { Observable } from 'rxjs';
2 import {Injectable} from '@angular/core';
3 import {AuthService} from '@eg/core/auth.service';
4 import {EventService} from '@eg/core/event.service';
5 import {IdlObject, IdlService} from '@eg/core/idl.service';
6 import {NetService} from '@eg/core/net.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9
10 @Injectable()
11 export class CourseService {
12
13     constructor(
14         private auth: AuthService,
15         private evt: EventService,
16         private idl: IdlService,
17         private net: NetService,
18         private org: OrgService,
19         private pcrud: PcrudService
20     ) {}
21
22     isOptedIn(): Promise<any> {
23         return new Promise((resolve) => {
24             this.org.settings('circ.course_materials_opt_in').then(res => {
25                 resolve(res['circ.course_materials_opt_in']);
26             });
27         });
28     }
29     getCourses(course_ids?: Number[]): Promise<IdlObject[]> {
30         if (!course_ids) {
31             return this.pcrud.retrieveAll('acmc',
32                 {}, {atomic: true}).toPromise();
33         } else {
34             return this.pcrud.search('acmc', {id: course_ids},
35                 {}, {atomic: true}).toPromise();
36         }
37     }
38
39     getMaterials(course_ids?: Number[]): Promise<IdlObject[]> {
40         if (!course_ids) {
41             return this.pcrud.retrieveAll('acmcm',
42                 {}, {atomic: true}).toPromise();
43         } else {
44             return this.pcrud.search('acmcm', {course: course_ids},
45                 {}, {atomic: true}).toPromise();
46         }
47     }
48
49     getUsers(course_ids?: Number[]): Observable<IdlObject> {
50         const flesher = {
51             flesh: 1,
52             flesh_fields: {'acmcu': ['usr']}
53         };
54         if (!course_ids) {
55             return this.pcrud.retrieveAll('acmcu',
56                 flesher);
57         } else {
58             return this.pcrud.search('acmcu', {course: course_ids},
59                 flesher);
60         }
61     }
62
63     getCoursesFromMaterial(copy_id): Promise<any> {
64         const id_list = [];
65         return new Promise((resolve, reject) => {
66
67             return this.pcrud.search('acmcm', {item: copy_id})
68             .subscribe(materials => {
69                 if (materials) {
70                     id_list.push(materials.course());
71                 }
72             }, err => {
73                 console.debug(err);
74                 reject(err);
75             }, () => {
76                 if (id_list.length) {
77                     return this.getCourses(id_list).then(courses => {
78                         resolve(courses);
79                     });
80                 }
81             });
82         });
83     }
84
85     fetchCopiesInCourseFromRecord(record_id) {
86         const cp_list = [];
87         return new Promise((resolve, reject) => {
88             this.net.request(
89                 'open-ils.cat',
90                 'open-ils.cat.asset.copy_tree.global.retrieve',
91                 this.auth.token(), record_id
92             ).subscribe(copy_tree => {
93                 copy_tree.forEach(cn => {
94                     cn.copies().forEach(cp => {
95                         cp_list.push(cp.id());
96                     });
97                 });
98             }, err => reject(err),
99             () => {
100                 resolve(this.getCoursesFromMaterial(cp_list));
101             });
102         });
103     }
104
105     // Creating a new acmcm Entry
106     associateMaterials(item, args) {
107         const material = this.idl.create('acmcm');
108         material.item(item.id());
109         if (item.call_number() && item.call_number().record()) {
110             material.record(item.call_number().record());
111         }
112         material.course(args.currentCourse.id());
113         if (args.relationship) { material.relationship(args.relationship); }
114
115         // Apply temporary fields to the item
116         if (args.isModifyingStatus && args.tempStatus) {
117             material.original_status(item.status());
118             item.status(args.tempStatus);
119         }
120         if (args.isModifyingLocation && args.tempLocation) {
121             material.original_location(item.location());
122             item.location(args.tempLocation);
123         }
124         if (args.isModifyingCircMod) {
125             material.original_circ_modifier(item.circ_modifier());
126             item.circ_modifier(args.tempCircMod);
127             if (!args.tempCircMod) { item.circ_modifier(null); }
128         }
129         if (args.isModifyingCallNumber) {
130             material.original_callnumber(item.call_number());
131         }
132         const response = {
133             item: item,
134             material: this.pcrud.create(material).toPromise()
135         };
136
137         return response;
138     }
139
140     associateUsers(patron_id, args) {
141         const new_user = this.idl.create('acmcu');
142         if (args.is_public) { new_user.is_public(args.is_public); }
143         if (args.role) { new_user.usr_role(args.role); }
144         new_user.course(args.currentCourse.id());
145         new_user.usr(patron_id);
146         return this.pcrud.create(new_user).toPromise();
147     }
148
149     disassociateMaterials(courses) {
150         return new Promise((resolve, reject) => {
151             const course_ids = [];
152             const course_library_hash = {};
153             courses.forEach(course => {
154                 course_ids.push(course.id());
155                 course_library_hash[course.id()] = course.owning_lib();
156             });
157             this.pcrud.search('acmcm', {course: course_ids}).subscribe(material => {
158                 material.isdeleted(true);
159                 this.resetItemFields(material, course_library_hash[material.course()]);
160                 this.pcrud.autoApply(material).subscribe(() => {
161                 }, err => {
162                     reject(err);
163                 }, () => {
164                     resolve(material);
165                 });
166             }, err => {
167                 reject(err);
168             }, () => {
169                 resolve(courses);
170             });
171         });
172     }
173
174     disassociateUsers(user) {
175         return new Promise((resolve, reject) => {
176             const user_ids = [];
177             const course_library_hash = {};
178             user.forEach(course => {
179                 user_ids.push(course.id());
180                 course_library_hash[course.id()] = course.owning_lib();
181             });
182             this.pcrud.search('acmcu', {user: user_ids}).subscribe(u => {
183                 u.course(user_ids);
184                 this.pcrud.autoApply(user).subscribe(res => {
185                     console.debug(res);
186                 }, err => {
187                     reject(err);
188                 }, () => {
189                     resolve(user);
190                 });
191             }, err => {
192                 reject(err);
193             }, () => {
194                 resolve(user_ids);
195             });
196         });
197     }
198
199     resetItemFields(material, course_lib) {
200         this.pcrud.retrieve('acp', material.item(),
201             {flesh: 3, flesh_fields: {acp: ['call_number']}}).subscribe(copy => {
202             if (material.original_status()) {
203                 copy.status(material.original_status());
204             }
205             if (copy.circ_modifier() !== material.original_circ_modifier()) {
206                 copy.circ_modifier(material.original_circ_modifier());
207             }
208             if (material.original_location()) {
209                 copy.location(material.original_location());
210             }
211             if (material.original_callnumber()) {
212                 this.pcrud.retrieve('acn', material.original_callnumber()).subscribe(cn => {
213                     this.updateItem(copy, course_lib, cn.label(), true);
214                 });
215             } else {
216                 this.updateItem(copy, course_lib, copy.call_number().label(), false);
217             }
218         });
219     }
220
221     updateItem(item: IdlObject, course_lib, call_number, updatingVolume) {
222         return new Promise((resolve, reject) => {
223             this.pcrud.update(item).subscribe(() => {
224                 if (updatingVolume) {
225                     const cn = item.call_number();
226                     return this.net.request(
227                         'open-ils.cat', 'open-ils.cat.call_number.find_or_create',
228                         this.auth.token(), call_number, cn.record(),
229                         course_lib, cn.prefix(), cn.suffix(),
230                         cn.label_class()
231                     ).subscribe(res => {
232                         const event = this.evt.parse(res);
233                         if (event) { return; }
234                         return this.net.request(
235                             'open-ils.cat', 'open-ils.cat.transfer_copies_to_volume',
236                             this.auth.token(), res.acn_id, [item.id()]
237                         ).subscribe(transfered_res => {
238                             console.debug('Copy transferred to volume with code ' + transfered_res);
239                         }, err => {
240                             reject(err);
241                         }, () => {
242                             resolve(item);
243                         });
244                     }, err => {
245                         reject(err);
246                     }, () => {
247                         resolve(item);
248                     });
249                 } else {
250                     this.pcrud.update(item).subscribe(() => {
251                         resolve(item);
252                     }, () => {
253                         reject(item);
254                     });
255                 }
256             });
257         });
258     }
259
260 }