Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/utils/response-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const RESPONSE_MESSAGES = {
RESPONSE_NOT_FOUND: 'Response not found',
RESPONSE_ALREADY_SUBMITTED: 'Response already submitted',
RESPONSE_NOT_ACCEPTING: 'Survey is not accepting responses',
SURVEY_EXPIRED: 'Survey has expired and is no longer accepting responses',
RESPONSE_DUPLICATE_SUBMISSION: 'You have already submitted a response to this survey',
RESPONSE_CANNOT_UPDATE: 'Cannot update a submitted response',
RESPONSE_VALIDATION_FAILED: 'Validation failed',
Expand Down
10 changes: 10 additions & 0 deletions src/modules/response/services/response.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ export class ResponseService {
throw new BadRequestException(RESPONSE_MESSAGES.RESPONSE_NOT_ACCEPTING);
}

if (survey.endDate && new Date() > new Date(survey.endDate)) {
this.loggerService.error(
'BAD_REQUEST',
RESPONSE_MESSAGES.SURVEY_EXPIRED,
apiId,
userId,
);
throw new BadRequestException(RESPONSE_MESSAGES.SURVEY_EXPIRED);
}

// Validate contextId is provided when survey requires a context entity
const requiresContext = survey.contextType &&
survey.contextType !== SurveyContextType.NONE &&
Expand Down
9 changes: 9 additions & 0 deletions src/modules/survey/dto/create-survey.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Min,
MaxLength,
IsNotEmpty,
IsDateString,
} from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
Expand Down Expand Up @@ -156,6 +157,14 @@ export class CreateSurveyDto {
@IsEnum(SurveyContextType)
context_type?: SurveyContextType;

@ApiPropertyOptional({
description: 'Date after which the survey can no longer be filled (ISO 8601)',
example: '2026-12-31T23:59:59.000Z',
})
@IsOptional()
@IsDateString()
end_date?: string;

@ApiPropertyOptional({ type: [CreateSectionDto] })
@IsOptional()
@IsArray()
Expand Down
3 changes: 3 additions & 0 deletions src/modules/survey/entities/survey.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,7 @@ export class Survey {

@Column({ name: 'closedAt', type: 'timestamp with time zone', nullable: true })
closedAt: Date;

@Column({ name: 'endDate', type: 'timestamp with time zone', nullable: true })
endDate: Date;
}
2 changes: 2 additions & 0 deletions src/modules/survey/services/survey.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class SurveyService {
theme: dto.theme || {},
targetRoles: dto.target_roles || null,
contextType: dto.context_type || SurveyContextType.NONE,
endDate: dto.end_date ? new Date(dto.end_date) : undefined,
createdBy: userId,
updatedBy: userId,
status: SurveyStatus.DRAFT,
Expand Down Expand Up @@ -303,6 +304,7 @@ export class SurveyService {
theme: dto.theme ?? survey.theme,
targetRoles: dto.target_roles !== undefined ? (dto.target_roles || null) : survey.targetRoles,
contextType: dto.context_type ?? survey.contextType,
endDate: dto.end_date !== undefined ? (dto.end_date ? new Date(dto.end_date) : null) : survey.endDate,
version: survey.version + 1,
});

Expand Down