Skip to content

Commit 200b431

Browse files
authored
Merge pull request #332 from Consdata/IKC-349-demo-1.7
IKC-349 Kouncil demo 1.7
2 parents 11834d2 + 65a66d1 commit 200b431

File tree

6 files changed

+226
-140
lines changed

6 files changed

+226
-140
lines changed

kouncil-frontend/apps/kouncil/src/app/app-factories.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {environment} from '../environments/environment';
44
import {TopicsBackendService, TopicsDemoService, TopicsService} from '@app/feat-topics';
55
import {SendBackendService, SendDemoService, SendService} from '@app/feat-send';
66
import {ResendBackendService, ResendDemoService, ResendService} from '@app/resend-events';
7+
import {SurveyBackendService} from './survey/survey.backend.service';
8+
import {SurveyService} from './survey/survey.service';
9+
import {SurveyDemoService} from './survey/survey.demo.service';
710

811
export function topicsServiceFactory(http: HttpClient): TopicsService {
912
switch (environment.backend) {
@@ -37,3 +40,14 @@ export function resendServiceFactory(http: HttpClient): ResendService {
3740
return new ResendDemoService();
3841
}
3942
}
43+
44+
export function surveyServiceFactory(http: HttpClient): SurveyService {
45+
switch (environment.backend) {
46+
case Backend.SERVER: {
47+
return new SurveyBackendService(http);
48+
}
49+
case Backend.DEMO:
50+
default:
51+
return new SurveyDemoService();
52+
}
53+
}

kouncil-frontend/apps/kouncil/src/app/app.module.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ import {Backend} from '@app/common-model';
6262
import {ConfirmModule} from '@app/feat-confirm';
6363
import {CommonUtilsModule, HttpClientInterceptor, SearchService} from '@app/common-utils';
6464
import {FeatTopicsModule, TopicsService} from '@app/feat-topics';
65-
import {resendServiceFactory, sendServiceFactory, topicsServiceFactory} from './app-factories';
65+
import {
66+
resendServiceFactory,
67+
sendServiceFactory,
68+
surveyServiceFactory,
69+
topicsServiceFactory
70+
} from './app-factories';
6671
import {FeatNoDataModule} from '@app/feat-no-data';
6772
import {ServersBackendService, ServersDemoService, ServersService} from '@app/common-servers';
6873
import {FeatSendModule, SendService} from '@app/feat-send';
@@ -86,6 +91,7 @@ import {SurveyComponent} from './survey/survey.component';
8691
import {
8792
SurveyScaleQuestionComponent
8893
} from './survey/survey-scale-question/survey-scale-question.component';
94+
import {SurveyService} from './survey/survey.service';
8995

9096

9197
export function configProviderFactory(provider: ServersService): Promise<boolean> {
@@ -242,6 +248,11 @@ export function trackServiceFactory(http: HttpClient, rxStompService: RxStompSer
242248
useFactory: authServiceFactory,
243249
deps: [HttpClient]
244250
},
251+
{
252+
provide: SurveyService,
253+
useFactory: surveyServiceFactory,
254+
deps: [HttpClient]
255+
},
245256
],
246257
bootstrap: [AppComponent]
247258
})

kouncil-frontend/apps/kouncil/src/app/login/auth.demo.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export class AuthDemoService implements AuthService {
7070
}
7171

7272
canAccess(roles: KouncilRole[]): boolean {
73+
const localStorageUserRoles = JSON.parse(localStorage.getItem(this.USER_ROLES));
74+
if (this.userRoles.length === 0 && localStorageUserRoles.length > 0) {
75+
this.userRoles = localStorageUserRoles;
76+
}
7377
return this.userRoles.some(userRole => roles.includes(userRole));
7478
}
7579

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {Injectable, QueryList} from '@angular/core';
2+
import {Observable, of} from 'rxjs';
3+
import {SurveyService} from './survey.service';
4+
import {
5+
SurveyScaleQuestionComponent
6+
} from './survey-scale-question/survey-scale-question.component';
7+
import {SurveyPending, SurveyQuestion} from './model/survey.model';
8+
9+
@Injectable()
10+
export class SurveyDemoService implements SurveyService {
11+
12+
constructor() {
13+
}
14+
15+
fetchSurveyBasePath$(): Observable<boolean> {
16+
return of(false);
17+
}
18+
19+
fetchSurvey$(_route: string): void {
20+
}
21+
22+
answerSurvey$(_questionComponents: QueryList<SurveyScaleQuestionComponent>, _route: string): void {
23+
}
24+
25+
get showPanelObservable$(): Observable<boolean> {
26+
return of(false);
27+
}
28+
29+
getSurveyObservable$(): Observable<SurveyPending> {
30+
return of();
31+
}
32+
33+
getQuestionsChanged$(): Observable<Array<SurveyQuestion>> {
34+
return of();
35+
}
36+
}

0 commit comments

Comments
 (0)