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/application/` + 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
Open Source Developer committed
512
    getStudentPerSchool(classId, sector, specialit) {
513

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

Open Source Developer's avatar
trans  
Open Source Developer committed
519 520 521 522 523
        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
524
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/studentperschool/` + classId + "/" + sector + "/" + specialit, options)
Open Source Developer's avatar
trans  
Open Source Developer committed
525
            .map(response => response.json());
526
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
527

Open Source Developer's avatar
transf  
Open Source Developer committed
528 529


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


544
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
545 546


Open Source Developer's avatar
Open Source Developer committed
547

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

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

562
    }
Open Source Developer's avatar
Open Source Developer committed
563

564 565 566 567 568 569 570 571
    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) => {
572
            this.http.post(`${AppSettings.API_ENDPOINT}/ministry/login`, { username: username, userpassword: userpassword }, options)
573 574 575 576 577 578
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
                },
                error => {
                    reject("Error Sending Ministry Credentials");
579
                });
580 581 582 583
        });

    }

584 585 586 587 588 589
    makeDistribution(username, userpassword) {

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

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

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

    }

606 607 608 609 610 611 612 613 614 615
    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) => {
616
            this.http.post(`${AppSettings.API_ENDPOINT}/epal/distribution-secondperiod`, { }, options)
617 618 619 620 621 622
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
                },
                error => {
                    reject("Error POST in makeDistributionSecondPeriod");
623
                });
624 625 626 627
        });

    }

628

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

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

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

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

    }

665
    informUnlocatedStudents(username, userpassword, unallocated) {
666

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

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

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

Open Source Developer's avatar
Open Source Developer committed
685

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

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

701
    getCoursePerPerfecture(PerfectureId) {
Open Source Developer's avatar
trans  
Open Source Developer committed
702 703 704 705 706 707 708 709 710 711 712 713
        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
714

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

719
    getRegions(username, userpassword) {
720

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

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

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

733
    }
734

735
    getAdminAreas(username, userpassword, regionid) {
736

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

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

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

748
    getSchoolsPerRegion(username, userpassword, regionid) {
749

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

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

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

761
    getSchoolsPerAdminArea(username, userpassword, adminid) {
762

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

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

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

774

775
    getUserRegistryNo(username, userpassword) {
776

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

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

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

788
    retrieveAdminSettings(username, userpassword) {
789

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