diff --git a/backend/src/core/api/analysis/analysis.controller.js b/backend/src/core/api/analysis/analysis.controller.js new file mode 100644 index 0000000..0e9f139 --- /dev/null +++ b/backend/src/core/api/analysis/analysis.controller.js @@ -0,0 +1,17 @@ +import { AnalysisService } from 'core/modules/analysis/service/analysis.service'; +import { ValidHttpResponse } from 'packages/handler/response/validHttp.response'; + +class Controller { + constructor() { + this.service = AnalysisService; + } + + getAnalysisHistoryDetail = async req => { + const userId = req.user.payload.id; + const analysisId = req.params.id; + const data = await this.service.getAnalysisHistoryDetail(userId, analysisId); + return ValidHttpResponse.toOkResponse(data); + }; +} + +export const AnalysisController = new Controller(); diff --git a/backend/src/core/api/analysis/analysis.resolver.js b/backend/src/core/api/analysis/analysis.resolver.js new file mode 100644 index 0000000..a869ad5 --- /dev/null +++ b/backend/src/core/api/analysis/analysis.resolver.js @@ -0,0 +1,21 @@ +import { Module } from 'packages/handler/Module'; +import { RecordId } from '../../common/swagger/record-id'; +import { AnalysisController } from './analysis.controller'; + +export const AnalysisResolver = Module.builder() + .addPrefix({ + prefixPath: '/analysis', + tag: 'analysis', + module: 'AnalysisModule' + }) + .register([ + { + route: '/history/:id', + method: 'get', + params: [RecordId], + controller: AnalysisController.getAnalysisHistoryDetail, + preAuthorization: true + } + ]); + + diff --git a/backend/src/core/api/analysis/index.js b/backend/src/core/api/analysis/index.js new file mode 100644 index 0000000..0cde22c --- /dev/null +++ b/backend/src/core/api/analysis/index.js @@ -0,0 +1 @@ +export * from './analysis.resolver'; diff --git a/backend/src/core/api/index.js b/backend/src/core/api/index.js index 1b01a34..bce0f32 100644 --- a/backend/src/core/api/index.js +++ b/backend/src/core/api/index.js @@ -8,6 +8,7 @@ import { DocumentResolver } from 'core/api/document'; import { HandlerResolver } from '../../packages/handler/HandlerResolver'; import { AuthResolver } from './auth/auth.resolver'; import { LawResolver } from './law/law.resolver'; +import { AnalysisResolver } from './analysis/analysis.resolver'; export const ModuleResolver = HandlerResolver .builder() @@ -20,6 +21,8 @@ export const ModuleResolver = HandlerResolver UploadResolver, LawResolver, TemplateResolver, - DocumentResolver + DocumentResolver, + AnalysisResolver ]); + diff --git a/backend/src/core/modules/analysis/analysis.repository.js b/backend/src/core/modules/analysis/analysis.repository.js new file mode 100644 index 0000000..d899112 --- /dev/null +++ b/backend/src/core/modules/analysis/analysis.repository.js @@ -0,0 +1,26 @@ +import prisma from 'core/database'; +import { BaseRepository } from '../../common/base.repository'; + +class Repository extends BaseRepository { + constructor() { + super('analysis'); + } + + async findAnalysisWithMessages(userId, analysisId) { + return prisma.analysis.findFirst({ + where: { + id: analysisId, + user_id: userId, + deleted_at: null, + }, + include: { + ai_messages: { + where: { deleted_at: null }, + orderBy: { created_at: 'asc' }, + }, + }, + }); + } +} + +export const AnalysisRepository = new Repository(); diff --git a/backend/src/core/modules/analysis/index.js b/backend/src/core/modules/analysis/index.js new file mode 100644 index 0000000..a850a3c --- /dev/null +++ b/backend/src/core/modules/analysis/index.js @@ -0,0 +1,2 @@ +export * from './analysis.repository'; +export * from './service'; diff --git a/backend/src/core/modules/analysis/service/analysis.service.js b/backend/src/core/modules/analysis/service/analysis.service.js new file mode 100644 index 0000000..4e3ea26 --- /dev/null +++ b/backend/src/core/modules/analysis/service/analysis.service.js @@ -0,0 +1,34 @@ +import { NotFoundException } from 'packages/httpException'; +import { AnalysisRepository } from '../analysis.repository'; + +class Service { + constructor() { + this.repository = AnalysisRepository; + } + + async getAnalysisHistoryDetail(userId, analysisId) { + const analysis = await this.repository.findAnalysisWithMessages(userId, analysisId); + if (!analysis) { + throw new NotFoundException(`Analysis with ID "${analysisId}" not found`); + } + + return { + id: analysis.id, + userId: analysis.user_id, + inputData: analysis.input_data, + result: analysis.result, + contextSummary: analysis.context_summary, + createdAt: analysis.created_at, + updatedAt: analysis.updated_at, + aiMessages: (analysis.ai_messages || []).map(msg => ({ + id: msg.id, + role: msg.role, + content: msg.content, + createdAt: msg.created_at, + updatedAt: msg.updated_at, + })), + }; + } +} + +export const AnalysisService = new Service(); diff --git a/backend/src/core/modules/analysis/service/index.js b/backend/src/core/modules/analysis/service/index.js new file mode 100644 index 0000000..d191378 --- /dev/null +++ b/backend/src/core/modules/analysis/service/index.js @@ -0,0 +1 @@ +export * from './analysis.service';