LP#1832897: add administrative interfaces for carousels
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / admin-carousel.component.ts
1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {AdminPageComponent} from '@eg/staff/share/admin-page/admin-page.component';
3 import {ActivatedRoute} from '@angular/router';
4 import {IdlService, IdlObject} from '@eg/core/idl.service';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {PermService} from '@eg/core/perm.service';
9 import {AuthService} from '@eg/core/auth.service';
10 import {NetService} from '@eg/core/net.service';
11 import {StringComponent} from '@eg/share/string/string.component';
12
13 @Component({
14     templateUrl: './admin-carousel.component.html'
15 })
16
17 export class AdminCarouselComponent extends AdminPageComponent implements OnInit {
18
19     idlClass = 'cc';
20     classLabel: string;
21
22     refreshSelected: (idlThings: IdlObject[]) => void;
23     createNew: () => void;
24     deleteSelected: (idlThings: IdlObject[]) => void;
25
26     @ViewChild('refreshString') refreshString: StringComponent;
27     @ViewChild('refreshErrString') refreshErrString: StringComponent;
28
29     constructor(
30         route: ActivatedRoute,
31         idl: IdlService,
32         org: OrgService,
33         auth: AuthService,
34         pcrud: PcrudService,
35         perm: PermService,
36         toast: ToastService,
37         private net: NetService
38     ) {
39         super(route, idl, org, auth, pcrud, perm, toast);
40     }
41
42     ngOnInit() {
43         super.ngOnInit();
44
45         this.classLabel = this.idlClassDef.label;
46         this.includeOrgDescendants = true;
47
48
49         this.createNew = () => {
50             super.createNew();
51         };
52
53         this.deleteSelected = (idlThings: IdlObject[]) => {
54             super.deleteSelected(idlThings);
55         };
56
57         this.refreshSelected = (idlThings: IdlObject[]) =>  {
58             idlThings.forEach(cc => {
59                 if (cc.type().automatic() === 't') {
60                     this.net.request(
61                         'open-ils.actor',
62                         'open-ils.actor.carousel.refresh',
63                         this.auth.token(), cc.id()
64                     ).toPromise(); // fire and forget, as this could take a couple minutes
65                     this.refreshString.current({ name: cc.name() }).then(str => this.toast.success(str));
66                 } else {
67                     this.refreshErrString.current({ name: cc.name() }).then(str => this.toast.warning(str));
68                 }
69             });
70         };
71     }
72
73     mungeCarousel(editMode: string, rec: IdlObject) {
74         if (editMode === 'create') {
75             rec.creator(this.auth.user().id());
76         }
77         rec.editor(this.auth.user().id());
78         rec.edit_time('now');
79
80         // convert empty string to nulls as needed
81         // for int[] columns
82         if (rec.owning_lib_filter() === '') {
83             rec.owning_lib_filter(null);
84         }
85         if (rec.copy_location_filter() === '') {
86             rec.copy_location_filter(null);
87         }
88     }
89
90     postSave(rec: IdlObject) {
91         if (rec._isfieldmapper) {
92             // if we got an actual IdlObject back, the
93             // record had just been created, not just
94             // edited. therefore, we probably need
95             if (rec.bucket() == null) {
96                 const bucket = this.idl.create('cbreb');
97                 bucket.owner(this.auth.user().id());
98                 bucket.name('System-generated bucket for carousel: ' + rec.id()); // FIXME I18N
99                 bucket.btype('carousel');
100                 bucket.pub('t');
101                 bucket.owning_lib(rec.owner());
102                 rec.bucket(bucket);
103                 this.net.request(
104                     'open-ils.actor',
105                     'open-ils.actor.container.create',
106                     this.auth.token(), 'biblio', bucket
107                 ).toPromise().then(
108                     newBucket => {
109                         const ccou = this.idl.create('ccou');
110                         ccou.carousel(rec.id());
111                         ccou.org_unit(rec.owner());
112                         ccou.seq(0);
113                         rec.bucket(newBucket);
114                         this.pcrud.create(ccou).subscribe(
115                             ok => {
116                                 this.pcrud.update(rec).subscribe(
117                                     ok2 => console.debug('updated'),
118                                     err => console.error(err),
119                                     () => { this.grid.reload(); }
120                                 );
121                             },
122                             err => console.error(err),
123                             () => { this.grid.reload(); }
124                         );
125                     }
126                 );
127             }
128         }
129     }
130 }