]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/part-merge-dialog.component.ts
LP1892077 Staff catalog Holdings grid more columns
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / record / part-merge-dialog.component.ts
1 import {Component, Input, ViewChild, TemplateRef} from '@angular/core';
2 import {DialogComponent} from '@eg/share/dialog/dialog.component';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
6
7 @Component({
8   selector: 'eg-catalog-part-merge-dialog',
9   templateUrl: './part-merge-dialog.component.html'
10 })
11
12 /**
13  * Ask the user which part is the lead part then merge others parts in.
14  */
15 export class PartMergeDialogComponent extends DialogComponent {
16
17     // What parts are we merging
18     parts: IdlObject[];
19     copyPartMaps: IdlObject[];
20     leadPart: number;
21
22     constructor(
23         private idl: IdlService,
24         private pcrud: PcrudService,
25         private modal: NgbModal) {
26         super(modal);
27     }
28
29     mergeParts() {
30         console.log('Merging parts into lead part ', this.leadPart);
31
32         if (!this.leadPart) { return; }
33
34         this.leadPart = Number(this.leadPart);
35
36         // 1. Migrate copy maps to the lead part.
37         const partIds = this.parts
38             .filter(p => Number(p.id()) !== this.leadPart)
39                .map(p => Number(p.id()));
40
41         const maps = [];
42         this.pcrud.search('acpm', {part: partIds})
43         .subscribe(
44             map => {
45                 map.part(this.leadPart);
46                 map.ischanged(true);
47                 maps.push(map);
48             },
49             err => {},
50             ()  => {
51                 // 2. Delete the now-empty subordinate parts.  Note the
52                 // delete must come after the part map changes are committed.
53                 if (maps.length > 0) {
54                     this.pcrud.autoApply(maps)
55                         .toPromise().then(() => this.deleteParts());
56                 } else {
57                     this.deleteParts();
58                 }
59             }
60         );
61     }
62
63     deleteParts() {
64         const parts = this.parts.filter(p => Number(p.id()) !== this.leadPart);
65         parts.forEach(p => p.isdeleted(true));
66         this.pcrud.autoApply(parts).toPromise().then(res => this.close(res));
67     }
68 }
69
70