Skip to content

Commit 66f8906

Browse files
committed
feat: implement use case and controller method to delete user threads with AI assistant
1 parent 77b2ac2 commit 66f8906

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

backend/src/common/data-injection.tokens.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,5 @@ export enum UseCaseType {
169169
ADD_MESSAGE_TO_THREAD_WITH_AI_ASSISTANT = 'ADD_MESSAGE_TO_THREAD_WITH_AI_ASSISTANT',
170170
GET_ALL_USER_THREADS_WITH_AI_ASSISTANT = 'GET_ALL_USER_THREADS_WITH_AI_ASSISTANT',
171171
GET_ALL_THREAD_MESSAGES = 'GET_ALL_THREAD_MESSAGES',
172+
DELETE_THREAD_WITH_AI_ASSISTANT = 'DELETE_THREAD_WITH_AI_ASSISTANT',
172173
}

backend/src/entities/ai/ai-use-cases.interface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { InTransactionEnum } from '../../enums/in-transaction.enum.js';
2+
import { SuccessResponse } from '../../microservices/saas-microservice/data-structures/common-responce.ds.js';
23
import { AddMessageToThreadWithAssistantDS } from './application/data-structures/add-message-to-thread-with-assistant.ds.js';
34
import { CreateThreadWithAssistantDS } from './application/data-structures/create-thread-with-assistant.ds.js';
45
import { FindAllThreadMessagesDS } from './application/data-structures/find-all-thread-messages.ds.js';
@@ -29,3 +30,7 @@ export interface IGetAllThreadMessages {
2930
inTransaction: InTransactionEnum,
3031
): Promise<Array<FoundUserThreadMessagesRO>>;
3132
}
33+
34+
export interface IDeleteThreadWithAIAssistant {
35+
execute(inputData: FindAllThreadMessagesDS, inTransaction: InTransactionEnum): Promise<SuccessResponse>;
36+
}

backend/src/entities/ai/ai.module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { CreateThreadWithAIAssistantUseCase } from './use-cases/create-thread-wi
1212
import { AddMessageToThreadWithAIAssistantUseCase } from './use-cases/add-message-to-thread-with-ai.use.case.js';
1313
import { FindAllUserThreadsWithAssistantUseCase } from './use-cases/find-all-user-threads-with-assistant.use.case.js';
1414
import { FindAllMessagesInAiThreadUseCase } from './use-cases/find-all-messages-in-ai-thread.use.case.js';
15+
import { DeleteThreadWithAIAssistantUseCase } from './use-cases/delete-thread-with-ai-assistant.use.case.js';
1516

1617
@Module({
1718
imports: [TypeOrmModule.forFeature([UserEntity, LogOutEntity])],
@@ -40,6 +41,10 @@ import { FindAllMessagesInAiThreadUseCase } from './use-cases/find-all-messages-
4041
provide: UseCaseType.GET_ALL_THREAD_MESSAGES,
4142
useClass: FindAllMessagesInAiThreadUseCase,
4243
},
44+
{
45+
provide: UseCaseType.DELETE_THREAD_WITH_AI_ASSISTANT,
46+
useClass: DeleteThreadWithAIAssistantUseCase,
47+
},
4348
],
4449
controllers: [UserAIRequestsController, UserAIThreadsController],
4550
})
@@ -53,6 +58,7 @@ export class AIModule implements NestModule {
5358
{ path: '/ai/thread/message/:connectionId/:threadId', method: RequestMethod.POST },
5459
{ path: '/ai/threads', method: RequestMethod.GET },
5560
{ path: '/ai/thread/messages/:threadId', method: RequestMethod.GET },
61+
{ path: '/ai/thread/:threadId', method: RequestMethod.DELETE },
5662
);
5763
}
5864
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Inject, Injectable, NotFoundException, Scope } from '@nestjs/common';
2+
import AbstractUseCase from '../../../common/abstract-use.case.js';
3+
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
4+
import { BaseType } from '../../../common/data-injection.tokens.js';
5+
import { IDeleteThreadWithAIAssistant } from '../ai-use-cases.interface.js';
6+
import { FindAllThreadMessagesDS } from '../application/data-structures/find-all-thread-messages.ds.js';
7+
import { SuccessResponse } from '../../../microservices/saas-microservice/data-structures/common-responce.ds.js';
8+
import { Messages } from '../../../exceptions/text/messages.js';
9+
import { getOpenAiClient } from '../utils/get-open-ai-client.js';
10+
11+
@Injectable({ scope: Scope.REQUEST })
12+
export class DeleteThreadWithAIAssistantUseCase
13+
extends AbstractUseCase<FindAllThreadMessagesDS, SuccessResponse>
14+
implements IDeleteThreadWithAIAssistant
15+
{
16+
constructor(
17+
@Inject(BaseType.GLOBAL_DB_CONTEXT)
18+
protected _dbContext: IGlobalDatabaseContext,
19+
) {
20+
super();
21+
}
22+
23+
public async implementation(inputData: FindAllThreadMessagesDS): Promise<SuccessResponse> {
24+
const { threadId, userId } = inputData;
25+
const foundThread = await this._dbContext.aiUserThreadsRepository.findThreadByIdAndUserId(threadId, userId);
26+
if (!foundThread) {
27+
throw new NotFoundException(Messages.AI_THREAD_NOT_FOUND);
28+
}
29+
30+
const { openai } = getOpenAiClient();
31+
32+
await openai.beta.threads.del(foundThread.thread_ai_id);
33+
await this._dbContext.aiUserThreadsRepository.delete(foundThread.id);
34+
35+
return {
36+
success: true,
37+
};
38+
}
39+
}

backend/src/entities/ai/user-ai-threads.controller.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
import { UseInterceptors, Controller, Injectable, Inject, UseGuards, Post, Body, Res, Get } from '@nestjs/common';
1+
import {
2+
UseInterceptors,
3+
Controller,
4+
Injectable,
5+
Inject,
6+
UseGuards,
7+
Post,
8+
Body,
9+
Res,
10+
Get,
11+
Delete,
12+
} from '@nestjs/common';
213
import { ApiBearerAuth, ApiBody, ApiOperation, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
314
import { SentryInterceptor } from '../../interceptors/sentry.interceptor.js';
415
import { UseCaseType } from '../../common/data-injection.tokens.js';
516
import {
617
IAddMessageToThreadWithAIAssistant,
718
ICreateThreadWithAIAssistant,
19+
IDeleteThreadWithAIAssistant,
820
IGetAllThreadMessages,
921
IGetAllUserThreadsWithAIAssistant,
1022
} from './ai-use-cases.interface.js';
@@ -21,6 +33,7 @@ import { AddMessageToThreadWithAssistantDS } from './application/data-structures
2133
import { FoundUserThreadsWithAiRO } from './application/dto/found-user-threads-with-ai.ro.js';
2234
import { FoundUserThreadMessagesRO } from './application/dto/found-user-thread-messages.ro.js';
2335
import { FindAllThreadMessagesDS } from './application/data-structures/find-all-thread-messages.ds.js';
36+
import { SuccessResponse } from '../../microservices/saas-microservice/data-structures/common-responce.ds.js';
2437

2538
@UseInterceptors(SentryInterceptor)
2639
@Controller()
@@ -37,6 +50,8 @@ export class UserAIThreadsController {
3750
private readonly getAllUserThreadsWithAIAssistantUseCase: IGetAllUserThreadsWithAIAssistant,
3851
@Inject(UseCaseType.GET_ALL_THREAD_MESSAGES)
3952
private readonly getAllThreadMessagesUseCase: IGetAllThreadMessages,
53+
@Inject(UseCaseType.DELETE_THREAD_WITH_AI_ASSISTANT)
54+
private readonly deleteThreadWithAIAssistantUseCase: IDeleteThreadWithAIAssistant,
4055
) {}
4156

4257
@ApiOperation({ summary: 'Create new thread with ai assistant' })
@@ -127,4 +142,23 @@ export class UserAIThreadsController {
127142
};
128143
return await this.getAllThreadMessagesUseCase.execute(inputData, InTransactionEnum.OFF);
129144
}
145+
146+
@ApiOperation({ summary: 'Delete users thread with ai assistant' })
147+
@ApiResponse({
148+
status: 201,
149+
description: 'Delete users thread.',
150+
type: SuccessResponse,
151+
isArray: true,
152+
})
153+
@Delete('/ai/thread/:threadId')
154+
public async deleteThreadWithAssistant(
155+
@UserId() userId: string,
156+
@SlugUuid('threadId') threadId: string,
157+
): Promise<SuccessResponse> {
158+
const inputData: FindAllThreadMessagesDS = {
159+
threadId,
160+
userId,
161+
};
162+
return await this.deleteThreadWithAIAssistantUseCase.execute(inputData, InTransactionEnum.ON);
163+
}
130164
}

0 commit comments

Comments
 (0)