-
Notifications
You must be signed in to change notification settings - Fork 70
/
survey-response.service.ts
308 lines (287 loc) · 9.55 KB
/
survey-response.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import {BindingScope, inject, injectable} from '@loopback/core';
import {ILogger, LOGGER} from '@sourceloop/core';
import {repository, Where} from '@loopback/repository';
import {HttpErrors} from '@loopback/rest';
import {JwtPayload, Secret, verify} from 'jsonwebtoken';
import {isArray} from 'lodash';
import {AuthorizeErrorKeys} from 'loopback4-authorization';
import moment from 'moment';
import {ErrorKeys} from '../enum/error-keys.enum';
import {QuestionType} from '../enum/question.enum';
import {
Question,
SurveyCycle,
SurveyResponse,
SurveyResponseDetailDto,
SurveyResponseDto,
} from '../models';
import {SurveyResponseDetail} from '../models/survey-response-detail.model';
import {SurveyCycleRepository} from '../repositories/survey-cycle.repository';
import {SurveyResponderRepository} from '../repositories/survey-responder.repository';
import {SurveyResponseDetailRepository} from '../repositories/survey-response-detail.repository';
import {SurveyResponseRepository} from '../repositories/survey-response.repository';
import {SurveyRepository} from '../repositories/survey.repository';
const sqlDateFormat = 'YYYY-MM-DD';
@injectable({scope: BindingScope.TRANSIENT})
export class SurveyResponseService {
constructor(
@repository(SurveyResponseRepository)
public surveyResponseRepository: SurveyResponseRepository,
@repository(SurveyResponseDetailRepository)
public surveyResponseDetailRepository: SurveyResponseDetailRepository,
@repository(SurveyRepository)
public surveyRepository: SurveyRepository,
@repository(SurveyCycleRepository)
protected surveyCycleRepository: SurveyCycleRepository,
@inject(LOGGER.LOGGER_INJECT) public logger: ILogger,
@repository(SurveyResponderRepository)
protected surveyResponderRepository: SurveyResponderRepository,
) {}
async createResponse(
token: string,
surveyId: string,
surveyResponseDto: SurveyResponseDto,
) {
const secret = process.env.JWT_SECRET as Secret;
const decodedToken = verify(token, secret) as JwtPayload;
if (decodedToken.surveyCycleId) {
const responders = await this.surveyRepository
.surveyResponders(surveyId)
.find({
where: {
surveyCycleId: decodedToken.surveyCycleId,
},
});
const responderEmails: string[] = [];
responders?.forEach(responder => {
if (responder.email) {
responderEmails.push(responder.email);
}
});
const validUser = responderEmails.includes(decodedToken.email);
if (!validUser) {
throw new HttpErrors.Unauthorized(ErrorKeys.NotAuthorised);
}
}
const surveyResponseDetailsDto =
surveyResponseDto.surveyResponseDetailArray;
if (!surveyResponseDetailsDto) {
throw new HttpErrors.BadRequest(ErrorKeys.SurveyResponseDetailNotFound);
}
delete surveyResponseDto.surveyResponseDetailArray;
const survey = await this.surveyRepository.findById(surveyId, {
include: [
{
relation: 'questions',
scope: {
include: [
{
relation: 'followUpQuestions',
scope: {
include: [
{
relation: 'followUpQuestions',
},
],
},
},
],
},
},
{
relation: 'surveyCycles',
scope: {
where: {
and: [
{isActivated: true},
{endDate: {gte: moment().format(sqlDateFormat)}},
{startDate: {lte: moment().format(sqlDateFormat)}},
],
},
order: ['created_on DESC'],
},
},
],
});
const activeCycleId = this.validateSurveyCycleAndGetCycleId(
survey.surveyCycles,
);
await this.validateAndGetResponder(
surveyResponseDto.surveyResponderId,
surveyId,
activeCycleId,
);
const surveyResponse = new SurveyResponse({
surveyResponderId: surveyResponseDto.surveyResponderId,
surveyCycleId: activeCycleId,
extId: surveyResponseDto.extId,
extMetadata: surveyResponseDto.extMetadata,
});
await this.surveyResponseRepository.create(surveyResponse);
// fetch createdSurveyResponse with id
const createdSurveyResponse = await this.surveyResponseRepository.findOne({
where: {
and: [
{surveyCycleId: activeCycleId},
{surveyResponderId: surveyResponseDto.surveyResponderId},
],
} as Where<SurveyResponse>,
order: ['created_on DESC'],
});
if (!createdSurveyResponse?.id) {
throw new HttpErrors.NotFound();
}
// create map of question with question id
let questions: {[key: string]: Question} = {};
if (survey?.questions?.length) {
questions = this.getQuestionMapId(survey?.questions);
}
const surveyResponseDetails = this.handleSurveyResponseDetail(
surveyResponseDetailsDto,
questions,
createdSurveyResponse.id,
);
if (createdSurveyResponse.extId || createdSurveyResponse.extMetadata) {
surveyResponseDetails.forEach(quesResp => {
quesResp.extId = createdSurveyResponse.extId;
quesResp.extMetadata = createdSurveyResponse.extMetadata;
});
}
await this.surveyResponseDetailRepository.createAll(surveyResponseDetails);
return createdSurveyResponse;
}
validateSurveyCycleAndGetCycleId(surveyCycles: SurveyCycle[]) {
if (!surveyCycles?.length || !surveyCycles[0].id) {
throw new HttpErrors.Unauthorized(ErrorKeys.NotAuthorised);
}
return surveyCycles[0].id;
}
handleSurveyResponseDetail(
surveyResponseDetailsDto: SurveyResponseDetailDto[],
questions: {[key: string]: Question},
surveyResponseId: string,
) {
const surveyResponseDetails: SurveyResponseDetail[] = [];
surveyResponseDetailsDto?.forEach(questionResponse => {
this.handleResponseWithType(
questionResponse,
surveyResponseDetails,
questions[questionResponse.questionId],
surveyResponseId,
);
});
return surveyResponseDetails;
}
handleResponseWithType(
surveyResponseDetailDto: SurveyResponseDetailDto,
surveyResponseDetails: SurveyResponseDetail[],
question: Question,
surveyResponseId: string,
) {
if (
surveyResponseDetailDto.answer?.optionId &&
[
QuestionType.SCALE,
QuestionType.DROPDOWN,
QuestionType.SINGLE_SELECTION,
].includes(question?.questionType)
) {
surveyResponseDetails.push(
this.handleAnswer(
surveyResponseDetailDto,
surveyResponseId,
question?.questionType,
undefined,
surveyResponseDetailDto.answer.optionId,
),
);
} else if (
isArray(surveyResponseDetailDto.answer?.optionIds) &&
QuestionType.MULTI_SELECTION === question.questionType
) {
surveyResponseDetailDto.answer?.optionIds.forEach(optionId => {
surveyResponseDetails.push(
this.handleAnswer(
surveyResponseDetailDto,
surveyResponseId,
QuestionType.MULTI_SELECTION,
undefined,
optionId,
),
);
});
} else if (
surveyResponseDetailDto.answer?.text &&
QuestionType.TEXT === question.questionType
) {
surveyResponseDetails.push(
this.handleAnswer(
surveyResponseDetailDto,
surveyResponseId,
QuestionType.TEXT,
surveyResponseDetailDto.answer.text,
),
);
} else {
throw new HttpErrors.BadRequest(ErrorKeys.NotAuthorised);
}
}
handleAnswer(
surveyResponseDetailDto: SurveyResponseDetailDto,
surveyResponseId: string,
questionType: QuestionType,
textAnswer?: string,
optionId?: string,
) {
return new SurveyResponseDetail({
surveyResponseId,
questionId: surveyResponseDetailDto.questionId,
score: surveyResponseDetailDto.score,
responseType: questionType,
textAnswer: textAnswer ?? '', // the create all issue
optionId: optionId ?? '', // the create all issue
});
}
getQuestionMapId(questions: Question[]) {
// create map of question with question id
let questionsMap: {[key: string]: Question} = {};
if (questions?.length) {
questions.forEach(question => {
if (question.id) {
questionsMap[question.id] = question;
if (question.followUpQuestions?.length) {
const followUpQuestionsMap = this.getQuestionMapId(
question.followUpQuestions,
);
questionsMap = {...questionsMap, ...followUpQuestionsMap};
}
}
});
}
return questionsMap;
}
/*
combination = vendorName, domain and businessUnit.
if all or any of two combination present then message will be 'You have already submitted response for this vendorName, domain and businessUnit combination.'
else if only one combination present then message will be 'You have already submitted response for this vendorName.
else message will be 'You have already submitted response.'
*/
async validateAndGetResponder(
responderId: string,
surveyId: string,
surveyCycleId: string,
) {
const responder = await this.surveyResponderRepository.findOne({
where: {
id: responderId,
surveyId,
surveyCycleId,
},
fields: {fullName: true, email: true},
});
if (!responder) {
throw new HttpErrors.Unauthorized(AuthorizeErrorKeys.NotAllowedAccess);
}
return responder;
}
}