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