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