1+ import Project from "../Models/project.model.js" ;
2+ import Notification from "../Models/notification.model.js" ;
3+ import Comment from "../Models/comment.model.js" ;
4+
5+ export const likeProject = async ( req , res ) => {
6+ let user_id = req . user ;
7+
8+ let { _id, islikedByUser } = req . body ;
9+
10+ let incrementVal = ! islikedByUser ? 1 : - 1 ;
11+
12+ Project . findOneAndUpdate ( { _id } , { $inc : { "activity.total_likes" : incrementVal } } )
13+ . then ( project => {
14+ if ( ! islikedByUser ) {
15+ let like = new Notification ( {
16+ type : "like" ,
17+ project : _id ,
18+ notification_for : project . author ,
19+ user : user_id
20+ } ) ;
21+
22+ like . save ( ) . then ( notification => {
23+ return res . status ( 200 ) . json ( { liked_by_user : true } ) ;
24+ } )
25+ } else {
26+ Notification . findOneAndDelete ( { type : "like" , project : _id , user : user_id } )
27+ . then ( ( ) => {
28+ return res . status ( 200 ) . json ( { liked_by_user : false } ) ;
29+ } )
30+ . catch ( err => {
31+ return res . status ( 500 ) . json ( { error : err . message } ) ;
32+ } )
33+ }
34+ } )
35+ }
36+
37+ export const likeStatus = async ( req , res ) => {
38+ let user_id = req . user ;
39+
40+ let { _id } = req . body ;
41+
42+ Notification . exists ( { type : "like" , project : _id , user : user_id } )
43+ . then ( isLiked => {
44+ return res . status ( 200 ) . json ( { isLiked } ) ;
45+ } )
46+ . catch ( err => {
47+ return res . status ( 500 ) . json ( { error : err . message } ) ;
48+ } )
49+ }
50+
51+ export const addComment = async ( req , res ) => {
52+ let user_id = req . user ;
53+
54+ let { _id, comment, project_author, replying_to } = req . body ;
55+
56+ if ( ! comment . length ) {
57+ return res . status ( 403 ) . json ( { error : "Write something to leave a comment" } ) ;
58+ }
59+
60+ let commentObj = {
61+ project_id : _id ,
62+ project_author,
63+ comment,
64+ commented_by : user_id ,
65+ }
66+
67+ if ( replying_to ) {
68+ commentObj . parent = replying_to ;
69+ commentObj . isReply = true ;
70+ }
71+
72+ new Comment ( commentObj ) . save ( ) . then ( async commentFile => {
73+ let { comment, commentedAt, children } = commentFile ;
74+
75+ Project . findOneAndUpdate ( { _id } , { $push : { "comments" : commentFile . _id } , $inc : { "activity.total_comments" : 1 , "activity.total_parent_comments" : replying_to ? 0 : 1 } } )
76+ . then ( project => {
77+ console . log ( 'New comment created' )
78+ } ) ;
79+
80+ let notificationObj = new Notification ( {
81+ type : replying_to ? "reply" : "comment" ,
82+ project : _id ,
83+ notification_for : project_author ,
84+ user : user_id ,
85+ comment : commentFile . _id ,
86+ } )
87+
88+ if ( replying_to ) {
89+ notificationObj . replied_on_comment = replying_to ;
90+ await Comment . findOneAndUpdate ( { _id : replying_to } , { $push : { children : commentFile . _id } } )
91+ . then ( replyingToCommentDoc => {
92+ notificationObj . notification_for = replyingToCommentDoc . commented_by ;
93+ } ) ;
94+ }
95+
96+ notificationObj . save ( ) . then ( notification => {
97+ console . log ( 'New notification created' )
98+ } ) ;
99+
100+ return res . status ( 200 ) . json ( { comment, commentedAt, _id : commentFile . _id , user_id, children } ) ;
101+ } )
102+ }
103+
104+ export const getComments = async ( req , res ) => {
105+ let { project_id, skip } = req . body ;
106+
107+ let maxLimit = 5 ;
108+
109+ Comment . find ( { project_id, isReply : false } )
110+ . populate ( "commented_by" , "personal_info.username personal_info.fullname personal_info.profile_img" )
111+ . skip ( skip )
112+ . limit ( maxLimit )
113+ . sort ( { "commentedAt" : - 1 } )
114+ . then ( comment => {
115+ return res . status ( 200 ) . json ( comment ) ;
116+ } )
117+ . catch ( err => {
118+ return res . status ( 500 ) . json ( { error : err . message } ) ;
119+ } )
120+ }
121+
122+ export const getReplies = async ( req , res ) => {
123+ let { _id, skip } = req . body ;
124+
125+ let maxLimit = 5 ;
126+
127+ Comment . findOne ( { _id } )
128+ . populate ( {
129+ path : "children" ,
130+ option : {
131+ limit : maxLimit ,
132+ skip : skip ,
133+ sort : { "commentedAt" : - 1 }
134+ } ,
135+ populate : {
136+ path : 'commented_by' ,
137+ select : 'personal_info.username personal_info.fullname personal_info.profile_img'
138+ } ,
139+ select : "-project_id -updatedAt"
140+ } )
141+ . select ( "children" )
142+ . then ( doc => {
143+ return res . status ( 200 ) . json ( { replies : doc . children } ) ;
144+ } )
145+ . catch ( err => {
146+ return res . status ( 500 ) . json ( { error : err . message } ) ;
147+ } )
148+ }
149+
150+ const deleteComments = ( _id ) => {
151+ Comment . findOneAndDelete ( { _id } )
152+ . then ( comment => {
153+ if ( comment . parent ) {
154+ Comment . findOneAndUpdate ( { _id : comment . parent } , { $pull : { children : _id } } )
155+ . then ( data => {
156+ console . log ( 'Comment deleted successfully' )
157+ } )
158+ . catch ( err => {
159+ console . log ( err ) ;
160+ } )
161+ }
162+
163+ Notification . findOneAndDelete ( { comment : _id } )
164+ . then ( notification => console . log ( 'Notification deleted successfully' ) )
165+ . catch ( err => console . log ( err ) ) ;
166+
167+ Notification . findOneAndDelete ( { reply : _id } )
168+ . then ( notification => console . log ( 'Notification deleted successfully' ) )
169+ . catch ( err => console . log ( err ) ) ;
170+
171+ Project . findOneAndUpdate ( { _id : comment . project_id } , { $pull : { comments : _id } , $inc : { "activity.total_comments" : - 1 } , "activity.total_parent_comments" : comment . parent ? 0 : - 1 } )
172+ . then ( project => {
173+ if ( comment . children . length ) {
174+ comment . children . map ( replies => {
175+ deleteComments ( replies ) ;
176+ } )
177+ }
178+ } )
179+ } )
180+ . catch ( err => {
181+ console . log ( err . message ) ;
182+ } )
183+ }
184+
185+ export const deleteComment = async ( req , res ) => {
186+ let user_id = req . user ;
187+
188+ let { _id } = req . body ;
189+
190+ Comment . findOne ( { _id } )
191+ . then ( comment => {
192+ if ( user_id == comment . commented_by || user_id == comment . project_author ) {
193+ deleteComments ( _id ) ;
194+ return res . status ( 200 ) . json ( { message : "Comment deleted successfully" } ) ;
195+ } else {
196+ return res . status ( 403 ) . json ( { error : "You are not authorized to delete this comment" } ) ;
197+ }
198+ } )
199+ }
0 commit comments