helper-data-service.ts 39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { Http, Headers, RequestOptions, ResponseContentType, Response } from "@angular/http";
import { Injectable, OnInit, OnDestroy } from "@angular/core";
import { BehaviorSubject } from "rxjs/Rx";
import "rxjs/add/operator/map";
import { ICourseField } from "../store/coursefields/coursefields.types";
import { ISectorField } from "../store/sectorfields/sectorfields.types";
import { IRegion, IRegions, IRegionSchool } from "../store/regionschools/regionschools.types";
import { ISector, ISectors, ISectorCourse } from "../store/sectorcourses/sectorcourses.types";
import { AppSettings } from "../app.settings";
import { NgRedux, select } from "ng2-redux";
import { IAppState } from "../store/store";
import { ILoginInfo, ILoginInfoToken } from "../store/logininfo/logininfo.types";
import { LOGININFO_INITIAL_STATE } from "../store/logininfo/logininfo.initial-state";
import { SCHOOL_ROLE, STUDENT_ROLE, PDE_ROLE, DIDE_ROLE, MINISTRY_ROLE } from "../constants";
import { CookieService } from "ngx-cookie";

import * as FileSaver from "file-saver";

const HEADER = { headers: new Headers({ "Content-Type": "application/json" }) };
20
21

@Injectable()
22
export class HelperDataService implements OnInit, OnDestroy {
23
24

    private authToken: string;
25
    private authRole: string;
26
27
    private minedu_userName: string;
    private minedu_userPassword: string;
28
    private loginInfo$: BehaviorSubject<ILoginInfo>;
29
30
31

    constructor(
        private http: Http,
32
        private _ngRedux: NgRedux<IAppState>,
33
        private _cookieService: CookieService) {
34
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
35

36

37
    };
38

39
40
41
    ngOnInit() {
        this._ngRedux.select(state => {
            if (state.loginInfo.size > 0) {
42
                state.loginInfo.reduce(({ }, loginInfoToken) => {
43
44
45
46
47
48
49
                    this.authToken = loginInfoToken.auth_token;
                    this.authRole = loginInfoToken.auth_role;
                    return loginInfoToken;
                }, {});
            }
            return state.loginInfo;
        }).subscribe(this.loginInfo$);
50

51
52
53
54
55
    }

    ngOnDestroy() {
        this.loginInfo$.unsubscribe();
    }
56

57
    createAuthorizationHeader(headers: Headers) {
58
        headers.append("Authorization", "Basic " + btoa(this.authToken + ":" + this.authToken));
59
60
    }

61
    createMinistryAuthorizationHeader(headers: Headers, username: string, passwd: string) {
62
        headers.append("Authorization", "Basic " + btoa(username + ":" + passwd));
63
64
    }

65
    getEpalUserData() {
66
        this.loginInfo$.getValue().forEach(loginInfoToken => {
67
68
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
69
70
71
72
73
74
75
76
77
78
        });
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/userdata`, options)
            .map(response => response.json());
    };

79
    sendVerificationCode(email) {
80
        this.loginInfo$.getValue().forEach(loginInfoToken => {
81
82
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
83
84
        });
        let headers = new Headers({
85
86
87
            "Content-Type": "application/json",
            //            "Accept": "*/*",
            //            "Access-Control-Allow-Credentials": "true",
88
89
        });
        this.createAuthorizationHeader(headers);
90
        //        let options = new RequestOptions({ headers: headers, withCredentials: true });
91
92
        let options = new RequestOptions({ headers: headers });
        return new Promise((resolve, reject) => {
93
94
95
96
            this.http.post(`${AppSettings.API_ENDPOINT}/epal/user/sendvercode`, { userEmail: email }, options)
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
97
                },
98
                error => {
99
                    console.log("Error Sending Verification Code");
100
                    reject("Error Sending Verification Code");
101
                });
102
103
104
105
        });
    }

    verifyVerificationCode(verificationCode) {
106
        this.loginInfo$.getValue().forEach(loginInfoToken => {
107
108
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
109
110
        });
        let headers = new Headers({
111
            "Content-Type": "application/json",
112
113
        });
        this.createAuthorizationHeader(headers);
114
        //        let options = new RequestOptions({ headers: headers, withCredentials: true });
115
116
        let options = new RequestOptions({ headers: headers });
        return new Promise((resolve, reject) => {
117
118
119
120
            this.http.post(`${AppSettings.API_ENDPOINT}/epal/user/verifyvercode`, { verificationCode: verificationCode }, options)
                .map(response => response.json())
                .subscribe(data => {
                    resolve(<any>data);
121
                },
122
                error => {
123
                    console.log("Error Verifying Verification Code");
124
                    reject("Error Verifying Verification Code");
125
                });
126
127
128
        });
    }

129
    saveProfile(userProfile) {
130
        this.loginInfo$.getValue().forEach(loginInfoToken => {
131
132
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
133
134
        });
        let headers = new Headers({
135
            "Content-Type": "application/json",
136
137
138
139
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
        return new Promise((resolve, reject) => {
140
141
142
143
144
145
146
147
            this.http.post(`${AppSettings.API_ENDPOINT}/epal/user/save`, { userProfile: userProfile }, options)
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
                },
                error => {
                    console.log("Error Saving Profile");
                    reject("Error Saving Profile");
148
                });
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
        });
    }

    deleteApplication(appId) {
        this.loginInfo$.getValue().forEach(loginInfoToken => {
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
        });
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        //        let options = new RequestOptions({ headers: headers, withCredentials: true });
        let options = new RequestOptions({ headers: headers });
        return new Promise((resolve, reject) => {
            this.http.post(`${AppSettings.API_ENDPOINT}/epal/application/delete`, { applicationId: appId }, options)
                .map(response => response.json())
                .subscribe(data => {
                    resolve(<any>data);
                },
                error => {
                    console.log("Error Removing Application");
                    reject("Error Removing Application");
172
                });
173
174
175
        });
    }

176
    getCourseFields() {
177

178
        this.loginInfo$.getValue().forEach(loginInfoToken => {
179
180
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
181
        });
182
183
184
        let headers = new Headers({
            "Content-Type": "application/json",
        });
185
        this.createAuthorizationHeader(headers);
186
        let options = new RequestOptions({ headers: headers });
187
        return new Promise((resolve, reject) => {
188
            this.http.get(`${AppSettings.API_ENDPOINT}/coursefields/list`, options)
189
190
191
                .map(response => <ICourseField[]>response.json())
                .subscribe(data => {
                    resolve(data);
192
                },
193
                error => {
194
                    console.log("Error HTTP GET Service");
195
                    reject("Error HTTP GET Service");
196
                });
197
198
        });
    };
199

200
    getSectorFields() {
201
        this.loginInfo$.getValue().forEach(loginInfoToken => {
202
203
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
204
205
206
207
208
209
        });
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
210
        return new Promise((resolve, reject) => {
211
            this.http.get(`${AppSettings.API_ENDPOINT}/sectorfields/list`, options)
212
213
214
                .map(response => <ISectorField[]>response.json())
                .subscribe(data => {
                    resolve(data);
215
                },
216
                error => {
217
                    console.log("Error HTTP GET Service");
218
                    reject("Error HTTP GET Service");
219
                });
220
221
222
        });
    };

223
    getRegionsWithSchools(classActive, courseActive) {
224
        this.loginInfo$.getValue().forEach(loginInfoToken => {
225
226
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
227
        });
228
229
230
        let headers = new Headers({
            "Content-Type": "application/json",
        });
231
        this.createAuthorizationHeader(headers);
232
        let options = new RequestOptions({ headers: headers });
233
        return new Promise((resolve, reject) => {
234
            let getConnectionString = null;
235
236

            if (classActive === 1)
237
238
239
                getConnectionString = `${AppSettings.API_ENDPOINT}/regions/list`;
            else if (classActive === 2)
                getConnectionString = `${AppSettings.API_ENDPOINT}/sectorsperschool/list?sector_id=${courseActive}`;
240
            else if (classActive === 3)
241
                getConnectionString = `${AppSettings.API_ENDPOINT}/coursesperschool/list?course_id=${courseActive}`;
242
243
244
            else if (classActive === 4)
                getConnectionString = `${AppSettings.API_ENDPOINT}/coursesperschool_night/list?course_id=${courseActive}`;

245
            this.http.get(getConnectionString, options)
246
247
248
                .map(response => response.json())
                .subscribe(data => {
                    resolve(this.transformRegionSchoolsSchema(data));
249
                },
250
                error => {
251
                    console.log("Error HTTP GET Service in getRegionsWithSchools method");
252
                    reject("Error HTTP GET Service");
253
                });
254
255
256
        });
    };

257
    getSectorsWithCourses() {
258
        this.loginInfo$.getValue().forEach(loginInfoToken => {
259
260
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
261
262
263
264
265
266
        });
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
267
        return new Promise((resolve, reject) => {
268
            this.http.get(`${AppSettings.API_ENDPOINT}/coursesectorfields/list`, options)
269
270
271
                .map(response => response.json())
                .subscribe(data => {
                    resolve(this.transformSectorCoursesSchema(data));
272
                },
273
                error => {
274
                    console.log("Error HTTP GET Service");
275
                    reject("Error HTTP GET Service");
276
                });
277
278
279
        });
    };

280
281
282
283
284
285
286
287
    transformRegionSchoolsSchema(regionSchools: any) {
        let rsa = Array<IRegion>();
        let trackRegionId: string;
        let trackIndex: number;

        trackRegionId = "";
        trackIndex = -1;

288
        let j = 0;
289
290
291
        regionSchools.forEach(regionSchool => {
            if (trackRegionId !== regionSchool.region_id) {
                trackIndex++;
292
                rsa.push(<IRegion>{ "region_id": regionSchool.region_id, "region_name": regionSchool.region_name, "epals": Array<IRegionSchool>() });
293
294
                trackRegionId = regionSchool.region_id;
            }
295
            rsa[trackIndex].epals.push(<IRegionSchool>{ "epal_id": regionSchool.epal_id, "epal_name": regionSchool.epal_name, "epal_special_case": regionSchool.epal_special_case, "globalIndex": j, "selected": false, "order_id": 0 });
296
297
298
299
300
            j++;
        });
        return rsa;
    }

301
302
303
304
305
306
307
308
    transformSectorCoursesSchema(sectorCourses: any) {
        let rsa = Array<ISector>();
        let trackSectorId: string;
        let trackIndex: number;

        trackSectorId = "";
        trackIndex = -1;

309
        let j = 0;
310
311
312
        sectorCourses.forEach(sectorCourse => {
            if (trackSectorId !== sectorCourse.sector_id) {
                trackIndex++;
313
                rsa.push(<ISector>{ "sector_id": sectorCourse.sector_id, "sector_name": sectorCourse.sector_name, "sector_selected": false, "courses": Array<ISectorCourse>() });
314
315
                trackSectorId = sectorCourse.sector_id;
            }
316
            rsa[trackIndex].courses.push(<ISectorCourse>{ "course_id": sectorCourse.course_id, "course_name": sectorCourse.course_name, "globalIndex": j, "selected": false });
317
318
319
320
321
            j++;
        });
        return rsa;
    }

322
    getCriteria() {
323

324
        this.loginInfo$.getValue().forEach(loginInfoToken => {
325
326
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
327
328
329
330
331
332
333
334
        });
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
        return new Promise((resolve, reject) => {
            this.http.get(`${AppSettings.API_ENDPOINT}/criteria/list`, options)
335
336
337
338
                //this.http.get(`${AppSettings.API_ENDPOINT}/criteria/list?category=${category}`, options)
                .map(response => <ISectorField[]>response.json())
                .subscribe(data => {
                    resolve(data);
339
                },
340
                error => {
341
                    console.log("Error HTTP GET Service");
342
                    reject("Error HTTP GET Service");
343
                });
344
345
        });
    };
Open Source Developer's avatar
Open Source Developer committed
346

347
    getCurrentUser(oauthtoken, oauthrole) {
Open Source Developer's avatar
Open Source Developer committed
348
349

        this.authToken = oauthtoken;
350
        this.authRole = oauthrole;
351

Open Source Developer's avatar
Open Source Developer committed
352
353
        let headers = new Headers({
            "Content-Type": "application/json",
354
            "Access-Control-Allow-Credentials": "true",
Open Source Developer's avatar
Open Source Developer committed
355
356
357
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
358
        return new Promise((resolve, reject) => {
Open Source Developer's avatar
Open Source Developer committed
359
            this.http.get(`${AppSettings.API_ENDPOINT}/epal/curuser`, options)
360
361
                .map(response => response.json())
                .subscribe(data => {
362
                    window.onbeforeunload = function(e) {
363
                        let confirmationMessage = "\o/";
364
365
366
                        e.returnValue = confirmationMessage;
                        return confirmationMessage;
                    };
367
                    resolve(this.transformUserSchema(data, oauthtoken, oauthrole));
368
                },
369
                error => {
370
                    console.log("Error HTTP GET Service");
371
                    reject("Error HTTP GET Service");
372
                });
Open Source Developer's avatar
Open Source Developer committed
373
        });
374
    }
Open Source Developer's avatar
Open Source Developer committed
375

376
377
378
379
    setMineduCurrentUser(minedu_username, minedu_pwd, role) {
        this.minedu_userName = minedu_username;
        this.minedu_userPassword = minedu_pwd;
        this.authRole = role;
380
    }
381

Open Source Developer's avatar
Open Source Developer committed
382

383
    transformUserSchema(userlogin: any, oauthtoken: string, oauthrole: string) {
384
        let rsa = Array<ILoginInfoToken>();
385

386
        rsa.push(<ILoginInfoToken>{
387
388
389
390
391
392
393
394
395
396
397
398
399
            "auth_token": oauthtoken,
            "auth_role": oauthrole,
            "cu_name": userlogin.cu_name,
            "cu_surname": userlogin.cu_surname,
            "cu_fathername": userlogin.cu_fathername,
            "cu_mothername": userlogin.cu_mothername,
            "cu_email": userlogin.cu_email,
            "minedu_username": userlogin.minedu_username,
            "minedu_userpassword": userlogin.minedu_userpassword,
            "lock_capacity": parseInt(userlogin.lock_capacity),
            "lock_students": parseInt(userlogin.lock_students),
            "lock_application": parseInt(userlogin.lock_application),
            "disclaimer_checked": parseInt(userlogin.disclaimer_checked)
400
        });
401
        return rsa;
402

403
    }
Open Source Developer's avatar
Open Source Developer committed
404

405
    signOut() {
406
        // loginInfo$ take values only in case getCurrentUser (epal module) has been used ...WHY? TO BE CHECKED..
407
        this.loginInfo$.getValue().forEach(loginInfoToken => {
408
409
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
410
        });
411

412
        let headers = new Headers({
413
            "Content-Type": "application/json",
414
        });
415

416
        let options = new RequestOptions({ headers: headers, withCredentials: true });
417
        let logoutRoute = "/oauth/logout";
418
419
420
421
422
        if (this.authRole === MINISTRY_ROLE) {
            this.createMinistryAuthorizationHeader(headers, this.minedu_userName, this.minedu_userPassword);
        } else {
            this.createAuthorizationHeader(headers);
        }
423
        if (this.authRole === SCHOOL_ROLE || this.authRole === PDE_ROLE || this.authRole === DIDE_ROLE) {
424
            logoutRoute = "/cas/logout";
425
        } else if (this.authRole === MINISTRY_ROLE) {
426
            logoutRoute = "/ministry/logout";
427
        }
428

429
        return new Promise((resolve, reject) => {
430
            this.http.post(`${AppSettings.API_ENDPOINT}${logoutRoute}${AppSettings.API_ENDPOINT_PARAMS}`, {}, options)
431
                .map(response => response.json())
432
                .subscribe(data => {
433
                    this._cookieService.removeAll();
434
                    window.onbeforeunload = function() { console.log("unloading") };
435
                    resolve(data);
436
                },
437
                error => {
438
                    console.log("Error Logout");
439
                    reject("Error Logout");
440
                });
441
442
        });
    }
Open Source Developer's avatar
Open Source Developer committed
443

444
445
446
447
448
449
450
451
452
    casSignOut() {
        let headers = new Headers({
            "Content-Type": "application/json"
        });
        return new Promise((resolve, reject) => {
            this.http.post(`${AppSettings.API_ENDPOINT}/cas/logoutcas${AppSettings.API_ENDPOINT_PARAMS}`, {}, { headers: headers })
                .map(response => response.json())
                .subscribe(data => {
                    this._cookieService.removeAll();
453
                    window.onbeforeunload = function() { console.log("unloading") };
454
455
456
                    resolve(data);
                },
                error => {
457
                    console.log("Error Logout");
458
                    reject("Error Logout");
459
                });
460
461
462
        });
    }

Open Source Developer's avatar
tranfer    
Open Source Developer committed
463
    getSubmittedPreviw() {
Open Source Developer's avatar
Open Source Developer committed
464

465
        this.loginInfo$.getValue().forEach(loginInfoToken => {
466
467
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
Open Source Developer's avatar
Open Source Developer committed
468
469
470
471
472
473
        });
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
474
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/subapplic`, options)
Open Source Developer's avatar
Open Source Developer committed
475
            .map(response => response.json());
Open Source Developer's avatar
Open Source Developer committed
476
    }
Open Source Developer's avatar
Open Source Developer committed
477

Open Source Developer's avatar
tranfer    
Open Source Developer committed
478

479
480
481
    getStudentDetails(headerid) {
        let headerIdNew = headerid.toString();
        this.loginInfo$.getValue().forEach(loginInfoToken => {
482
483
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
484
        });
Open Source Developer's avatar
Open Source Developer committed
485
486
487
488
489
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
490
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/student/` + headerIdNew, options)
Open Source Developer's avatar
Open Source Developer committed
491
            .map(response => response.json());
492
    }
Open Source Developer's avatar
Open Source Developer committed
493
494


495
496
497
    getEpalchosen(headerid) {
        let headerIdNew = headerid.toString();
        this.loginInfo$.getValue().forEach(loginInfoToken => {
498
499
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
500
        });
Open Source Developer's avatar
Open Source Developer committed
501
502
503
504
505
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
506
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/epalchosen/` + headerIdNew, options)
Open Source Developer's avatar
Open Source Developer committed
507
            .map(response => response.json());
508
    }
Open Source Developer's avatar
tranfer    
Open Source Developer committed
509
510


511

Open Source Developer's avatar
trans    
Open Source Developer committed
512
513


Open Source Developer's avatar
Open Source Developer committed
514
    getStudentPerSchool(classId, sector, specialit) {
515

516
        this.loginInfo$.getValue().forEach(loginInfoToken => {
517
518
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
519
        });
520

Open Source Developer's avatar
trans    
Open Source Developer committed
521
522
523
524
525
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
Open Source Developer's avatar
Open Source Developer committed
526
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/studentperschool/` + classId + "/" + sector + "/" + specialit, options)
Open Source Developer's avatar
trans    
Open Source Developer committed
527
            .map(response => response.json());
528
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
529

Open Source Developer's avatar
transf    
Open Source Developer committed
530
531


Open Source Developer's avatar
trans    
Open Source Developer committed
532
    saveConfirmStudents(students, type) {
533
        this.loginInfo$.getValue().forEach(loginInfoToken => {
534
535
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
Open Source Developer's avatar
trans    
Open Source Developer committed
536
537
        });
        let headers = new Headers({
538
            "Content-Type": "application/json",
Open Source Developer's avatar
trans    
Open Source Developer committed
539
540
541
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
542
        return this.http.post(`${AppSettings.API_ENDPOINT}/epal/confirmstudent`, { students, type }, options)
Open Source Developer's avatar
Open Source Developer committed
543
            .map(response => response.json());
544
545


546
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
547
548


Open Source Developer's avatar
Open Source Developer committed
549

Open Source Developer's avatar
Open Source Developer committed
550
    saveCapacity(taxi, tomeas, specialit, capacity) {
Open Source Developer's avatar
Open Source Developer committed
551

552
        this.loginInfo$.getValue().forEach(loginInfoToken => {
553
554
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
Open Source Developer's avatar
Open Source Developer committed
555
556
        });
        let headers = new Headers({
557
            "Content-Type": "application/json",
Open Source Developer's avatar
Open Source Developer committed
558
559
560
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
561
        return this.http.post(`${AppSettings.API_ENDPOINT}/epal/savecapacity/` + taxi + "/" + tomeas + "/" + specialit, { capacity }, options)
Open Source Developer's avatar
trans    
Open Source Developer committed
562
            .map(response => response.json());
Open Source Developer's avatar
Open Source Developer committed
563

564
    }
Open Source Developer's avatar
Open Source Developer committed
565

566
567
568
569
570
571
572
573
    sendMinisrtyCredentials(username, userpassword) {

        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
        return new Promise((resolve, reject) => {
574
            this.http.post(`${AppSettings.API_ENDPOINT}/ministry/login`, { username: username, userpassword: userpassword }, options)
575
576
577
578
579
580
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
                },
                error => {
                    reject("Error Sending Ministry Credentials");
581
                });
582
583
584
585
        });

    }

586
587
588
589
590
591
    makeDistribution(username, userpassword) {

        let headers = new Headers({
            "Content-Type": "application/json",
        });

592
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
593
594
595
        let options = new RequestOptions({ headers: headers });

        return new Promise((resolve, reject) => {
596
            this.http.post(`${AppSettings.API_ENDPOINT}/epal/distribution`, { }, options)
597
598
599
600
601
602
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
                },
                error => {
                    reject("Error POST in makeDistribution");
603
                });
604
605
606
607
        });

    }

608
609
610
611
612
613
614
615
616
617
    makeDistributionSecondPeriod(username, userpassword) {

        let headers = new Headers({
            "Content-Type": "application/json",
        });

        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });

        return new Promise((resolve, reject) => {
618
            this.http.post(`${AppSettings.API_ENDPOINT}/epal/distribution-secondperiod`, { }, options)
619
620
621
622
623
624
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
                },
                error => {
                    reject("Error POST in makeDistributionSecondPeriod");
625
                });
626
627
628
629
        });

    }

630

631
    makeReport(username, userpassword, routepath, regionsel, adminsel, schsel, clsel, secsel, coursel, distribfinal) {
632
633
634
635
636

        let headers = new Headers({
            "Content-Type": "application/json",
        });

637
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
638
639
        let options = new RequestOptions({ headers: headers });

640
641
642
643
644
        if (routepath == "/ministry/report-users/") {
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath, options)
                .map(response => response.json());
        }
        else if (routepath == "/ministry/general-report/") {
645
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath, options)
646
647
648
                .map(response => response.json());
        }
        else if (routepath == "/ministry/report-completeness/") {
649
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath + regionsel + "/" + adminsel + "/" + schsel, options)
650
651
                .map(response => response.json());
        }
652
653
654
        else if (routepath == "/ministry/report-all-stat/") {
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath + regionsel + "/" + adminsel + "/" + schsel + "/" +
                clsel + "/" + secsel + "/" + coursel + "/" + distribfinal, options)
655
                .map(response => response.json());
656
657
658
659
        }
        else if (routepath == "/ministry/report-no-capacity/") {
            let capacityFilter = 0;
            if (regionsel)
660
                capacityFilter = 1;
661
662
663
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath + capacityFilter, options)
                .map(response => response.json());
        }
664
665
666

    }

667
    informUnlocatedStudents(username, userpassword, unallocated) {
668

669
670
671
672
673
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
674

675
        let route = "";
676
        if (unallocated == 1)
677
            route = "ministry/send-unallocated-massive-mail";
678
        else if (unallocated == 2)
679
            route = "ministry/send-unallocated-sc-massive-mail";
680
        else if (unallocated == 3)
681
            route = "ministry/send-located-massive-mail";
682

683
684
        return this.http.get(`${AppSettings.API_ENDPOINT}/` + route, options)
            .map(response => response.json());
685
686
    }

Open Source Developer's avatar
Open Source Developer committed
687

688
    getSchools() {
Open Source Developer's avatar
trans    
Open Source Developer committed
689
690
691
692
693
694
695
696
697
698

        this.loginInfo$.getValue().forEach(loginInfoToken => {
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
        });
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
699
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/schools-per-perf/`, options)
Open Source Developer's avatar
trans    
Open Source Developer committed
700
701
702
            .map(response => response.json());
    }

703
    getCoursePerPerfecture(PerfectureId) {
Open Source Developer's avatar
trans    
Open Source Developer committed
704
705
706
707
708
709
710
711
712
713
714
715
        let PerfectureIdNew = PerfectureId.toString();

        this.loginInfo$.getValue().forEach(loginInfoToken => {
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
        });

        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
Open Source Developer's avatar
bug    
Open Source Developer committed
716

717
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/coursespersch/` + PerfectureIdNew, options)
Open Source Developer's avatar
trans    
Open Source Developer committed
718
            .map(response => response.json());
719
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
720

721
    getRegions(username, userpassword) {
722

723
724
725
        let headers = new Headers({
            "Content-Type": "application/json",
        });
726

727
728
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
729

730
        // return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath , options)
731
732
733
        //    .map(response => response.json());
        return this.http.get(`${AppSettings.API_ENDPOINT}/regionfields/list`, options)
            .map(response => response.json());
734

735
    }
736

737
    getAdminAreas(username, userpassword, regionid) {
738

739
740
741
        let headers = new Headers({
            "Content-Type": "application/json",
        });
742

743
744
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
745

746
747
748
        return this.http.get(`${AppSettings.API_ENDPOINT}/adminfields/list/?region=` + regionid, options)
            .map(response => response.json());
    }
749

750
    getSchoolsPerRegion(username, userpassword, regionid) {
751

752
753
754
        let headers = new Headers({
            "Content-Type": "application/json",
        });
755

756
757
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
758

759
760
761
        return this.http.get(`${AppSettings.API_ENDPOINT}/schoolfields_per_region/list/?region=` + regionid, options)
            .map(response => response.json());
    }
762

763
    getSchoolsPerAdminArea(username, userpassword, adminid) {
764

765
766
767
        let headers = new Headers({
            "Content-Type": "application/json",
        });
768

769
770
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
771

772
773
774
        return this.http.get(`${AppSettings.API_ENDPOINT}/schoolfields_per_admin/list/?adminarea=` + adminid, options)
            .map(response => response.json());
    }
775

776

777
    getUserRegistryNo(username, userpassword) {
778

779
780
781
        let headers = new Headers({
            "Content-Type": "application/json",
        });
782

783
784
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
785

786
787
788
        return this.http.get(`${AppSettings.API_ENDPOINT}/ministry/retrieve-registry-id`, options)
            .map(response => response.json());
    }
789

790
    retrieveAdminSettings(username, userpassword) {
791

792
793
794
        let headers = new Headers({
            "Content-Type": "application/json",
        });
795

796
797
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
798

799
800
801
        return this.http.get(`${AppSettings.API_ENDPOINT}/ministry/retrieve-settings`, options)
            .map(response => response.json());
    }
802

803
    storeAdminSettings(username, userpassword, capac, dirview, applogin, appresults, secondperiod) {
804

805
806
807
        let headers = new Headers({
            "Content-Type": "application/json",
        });
808

809
810
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
811

812
        return this.http.get(`${AppSettings.API_ENDPOINT}/ministry/store-settings/` +
813
            Number(capac) + "/" + Number(dirview) + "/" + Number(applogin) + "/" + Number(appresults) + "/" + Number(secondperiod), options)
814
            .map(response => response.json());
815
    }
816
817


818
    getSectors(username, userpassword, classid) {
819

820
821
822
        let headers = new Headers({
            "Content-Type": "application/json",
        });
823

824
825
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
826

827
828
829
        return this.http.get(`${AppSettings.API_ENDPOINT}/sectorfields/list`, options)
            .map(response => response.json());
    }
830

831
    getCourses(username, userpassword, sectorid) {
832

833
834
835
        let headers = new Headers({
            "Content-Type": "application/json",
        });
836

837