-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #332 from Consdata/IKC-349-demo-1.7
IKC-349 Kouncil demo 1.7
- Loading branch information
Showing
6 changed files
with
226 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
kouncil-frontend/apps/kouncil/src/app/survey/survey.backend.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import {Injectable, QueryList} from '@angular/core'; | ||
import {HttpClient, HttpHeaders} from '@angular/common/http'; | ||
import {Observable, Subject} from 'rxjs'; | ||
import {map} from 'rxjs/operators'; | ||
import {SurveyPending, SurveyQuestion} from './model/survey.model'; | ||
import { | ||
SurveyAnswer, | ||
SurveyQuestionResult, | ||
SurveyResultStatus, | ||
SurveyResultValue | ||
} from './model/survey-answer'; | ||
import { | ||
SurveyScaleQuestionComponent | ||
} from './survey-scale-question/survey-scale-question.component'; | ||
import {SurveyService} from './survey.service'; | ||
|
||
@Injectable() | ||
export class SurveyBackendService implements SurveyService { | ||
|
||
surveyBasePath: string; | ||
private header: string = 'x-api-key'; | ||
private secret: string = ']TCz)x\'t~$"UeRgZ;zt*:YK=W-`Xtq(<]@GtTL;DNf+eW'; | ||
|
||
private showPanelChanged$: Subject<boolean> = new Subject<boolean>(); | ||
private surveyChanged$: Subject<SurveyPending> = new Subject<SurveyPending>(); | ||
private questionsChanged$: Subject<Array<SurveyQuestion>> = new Subject<Array<SurveyQuestion>>(); | ||
|
||
private survey: SurveyPending; | ||
private questions: SurveyQuestion[]; | ||
private position: string; | ||
|
||
constructor(protected http: HttpClient) { | ||
} | ||
|
||
fetchSurvey$(route: string): void { | ||
this.fetchSurveyBasePath$().subscribe((urlExist) => { | ||
if (urlExist) { | ||
this.fetchSurvey(route); | ||
} | ||
}); | ||
} | ||
|
||
private fetchSurvey(route: string): void { | ||
this.http.post<Array<SurveyPending>>( | ||
`${this.surveyBasePath}/result/pending-with-definition/${localStorage.getItem('userId')}/${localStorage.getItem('installationId')}`, | ||
{ | ||
receiverAttributes: [ | ||
{ | ||
name: 'kouncil-user-id', | ||
value: localStorage.getItem('userId') | ||
}, | ||
{ | ||
name: 'installation-id', | ||
value: localStorage.getItem('installationId') | ||
} | ||
] | ||
}, { | ||
headers: this.getHeaders() | ||
}).subscribe(data => { | ||
this.processSurvey(data, route); | ||
}); | ||
} | ||
|
||
answerSurvey$(questionComponents: QueryList<SurveyScaleQuestionComponent>, route: string): void { | ||
const answer = this.prepareSurveyAnswer(questionComponents); | ||
|
||
this.http.patch<void>(`${this.surveyBasePath}/result/answer/${localStorage.getItem('userId')}/${localStorage.getItem('installationId')}`, answer, { | ||
headers: this.getHeaders() | ||
}).subscribe(() => { | ||
setTimeout(() => { | ||
this.fetchSurvey$(route); | ||
}, 2000); | ||
}); | ||
} | ||
|
||
markSurveyAsOpened(sentId: string): void { | ||
const params = {'sentId': sentId}; | ||
this.http.post<void>(`${this.surveyBasePath}/activity/SURVEY_OPENED/${localStorage.getItem('userId')}/${localStorage.getItem('installationId')}`, {}, { | ||
headers: this.getHeaders(), | ||
params | ||
}).pipe().subscribe(); | ||
} | ||
|
||
fetchSurveyBasePath$(): Observable<boolean> { | ||
return this.http.get('/api/survey/config', {responseType: 'text'}).pipe(map((basePath) => { | ||
this.surveyBasePath = basePath; | ||
return this.surveyBasePath.length > 0; | ||
})); | ||
} | ||
|
||
private getHeaders(): HttpHeaders { | ||
return new HttpHeaders().set(this.header, this.secret); | ||
} | ||
|
||
get showPanelObservable$(): Observable<boolean> { | ||
return this.showPanelChanged$.asObservable(); | ||
} | ||
|
||
private processSurvey(result: Array<SurveyPending>, route: string): void { | ||
if (result.length > 0) { | ||
this.survey = result[0]; | ||
this.surveyChanged$.next(this.survey); | ||
|
||
this.showPanelChanged$.next( | ||
this.survey.triggers.some(trigger => { | ||
if (route.endsWith(trigger.elementId)) { | ||
this.position = trigger.elementId; | ||
return true; | ||
} | ||
return false; | ||
}) | ||
); | ||
|
||
const surveyDesign = JSON.parse(this.survey.surveyDefinition.design); | ||
this.questions = surveyDesign['questions']; | ||
this.questionsChanged$.next(this.questions); | ||
this.markSurveyAsOpened(this.survey.sentId); | ||
} else { | ||
this.showPanelChanged$.next(false); | ||
} | ||
} | ||
|
||
getSurveyObservable$(): Observable<SurveyPending> { | ||
return this.surveyChanged$.asObservable(); | ||
} | ||
|
||
getQuestionsChanged$(): Observable<Array<SurveyQuestion>> { | ||
return this.questionsChanged$.asObservable(); | ||
} | ||
|
||
private prepareSurveyAnswer(questionComponents: QueryList<SurveyScaleQuestionComponent>): SurveyAnswer { | ||
const answers = []; | ||
questionComponents.forEach(component => { | ||
answers.push({ | ||
questionId: component.question.id, | ||
value: String(component.selectedValue), | ||
answer: component.reason | ||
} as SurveyQuestionResult); | ||
}); | ||
|
||
return { | ||
status: SurveyResultStatus.FILLED, | ||
sentId: this.survey.sentId, | ||
position: this.position, | ||
result: { | ||
questions: answers | ||
} as SurveyResultValue | ||
} as SurveyAnswer; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
kouncil-frontend/apps/kouncil/src/app/survey/survey.demo.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {Injectable, QueryList} from '@angular/core'; | ||
import {Observable, of} from 'rxjs'; | ||
import {SurveyService} from './survey.service'; | ||
import { | ||
SurveyScaleQuestionComponent | ||
} from './survey-scale-question/survey-scale-question.component'; | ||
import {SurveyPending, SurveyQuestion} from './model/survey.model'; | ||
|
||
@Injectable() | ||
export class SurveyDemoService implements SurveyService { | ||
|
||
constructor() { | ||
} | ||
|
||
fetchSurveyBasePath$(): Observable<boolean> { | ||
return of(false); | ||
} | ||
|
||
fetchSurvey$(_route: string): void { | ||
} | ||
|
||
answerSurvey$(_questionComponents: QueryList<SurveyScaleQuestionComponent>, _route: string): void { | ||
} | ||
|
||
get showPanelObservable$(): Observable<boolean> { | ||
return of(false); | ||
} | ||
|
||
getSurveyObservable$(): Observable<SurveyPending> { | ||
return of(); | ||
} | ||
|
||
getQuestionsChanged$(): Observable<Array<SurveyQuestion>> { | ||
return of(); | ||
} | ||
} |
Oops, something went wrong.