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';
20 selector: 'eg-patron-messages',
21 templateUrl: 'messages.component.html'
23 export class PatronMessagesComponent implements OnInit {
25 @Input() patronId: number;
27 mainDataSource: GridDataSource = new GridDataSource();
28 archiveDataSource: GridDataSource = new GridDataSource();
33 @ViewChild('mainGrid') private mainGrid: GridComponent;
34 @ViewChild('archiveGrid') private archiveGrid: GridComponent;
35 @ViewChild('penaltyDialog')
36 private penaltyDialog: PatronPenaltyDialogComponent;
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
50 const orgIds = this.org.fullPath(this.auth.user().ws_ou(), true);
52 const start = new Date();
53 start.setFullYear(start.getFullYear() - 1);
54 this.startDateYmd = DateUtil.localYmdFromDate(start);
55 this.endDateYmd = DateUtil.localYmdFromDate(); // now
60 ausp: ['standing_penalty', 'staff']
65 this.mainDataSource.getRows = (pager: Pager, sort: any[]) => {
67 const orderBy: any = {ausp: 'set_date'};
69 orderBy.ausp = sort[0].name + ' ' + sort[0].dir;
77 {stop_date: {'>' : 'now'}}
81 flesh.order_by = orderBy;
82 return this.pcrud.search('ausp', query, flesh, {authoritative: true});
85 this.archiveDataSource.getRows = (pager: Pager, sort: any[]) => {
86 const orderBy: any = {ausp: 'set_date'};
88 orderBy.ausp = sort[0].name + ' ' + sort[0].dir;
94 stop_date: {'<' : 'now'},
95 set_date: {between: this.dateRange()}
98 flesh.order_by = orderBy;
100 return this.pcrud.search('ausp', query, flesh, {authoritative: true});
104 dateRange(): string[] {
106 let endDate = this.endDateYmd;
107 const today = DateUtil.localYmdFromDate();
109 if (endDate === today) { endDate = 'now'; }
111 return [this.startDateYmd, endDate];
114 dateChange(iso: string, start?: boolean) {
116 this.startDateYmd = iso;
118 this.endDateYmd = iso;
120 this.archiveGrid.reload();
124 this.penaltyDialog.penalty = null;
125 this.penaltyDialog.open({size: 'lg'}).subscribe(changes => {
127 this.context.refreshPatron()
128 .then(_ => this.mainGrid.reload());
133 archive(penalties: IdlObject[]) {
134 penalties.forEach(p => p.stop_date('now'));
135 this.pcrud.update(penalties).toPromise()
137 this.mainGrid.reload();
138 this.archiveGrid.reload();
142 remove(penalties: IdlObject[]) {
143 this.pcrud.remove(penalties).toPromise()
145 this.mainGrid.reload();
146 this.archiveGrid.reload();
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; }
157 })).toPromise().then(_ => {
159 this.mainGrid.reload();
160 this.archiveGrid.reload();