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