|
1 | 1 | // Copyright The Linux Foundation and each contributor to LFX. |
2 | 2 | // SPDX-License-Identifier: MIT |
3 | 3 |
|
4 | | -import { CreateVoteRequest, UpdateVoteRequest } from '@lfx-one/shared/interfaces'; |
| 4 | +import { CreateVoteRequest, CreateVoteResponseRequest, UpdateVoteRequest } from '@lfx-one/shared/interfaces'; |
5 | 5 | import { NextFunction, Request, Response } from 'express'; |
6 | 6 |
|
7 | | -import { validateUidParameter } from '../helpers/validation.helper'; |
| 7 | +import { ServiceValidationError } from '../errors'; |
| 8 | +import { validateRequestBody, validateRequiredParameter, validateUidParameter } from '../helpers/validation.helper'; |
8 | 9 | import { logger } from '../services/logger.service'; |
9 | 10 | import { VoteService } from '../services/vote.service'; |
10 | 11 |
|
@@ -235,6 +236,90 @@ export class VoteController { |
235 | 236 | } |
236 | 237 | } |
237 | 238 |
|
| 239 | + /** |
| 240 | + * POST /votes/responses |
| 241 | + */ |
| 242 | + public async createVoteResponse(req: Request, res: Response, next: NextFunction): Promise<void> { |
| 243 | + const payload: CreateVoteResponseRequest = req.body; |
| 244 | + const startTime = logger.startOperation(req, 'create_vote_response', { |
| 245 | + vote_uid: payload?.vote_uid, |
| 246 | + vote_response_uid: payload?.vote_response_uid, |
| 247 | + abstain: payload?.abstain, |
| 248 | + }); |
| 249 | + |
| 250 | + try { |
| 251 | + const validationContext = { operation: 'create_vote_response', service: 'vote_controller' } as const; |
| 252 | + |
| 253 | + if (!validateRequestBody(payload, req, next, validationContext)) { |
| 254 | + return; |
| 255 | + } |
| 256 | + |
| 257 | + if (!validateRequiredParameter(payload.vote_uid, 'vote_uid', req, next, validationContext)) { |
| 258 | + return; |
| 259 | + } |
| 260 | + |
| 261 | + if (!validateRequiredParameter(payload.vote_response_uid, 'vote_response_uid', req, next, validationContext)) { |
| 262 | + return; |
| 263 | + } |
| 264 | + |
| 265 | + if (typeof payload.abstain !== 'boolean') { |
| 266 | + throw ServiceValidationError.forField('abstain', 'abstain is required and must be a boolean', validationContext); |
| 267 | + } |
| 268 | + |
| 269 | + // Build the upstream payload immutably: when abstaining we drop user_vote_content entirely; |
| 270 | + // when not abstaining we validate each answer and forward the original content. |
| 271 | + let upstreamPayload: CreateVoteResponseRequest; |
| 272 | + |
| 273 | + if (payload.abstain) { |
| 274 | + upstreamPayload = { |
| 275 | + vote_uid: payload.vote_uid, |
| 276 | + vote_response_uid: payload.vote_response_uid, |
| 277 | + abstain: true, |
| 278 | + }; |
| 279 | + } else { |
| 280 | + if (!Array.isArray(payload.user_vote_content) || payload.user_vote_content.length === 0) { |
| 281 | + throw ServiceValidationError.forField('user_vote_content', 'user_vote_content is required when not abstaining', validationContext); |
| 282 | + } |
| 283 | + |
| 284 | + for (const [index, answer] of payload.user_vote_content.entries()) { |
| 285 | + if (!answer || typeof answer !== 'object') { |
| 286 | + throw ServiceValidationError.forField(`user_vote_content[${index}]`, 'Each answer must be a non-null object', validationContext); |
| 287 | + } |
| 288 | + |
| 289 | + if (!answer.question_id || typeof answer.question_id !== 'string') { |
| 290 | + throw ServiceValidationError.forField(`user_vote_content[${index}].question_id`, 'question_id is required for each answer', validationContext); |
| 291 | + } |
| 292 | + |
| 293 | + const hasChoiceIds = Array.isArray(answer.choice_ids) && answer.choice_ids.length > 0; |
| 294 | + const hasRankedChoices = Array.isArray(answer.ranked_choices) && answer.ranked_choices.length > 0; |
| 295 | + |
| 296 | + if (!hasChoiceIds && !hasRankedChoices) { |
| 297 | + throw ServiceValidationError.forField( |
| 298 | + `user_vote_content[${index}]`, |
| 299 | + 'Each answer must include either choice_ids or ranked_choices', |
| 300 | + validationContext |
| 301 | + ); |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + upstreamPayload = payload; |
| 306 | + } |
| 307 | + |
| 308 | + await this.voteService.createVoteResponse(req, upstreamPayload); |
| 309 | + |
| 310 | + logger.success(req, 'create_vote_response', startTime, { |
| 311 | + vote_uid: upstreamPayload.vote_uid, |
| 312 | + vote_response_uid: upstreamPayload.vote_response_uid, |
| 313 | + abstain: upstreamPayload.abstain, |
| 314 | + status_code: 204, |
| 315 | + }); |
| 316 | + |
| 317 | + res.status(204).send(); |
| 318 | + } catch (error) { |
| 319 | + next(error); |
| 320 | + } |
| 321 | + } |
| 322 | + |
238 | 323 | /** |
239 | 324 | * PUT /votes/:uid/enable |
240 | 325 | */ |
|
0 commit comments