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
|
|
@ -1,10 +1,18 @@
|
|||
import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common';
|
||||
import { isOk } from '@manacore/shared-errors';
|
||||
import {
|
||||
ConversationService,
|
||||
type Conversation,
|
||||
type Message,
|
||||
} from './conversation.service';
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { isOk } from '@manacore/shared-errors';
|
||||
import { ConversationService } from './conversation.service';
|
||||
import { type Conversation } from '../db/schema/conversations.schema';
|
||||
import { type Message } from '../db/schema/messages.schema';
|
||||
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
|
||||
import {
|
||||
CurrentUser,
|
||||
|
|
@ -19,8 +27,27 @@ export class ConversationController {
|
|||
@Get()
|
||||
async getConversations(
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
@Query('spaceId') spaceId?: string,
|
||||
): Promise<Conversation[]> {
|
||||
const result = await this.conversationService.getConversations(user.userId);
|
||||
const result = await this.conversationService.getConversations(
|
||||
user.userId,
|
||||
spaceId,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
@Get('archived')
|
||||
async getArchivedConversations(
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Conversation[]> {
|
||||
const result = await this.conversationService.getArchivedConversations(
|
||||
user.userId,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
|
|
@ -34,8 +61,10 @@ export class ConversationController {
|
|||
@Param('id') id: string,
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Conversation> {
|
||||
// TODO: Add ownership check - ensure conversation belongs to user
|
||||
const result = await this.conversationService.getConversation(id);
|
||||
const result = await this.conversationService.getConversation(
|
||||
id,
|
||||
user.userId,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
|
|
@ -49,8 +78,7 @@ export class ConversationController {
|
|||
@Param('id') id: string,
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Message[]> {
|
||||
// TODO: Add ownership check - ensure conversation belongs to user
|
||||
const result = await this.conversationService.getMessages(id);
|
||||
const result = await this.conversationService.getMessages(id, user.userId);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
|
|
@ -61,13 +89,27 @@ export class ConversationController {
|
|||
|
||||
@Post()
|
||||
async createConversation(
|
||||
@Body() body: { modelId: string; title?: string },
|
||||
@Body()
|
||||
body: {
|
||||
modelId: string;
|
||||
title?: string;
|
||||
templateId?: string;
|
||||
conversationMode?: 'free' | 'guided' | 'template';
|
||||
documentMode?: boolean;
|
||||
spaceId?: string;
|
||||
},
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Conversation> {
|
||||
const result = await this.conversationService.createConversation(
|
||||
user.userId,
|
||||
body.modelId,
|
||||
body.title,
|
||||
{
|
||||
title: body.title,
|
||||
templateId: body.templateId,
|
||||
conversationMode: body.conversationMode,
|
||||
documentMode: body.documentMode,
|
||||
spaceId: body.spaceId,
|
||||
},
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
|
|
@ -83,9 +125,9 @@ export class ConversationController {
|
|||
@Body() body: { sender: 'user' | 'assistant' | 'system'; messageText: string },
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Message> {
|
||||
// TODO: Add ownership check - ensure conversation belongs to user
|
||||
const result = await this.conversationService.addMessage(
|
||||
id,
|
||||
user.userId,
|
||||
body.sender,
|
||||
body.messageText,
|
||||
);
|
||||
|
|
@ -96,4 +138,74 @@ export class ConversationController {
|
|||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
@Patch(':id/title')
|
||||
async updateTitle(
|
||||
@Param('id') id: string,
|
||||
@Body() body: { title: string },
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Conversation> {
|
||||
const result = await this.conversationService.updateTitle(
|
||||
id,
|
||||
user.userId,
|
||||
body.title,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
@Patch(':id/archive')
|
||||
async archiveConversation(
|
||||
@Param('id') id: string,
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Conversation> {
|
||||
const result = await this.conversationService.archiveConversation(
|
||||
id,
|
||||
user.userId,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
@Patch(':id/unarchive')
|
||||
async unarchiveConversation(
|
||||
@Param('id') id: string,
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<Conversation> {
|
||||
const result = await this.conversationService.unarchiveConversation(
|
||||
id,
|
||||
user.userId,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async deleteConversation(
|
||||
@Param('id') id: string,
|
||||
@CurrentUser() user: CurrentUserData,
|
||||
): Promise<{ success: boolean }> {
|
||||
const result = await this.conversationService.deleteConversation(
|
||||
id,
|
||||
user.userId,
|
||||
);
|
||||
|
||||
if (!isOk(result)) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue