File

apps/recallassess/recallassess-api/src/api/admin/assessment/controllers/assessment-question.controller.ts

Prefix

api/admin/assessment/:id/question

Index

Methods

Methods

Async addQuestion
addQuestion(assessmentIdFromRoute: number, data: Record<string | unknown>)
Decorators :
@Post()
Parameters :
Name Type Optional
assessmentIdFromRoute number No
data Record<string | unknown> No
Async deleteQuestion
deleteQuestion(id: number)
Decorators :
@Delete(':id')
Parameters :
Name Type Optional
id number No
Returns : Promise<void>
Async getQuestionsByAssessment
getQuestionsByAssessment(assessmentId: number)
Decorators :
@Get()
Parameters :
Name Type Optional
assessmentId number No
Async reorderQuestions
reorderQuestions(questions: literal type[])
Decorators :
@Put('reorder')
Parameters :
Name Type Optional
questions literal type[] No
Returns : Promise<void>
Async saveQuestion
saveQuestion(id: number, data: any)
Decorators :
@Put(':id')
Parameters :
Name Type Optional
id number No
data any No
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Post,
  Put,
  UnprocessableEntityException,
} from "@nestjs/common";
import { plainToInstance } from "class-transformer";
import { validate } from "class-validator";
import { AssessmentQuestionDto } from "./../dto/quiz/assessment-question.dto";
import { AssessmentQuestionAddDto } from "./../dto/quiz/assessment-question-add.dto";
import { AssessmentQuestionListDto } from "./../dto/quiz/assessment-question-list.dto";
import { AssessmentQuestionSaveDto } from "./../dto/quiz/assessment-question-save.dto";
import { AssessmentQuestionService } from "./../services/assessment-question.service";

@Controller("api/admin/assessment/:id/question")
export class AssessmentQuestionController {
  constructor(private readonly assessmentQuestionService: AssessmentQuestionService) {}

  @Get()
  async getQuestionsByAssessment(
    @Param("id", ParseIntPipe) assessmentId: number,
  ): Promise<AssessmentQuestionListDto[]> {
    return this.assessmentQuestionService.getQuestionsByAssessment(assessmentId);
  }

  @Post()
  async addQuestion(
    @Param("id", ParseIntPipe) assessmentIdFromRoute: number,
    @Body() data: Record<string, unknown>,
  ): Promise<AssessmentQuestionDto> {
    // URL is source of truth; body may omit assessment_id (avoids undefined → Prisma DecimalError)
    const payload = { ...data, assessment_id: (data["assessment_id"] as number | undefined) ?? assessmentIdFromRoute };
    const addQuestionDto = plainToInstance(AssessmentQuestionAddDto, payload);
    const errors = await validate(addQuestionDto);

    if (errors.length > 0) {
      throw new UnprocessableEntityException(errors);
    }

    return this.assessmentQuestionService.addQuestion(addQuestionDto, assessmentIdFromRoute);
  }

  @Put(":id")
  async saveQuestion(@Param("id", ParseIntPipe) id: number, @Body() data: any): Promise<AssessmentQuestionDto> {
    // we need to convert the updateQuestionDto to AssessmentQuestionSaveDto by getting the data first as any
    const updateQuestionDto = plainToInstance(AssessmentQuestionSaveDto, data);
    const errors = await validate(updateQuestionDto);
    if (errors.length > 0) {
      throw new UnprocessableEntityException(errors);
    }

    return this.assessmentQuestionService.saveQuestion(id, updateQuestionDto);
  }

  @Delete(":id")
  async deleteQuestion(@Param("id", ParseIntPipe) id: number): Promise<void> {
    return this.assessmentQuestionService.deleteQuestion(id);
  }

  @Put("reorder")
  async reorderQuestions(@Body() questions: { id: number; sort_order: number }[]): Promise<void> {
    return this.assessmentQuestionService.reorderQuestions(questions);
  }
}

results matching ""

    No results matching ""