application.service.ts
8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import {Injectable} from '@angular/core';
import { Http, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { AppSettings, User, UserProfile } from '../model/data-model';
import { State } from '../model/db-tables';
import { AccountType } from '../model/db-tables';
import { Country } from '../model/db-tables';
import { SecurityQuestions } from '../model/db-tables';
import { LicenseType } from '../model/db-tables';
@Injectable()
export class ApplicationService {
appSettingsLoaded: boolean;
appSettings: AppSettings;
currentUser: User;
loggedIn: boolean;
userId: number;
firstName: string;
lastName: string;
emailId: string;
userInfo: UserProfile;
apiUrl: "http://localhost:85/AIAHTML5.Server/api/";
constructor(private http: Http, private router: Router) {
if (this.currentUser == null) {
var user = localStorage.getItem("currentUser");
if (user != null) {
this.currentUser = JSON.parse(user);
this.loggedIn = true;
}
}
this.userId = 0;
this.firstName = "";
this.lastName = "";
this.emailId = "";
}
public getAppSettings(applicationName: string): Observable<any> {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
let params = new URLSearchParams();
params.set("applicationName", applicationName);
params.set("userId", this.currentUser._id);
return this.http.get('DentalDecks.Server/api/AppSettings', { headers: headers, search: params })
.map(this.extractData)
.catch(this.handleError);
}
public getMenuItemsWithNoChildren(menuItems: any): any {
if (this.appSettingsLoaded) {
let menuItemsSubset: Array<any> = new Array<any>();
for (var i = 0; i < menuItems.length; i++) {
if (menuItems[i].menuItems == null) {
for (var j = 0; j < menuItems[i].roles.length; j++) {
if (menuItems[i].roles[j] == this.currentUser.role) {
menuItemsSubset.push(menuItems[i]);
break;
}
}
}
}
return menuItemsSubset;
}
}
public getMenuItemsWithChildren(menuItems: any): any {
if (this.appSettingsLoaded) {
let menuItemsSubset: Array<any> = new Array<any>();
for (var i = 0; i < menuItems.length; i++) {
if (menuItems[i].menuItems != null) {
for (var j = 0; j < menuItems[i].roles.length; j++) {
if (menuItems[i].roles[j] == this.currentUser.role) {
menuItemsSubset.push(menuItems[i]);
break;
}
}
}
}
return menuItemsSubset;
}
}
public login(applicationName: string, username: string, password: string): Observable<any> {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
let params = new URLSearchParams();
params.set("applicationName", applicationName);
params.set("username", username);
params.set("password", password);
return this.http.get('DentalDecks.Server/api/Authenticate', { headers: headers, search: params })
.map(this.extractData)
.catch(this.handleError);
}
public logout(): void {
this.currentUser = null;
localStorage.removeItem("currentUser");
this.router.navigate(['/login']);
}
public resetPassword(username: string): Observable<any> {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
let user: any = new Object();
user.emailAddress = username;
return this.http.post('DentalDecks.Server/api/Password', JSON.stringify(user), { headers: headers })
.map(this.extractData)
.catch(this.handleError);
}
public updatePassword(userId: string, password: string): Observable<any> {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
let user: any = new Object();
user.userId = userId;
user.password = password;
return this.http.post('DentalDecks.Server/api/UpdatePassword', JSON.stringify(user), { headers: headers })
.map(this.extractData)
.catch(this.handleError);
}
public isResetPasswordExpired(userId: string): Observable<any> {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
let params = new URLSearchParams();
params.set("userId", userId);
return this.http.get('DentalDecks.Server/api/Password', { headers: headers, search: params })
.map(this.extractData)
.catch(this.handleError);
}
public getLongDate(date: any): string {
let returnValue: Date = new Date(date);
return returnValue.toLocaleDateString();
}
getDateTime(date: any): string {
let orderDate: string = "";
if (date != null && date != "")
orderDate = new Date(date).toLocaleDateString() + " " + new Date(date).toLocaleTimeString();
return orderDate;
}
getRelativeDate(date: any): string {
if (date != null) {
date = new Date(date);
var delta = Math.round((+new Date - date) / 1000);
var minute = 60,
hour = minute * 60,
day = hour * 24,
week = day * 7;
var fuzzy: string;
if (delta < 30) {
fuzzy = 'just now.';
} else if (delta < minute) {
fuzzy = delta + ' seconds ago.';
} else if (delta < 2 * minute) {
fuzzy = 'a minute ago.'
} else if (delta < hour) {
fuzzy = Math.floor(delta / minute) + ' minutes ago.';
} else if (Math.floor(delta / hour) == 1) {
fuzzy = '1 hour ago.'
} else if (delta < day) {
fuzzy = Math.floor(delta / hour) + ' hours ago.';
} else if (delta < day * 2 && delta > day) {
fuzzy = 'yesterday';
} else {
fuzzy = Math.floor(delta / (60 * 60 * 24)) + ' days ago';
}
return fuzzy;
} else {
return "";
}
}
private extractData(res: Response) {
let body = res.json();
return body || {};
}
private handleError(error: Response | any) {
// In a real world app, we might use a remote logging infrastructure
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
public getAccountTypes(): Observable<any> {
return this.http.get('http://localhost:85/AIAHTML5.Server/api/accounttype')
.map(this.extractData)
.catch(this.handleError);
}
public getStates(): Observable<any> {
return this.http.get('http://localhost:85/AIAHTML5.Server/api/state')
.map(this.extractData)
.catch(this.handleError);
}
public getCountries(): Observable<any> {
return this.http.get('http://localhost:85/AIAHTML5.Server/api/country')
.map(this.extractData)
.catch(this.handleError);
}
public getSecurityQuestions(): Observable<any> {
return this.http.get('http://localhost:85/AIAHTML5.Server/api/securityquestionlist')
.map(this.extractData)
.catch(this.handleError);
}
public getLicenseTypes(): Observable<any> {
return this.http.get('http://localhost:85/AIAHTML5.Server/api/licensetype')
.map(this.extractData)
.catch(this.handleError);
}
}