helper-data-service.ts 42.3 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        });
    }

    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");
                },
                () => console.log("Removing Application"));
179 180 181
        });
    }

182
    getCourseFields() {
183

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

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

231
    getRegionsWithSchools(classActive, courseActive) {
232
        this.loginInfo$.getValue().forEach(loginInfoToken => {
233 234
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
235
        });
236 237 238
        let headers = new Headers({
            "Content-Type": "application/json",
        });
239
        this.createAuthorizationHeader(headers);
240
        let options = new RequestOptions({ headers: headers });
241
        return new Promise((resolve, reject) => {
242
            let getConnectionString = null;
243 244

            if (classActive === 1)
245 246 247
                getConnectionString = `${AppSettings.API_ENDPOINT}/regions/list`;
            else if (classActive === 2)
                getConnectionString = `${AppSettings.API_ENDPOINT}/sectorsperschool/list?sector_id=${courseActive}`;
248
            else if (classActive === 3)
249
                getConnectionString = `${AppSettings.API_ENDPOINT}/coursesperschool/list?course_id=${courseActive}`;
250 251 252
            else if (classActive === 4)
                getConnectionString = `${AppSettings.API_ENDPOINT}/coursesperschool_night/list?course_id=${courseActive}`;

253
            this.http.get(getConnectionString, options)
254 255 256
                .map(response => response.json())
                .subscribe(data => {
                    resolve(this.transformRegionSchoolsSchema(data));
257
                },
258
                error => {
259
                    console.log("Error HTTP GET Service in getRegionsWithSchools method");
260 261
                    reject("Error HTTP GET Service");
                },
262
                () => console.log("region schools service"));
263 264 265
        });
    };

266
    getSectorsWithCourses() {
267
        this.loginInfo$.getValue().forEach(loginInfoToken => {
268 269
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
270 271 272 273 274 275
        });
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
276
        return new Promise((resolve, reject) => {
277
            this.http.get(`${AppSettings.API_ENDPOINT}/coursesectorfields/list`, options)
278 279 280
                .map(response => response.json())
                .subscribe(data => {
                    resolve(this.transformSectorCoursesSchema(data));
281
                },
282
                error => {
283
                    console.log("Error HTTP GET Service");
284 285
                    reject("Error HTTP GET Service");
                },
286
                () => console.log("region schools service"));
287 288 289
        });
    };

290 291 292 293 294 295 296 297
    transformRegionSchoolsSchema(regionSchools: any) {
        let rsa = Array<IRegion>();
        let trackRegionId: string;
        let trackIndex: number;

        trackRegionId = "";
        trackIndex = -1;

298
        let j = 0;
299 300 301
        regionSchools.forEach(regionSchool => {
            if (trackRegionId !== regionSchool.region_id) {
                trackIndex++;
302
                rsa.push(<IRegion>{ "region_id": regionSchool.region_id, "region_name": regionSchool.region_name, "epals": Array<IRegionSchool>() });
303 304
                trackRegionId = regionSchool.region_id;
            }
305
            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 });
306 307 308 309 310
            j++;
        });
        return rsa;
    }

311 312 313 314 315 316 317 318
    transformSectorCoursesSchema(sectorCourses: any) {
        let rsa = Array<ISector>();
        let trackSectorId: string;
        let trackIndex: number;

        trackSectorId = "";
        trackIndex = -1;

319
        let j = 0;
320 321 322
        sectorCourses.forEach(sectorCourse => {
            if (trackSectorId !== sectorCourse.sector_id) {
                trackIndex++;
323
                rsa.push(<ISector>{ "sector_id": sectorCourse.sector_id, "sector_name": sectorCourse.sector_name, "sector_selected": false, "courses": Array<ISectorCourse>() });
324 325
                trackSectorId = sectorCourse.sector_id;
            }
326
            rsa[trackIndex].courses.push(<ISectorCourse>{ "course_id": sectorCourse.course_id, "course_name": sectorCourse.course_name, "globalIndex": j, "selected": false });
327 328 329 330 331
            j++;
        });
        return rsa;
    }

332
    getCriteria() {
333

334
        this.loginInfo$.getValue().forEach(loginInfoToken => {
335 336
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
337 338 339 340 341 342 343 344
        });
        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)
345 346 347 348
                //this.http.get(`${AppSettings.API_ENDPOINT}/criteria/list?category=${category}`, options)
                .map(response => <ISectorField[]>response.json())
                .subscribe(data => {
                    resolve(data);
349
                },
350
                error => {
351
                    console.log("Error HTTP GET Service");
352 353
                    reject("Error HTTP GET Service");
                },
354
                () => console.log("Sector Fields Received"));
355 356
        });
    };
357

358
    getCurrentUser(oauthtoken, oauthrole) {
Open Source Developer's avatar
Open Source Developer committed
359 360

        this.authToken = oauthtoken;
361
        this.authRole = oauthrole;
362

Open Source Developer's avatar
Open Source Developer committed
363 364
        let headers = new Headers({
            "Content-Type": "application/json",
365
            "Access-Control-Allow-Credentials": "true",
Open Source Developer's avatar
Open Source Developer committed
366 367 368
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
369
        return new Promise((resolve, reject) => {
Open Source Developer's avatar
Open Source Developer committed
370
            this.http.get(`${AppSettings.API_ENDPOINT}/epal/curuser`, options)
371 372
                .map(response => response.json())
                .subscribe(data => {
373
                    window.onbeforeunload = function (e) {
374
                        let confirmationMessage = "\o/";
375 376 377
                        e.returnValue = confirmationMessage;
                        return confirmationMessage;
                    };
378
                    resolve(this.transformUserSchema(data, oauthtoken, oauthrole));
379
                },
380
                error => {
381
                    console.log("Error HTTP GET Service");
382 383 384
                    reject("Error HTTP GET Service");
                },
                () => console.log("UserName Received"));
385
        });
386
    }
Open Source Developer's avatar
Open Source Developer committed
387

388 389 390 391
    setMineduCurrentUser(minedu_username, minedu_pwd, role) {
        this.minedu_userName = minedu_username;
        this.minedu_userPassword = minedu_pwd;
        this.authRole = role;
392
    }
393

Open Source Developer's avatar
Open Source Developer committed
394

395
    transformUserSchema(userlogin: any, oauthtoken: string, oauthrole: string) {
396
        let rsa = Array<ILoginInfoToken>();
397

398
        rsa.push(<ILoginInfoToken>{
399 400 401 402 403 404 405 406 407 408 409 410 411
            "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)
412
        });
413
        return rsa;
414

415
    }
416

417
    signOut() {
418
        // loginInfo$ take values only in case getCurrentUser (epal module) has been used ...WHY? TO BE CHECKED..
419
        this.loginInfo$.getValue().forEach(loginInfoToken => {
420 421
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
422
        });
423

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

428
        let options = new RequestOptions({ headers: headers, withCredentials: true });
429
        let logoutRoute = "/oauth/logout";
430 431 432 433 434
        if (this.authRole === MINISTRY_ROLE) {
            this.createMinistryAuthorizationHeader(headers, this.minedu_userName, this.minedu_userPassword);
        } else {
            this.createAuthorizationHeader(headers);
        }
435
        if (this.authRole === SCHOOL_ROLE || this.authRole === PDE_ROLE || this.authRole === DIDE_ROLE) {
436
            logoutRoute = "/cas/logout";
437
        } else if (this.authRole === MINISTRY_ROLE) {
438
            logoutRoute = "/ministry/logout";
439
        }
440

441
        return new Promise((resolve, reject) => {
442
            this.http.post(`${AppSettings.API_ENDPOINT}${logoutRoute}${AppSettings.API_ENDPOINT_PARAMS}`, {}, options)
443
                .map(response => response.json())
444
                .subscribe(data => {
445
                    this._cookieService.removeAll();
446
                    window.onbeforeunload = function () { console.log("unloading") };
447
                    resolve(data);
448
                },
449
                error => {
450
                    console.log("Error Logout");
451 452
                    reject("Error Logout");
                },
453
                () => console.log("Logging out"));
454 455
        });
    }
456

457 458 459 460 461 462 463 464 465 466 467 468 469
    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 => {
470
                    console.log("Error Logout");
471 472
                    reject("Error Logout");
                },
473
                () => console.log("Logging out"));
474 475 476
        });
    }

Open Source Developer's avatar
Open Source Developer committed
477
    getSubmittedPreviw() {
Open Source Developer's avatar
Open Source Developer committed
478

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

Open Source Developer's avatar
Open Source Developer committed
492

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


510 511 512
    getEpalchosen(headerid) {
        let headerIdNew = headerid.toString();
        this.loginInfo$.getValue().forEach(loginInfoToken => {
513 514
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
515
        });
Open Source Developer's avatar
Open Source Developer committed
516 517 518 519 520
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
521
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/epalchosen/` + headerIdNew, options)
Open Source Developer's avatar
Open Source Developer committed
522
            .map(response => response.json());
523
    }
Open Source Developer's avatar
Open Source Developer committed
524 525


Open Source Developer's avatar
Open Source Developer committed
526
    getSectorPerSchool() {
527

528
        this.loginInfo$.getValue().forEach(loginInfoToken => {
529 530
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
531
        });
Open Source Developer's avatar
Open Source Developer committed
532 533 534 535 536
        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
537

538 539
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/sectorperschool/`, options)
            .map(response => response.json());
540
    }
Open Source Developer's avatar
Open Source Developer committed
541

Open Source Developer's avatar
Open Source Developer committed
542

543
    getSpecialityPerSchool(SectorId) {
544

545 546
        let SectorIdNew = SectorId.toString();
        this.loginInfo$.getValue().forEach(loginInfoToken => {
547 548
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
549
        });
Open Source Developer's avatar
Open Source Developer committed
550 551 552 553 554
        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
555
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/specialityperschool/` + SectorIdNew, options)
Open Source Developer's avatar
Open Source Developer committed
556
            .map(response => response.json());
557
    }
Open Source Developer's avatar
Open Source Developer committed
558 559 560



561
    getStudentPerSchool(SelectId, classId, limitdown, limitup) {
562

563
        let SelectIdNew = SelectId.toString();
564 565


566
        this.loginInfo$.getValue().forEach(loginInfoToken => {
567 568
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
569
        });
570

Open Source Developer's avatar
Open Source Developer committed
571 572 573 574 575
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
576
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/studentperschool/` + SelectIdNew + "/" + classId + "/" + limitdown + "/" + limitup, options)
Open Source Developer's avatar
Open Source Developer committed
577
            .map(response => response.json());
578
    }
Open Source Developer's avatar
Open Source Developer committed
579

Open Source Developer's avatar
Open Source Developer committed
580 581


Open Source Developer's avatar
Open Source Developer committed
582
    saveConfirmStudents(students, type) {
583
        this.loginInfo$.getValue().forEach(loginInfoToken => {
584 585
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
Open Source Developer's avatar
Open Source Developer committed
586 587
        });
        let headers = new Headers({
588
            "Content-Type": "application/json",
Open Source Developer's avatar
Open Source Developer committed
589 590 591
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
592
        return this.http.post(`${AppSettings.API_ENDPOINT}/epal/confirmstudent`, { students, type }, options)
Open Source Developer's avatar
Open Source Developer committed
593
            .map(response => response.json());
594 595


596
    }
Open Source Developer's avatar
Open Source Developer committed
597 598


Open Source Developer's avatar
Open Source Developer committed
599

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

602
        this.loginInfo$.getValue().forEach(loginInfoToken => {
603 604
            this.authToken = loginInfoToken.auth_token;
            this.authRole = loginInfoToken.auth_role;
Open Source Developer's avatar
Open Source Developer committed
605 606
        });
        let headers = new Headers({
607
            "Content-Type": "application/json",
Open Source Developer's avatar
Open Source Developer committed
608 609 610
        });
        this.createAuthorizationHeader(headers);
        let options = new RequestOptions({ headers: headers });
611
        return this.http.post(`${AppSettings.API_ENDPOINT}/epal/savecapacity/` + taxi + "/" + tomeas + "/" + specialit, { capacity }, options)
Open Source Developer's avatar
Open Source Developer committed
612
            .map(response => response.json());
Open Source Developer's avatar
Open Source Developer committed
613

614
    }
Open Source Developer's avatar
Open Source Developer committed
615

616 617 618 619 620 621 622 623
    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) => {
624
            this.http.post(`${AppSettings.API_ENDPOINT}/ministry/login`, { username: username, userpassword: userpassword }, options)
625 626 627 628 629 630 631 632 633 634 635 636
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
                },
                error => {
                    reject("Error Sending Ministry Credentials");
                },
                () => console.log(""));
        });

    }

637 638 639 640 641 642
    makeDistribution(username, userpassword) {

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

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

        return new Promise((resolve, reject) => {
647
            this.http.post(`${AppSettings.API_ENDPOINT}/epal/distribution`, { username: username, userpassword: userpassword }, options)
648 649 650 651 652 653 654 655 656 657 658 659
                .map(response => response.json())
                .subscribe(data => {
                    resolve(data);
                },
                error => {
                    reject("Error POST in makeDistribution");
                },
                () => console.log(""));
        });

    }

660

661
    makeReport(username, userpassword, routepath, regionsel, adminsel, schsel, clsel, secsel, coursel, distribfinal) {
662 663 664 665 666

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

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

670
        if (routepath == "/ministry/general-report/") {
671
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath, options)
672 673 674
                .map(response => response.json());
        }
        else if (routepath == "/ministry/report-completeness/") {
675
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath + regionsel + "/" + adminsel + "/" + schsel, options)
676 677
                .map(response => response.json());
        }
678 679 680
        else if (routepath == "/ministry/report-all-stat/") {
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath + regionsel + "/" + adminsel + "/" + schsel + "/" +
                clsel + "/" + secsel + "/" + coursel + "/" + distribfinal, options)
681
                .map(response => response.json());
682 683 684 685
        }
        else if (routepath == "/ministry/report-no-capacity/") {
            let capacityFilter = 0;
            if (regionsel)
686
                capacityFilter = 1;
687 688 689
            return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath + capacityFilter, options)
                .map(response => response.json());
        }
690 691 692

    }

693
    informUnlocatedStudents(username, userpassword, unallocated) {
694

695 696 697 698 699
        let headers = new Headers({
            "Content-Type": "application/json",
        });
        this.createMinistryAuthorizationHeader(headers, username, userpassword);
        let options = new RequestOptions({ headers: headers });
700

701
        let route = "";
702
        if (unallocated == 1)
703
            route = "ministry/send-unallocated-massive-mail";
704 705 706
        else if(unallocated == 2)
            route = "ministry/send-unallocated-sc-massive-mail";
        else if(unallocated == 3)
707
            route = "ministry/send-located-massive-mail";
708

709 710
        return this.http.get(`${AppSettings.API_ENDPOINT}/` + route, options)
            .map(response => response.json());
711 712
    }

Open Source Developer's avatar
Open Source Developer committed
713

714
    getSchools() {
Open Source Developer's avatar
Open Source Developer committed
715 716 717 718 719 720 721 722 723 724

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

729
    getCoursePerPerfecture(PerfectureId) {
Open Source Developer's avatar
Open Source Developer committed
730 731 732 733 734 735 736 737 738 739 740 741 742
        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
743

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

748
    getRegions(username, userpassword) {
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
        // return this.http.get(`${AppSettings.API_ENDPOINT}` + routepath , options)
758 759 760
        //    .map(response => response.json());
        return this.http.get(`${AppSettings.API_ENDPOINT}/regionfields/list`, options)
            .map(response => response.json());
761

762
    }
763

764
    getAdminAreas(username, userpassword, regionid) {
765

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

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

773 774 775
        return this.http.get(`${AppSettings.API_ENDPOINT}/adminfields/list/?region=` + regionid, options)
            .map(response => response.json());
    }
776

777
    getSchoolsPerRegion(username, userpassword, regionid) {
778

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

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

786 787 788
        return this.http.get(`${AppSettings.API_ENDPOINT}/schoolfields_per_region/list/?region=` + regionid, options)
            .map(response => response.json());
    }
789

790
    getSchoolsPerAdminArea(username, userpassword, adminid) {
791

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

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

799 800 801
        return this.http.get(`${AppSettings.API_ENDPOINT}/schoolfields_per_admin/list/?adminarea=` + adminid, options)
            .map(response => response.json());
    }
802

803

804
    getUserRegistryNo(username, userpassword) {
805

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

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

813 814 815
        return this.http.get(`${AppSettings.API_ENDPOINT}/ministry/retrieve-registry-id`, options)
            .map(response => response.json());
    }
816

817
    retrieveAdminSettings(username, userpassword) {
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}/ministry/retrieve-settings`, options)
            .map(response => response.json());
    }
829

830
    storeAdminSettings(username, userpassword, capac, dirview, applogin, appresults) {
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
        return this.http.get(`${AppSettings.API_ENDPOINT}/ministry/store-settings/` +
840 841
            Number(capac) + "/" + Number(dirview) + "/" + Number(applogin) + "/" + Number(appresults), options)
            .map(response => response.json());
842
    }
843 844


845
    getSectors(username, userpassword, classid) {
846

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

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

854 855 856
        return this.http.get(`${AppSettings.API_ENDPOINT}/sectorfields/list`, options)
            .map(response => response.json());
    }
857

858
    getCourses(username, userpassword, sectorid) {
859

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

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

867 868 869
        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
870

Open Source Developer's avatar
Open Source Developer committed
871

Open Source Developer's avatar
Open Source Developer committed
872 873 874 875 876 877 878 879 880 881 882
    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 });
883
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/critiriachosen/` + headerIdNew + "/" + type, options)
Open Source Developer's avatar
Open Source Developer committed
884 885 886
            .map(response => response.json());
    }

Open Source Developer's avatar
Open Source Developer committed
887

888
    getCapacityPerSchool(taxi, tomeas, specialit) {
889

Open Source Developer's avatar
Open Source Developer committed
890 891 892 893 894 895 896 897 898
        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 });
899
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/findcapacity/` + taxi + "/" + tomeas + "/" + specialit, options)
Open Source Developer's avatar
Open Source Developer committed
900 901 902
            .map(response => response.json());

    }
Open Source Developer's avatar
Open Source Developer committed
903

904
    getSchoolId() {
Open Source Developer's avatar
Open Source Developer committed
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919

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

    }

920
    gettypeofschool() {
Open Source Developer's avatar
Open Source Developer committed
921

922
        this.loginInfo$.getValue().forEach(loginInfoToken => {
Open Source Developer's avatar
Open Source Developer committed
923 924 925 926 927 928 929 930 931 932
            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/gettypeofschool/`, options)
            .map(response => response.json());
Open Source Developer's avatar
Open Source Developer committed
933

934
    }
Open Source Developer's avatar
Open Source Developer committed
935

936
    getlimitsofcourse(classid) {
937

938
        this.loginInfo$.getValue().forEach(loginInfoToken => {
939 940 941 942 943 944 945 946
            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 });
947
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/getlimitspercourse/` + classid, options)
Open Source Developer's avatar
Open Source Developer committed
948 949
            .map(response => response.json());

950
    }
Open Source Developer's avatar
Open Source Developer committed
951 952


953
    FindCapacityPerSchool() {
Open Source Developer's avatar
Open Source Developer committed
954

955
        this.loginInfo$.getValue().forEach(loginInfoToken => {
Open Source Developer's avatar
Open Source Developer committed
956 957 958 959 960 961 962 963
            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 });
964
        return this.http.get(`${AppSettings.API_ENDPOINT}/epal/findcapacityperschool/`, options)
Open Source Developer's avatar
Open Source Developer committed
965

966 967
            .map(response => response.json());

968
    }
969

970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    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());
    }

1014
createPdfServerSide_OLD(auth_token, role)  {
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

  /*
  this.loginInfo$.getValue().forEach(loginInfoToken => {
      this.authToken = loginInfoToken.auth_token;
      this.authRole = loginInfoToken.auth_role;
      console.log("Θα μπει;");
      console.log(this.authToken);
  });
  */

  let headers = new Headers({
1026
      //"Content-Type": "application/json",
1027
      "Content-Type": "application/json",
1028 1029
       //"Access-Control-Allow-Origin": "true",

1030
  });
1031 1032 1033
  //this.authToken = auth_token;
  //this.authRole = role;
  console.log("Αυτά είναι:");
1034 1035 1036
  console.log(this.authToken);
  console.log(this.authRole);
  this.createAuthorizationHeader(headers);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
  //let options = new RequestOptions([{ headers: headers }, { responseType: ResponseContentType.Blob }]);
  let options = new RequestOptions({ headers: headers });

  console.log("Nikos1");

  //return this.http.get(`${AppSettings.API_ENDPOINT}/epal/pdf-application/`, options)
  //    .map(response => response.json());





1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
  return this.http.get(`${AppSettings.API_ENDPOINT}/epal/pdf-application/`,   options)

    .map(response => response.json())
    //.map(response => (response.blob())
    //.map((res:Response) => res.blob())
    //.map(response => (<Response>response).blob())
    .subscribe(
        data => {

            console.log("Hello!");
            console.log(data);
            var blob = new Blob([data['pdfString']], {type: 'application/pdf'});
            console.log(blob);
1062
            FileSaver.saveAs(blob, "appConfirmation.pdf");
1063

1064 1065 1066 1067
    },
        err => console.error(err),
    () => console.log('done')
);
1068 1069


1070

1071
  /*
1072 1073
  return new Promise((resolve, reject) => {
      this.http.post(`${AppSettings.API_ENDPOINT}/epal/pdf-application`, options)
1074
          //.map(response => response.json())
1075 1076 1077
          .subscribe(data => {
              resolve(data);
              console.log("Nik");
1078 1079 1080

              var blob = new Blob([data['_body']], {type: 'application/pdf'});
              console.log(blob);
1081
              FileSaver.saveAs(blob, "appConfirmation.pdf");
1082 1083 1084 1085 1086 1087 1088
              //console.log(data['_body']);
          },
          error => {
              reject("Error POST in createPdfServerSide");
          },
          () => console.log("Nikos!!!"));
  });
1089
  */
1090