]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/circ/patron/messages.component.ts
LP1904036 Penalty dialog resets after each use
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / circ / patron / messages.component.ts
1 import {Component, ViewChild, OnInit, Input, AfterViewInit} from '@angular/core';
2 import {empty, from} from 'rxjs';
3 import {concatMap, tap} from 'rxjs/operators';
4 import {IdlObject} 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 import {AuthService} from '@eg/core/auth.service';
9 import {ServerStoreService} from '@eg/core/server-store.service';
10 import {PatronService} from '@eg/staff/share/patron/patron.service';
11 import {PatronContextService} from './patron.service';
12 import {GridDataSource, GridColumn, GridCellTextGenerator} from '@eg/share/grid/grid';
13 import {GridComponent} from '@eg/share/grid/grid.component';
14 import {Pager} from '@eg/share/util/pager';
15 import {DateUtil} from '@eg/share/util/date';
16 import {PatronPenaltyDialogComponent
17     } from '@eg/staff/share/patron/penalty-dialog.component';
18
19 @Component({
20   selector: 'eg-patron-messages',
21   templateUrl: 'messages.component.html'
22 })
23 export class PatronMessagesComponent implements OnInit {
24
25     @Input() patronId: number;
26
27     mainDataSource: GridDataSource = new GridDataSource();
28     archiveDataSource: GridDataSource = new GridDataSource();
29
30     startDateYmd: string;
31     endDateYmd: string;
32
33     @ViewChild('mainGrid') private mainGrid: GridComponent;
34     @ViewChild('archiveGrid') private archiveGrid: GridComponent;
35     @ViewChild('penaltyDialog')
36         private penaltyDialog: PatronPenaltyDialogComponent;
37
38     constructor(
39         private org: OrgService,
40         private net: NetService,
41         private pcrud: PcrudService,
42         private auth: AuthService,
43         private serverStore: ServerStoreService,
44         public patronService: PatronService,
45         public context: PatronContextService
46     ) {}
47
48     ngOnInit() {
49
50         const orgIds = this.org.fullPath(this.auth.user().ws_ou(), true);
51
52         const start = new Date();
53         start.setFullYear(start.getFullYear() - 1);
54         this.startDateYmd = DateUtil.localYmdFromDate(start);
55         this.endDateYmd = DateUtil.localYmdFromDate(); // now
56
57         const flesh = {
58             flesh: 1,
59             flesh_fields: {
60                 ausp: ['standing_penalty', 'staff']
61             },
62             order_by: {}
63         };
64
65         this.mainDataSource.getRows = (pager: Pager, sort: any[]) => {
66
67             const orderBy: any = {ausp: 'set_date'};
68             if (sort.length) {
69                 orderBy.ausp = sort[0].name + ' ' + sort[0].dir;
70             }
71
72             const query = {
73                 usr: this.patronId,
74                 org_unit: orgIds,
75                 '-or' : [
76                     {stop_date: null},
77                     {stop_date: {'>' : 'now'}}
78                 ]
79             };
80
81             flesh.order_by = orderBy;
82             return this.pcrud.search('ausp', query, flesh, {authoritative: true});
83         };
84
85         this.archiveDataSource.getRows = (pager: Pager, sort: any[]) => {
86             const orderBy: any = {ausp: 'set_date'};
87             if (sort.length) {
88                 orderBy.ausp = sort[0].name + ' ' + sort[0].dir;
89             }
90
91             const query = {
92                 usr: this.patronId,
93                 org_unit: orgIds,
94                 stop_date: {'<' : 'now'},
95                 set_date: {between: this.dateRange()}
96             };
97
98             flesh.order_by = orderBy;
99
100             return this.pcrud.search('ausp', query, flesh, {authoritative: true});
101         };
102     }
103
104     dateRange(): string[] {
105
106         let endDate = this.endDateYmd;
107         const today = DateUtil.localYmdFromDate();
108
109         if (endDate === today) { endDate = 'now'; }
110
111         return [this.startDateYmd, endDate];
112     }
113
114     dateChange(iso: string, start?: boolean) {
115         if (start) {
116             this.startDateYmd = iso;
117         } else {
118             this.endDateYmd = iso;
119         }
120         this.archiveGrid.reload();
121     }
122
123     applyPenalty() {
124         this.penaltyDialog.penalty = null;
125         this.penaltyDialog.open({size: 'lg'}).subscribe(changes => {
126             if (changes) {
127                 this.context.refreshPatron()
128                 .then(_ => this.mainGrid.reload());
129             }
130         });
131     }
132
133     archive(penalties: IdlObject[]) {
134         penalties.forEach(p => p.stop_date('now'));
135         this.pcrud.update(penalties).toPromise()
136         .then(_ => {
137             this.mainGrid.reload();
138             this.archiveGrid.reload();
139         });
140     }
141
142     remove(penalties: IdlObject[]) {
143         this.pcrud.remove(penalties).toPromise()
144         .then(_ => {
145             this.mainGrid.reload();
146             this.archiveGrid.reload();
147         });
148     }
149
150     modify(penalties: IdlObject[]) {
151         let modified = false;
152         from(penalties).pipe(concatMap(penalty => {
153             this.penaltyDialog.penalty = penalty;
154             return this.penaltyDialog.open({size: 'lg'}).pipe(tap(changed => {
155                 if (changed) { modified = true; }
156             }));
157         })).toPromise().then(_ => {
158             if (modified) {
159                 this.mainGrid.reload();
160                 this.archiveGrid.reload();
161             }
162         });
163     }
164 }
165
166
167