]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/copy-notes-dialog.component.ts
LP1888723 Item notes dialog and volcopy entry point
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / copy-notes-dialog.component.ts
1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {Observable, throwError, from, empty} from 'rxjs';
3 import {tap, map, switchMap} from 'rxjs/operators';
4 import {NetService} from '@eg/core/net.service';
5 import {IdlService, IdlObject} from '@eg/core/idl.service';
6 import {EventService} from '@eg/core/event.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {AuthService} from '@eg/core/auth.service';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {OrgService} from '@eg/core/org.service';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {DialogComponent} from '@eg/share/dialog/dialog.component';
13 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
14
15 /**
16  * Dialog for managing copy notes.
17  */
18
19 @Component({
20   selector: 'eg-copy-notes-dialog',
21   templateUrl: 'copy-notes-dialog.component.html'
22 })
23
24 export class CopyNotesDialogComponent
25     extends DialogComponent implements OnInit {
26
27     // If there are multiple copyIds, only new notes may be applied.
28     // If there is only one copyId, then notes may be applied or removed.
29     @Input() copyIds: number[] = [];
30
31     mode: string; // create | manage
32
33     // If true, no attempt is made to save the new notes to the
34     // database.  It's assumed this takes place in the calling code.
35     // This is useful for creating notes for new copies.
36     @Input() inPlaceMode = false;
37
38     // In 'create' mode, we may be adding notes to multiple copies.
39     copies: IdlObject[] = [];
40
41     // In 'manage' mode we only handle a single copy.
42     copy: IdlObject;
43
44     curNote: string;
45     curNoteTitle: string;
46     curNotePublic = false;
47     newNotes: IdlObject[] = [];
48     delNotes: IdlObject[] = [];
49
50     autoId = -1;
51
52     @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
53     @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
54
55     constructor(
56         private modal: NgbModal, // required for passing to parent
57         private toast: ToastService,
58         private net: NetService,
59         private idl: IdlService,
60         private pcrud: PcrudService,
61         private org: OrgService,
62         private auth: AuthService) {
63         super(modal); // required for subclassing
64     }
65
66     ngOnInit() {
67     }
68
69     /**
70      */
71     open(args: NgbModalOptions): Observable<IdlObject[]> {
72         this.copy = null;
73         this.copies = [];
74         this.newNotes = [];
75
76         if (this.copyIds.length === 0 && !this.inPlaceMode) {
77             return throwError('copy ID required');
78         }
79
80         // In manage mode, we can only manage a single copy.
81         // But in create mode, we can add notes to multiple copies.
82
83         if (this.copyIds.length === 1 && !this.inPlaceMode) {
84             this.mode = 'manage';
85         } else {
86             this.mode = 'create';
87         }
88
89         // Observify data loading
90         const obs = from(this.getCopies());
91
92         // Return open() observable to caller
93         return obs.pipe(switchMap(_ => super.open(args)));
94     }
95
96     getCopies(): Promise<any> {
97         if (this.inPlaceMode) { return Promise.resolve(); }
98
99         return this.pcrud.search('acp', {id: this.copyIds},
100             {flesh: 1, flesh_fields: {acp: ['notes']}},
101             {atomic: true}
102         )
103         .toPromise().then(copies => {
104             this.copies = copies;
105             if (copies.length === 1) {
106                 this.copy = copies[0];
107             }
108         });
109     }
110
111     removeNote(note: IdlObject) {
112         this.newNotes = this.newNotes.filter(t => t.id() !== note.id());
113
114         if (note.isnew() || this.mode === 'create') { return; }
115
116         const existing = this.copy.notes().filter(n => n.id() === note.id())[0];
117         if (!existing) { return; }
118
119         existing.isdeleted(true);
120         this.delNotes.push(existing);
121
122         // Remove from copy for dialog display
123         this.copy.notes(this.copy.notes().filter(n => n.id() !== note.id()));
124     }
125
126     addNew() {
127         if (!this.curNoteTitle || !this.curNote) { return; }
128
129         const note = this.idl.create('acpn');
130         note.isnew(true);
131         note.creator(this.auth.user().id());
132         note.pub(this.curNotePublic ? 't' : 'f');
133         note.title(this.curNoteTitle);
134         note.value(this.curNote);
135         note.id(this.autoId--);
136
137         this.newNotes.push(note);
138
139         this.curNote = '';
140         this.curNoteTitle = '';
141         this.curNotePublic = false;
142     }
143
144     applyChanges() {
145
146         if (this.inPlaceMode) {
147             this.close(this.newNotes);
148             return;
149         }
150
151         const notes = [];
152         this.newNotes.forEach(note => {
153             this.copies.forEach(copy => {
154                 const n = this.idl.clone(note);
155                 n.id(null); // remove temp ID, it will be duped
156                 n.owning_copy(copy.id());
157                 notes.push(n);
158             });
159         });
160
161         this.pcrud.create(notes).toPromise()
162         .then(_ => {
163             if (this.delNotes.length) {
164                 return this.pcrud.remove(this.delNotes).toPromise();
165             }
166         }).then(_ => {
167             this.successMsg.current().then(msg => this.toast.success(msg));
168             this.close(this.newNotes.length > 0 || this.delNotes.length > 0);
169         });
170     }
171 }
172