mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-14 19:01:08 +02:00
make auth working
This commit is contained in:
parent
7a1f1e9aef
commit
25824ed0ac
73 changed files with 9093 additions and 3877 deletions
129
chat/backend/src/document/document.controller.ts
Normal file
129
chat/backend/src/document/document.controller.ts
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { isOk } from '@manacore/shared-errors';
|
||||
import { DocumentService } from './document.service';
|
||||
import { type Document } from '../db/schema/documents.schema';
|
||||
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
|
||||
import {
|
||||
CurrentUser,
|
||||
CurrentUserData,
|
||||
} from '../common/decorators/current-user.decorator';
|
||||
|
||||
@Controller('documents')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class DocumentController {
|
||||
constructor(private readonly documentService: DocumentService) {}
|
||||
|
||||
@Get('conversation/:conversationId')
|
||||
async getLatestDocument(
|
||||
@Param('conversationId') conversationId: string,
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Document | null> {
|
||||
const result = await this.documentService.getLatestDocument(
|
||||
conversationId,
|
||||
user.userId,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
@Get('conversation/:conversationId/versions')
|
||||
async getAllDocumentVersions(
|
||||
@Param('conversationId') conversationId: string,
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Document[]> {
|
||||
const result = await this.documentService.getAllDocumentVersions(
|
||||
conversationId,
|
||||
user.userId,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
@Get('conversation/:conversationId/exists')
|
||||
async hasDocument(
|
||||
@Param('conversationId') conversationId: string,
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<{ exists: boolean }> {
|
||||
const result = await this.documentService.hasDocument(
|
||||
conversationId,
|
||||
user.userId,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return { exists: result.value };
|
||||
}
|
||||
|
||||
@Post('conversation/:conversationId')
|
||||
async createDocument(
|
||||
@Param('conversationId') conversationId: string,
|
||||
@Body() body: { content: string },
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Document> {
|
||||
const result = await this.documentService.createDocument(
|
||||
conversationId,
|
||||
user.userId,
|
||||
body.content,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
@Post('conversation/:conversationId/version')
|
||||
async createDocumentVersion(
|
||||
@Param('conversationId') conversationId: string,
|
||||
@Body() body: { content: string },
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Document> {
|
||||
const result = await this.documentService.createDocumentVersion(
|
||||
conversationId,
|
||||
user.userId,
|
||||
body.content,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async deleteDocumentVersion(
|
||||
@Param('id') id: string,
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<{ success: boolean }> {
|
||||
const result = await this.documentService.deleteDocumentVersion(
|
||||
id,
|
||||
user.userId,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue