Skip to content

Commit

Permalink
fix: survey responses based on user
Browse files Browse the repository at this point in the history
  • Loading branch information
harshalranjhani committed Jul 10, 2024
1 parent 70f9f4d commit 546357d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 45 deletions.
85 changes: 59 additions & 26 deletions src/survey/survey.controller.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,119 @@
import { Controller, Get, Post, Body, Param, Delete, Query, UseFilters, UseGuards } from '@nestjs/common';
import {
Controller,
Get,
Post,
Body,
Param,
Delete,
Query,
UseFilters,
UseGuards,
} from '@nestjs/common';
import { SurveyService } from './survey.service';
import { CreateSurveyDto } from './dto/create-survey.dto';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import {
ApiBearerAuth,
ApiOperation,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';
import { UserInterface } from '../auth/interfaces/user.interface';
import { User } from '../auth/auth.decorator';
import { SurveyResponseDto } from './dto/survey-response.dto';
import { AllExceptionsFilter } from '../custom-exception/custom-exception.filter';
import { JwtGuard } from "../auth/guards/auth.guard";
import { JwtGuard } from '../auth/guards/auth.guard';

@UseFilters(AllExceptionsFilter)
@ApiTags("Survey")
@ApiTags('Survey')
@Controller('survey')
export class SurveyController {
constructor(private readonly surveyService: SurveyService) { }
constructor(private readonly surveyService: SurveyService) {}

@Post()
@ApiOperation({ summary: "Create a new survey" })
@ApiResponse({ status: 201, description: 'The survey has been successfully created.' })
@ApiOperation({ summary: 'Create a new survey' })
@ApiResponse({
status: 201,
description: 'The survey has been successfully created.',
})
create(@Body() createSurveyDto: CreateSurveyDto) {
const userId = 1;
return this.surveyService.create(createSurveyDto, userId);
}

@Get(":topic")
@ApiOperation({ summary: "Find all surveys based on topics" })
@Get(':topic')
@ApiOperation({ summary: 'Find all surveys based on topics' })
@ApiResponse({ status: 200, description: 'Surveys retrieved successfully.' })
findAllByTopic(@Param('topic') topic: string) {
return this.surveyService.findAll(topic);
}


@Get()
@ApiOperation({ summary: "Find all surveys" })
@ApiOperation({ summary: 'Find all surveys' })
@ApiResponse({ status: 200, description: 'Surveys retrieved successfully.' })
findAll() {
return this.surveyService.findAll("");
return this.surveyService.findAll('');
}

@ApiBearerAuth()
@UseGuards(JwtGuard)
@Get("/user/:id")
@ApiOperation({ summary: "Find all surveys for a user" })
@Get('/user/:id')
@ApiOperation({ summary: 'Find all surveys for a user' })
@ApiResponse({ status: 200, description: 'Surveys retrieved successfully.' })
async findSurveysForUser(@Param('id') id: number) {
return await this.surveyService.findSurveysForUser(+id);
}

@Get(':id')
@ApiOperation({ summary: "Find a specific survey" })
@ApiOperation({ summary: 'Find a specific survey' })
@ApiResponse({ status: 200, description: 'Survey retrieved successfully.' })
findOne(@Param('id') id: string) {
return this.surveyService.findOne(+id);
}


@Delete(':id')
@ApiOperation({ summary: "Remove a specific survey" })
@ApiOperation({ summary: 'Remove a specific survey' })
@ApiResponse({ status: 200, description: 'Survey removed successfully.' })
remove(@Param('id') id: string) {
return this.surveyService.remove(+id);
}

@Get('inferences')
@ApiOperation({ summary: 'Get inferences for a topic' })
@ApiResponse({ status: 200, description: 'Inferences retrieved successfully.' })
@ApiResponse({ status: 404, description: 'No surveys found for the given topic.' })
@ApiResponse({ status: 500, description: 'Failed to get inferences from Gemini API.' })
@ApiResponse({
status: 200,
description: 'Inferences retrieved successfully.',
})
@ApiResponse({
status: 404,
description: 'No surveys found for the given topic.',
})
@ApiResponse({
status: 500,
description: 'Failed to get inferences from Gemini API.',
})
async getInferences(@Query('topic') topic: string) {
return await this.surveyService.getInferences(topic);
}

@ApiBearerAuth()
@UseGuards(JwtGuard)
@Post('surveyresponse')
@ApiOperation({ summary: "Response to a survey" })
async postResponse(@User() user: UserInterface, @Body() surveyResponse: SurveyResponseDto) {
return await this.surveyService.surveyResponse(surveyResponse, user.id)
@Post('/surveyresponse/:surveyId')
@ApiOperation({ summary: 'Respond to a survey' })
async postResponse(
@User() user: UserInterface,
@Body() surveyResponse: SurveyResponseDto,
@Param('surveyId') surveyId: string,
) {
return await this.surveyService.surveyResponse(
surveyResponse,
user.id,
+surveyId,
);
}


@Get('/response/:id')
@ApiOperation({ summary: "Response to a survey based on id" })
@ApiOperation({ summary: 'Response to a survey based on id' })
async ReponseBasedOnId(@Param('id') surveyId: string) {
return await this.surveyService.surveyResponseBasedOnId(+surveyId);
}
Expand Down
44 changes: 25 additions & 19 deletions src/survey/survey.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,35 +194,41 @@ export class SurveyService {
return response.data;
}

async surveyResponse(surveyResponse: SurveyResponseDto, userId: number) {
async surveyResponse(
surveyResponse: SurveyResponseDto,
userId: number,
surveyId: number
) {
const { answers } = surveyResponse;
try {

// check if the user has already responded to the survey
const userResponse = await this.prismaService.answer.findMany({
// Check if the user has already responded to the survey
const existingResponses = await this.prismaService.answer.findMany({
where: {
userId
}
})
userId,
question: {
surveyId,
},
},
});

if (userResponse.length > 0) {
throw new HttpException('User has already responded to the survey', 400)
if (existingResponses.length > 0) {
throw new HttpException('User has already responded to this survey', 400);
}

await Promise.all(answers.map(
(answer) => {
// Create each answer and link it to the survey
await Promise.all(
answers.map((answer) => {
return this.prismaService.answer.create({
data: {
response: answer.response,
questionId: answer.questionId,
userId
}
})
}
))
}
catch (error) {
throw (error)
userId,
},
});
})
);
} catch (error) {
throw error;
}
}

Expand Down

0 comments on commit 546357d

Please sign in to comment.