helper-data-service.ts 38.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 { IClassField } from "../store/classfields/classfields.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" }) };
21
22

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

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

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

37

38
    };
39

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

53
54
55
56
57
    }

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

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

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

67
    getEpalUserData() {
68
        this.loginInfo$.getValue().forEach(loginInfoToken => {
69
70
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
71
72
73
74
75
76
77
78
79
80
        });
        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());
    };

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

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

133
    saveProfile(userProfile) {
134
        this.loginInfo$.getValue().forEach(loginInfoToken => {
135
136
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
137
138
        });
        let headers = new Headers({
139
            "Content-Type": "application/json",
140
141
142
143
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
        return new Promise((resolve, reject) => {
144
145
146
147
148
149
150
151
152
153
            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");
                },
                () => console.log("Saving Profile"));
154
155
156
        });
    }

157
    getCourseFields() {
158

159
        this.loginInfo$.getValue().forEach(loginInfoToken => {
160
161
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
162
        });
163
164
165
        let headers = new Headers({
            "Content-Type": "application/json",
        });
166
        this.createAuthorizationHeader(headers);
167
        let options = new RequestOptions({ headers: headers });
168
        return new Promise((resolve, reject) => {
169
            this.http.get(`${AppSettings.API_ENDPOINT}/coursefields/list`, options)
170
171
172
                .map(response => <ICourseField[]>response.json())
                .subscribe(data => {
                    resolve(data);
173
                },
174
                error => {
175
                    console.log("Error HTTP GET Service");
176
177
                    reject("Error HTTP GET Service");
                },
178
                () => console.log("Course Fields Received"));
179
180
        });
    };
181

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

206
    getRegionsWithSchools(classActive, courseActive) {
207
        this.loginInfo$.getValue().forEach(loginInfoToken => {
208
209
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
210
        });
211
212
213
        let headers = new Headers({
            "Content-Type": "application/json",
        });
214
        this.createAuthorizationHeader(headers);
215
        let options = new RequestOptions({ headers: headers });
216
        return new Promise((resolve, reject) => {
217
            let getConnectionString = null;
218
219

            if (classActive === 1)
220
221
222
                getConnectionString = `${AppSettings.API_ENDPOINT}/regions/list`;
            else if (classActive === 2)
                getConnectionString = `${AppSettings.API_ENDPOINT}/sectorsperschool/list?sector_id=${courseActive}`;
223
            else if (classActive === 3)
224
                getConnectionString = `${AppSettings.API_ENDPOINT}/coursesperschool/list?course_id=${courseActive}`;
225
226
227
            else if (classActive === 4)
                getConnectionString = `${AppSettings.API_ENDPOINT}/coursesperschool_night/list?course_id=${courseActive}`;

228
            this.http.get(getConnectionString, options)
229
230
231
                .map(response => response.json())
                .subscribe(data => {
                    resolve(this.transformRegionSchoolsSchema(data));
232
                },
233
                error => {
234
                    console.log("Error HTTP GET Service in getRegionsWithSchools method");
235
236
                    reject("Error HTTP GET Service");
                },
237
                () => console.log("region schools service"));
238
239
240
        });
    };

241
    getSectorsWithCourses() {
242
        this.loginInfo$.getValue().forEach(loginInfoToken => {
243
244
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
245
246
247
248
249
250
        });
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
251
        return new Promise((resolve, reject) => {
252
            this.http.get(`${AppSettings.API_ENDPOINT}/coursesectorfields/list`, options)
253
254
255
                .map(response => response.json())
                .subscribe(data => {
                    resolve(this.transformSectorCoursesSchema(data));
256
                },
257
                error => {
258
                    console.log("Error HTTP GET Service");
259
260
                    reject("Error HTTP GET Service");
                },
261
                () => console.log("region schools service"));
262
263
264
        });
    };

265
266
267
268
269
270
271
272
    transformRegionSchoolsSchema(regionSchools: any) {
        let rsa = Array<IRegion>();
        let trackRegionId: string;
        let trackIndex: number;

        trackRegionId = "";
        trackIndex = -1;

273
        let j = 0;
274
275
276
        regionSchools.forEach(regionSchool => {
            if (trackRegionId !== regionSchool.region_id) {
                trackIndex++;
277
                rsa.push(<IRegion>{ "region_id": regionSchool.region_id, "region_name": regionSchool.region_name, "epals": Array<IRegionSchool>() });
278
279
                trackRegionId = regionSchool.region_id;
            }
280
            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 });
281
282
283
284
285
            j++;
        });
        return rsa;
    }

286
287
288
289
290
291
292
293
    transformSectorCoursesSchema(sectorCourses: any) {
        let rsa = Array<ISector>();
        let trackSectorId: string;
        let trackIndex: number;

        trackSectorId = "";
        trackIndex = -1;

294
        let j = 0;
295
296
297
        sectorCourses.forEach(sectorCourse => {
            if (trackSectorId !== sectorCourse.sector_id) {
                trackIndex++;
298
                rsa.push(<ISector>{ "sector_id": sectorCourse.sector_id, "sector_name": sectorCourse.sector_name, "sector_selected": false, "courses": Array<ISectorCourse>() });
299
300
                trackSectorId = sectorCourse.sector_id;
            }
301
            rsa[trackIndex].courses.push(<ISectorCourse>{ "course_id": sectorCourse.course_id, "course_name": sectorCourse.course_name, "globalIndex": j, "selected": false });
302
303
304
305
306
            j++;
        });
        return rsa;
    }

307
    getCriteria() {
308

309
        this.loginInfo$.getValue().forEach(loginInfoToken => {
310
311
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
312
313
314
315
316
317
318
319
        });
        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)
320
321
322
323
                //this.http.get(`${AppSettings.API_ENDPOINT}/criteria/list?category=${category}`, options)
                .map(response => <ISectorField[]>response.json())
                .subscribe(data => {
                    resolve(data);
324
                },
325
                error => {
326
                    console.log("Error HTTP GET Service");
327
328
                    reject("Error HTTP GET Service");
                },
329
                () => console.log("Sector Fields Received"));
330
331
        });
    };
Open Source Developer's avatar
Open Source Developer committed
332

333
    getCurrentUser(oauthtoken, oauthrole) {
Open Source Developer's avatar
Open Source Developer committed
334
335

        this.authToken = oauthtoken;
336
        this.authRole = oauthrole;
337

Open Source Developer's avatar
Open Source Developer committed
338
339
        let headers = new Headers({
            "Content-Type": "application/json",
340
            "Access-Control-Allow-Credentials": "true",
Open Source Developer's avatar
Open Source Developer committed
341
342
343
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
344
        return new Promise((resolve, reject) => {
Open Source Developer's avatar
Open Source Developer committed
345
            this.http.get(`${AppSettings.API_ENDPOINT}/epal/curuser`, options)
346
347
                .map(response => response.json())
                .subscribe(data => {
348
                    window.onbeforeunload = function (e) {
349
                        let confirmationMessage = "\o/";
350
351
352
                        e.returnValue = confirmationMessage;
                        return confirmationMessage;
                    };
353
                    resolve(this.transformUserSchema(data, oauthtoken, oauthrole));
354
                },
355
                error => {
356
                    console.log("Error HTTP GET Service");
357
358
359
                    reject("Error HTTP GET Service");
                },
                () => console.log("UserName Received"));
Open Source Developer's avatar
Open Source Developer committed
360
        });
361
    }
Open Source Developer's avatar
Open Source Developer committed
362

363
364
365
366
    setMineduCurrentUser(minedu_username, minedu_pwd, role) {
        this.minedu_userName = minedu_username;
        this.minedu_userPassword = minedu_pwd;
        this.authRole = role;
367
    }
368

Open Source Developer's avatar
Open Source Developer committed
369

370
    transformUserSchema(userlogin: any, oauthtoken: string, oauthrole: string) {
371
        let rsa = Array<ILoginInfoToken>();
372

373
        rsa.push(<ILoginInfoToken>{
374
375
376
377
378
379
380
381
382
383
384
385
386
            "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)
387
        });
388
        return rsa;
389

390
    }
Open Source Developer's avatar
Open Source Developer committed
391

392
    signOut() {
393
        // loginInfo$ take values only in case getCurrentUser (epal module) has been used ...WHY? TO BE CHECKED..
394
        this.loginInfo$.getValue().forEach(loginInfoToken => {
395
396
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
397
        });
398

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

403
        let options = new RequestOptions({ headers: headers, withCredentials: true });
404
        let logoutRoute = "/oauth/logout";
405
406
407
408
409
        if (this.authRole === MINISTRY_ROLE) {
            this.createMinistryAuthorizationHeader(headers, this.minedu_userName, this.minedu_userPassword);
        } else {
            this.createAuthorizationHeader(headers);
        }
410
        if (this.authRole === SCHOOL_ROLE || this.authRole === PDE_ROLE || this.authRole === DIDE_ROLE) {
411
            logoutRoute = "/cas/logout";
412
        } else if (this.authRole === MINISTRY_ROLE) {
413
            logoutRoute = "/ministry/logout";
414
        }
415

416
        return new Promise((resolve, reject) => {
417
            this.http.post(`${AppSettings.API_ENDPOINT}${logoutRoute}${AppSettings.API_ENDPOINT_PARAMS}`, {}, options)
418
                .map(response => response.json())
419
                .subscribe(data => {
420
                    this._cookieService.removeAll();
421
                    window.onbeforeunload = function () { console.log("unloading") };
422
                    resolve(data);
423
                },
424
                error => {
425
                    console.log("Error Logout");
426
427
                    reject("Error Logout");
                },
428
                () => console.log("Logging out"));
429
430
        });
    }
Open Source Developer's avatar
Open Source Developer committed
431

432
433
434
435
436
437
438
439
440
441
442
443
444
    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();
                    window.onbeforeunload = function () { console.log("unloading") };
                    resolve(data);
                },
                error => {
445
                    console.log("Error Logout");
446
447
                    reject("Error Logout");
                },
448
                () => console.log("Logging out"));
449
450
451
        });
    }

Open Source Developer's avatar
tranfer    
Open Source Developer committed
452
    getSubmittedPreviw() {
Open Source Developer's avatar
Open Source Developer committed
453

454
        this.loginInfo$.getValue().forEach(loginInfoToken => {
455
456
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
Open Source Developer's avatar
Open Source Developer committed
457
458
459
460
461
462
        });
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
463
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/subapplic`, options)
Open Source Developer's avatar
Open Source Developer committed
464
            .map(response => response.json());
Open Source Developer's avatar
Open Source Developer committed
465
    }
Open Source Developer's avatar
Open Source Developer committed
466

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

468
469
470
    getStudentDetails(headerid) {
        let headerIdNew = headerid.toString();
        this.loginInfo$.getValue().forEach(loginInfoToken => {
471
            console.log("Nai");
472
473
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
474
        });
Open Source Developer's avatar
Open Source Developer committed
475
476
477
478
479
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
480
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/student/` + headerIdNew, options)
Open Source Developer's avatar
Open Source Developer committed
481
            .map(response => response.json());
482
    }
Open Source Developer's avatar
Open Source Developer committed
483
484


485
486
487
    getEpalchosen(headerid) {
        let headerIdNew = headerid.toString();
        this.loginInfo$.getValue().forEach(loginInfoToken => {
488
489
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
490
        });
Open Source Developer's avatar
Open Source Developer committed
491
492
493
494
495
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
496
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/epalchosen/` + headerIdNew, options)
Open Source Developer's avatar
Open Source Developer committed
497
            .map(response => response.json());
498
    }
Open Source Developer's avatar
tranfer    
Open Source Developer committed
499
500


Open Source Developer's avatar
changes    
Open Source Developer committed
501
    
Open Source Developer's avatar
trans    
Open Source Developer committed
502
503


Open Source Developer's avatar
Open Source Developer committed
504
    getStudentPerSchool(classId, sector, specialit) {
505

506
        this.loginInfo$.getValue().forEach(loginInfoToken => {
507
508
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
509
        });
510

Open Source Developer's avatar
trans    
Open Source Developer committed
511
512
513
514
515
        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
516
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/studentperschool/` + classId + "/" + sector + "/" + specialit, options)
Open Source Developer's avatar
trans    
Open Source Developer committed
517
            .map(response => response.json());
518
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
519

Open Source Developer's avatar
transf    
Open Source Developer committed
520
521


Open Source Developer's avatar
trans    
Open Source Developer committed
522
    saveConfirmStudents(students, type) {
Open Source Developer's avatar
Open Source Developer committed
523
        console.log(students, type, "confirm");
524
        this.loginInfo$.getValue().forEach(loginInfoToken => {
525
526
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
Open Source Developer's avatar
trans    
Open Source Developer committed
527
528
        });
        let headers = new Headers({
529
            "Content-Type": "application/json",
Open Source Developer's avatar
trans    
Open Source Developer committed
530
531
532
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
533
        return this.http.post(`${AppSettings.API_ENDPOINT}/epal/confirmstudent`, { students, type }, options)
Open Source Developer's avatar
Open Source Developer committed
534
            .map(response => response.json());
535
536


537
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
538
539


Open Source Developer's avatar
Open Source Developer committed
540

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

543
        this.loginInfo$.getValue().forEach(loginInfoToken => {
544
545
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
Open Source Developer's avatar
Open Source Developer committed
546
547
        });
        let headers = new Headers({
548
            "Content-Type": "application/json",
Open Source Developer's avatar
Open Source Developer committed
549
550
551
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
552
        return this.http.post(`${AppSettings.API_ENDPOINT}/epal/savecapacity/` + taxi + "/" + tomeas + "/" + specialit, { capacity }, options)
Open Source Developer's avatar
trans    
Open Source Developer committed
553
            .map(response => response.json());
Open Source Developer's avatar
Open Source Developer committed
554

555
    }
Open Source Developer's avatar
Open Source Developer committed
556

557
558
559
560
561
562
563
564
    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) => {
565
            this.http.post(`${AppSettings.API_ENDPOINT}/ministry/login`, { username: username, userpassword: userpassword }, options)
566
567
568
569
570
571
572
573
574
575
576
577
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
                },
                error => {
                    reject("Error Sending Ministry Credentials");
                },
                () => console.log(""));
        });

    }

578
579
580
581
582
583
    makeDistribution(username, userpassword) {

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

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

        return new Promise((resolve, reject) => {
588
            this.http.post(`${AppSettings.API_ENDPOINT}/epal/distribution`, { username: username, userpassword: userpassword }, options)
589
590
591
592
593
594
595
596
597
598
599
600
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
                },
                error => {
                    reject("Error POST in makeDistribution");
                },
                () => console.log(""));
        });

    }

601

602
    makeReport(username, userpassword, routepath, regionsel, adminsel, schsel, clsel, secsel, coursel, distribfinal) {
603
604
605
606
607

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

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

611
        if (routepath == "/ministry/general-report/") {
612
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath, options)
613
614
615
                .map(response => response.json());
        }
        else if (routepath == "/ministry/report-completeness/") {
616
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath + regionsel + "/" + adminsel + "/" + schsel, options)
617
618
                .map(response => response.json());
        }
619
620
621
        else if (routepath == "/ministry/report-all-stat/") {
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath + regionsel + "/" + adminsel + "/" + schsel + "/" +
                clsel + "/" + secsel + "/" + coursel + "/" + distribfinal, options)
622
                .map(response => response.json());
623
624
625
626
        }
        else if (routepath == "/ministry/report-no-capacity/") {
            let capacityFilter = 0;
            if (regionsel)
627
                capacityFilter = 1;
628
629
630
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath + capacityFilter, options)
                .map(response => response.json());
        }
631
632
633

    }

634
    informUnlocatedStudents(username, userpassword, unallocated) {
635

636
637
638
639
640
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
641

642
        let route = "";
643
        if (unallocated == 1)
644
            route = "ministry/send-unallocated-massive-mail";
645
646
647
        else if(unallocated == 2)
            route = "ministry/send-unallocated-sc-massive-mail";
        else if(unallocated == 3)
648
            route = "ministry/send-located-massive-mail";
649

650
651
        return this.http.get(`${AppSettings.API_ENDPOINT}/` + route, options)
            .map(response => response.json());
652
653
    }

Open Source Developer's avatar
Open Source Developer committed
654

655
    getSchools() {
Open Source Developer's avatar
trans    
Open Source Developer committed
656
657
658
659
660
661
662
663
664
665

        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 });
666
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/schools-per-perf/`, options)
Open Source Developer's avatar
trans    
Open Source Developer committed
667
668
669
            .map(response => response.json());
    }

670
    getCoursePerPerfecture(PerfectureId) {
Open Source Developer's avatar
trans    
Open Source Developer committed
671
672
673
674
675
676
677
678
679
680
681
682
683
        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
684

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

689
    getRegions(username, userpassword) {
690

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

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

698
        // return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath , options)
699
700
701
        //    .map(response => response.json());
        return this.http.get(`${AppSettings.API_ENDPOINT}/regionfields/list`, options)
            .map(response => response.json());
702

703
    }
704

705
    getAdminAreas(username, userpassword, regionid) {
706

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

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

714
715
716
        return this.http.get(`${AppSettings.API_ENDPOINT}/adminfields/list/?region=` + regionid, options)
            .map(response => response.json());
    }
717

718
    getSchoolsPerRegion(username, userpassword, regionid) {
719

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

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

727
728
729
        return this.http.get(`${AppSettings.API_ENDPOINT}/schoolfields_per_region/list/?region=` + regionid, options)
            .map(response => response.json());
    }
730

731
    getSchoolsPerAdminArea(username, userpassword, adminid) {
732

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

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

740
741
742
        return this.http.get(`${AppSettings.API_ENDPOINT}/schoolfields_per_admin/list/?adminarea=` + adminid, options)
            .map(response => response.json());
    }
743

744

745
    getUserRegistryNo(username, userpassword) {
746

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

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

754
755
756
        return this.http.get(`${AppSettings.API_ENDPOINT}/ministry/retrieve-registry-id`, options)
            .map(response => response.json());
    }
757

758
    retrieveAdminSettings(username, userpassword) {
759

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

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

767
768
769
        return this.http.get(`${AppSettings.API_ENDPOINT}/ministry/retrieve-settings`, options)
            .map(response => response.json());
    }
770

771
    storeAdminSettings(username, userpassword, capac, dirview, applogin, appresults) {
772

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

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

780
        return this.http.get(`${AppSettings.API_ENDPOINT}/ministry/store-settings/` +
781
782
            Number(capac) + "/" + Number(dirview) + "/" + Number(applogin) + "/" + Number(appresults), options)
            .map(response => response.json());
783
    }
784
785


786
    getSectors(username, userpassword, classid) {
787

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

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

795
796
797
        return this.http.get(`${AppSettings.API_ENDPOINT}/sectorfields/list`, options)
            .map(response => response.json());
    }
798

799
    getCourses(username, userpassword, sectorid) {
800

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

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

808
809
810
        return this.http.get(`${AppSettings.API_ENDPOINT}/coursefields/list/?sector_id=` + sectorid, options)
            .map(response => response.json());
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
811

Open Source Developer's avatar
Open Source Developer committed
812

Open Source Developer's avatar
trans    
Open Source Developer committed
813
814
815
816
817
818
819
820
821
822
823
    getCritiria(headerid, type) {
        let headerIdNew = headerid.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 });
824
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/critiriachosen/` + headerIdNew + "/" + type, options)
Open Source Developer's avatar
trans    
Open Source Developer committed
825
826
827
            .map(response => response.json());
    }

Open Source Developer's avatar
Open Source Developer committed
828

Open Source Developer's avatar
changes    
Open Source Developer committed
829
    
830
    getSchoolId() {
Open Source Developer's avatar
trans    
Open Source Developer committed
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845

        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 });
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/getschool/`, options)
            .map(response => response.json());

    }

Open Source Developer's avatar
changes    
Open Source Developer committed
846
    
Open Source Developer's avatar
trans    
Open Source Developer committed
847