|
| 1 | +import {Injectable, QueryList} from '@angular/core'; |
| 2 | +import {HttpClient, HttpHeaders} from '@angular/common/http'; |
| 3 | +import {Observable, Subject} from 'rxjs'; |
| 4 | +import {map} from 'rxjs/operators'; |
| 5 | +import {SurveyPending, SurveyQuestion} from './model/survey.model'; |
| 6 | +import { |
| 7 | + SurveyAnswer, |
| 8 | + SurveyQuestionResult, |
| 9 | + SurveyResultStatus, |
| 10 | + SurveyResultValue |
| 11 | +} from './model/survey-answer'; |
| 12 | +import { |
| 13 | + SurveyScaleQuestionComponent |
| 14 | +} from './survey-scale-question/survey-scale-question.component'; |
| 15 | +import {SurveyService} from './survey.service'; |
| 16 | + |
| 17 | +@Injectable() |
| 18 | +export class SurveyBackendService implements SurveyService { |
| 19 | + |
| 20 | + surveyBasePath: string; |
| 21 | + private header: string = 'x-api-key'; |
| 22 | + private secret: string = ']TCz)x\'t~$"UeRgZ;zt*:YK=W-`Xtq(<]@GtTL;DNf+eW'; |
| 23 | + |
| 24 | + private showPanelChanged$: Subject<boolean> = new Subject<boolean>(); |
| 25 | + private surveyChanged$: Subject<SurveyPending> = new Subject<SurveyPending>(); |
| 26 | + private questionsChanged$: Subject<Array<SurveyQuestion>> = new Subject<Array<SurveyQuestion>>(); |
| 27 | + |
| 28 | + private survey: SurveyPending; |
| 29 | + private questions: SurveyQuestion[]; |
| 30 | + private position: string; |
| 31 | + |
| 32 | + constructor(protected http: HttpClient) { |
| 33 | + } |
| 34 | + |
| 35 | + fetchSurvey$(route: string): void { |
| 36 | + this.fetchSurveyBasePath$().subscribe((urlExist) => { |
| 37 | + if (urlExist) { |
| 38 | + this.fetchSurvey(route); |
| 39 | + } |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + private fetchSurvey(route: string): void { |
| 44 | + this.http.post<Array<SurveyPending>>( |
| 45 | + `${this.surveyBasePath}/result/pending-with-definition/${localStorage.getItem('userId')}/${localStorage.getItem('installationId')}`, |
| 46 | + { |
| 47 | + receiverAttributes: [ |
| 48 | + { |
| 49 | + name: 'kouncil-user-id', |
| 50 | + value: localStorage.getItem('userId') |
| 51 | + }, |
| 52 | + { |
| 53 | + name: 'installation-id', |
| 54 | + value: localStorage.getItem('installationId') |
| 55 | + } |
| 56 | + ] |
| 57 | + }, { |
| 58 | + headers: this.getHeaders() |
| 59 | + }).subscribe(data => { |
| 60 | + this.processSurvey(data, route); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + answerSurvey$(questionComponents: QueryList<SurveyScaleQuestionComponent>, route: string): void { |
| 65 | + const answer = this.prepareSurveyAnswer(questionComponents); |
| 66 | + |
| 67 | + this.http.patch<void>(`${this.surveyBasePath}/result/answer/${localStorage.getItem('userId')}/${localStorage.getItem('installationId')}`, answer, { |
| 68 | + headers: this.getHeaders() |
| 69 | + }).subscribe(() => { |
| 70 | + setTimeout(() => { |
| 71 | + this.fetchSurvey$(route); |
| 72 | + }, 2000); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + markSurveyAsOpened(sentId: string): void { |
| 77 | + const params = {'sentId': sentId}; |
| 78 | + this.http.post<void>(`${this.surveyBasePath}/activity/SURVEY_OPENED/${localStorage.getItem('userId')}/${localStorage.getItem('installationId')}`, {}, { |
| 79 | + headers: this.getHeaders(), |
| 80 | + params |
| 81 | + }).pipe().subscribe(); |
| 82 | + } |
| 83 | + |
| 84 | + fetchSurveyBasePath$(): Observable<boolean> { |
| 85 | + return this.http.get('/api/survey/config', {responseType: 'text'}).pipe(map((basePath) => { |
| 86 | + this.surveyBasePath = basePath; |
| 87 | + return this.surveyBasePath.length > 0; |
| 88 | + })); |
| 89 | + } |
| 90 | + |
| 91 | + private getHeaders(): HttpHeaders { |
| 92 | + return new HttpHeaders().set(this.header, this.secret); |
| 93 | + } |
| 94 | + |
| 95 | + get showPanelObservable$(): Observable<boolean> { |
| 96 | + return this.showPanelChanged$.asObservable(); |
| 97 | + } |
| 98 | + |
| 99 | + private processSurvey(result: Array<SurveyPending>, route: string): void { |
| 100 | + if (result.length > 0) { |
| 101 | + this.survey = result[0]; |
| 102 | + this.surveyChanged$.next(this.survey); |
| 103 | + |
| 104 | + this.showPanelChanged$.next( |
| 105 | + this.survey.triggers.some(trigger => { |
| 106 | + if (route.endsWith(trigger.elementId)) { |
| 107 | + this.position = trigger.elementId; |
| 108 | + return true; |
| 109 | + } |
| 110 | + return false; |
| 111 | + }) |
| 112 | + ); |
| 113 | + |
| 114 | + const surveyDesign = JSON.parse(this.survey.surveyDefinition.design); |
| 115 | + this.questions = surveyDesign['questions']; |
| 116 | + this.questionsChanged$.next(this.questions); |
| 117 | + this.markSurveyAsOpened(this.survey.sentId); |
| 118 | + } else { |
| 119 | + this.showPanelChanged$.next(false); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + getSurveyObservable$(): Observable<SurveyPending> { |
| 124 | + return this.surveyChanged$.asObservable(); |
| 125 | + } |
| 126 | + |
| 127 | + getQuestionsChanged$(): Observable<Array<SurveyQuestion>> { |
| 128 | + return this.questionsChanged$.asObservable(); |
| 129 | + } |
| 130 | + |
| 131 | + private prepareSurveyAnswer(questionComponents: QueryList<SurveyScaleQuestionComponent>): SurveyAnswer { |
| 132 | + const answers = []; |
| 133 | + questionComponents.forEach(component => { |
| 134 | + answers.push({ |
| 135 | + questionId: component.question.id, |
| 136 | + value: String(component.selectedValue), |
| 137 | + answer: component.reason |
| 138 | + } as SurveyQuestionResult); |
| 139 | + }); |
| 140 | + |
| 141 | + return { |
| 142 | + status: SurveyResultStatus.FILLED, |
| 143 | + sentId: this.survey.sentId, |
| 144 | + position: this.position, |
| 145 | + result: { |
| 146 | + questions: answers |
| 147 | + } as SurveyResultValue |
| 148 | + } as SurveyAnswer; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments