280 lines
7.1 KiB
TypeScript
280 lines
7.1 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Param,
|
|
Post,
|
|
MessageEvent,
|
|
Sse,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { ApiResponse, ApiProduces } from '@nestjs/swagger';
|
|
import { Public } from '../auth/decorators/public.decorator';
|
|
import { OptionalJwtGuard } from '../auth/guards/optional-jwt.guard';
|
|
import { User } from '../users/entities/user.entity';
|
|
import { ActiveUser } from '@/common/decorators/active-user.decorator';
|
|
import { SubmissionsService } from './providers/submissions.service';
|
|
import { StartSubmissionDTO } from './dtos/start-submission.dto';
|
|
import { AnswerSubmissionDTO } from './dtos/answer-submission.dto';
|
|
import { ApiAppResponse } from '@/common/decorators/api-app-response.decorator';
|
|
import { StartAuthenticatedDTO } from './dtos/responses/start-authenticated.dto';
|
|
import { StartGuestDTO } from './dtos/responses/start-guest.dto';
|
|
import { GetAnswerSubmissionDTO } from './dtos/responses/get-answer-submission.dto';
|
|
import { GetSubmissionHistoryDTO } from './dtos/responses/get-submission-history.dto';
|
|
import { GetSubmissionDetailDTO } from './dtos/responses/get-submission-detail.dto';
|
|
import type { Response } from 'express';
|
|
import { Observable } from 'rxjs';
|
|
import { I18nService } from 'nestjs-i18n';
|
|
|
|
@Controller('submissions')
|
|
export class SubmissionsController {
|
|
constructor(
|
|
/**
|
|
* Inject Submissions Service
|
|
*/
|
|
private readonly submissionsService: SubmissionsService,
|
|
|
|
/**
|
|
* Inject I18n Service
|
|
*/
|
|
private readonly i18nService: I18nService,
|
|
) {}
|
|
|
|
@Public()
|
|
@Post('start')
|
|
@HttpCode(HttpStatus.OK)
|
|
@UseGuards(OptionalJwtGuard)
|
|
@ApiAppResponse([
|
|
{
|
|
status: HttpStatus.OK,
|
|
variants: [
|
|
{
|
|
model: StartAuthenticatedDTO,
|
|
example: {
|
|
data: {
|
|
submissionId: 1,
|
|
question: {
|
|
id: 1,
|
|
type: 'numeric',
|
|
content: 'string',
|
|
required: true,
|
|
choices: [],
|
|
metadata: {
|
|
min: 0,
|
|
max: 10,
|
|
step: 1,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
model: StartGuestDTO,
|
|
example: {
|
|
data: {
|
|
guestSubmissionId: 'sahfdih123ieg1231fds23ljhakshf',
|
|
question: {
|
|
id: 2,
|
|
type: 'single-choice',
|
|
content: 'string',
|
|
required: true,
|
|
choices: [
|
|
{
|
|
id: 1,
|
|
order: 1,
|
|
text: 'string',
|
|
},
|
|
],
|
|
metadata: null,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
status: HttpStatus.NOT_FOUND,
|
|
variants: [{ messageExample: 'تست مورد نظر یافت نشد.' }],
|
|
},
|
|
{
|
|
status: HttpStatus.FORBIDDEN,
|
|
variants: [{ messageExample: 'تست مورد نظر فعال نیست.' }],
|
|
},
|
|
])
|
|
public async startTest(
|
|
@Body() startSubmissionDto: StartSubmissionDTO,
|
|
@ActiveUser() user?: User,
|
|
) {
|
|
return await this.submissionsService.startSubmission(
|
|
startSubmissionDto.testId,
|
|
user,
|
|
);
|
|
}
|
|
|
|
@Public()
|
|
@Post(':id/answer')
|
|
@HttpCode(HttpStatus.OK)
|
|
@UseGuards(OptionalJwtGuard)
|
|
@ApiAppResponse([
|
|
{
|
|
status: HttpStatus.OK,
|
|
variants: [
|
|
{
|
|
model: GetAnswerSubmissionDTO,
|
|
},
|
|
],
|
|
},
|
|
])
|
|
public async answerSubmission(
|
|
@Param('id') id: string,
|
|
@Body() answerSubmissionDto: AnswerSubmissionDTO,
|
|
@ActiveUser() user?: User,
|
|
) {
|
|
return await this.submissionsService.answerSubmission(
|
|
id,
|
|
answerSubmissionDto,
|
|
user,
|
|
);
|
|
}
|
|
|
|
@Public()
|
|
@Get(':id/history')
|
|
@UseGuards(OptionalJwtGuard)
|
|
@ApiAppResponse([
|
|
{
|
|
status: HttpStatus.OK,
|
|
variants: [
|
|
{
|
|
model: GetSubmissionHistoryDTO,
|
|
},
|
|
],
|
|
},
|
|
])
|
|
public async getHistory(@Param('id') id: string, @ActiveUser() user?: User) {
|
|
return await this.submissionsService.getSubmissionHistory(id, user);
|
|
}
|
|
|
|
@Get()
|
|
@ApiAppResponse([
|
|
{
|
|
status: HttpStatus.OK,
|
|
variants: [
|
|
{
|
|
model: GetSubmissionDetailDTO,
|
|
isArray: true,
|
|
},
|
|
],
|
|
},
|
|
])
|
|
public async getUserSubmissions(@ActiveUser() user: User) {
|
|
return await this.submissionsService.getUserSubmissions(user);
|
|
}
|
|
|
|
@Sse(':id/analyze')
|
|
@ApiProduces('text/event-stream')
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
description:
|
|
'تحلیل سلامت به صورت Server-Sent Events استریم میشود. هر event شامل یک قطعه از تحلیل است و در پایان event نهایی با done: true ارسال میشود.',
|
|
content: {
|
|
'text/event-stream': {
|
|
schema: {
|
|
type: 'string',
|
|
example:
|
|
'data: {"content":"## خلاصه تحلیل\\n\\n..."}\n\ndata: {"content":"..."}\n\ndata: {"done":true}\n\n',
|
|
},
|
|
},
|
|
},
|
|
})
|
|
@ApiAppResponse([
|
|
{
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
variants: [
|
|
{
|
|
messageExample: 'ابتدا وارد حساب کاربری خود شوید.',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
status: HttpStatus.BAD_REQUEST,
|
|
variants: [
|
|
{
|
|
messageExample: 'شناسه Submission معتبر نیست.',
|
|
},
|
|
{
|
|
messageExample: 'این Submission هنوز تکمیل نشده است.',
|
|
},
|
|
{
|
|
messageExample: 'هیچ پاسخی برای تحلیل یافت نشد.',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
status: HttpStatus.NOT_FOUND,
|
|
variants: [
|
|
{
|
|
messageExample: 'Submission مورد نظر یافت نشد.',
|
|
},
|
|
],
|
|
},
|
|
])
|
|
public async analyzeSubmission(
|
|
@Param('id') id: string,
|
|
@ActiveUser() user: User,
|
|
): Promise<Observable<MessageEvent>> {
|
|
/**
|
|
* Validation and preparation happen before streaming starts, so any
|
|
* error here reaches the client as a regular HTTP error response.
|
|
*/
|
|
const analysis = await this.submissionsService.analyzeSubmission(id, user);
|
|
|
|
return new Observable<MessageEvent>((subscriber) => {
|
|
let cancelled = false;
|
|
|
|
const consume = async (): Promise<void> => {
|
|
try {
|
|
for await (const content of analysis) {
|
|
if (cancelled || subscriber.closed) {
|
|
break;
|
|
}
|
|
|
|
subscriber.next({
|
|
data: { content },
|
|
});
|
|
}
|
|
|
|
if (!cancelled && !subscriber.closed) {
|
|
subscriber.next({
|
|
data: { done: true },
|
|
});
|
|
|
|
subscriber.complete();
|
|
}
|
|
} catch {
|
|
if (!cancelled && !subscriber.closed) {
|
|
const message = this.i18nService.translate(
|
|
'submissions.errors.analysisFailed',
|
|
);
|
|
|
|
subscriber.next({
|
|
data: {
|
|
error: message,
|
|
},
|
|
});
|
|
|
|
subscriber.complete();
|
|
}
|
|
}
|
|
};
|
|
|
|
void consume();
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
});
|
|
}
|
|
}
|