]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/course.service.ts
7443a79c2d1836c5910e18bdafbf68021133cb74
[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         if (!course_ids) {
32             return this.pcrud.retrieveAll('acmc',
33                 {flesh: 1, flesh_fields: {'acmc': ['owning_lib']}},
34                 {atomic: true}).toPromise();
35         } else {
36             return this.pcrud.search('acmc', {id: course_ids},
37                 {flesh: 1, flesh_fields: {'acmc': ['owning_lib']}},
38                 {atomic: true}).toPromise();
39         }
40     }
41
42     getMaterials(course_ids?: Number[]): Promise<IdlObject[]> {
43         if (!course_ids) {
44             return this.pcrud.retrieveAll('acmcm',
45                 {}, {atomic: true}).toPromise();
46         } else {
47             return this.pcrud.search('acmcm', {course: course_ids},
48                 {}, {atomic: true}).toPromise();
49         }
50     }
51
52     getUsers(course_ids?: Number[]): Observable<IdlObject> {
53         const flesher = {
54             flesh: 1,
55             flesh_fields: {'acmcu': ['usr']}
56         };
57         if (!course_ids) {
58             return this.pcrud.retrieveAll('acmcu',
59                 flesher);
60         } else {
61             return this.pcrud.search('acmcu', {course: course_ids},
62                 flesher);
63         }
64     }
65
66     getCoursesFromMaterial(copy_id): Promise<any> {
67         const id_list = [];
68         return new Promise((resolve, reject) => {
69
70             return this.pcrud.search('acmcm', {item: copy_id})
71             .subscribe(materials => {
72                 if (materials) {
73                     id_list.push(materials.course());
74                 }
75             }, err => {
76                 console.debug(err);
77                 reject(err);
78             }, () => {
79                 if (id_list.length) {
80                     return this.getCourses(id_list).then(courses => {
81                         resolve(courses);
82                     });
83                 }
84             });
85         });
86     }
87
88     fetchCoursesForRecord(recordId) {
89         const courseIds = [];
90                  return this.pcrud.search(
91             'acmcm', {record: recordId}, {atomic: false}
92         ).pipe(tap(material => {
93             if (courseIds.indexOf(material.course()) === -1) {
94                 courseIds.push(material.course());
95             }
96         })).toPromise().then(() => this.getCourses(courseIds));
97     }
98
99     // Creating a new acmcm Entry
100     associateMaterials(item, args) {
101         const material = this.idl.create('acmcm');
102         material.item(item.id());
103         if (item.call_number() && item.call_number().record()) {
104             material.record(item.call_number().record());
105         }
106         material.course(args.currentCourse.id());
107         if (args.relationship) { material.relationship(args.relationship); }
108
109         // Apply temporary fields to the item
110         if (args.isModifyingStatus && args.tempStatus) {
111             material.original_status(item.status());
112             item.status(args.tempStatus);
113         }
114         if (args.isModifyingLocation && args.tempLocation) {
115             material.original_location(item.location());
116             item.location(args.tempLocation);
117         }
118         if (args.isModifyingCircMod) {
119             material.original_circ_modifier(item.circ_modifier());
120             item.circ_modifier(args.tempCircMod);
121             if (!args.tempCircMod) { item.circ_modifier(null); }
122         }
123         if (args.isModifyingCallNumber) {
124             material.original_callnumber(item.call_number());
125         }
126         const response = {
127             item: item,
128             material: this.pcrud.create(material).toPromise()
129         };
130
131         return response;
132     }
133
134     associateUsers(patron_id, args) {
135         const new_user = this.idl.create('acmcu');
136         if (args.is_public) { new_user.is_public(args.is_public); }
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 }