helper-data-service.ts 40.1 KB
Newer Older
1 2 3 4 5 6
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";
7
import { IRegionRecord, IRegionSchoolRecord, IRRegion, IRRegionSchool } from "../store/regionschools/regionschools.types";
8 9 10 11 12 13 14 15 16 17 18 19
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
    transformRegionSchoolsSchema(regionSchools: any) {
281
        let rsa = Array<IRRegion>();
282 283 284 285 286 287
        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(<IRRegion>{ "region_id": regionSchool.region_id, "region_name": regionSchool.region_name, "epals": Array<IRRegionSchool>() });
293 294
                trackRegionId = regionSchool.region_id;
            }
295
            rsa[trackIndex].epals.push(<IRRegionSchool>{ "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
        });
    };
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
                });
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
    }
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
        });
    }
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
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
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
Open Source Developer committed
509 510


511

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

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


Open Source Developer's avatar
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
Open Source Developer committed
534 535
        });
        let headers = new Headers({
536
            "Content-Type": "application/json",
Open Source Developer's avatar
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
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
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, period) {
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 675 676 677 678 679 680
        if (unallocated == 1) {
            route = "ministry/send-unallocated-massive-mail/" + period;
        } else if (unallocated == 2) {
            route = "ministry/send-unallocated-sc-massive-mail/" + period;
        } else if (unallocated == 3) {
            route = "ministry/send-located-massive-mail/" + period;
        }
681

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

Open Source Developer's avatar
Open Source Developer committed
686

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

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

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

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

720
    getRegions(username, userpassword) {
721

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

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

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

734
    }
735

736
    getAdminAreas(username, userpassword, regionid) {
737

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

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

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

749
    getSchoolsPerRegion(username, userpassword, regionid) {
750

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

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

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

762
    getSchoolsPerAdminArea(username, userpassword, adminid) {
763

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

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

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

775

776
    getUserRegistryNo(username, userpassword) {
777

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

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

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

789
    retrieveAdminSettings(username, userpassword) {
790

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

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

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

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

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

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

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


817
    getSectors(username, userpassword, classid) {
818

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

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

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

830
    getCourses(username, userpassword, sectorid) {
831

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

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

839 840 841
        return this.http.get(`${AppSettings.API_ENDPOINT}/coursefields/list/?sector_id=` + sectorid, options)
            .map(response => response.json());
    }
Open Source Developer's avatar
Open Source Developer committed
842

Open Source Developer's avatar
Open Source Developer committed
843

Open Source Developer's avatar
Open Source Developer committed
844 845 846 847 848 849 850 851 852 853 854
    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 });
855
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/critiriachosen/` + headerIdNew + "/" + type, options)
Open Source Developer's avatar
Open Source Developer committed
856 857 858
            .map(response => response.json());
    }

Open Source Developer's avatar
Open Source Developer committed
859

860

861
    getSchoolId() {
Open Source Developer's avatar
Open Source Developer committed
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

        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());

    }


878 879


Open Source Developer's avatar
Open Source Developer committed
880
    FindCapacityPerSchool() {
881

882
        this.loginInfo$.getValue().forEach(loginInfoToken => {
883 884 885 886 887 888 889 890
            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
Open Source Developer committed
891 892
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/findcapacityperschool/`, options)

Open Source Developer's avatar
Open Source Developer committed
893 894
            .map(response => response.json());

895
    }
Open Source Developer's avatar
Open Source Developer committed
896 897


Open Source Developer's avatar
Open Source Developer committed
898
    FindCoursesPerSchool() {
Open Source Developer's avatar
Open Source Developer committed
899

900
        this.loginInfo$.getValue().forEach(loginInfoToken => {
Open Source Developer's avatar
Open Source Developer committed
901 902 903 904 905 906 907 908
            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
Open Source Developer committed
909
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/findcoursesperschool/`, options)
Open Source Developer's avatar
Open Source Developer committed
910

911 912
            .map(response => response.json());

913
    }
914

915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
    getServiceAllDidacticYears() {
        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/get-didactic-years`, options)
            .map(response => response.json());
    }

    getServiceStudentPromotion(didactic_year_id, lastname, firstname, father_firstname, mother_firstname, birthdate, registry_no, level_name) {
        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 });
        let rpath = [didactic_year_id, lastname, firstname, father_firstname, mother_firstname, birthdate, registry_no, level_name].join("/");
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/get-student-promotion/` + rpath, options)
            .map(response => response.json());
    }

    getServiceStudentCertification(didactic_year_id, lastname, firstname, father_firstname, mother_firstname, birthdate, registry_no, level_name) {
        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 });
        let rpath = [didactic_year_id, lastname, firstname, father_firstname, mother_firstname, birthdate, registry_no, level_name].join("/");
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/get-student-certification/` + rpath, options)
            .map(response => response.json());
    }

959
    createPdfServerSide(headerid) {
960 961 962 963 964

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

966
        let options = new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob });
967 968
        let headerIdStr = headerid.toString();
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/pdf-application/` + headerIdStr, options)
969 970
            .map((res) => {
                return new Blob([res['_body']], { type: "application/octet-stream" })
971 972
            })
            .subscribe(
973 974 975 976
            data => {
                FileSaver.saveAs(data, "appConfirmation.pdf");
            },
            err => console.error(err));
977 978
    }

979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998

    sendmail(email, name, surname, message) {
         let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
        return new Promise((resolve, reject) => {
            this.http.post(`${AppSettings.API_ENDPOINT}/epal/user/sendmail`, { userEmail: email, userName : name, userSurname : surname, userMessage : message }, options)
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
                },
                error => {
                    console.log("Error Sending Email");
                    reject("Error Sending email");
                });
        });
    }

999
/*    showResults(headerid) {
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

      let headers = new Headers({
          "Content-Type": "application/json",
      });
      this.createAuthorizationHeader(headers);
      let options = new RequestOptions({ headers: headers });
      let headerIdStr = headerid.toString();
      return this.http.get(`${AppSettings.API_ENDPOINT}/epal/showresults/` + headerIdStr, options)
          .map(response => response.json());

1010
    } */
1011

Open Source Developer's avatar
Open Source Developer committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

    findTotalStudents() {

        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/totalstudent`, options)

            .map(response => response.json());
        }


Open Source Developer's avatar
Open Source Developer committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058


deleteApplicationforDirector(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/deleteFromDirector`, { applicationId: appId }, options)
                .map(response => response.json())
                .subscribe(data => {
                    resolve(<any>data);
                },
                error => {
                    console.log("Error Removing Application");
                    reject("Error Removing Application");
                });
        });
    }




1059
}