Skip to content

Commit

Permalink
Merge pull request #332 from Consdata/IKC-349-demo-1.7
Browse files Browse the repository at this point in the history
IKC-349 Kouncil demo 1.7
  • Loading branch information
pbelke authored Feb 19, 2024
2 parents 11834d2 + 65a66d1 commit 200b431
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 140 deletions.
14 changes: 14 additions & 0 deletions kouncil-frontend/apps/kouncil/src/app/app-factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {environment} from '../environments/environment';
import {TopicsBackendService, TopicsDemoService, TopicsService} from '@app/feat-topics';
import {SendBackendService, SendDemoService, SendService} from '@app/feat-send';
import {ResendBackendService, ResendDemoService, ResendService} from '@app/resend-events';
import {SurveyBackendService} from './survey/survey.backend.service';
import {SurveyService} from './survey/survey.service';
import {SurveyDemoService} from './survey/survey.demo.service';

export function topicsServiceFactory(http: HttpClient): TopicsService {
switch (environment.backend) {
Expand Down Expand Up @@ -37,3 +40,14 @@ export function resendServiceFactory(http: HttpClient): ResendService {
return new ResendDemoService();
}
}

export function surveyServiceFactory(http: HttpClient): SurveyService {
switch (environment.backend) {
case Backend.SERVER: {
return new SurveyBackendService(http);
}
case Backend.DEMO:
default:
return new SurveyDemoService();
}
}
13 changes: 12 additions & 1 deletion kouncil-frontend/apps/kouncil/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ import {Backend} from '@app/common-model';
import {ConfirmModule} from '@app/feat-confirm';
import {CommonUtilsModule, HttpClientInterceptor, SearchService} from '@app/common-utils';
import {FeatTopicsModule, TopicsService} from '@app/feat-topics';
import {resendServiceFactory, sendServiceFactory, topicsServiceFactory} from './app-factories';
import {
resendServiceFactory,
sendServiceFactory,
surveyServiceFactory,
topicsServiceFactory
} from './app-factories';
import {FeatNoDataModule} from '@app/feat-no-data';
import {ServersBackendService, ServersDemoService, ServersService} from '@app/common-servers';
import {FeatSendModule, SendService} from '@app/feat-send';
Expand All @@ -86,6 +91,7 @@ import {SurveyComponent} from './survey/survey.component';
import {
SurveyScaleQuestionComponent
} from './survey/survey-scale-question/survey-scale-question.component';
import {SurveyService} from './survey/survey.service';


export function configProviderFactory(provider: ServersService): Promise<boolean> {
Expand Down Expand Up @@ -242,6 +248,11 @@ export function trackServiceFactory(http: HttpClient, rxStompService: RxStompSer
useFactory: authServiceFactory,
deps: [HttpClient]
},
{
provide: SurveyService,
useFactory: surveyServiceFactory,
deps: [HttpClient]
},
],
bootstrap: [AppComponent]
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class AuthDemoService implements AuthService {
}

canAccess(roles: KouncilRole[]): boolean {
const localStorageUserRoles = JSON.parse(localStorage.getItem(this.USER_ROLES));
if (this.userRoles.length === 0 && localStorageUserRoles.length > 0) {
this.userRoles = localStorageUserRoles;
}
return this.userRoles.some(userRole => roles.includes(userRole));
}

Expand Down
151 changes: 151 additions & 0 deletions kouncil-frontend/apps/kouncil/src/app/survey/survey.backend.service.ts
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;
}

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

0 comments on commit 200b431

Please sign in to comment.