@@ -3,7 +3,6 @@ import { Comment } from 'src/comments/types/comments';
33import { PrismaService } from 'src/database/prisma.service' ;
44import { Comments } from '@prisma/client' ;
55import { LoggerService } from 'src/logger/logger.service' ;
6- import { i18n } from 'src/i18n' ;
76import { ProjectsService } from 'src/projects/services/projects.service' ;
87import { UserService } from 'src/user/services/user.service' ;
98
@@ -13,81 +12,65 @@ export class CommentsService {
1312 private prismaService : PrismaService ,
1413 private userService : UserService ,
1514 private projectService : ProjectsService ,
16- private logger : LoggerService ,
15+ private logging : LoggerService ,
1716 ) { }
17+
18+ async save ( data : Comment ) : Promise < Comments > {
19+ this . logging . log (
20+ `[CommentService] Starting to create comment for user ${ data . userId } on project ${ data . projectId } ` ,
21+ ) ;
1822
19- async createComment ( data : Comment ) : Promise < Comments > {
20- try {
21- await this . userService . checkUserExistence ( data . userId ) ;
22- await this . projectService . checkProjectExistence ( data . projectId ) ;
23- //add logic to verify question_id later
24- const createComment = await this . prismaService . comments . create ( {
25- data : {
26- ...data ,
27- } ,
28- } ) ;
29- return createComment ;
30- } catch ( error ) {
31- this . logger . error ( `some errror ocurred : ${ error . message } ` ) ;
32- throw error ;
33- }
23+ await Promise . all ( [
24+ this . userService . checkUserExistence ( data . userId ) ,
25+ this . projectService . checkProjectExistence ( data . projectId ) ,
26+ ] ) ;
27+
28+ return this . prismaService . comments . create ( { data } ) ;
3429 }
3530
36- async findCommentById ( comment_id : string ) : Promise < Comments | null > {
37- try {
38- const comment = await this . prismaService . comments . findUnique ( {
39- where : {
40- id : comment_id ,
41- } ,
42- } ) ;
43- if ( ! comment ) {
44- throw new NotFoundException ( ) ;
45- }
31+ async findById ( id : string ) : Promise < Comments > {
32+ this . logging . log ( `[CommentService] Finding comment with ID: ${ id } ` ) ;
4633
47- return comment ;
48- } catch ( error ) {
49- if ( error instanceof NotFoundException ) {
50- throw new NotFoundException ( i18n ( ) [ 'exception.notFound' ] , comment_id ) ;
51- }
34+ const comment = await this . prismaService . comments . findUnique ( {
35+ where : { id } ,
36+ } ) ;
5237
53- this . logger . error ( `some error ocurred : ${ error . message } ` ) ;
54- throw error ;
38+ if ( ! comment ) {
39+ this . logging . warn ( `[CommentService] Comment not found with ID: ${ id } ` ) ;
40+ throw new NotFoundException ( `Comment with ID ${ id } not found` ) ;
5541 }
42+
43+ return comment ;
5644 }
5745
58- async findByIdAndUpdateComment (
59- comment_id : string ,
60- data : Comment ,
61- ) : Promise < Comments > {
62- try {
63- await this . userService . checkUserExistence ( data . userId ) ;
64- await this . projectService . checkProjectExistence ( data . projectId ) ;
65- const updateComment = await this . prismaService . comments . update ( {
66- where : {
67- id : comment_id ,
68- } ,
69- data : {
70- ...data ,
71- } ,
72- } ) ;
73- return updateComment ;
74- } catch ( error ) {
75- this . logger . error ( `some error ocurred : ${ error . message } ` ) ;
76- throw error ;
77- }
46+ async update ( id : string , data : Comment ) : Promise < Comments > {
47+ this . logging . log (
48+ `[CommentService] Starting to update comment for user ${ data . userId } on project ${ data . projectId } ` ,
49+ ) ;
50+
51+ await Promise . all ( [
52+ this . userService . checkUserExistence ( data . userId ) ,
53+ this . projectService . checkProjectExistence ( data . projectId ) ,
54+ ] ) ;
55+
56+ const sanitizedData = {
57+ comment : data . comment ,
58+ projectId : data . projectId ,
59+ questionId : data . questionId ,
60+ userId : data . userId ,
61+ } ;
62+
63+ return this . prismaService . comments . update ( {
64+ where : { id } ,
65+ data : sanitizedData ,
66+ } ) ;
7867 }
7968
80- async findByIdAndDeleteComment ( comment_id : string ) : Promise < Comments | null > {
81- try {
82- await this . findCommentById ( comment_id ) ;
83- return await this . prismaService . comments . delete ( {
84- where : {
85- id : comment_id ,
86- } ,
87- } ) ;
88- } catch ( error ) {
89- this . logger . error ( `some error ocurred : ${ error . message } ` ) ;
90- throw error ;
91- }
69+ async delete ( id : string ) : Promise < Comments > {
70+ this . logging . log ( `[CommentService] Deleting comment ${ id } ` ) ;
71+
72+ return this . prismaService . comments . delete ( {
73+ where : { id } ,
74+ } ) ;
9275 }
9376}
0 commit comments