@@ -3,20 +3,31 @@ const HANDLER = require('../utils/response-helper')
3
3
const HttpStatus = require ( 'http-status-codes' )
4
4
const permission = require ( '../utils/permission' )
5
5
const helper = require ( '../utils/paginate' )
6
+ const notificationHelper = require ( '../utils/notif-helper' )
7
+ const notification = {
8
+ heading : '' ,
9
+ content : '' ,
10
+ tag : ''
11
+ }
6
12
7
13
module . exports = {
8
14
createEvent : async ( req , res , next ) => {
9
15
const event = new Event ( req . body )
10
16
try {
11
17
event . createdBy = req . user . _id
12
18
await event . save ( )
19
+ req . io . emit ( 'new event created' , { data : event . eventName } )
20
+ notification . heading = 'New Event!'
21
+ notification . content = `${ event . eventName } is added!`
22
+ notification . tag = 'New!'
23
+ notificationHelper . addToNotificationForAll ( req , res , notification , next )
13
24
res . status ( HttpStatus . CREATED ) . json ( { event : event } )
14
25
} catch ( error ) {
15
26
res . status ( HttpStatus . BAD_REQUEST ) . json ( { error : error } )
16
27
}
17
28
} ,
18
29
19
- updateEvent : async ( req , res ) => {
30
+ updateEvent : async ( req , res , next ) => {
20
31
const { id } = req . params
21
32
const updates = Object . keys ( req . body )
22
33
try {
@@ -29,15 +40,21 @@ module.exports = {
29
40
event [ update ] = req . body [ update ]
30
41
} )
31
42
await event . save ( )
43
+ req . io . emit ( 'event update' , { data : `Event: ${ event . eventName } is updated!` } )
44
+ notification . heading = 'Event update!'
45
+ notification . content = `${ event . eventName } is updated!`
46
+ notification . tag = 'Update'
47
+ notificationHelper . addToNotificationForAll ( req , res , notification , next )
32
48
res . status ( HttpStatus . OK ) . json ( { event : event } )
33
49
} catch ( error ) {
34
50
HANDLER . handleError ( res , error )
35
51
}
36
52
} ,
37
53
38
- rsvp : async ( req , res ) => {
54
+ rsvp : async ( req , res , next ) => {
39
55
const { yes, no, maybe } = req . body
40
56
const { id } = req . params
57
+ notification . tag = 'RSVP'
41
58
try {
42
59
const data = await Event . findById ( id )
43
60
if ( ! data ) {
@@ -47,14 +64,23 @@ module.exports = {
47
64
if ( data . rsvpMaybe . includes ( req . user . id ) ||
48
65
data . rsvpNo . includes ( req . user . id ) ||
49
66
data . rsvpYes . includes ( req . user . id ) ) {
50
- return res . status ( HttpStatus . OK ) . json ( { msg : 'You have already done the rsvp' } )
67
+ req . io . emit ( 'already rsvp' , { data : 'You have already done the rsvp' } )
68
+ notification . heading = 'Already rsvp!'
69
+ notification . content = 'You have already done the rsvp'
70
+ notificationHelper . addToNotificationForUser ( req . user . _id , res , notification , next )
71
+ res . status ( HttpStatus . OK ) . json ( { msg : 'You have already done the rsvp' } )
72
+ return
51
73
}
52
74
const event = await Event . findByIdAndUpdate ( id )
53
75
if ( yes ) {
54
76
try {
55
77
event . rsvpYes . push ( req . user . id )
56
78
await event . save ( )
57
- return res . status ( HttpStatus . OK ) . json ( { rsvpData : data } )
79
+ req . io . emit ( 'rsvp done' , { data : 'RSVP successfully done!' } )
80
+ notification . heading = 'RSVP done!'
81
+ notification . content = 'RSVP successfully done!'
82
+ notificationHelper . addToNotificationForUser ( req . user . _id , res , notification , next )
83
+ res . status ( HttpStatus . OK ) . json ( { rsvpData : data } )
58
84
} catch ( error ) {
59
85
return res . status ( HttpStatus . BAD_REQUEST ) . json ( { error : error } )
60
86
}
@@ -63,7 +89,11 @@ module.exports = {
63
89
try {
64
90
event . rsvpNo . push ( req . user . id )
65
91
await event . save ( )
66
- return res . status ( HttpStatus . OK ) . json ( { rsvpData : data } )
92
+ req . io . emit ( 'rsvp done' , { data : 'RSVP successfully done!' } )
93
+ notification . heading = 'RSVP done!'
94
+ notification . content = 'RSVP successfully done!'
95
+ notificationHelper . addToNotificationForUser ( req . user . _id , res , notification , next )
96
+ res . status ( HttpStatus . OK ) . json ( { rsvpData : data } )
67
97
} catch ( error ) {
68
98
return res . status ( HttpStatus . BAD_REQUEST ) . json ( { error : error } )
69
99
}
@@ -72,7 +102,11 @@ module.exports = {
72
102
try {
73
103
event . rsvpMaybe . push ( req . user . id )
74
104
await event . save ( )
75
- return res . status ( HttpStatus . OK ) . json ( { rsvpData : data } )
105
+ req . io . emit ( 'rsvp done' , { data : 'RSVP successfully done!' } )
106
+ notification . heading = 'RSVP done!'
107
+ notification . content = 'RSVP successfully done!'
108
+ notificationHelper . addToNotificationForUser ( req . user . _id , res , notification , next )
109
+ res . status ( HttpStatus . OK ) . json ( { rsvpData : data } )
76
110
} catch ( error ) {
77
111
return res . status ( HttpStatus . BAD_REQUEST ) . json ( { error : error } )
78
112
}
@@ -98,12 +132,10 @@ module.exports = {
98
132
GetAllEvent : async ( req , res , next ) => {
99
133
try {
100
134
const EventData = await Event . find ( { } , { } , helper . paginate ( req ) )
135
+ . populate ( 'createdBy' , [ 'name.firstName' , 'name.lastName' , '_id' , 'isAdmin' ] )
101
136
. sort ( { eventDate : - 1 } )
102
137
. lean ( )
103
- if ( ! EventData ) {
104
- return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No such Event is available!' } )
105
- }
106
- return res . status ( HttpStatus . OK ) . json ( { Event : EventData } )
138
+ return res . status ( HttpStatus . OK ) . json ( { events : EventData } )
107
139
} catch ( error ) {
108
140
HANDLER . handleError ( res , error )
109
141
}
@@ -118,6 +150,11 @@ module.exports = {
118
150
}
119
151
if ( permission . check ( req , res , deleteEvent . createdBy ) ) {
120
152
await Event . findByIdAndRemove ( id )
153
+ req . io . emit ( 'event deleted' , { data : deleteEvent . eventName } )
154
+ notification . heading = 'Event deleted!'
155
+ notification . content = `Event ${ deleteEvent . eventName } is deleted!`
156
+ notification . tag = 'Deleted'
157
+ notificationHelper . addToNotificationForAll ( req , res , notification , next )
121
158
return res . status ( HttpStatus . OK ) . json ( { deleteEvent : deleteEvent , message : 'Deleted the event' } )
122
159
}
123
160
return res . status ( HttpStatus . BAD_REQUEST ) . json ( { msg : 'Not permitted!' } )
@@ -132,9 +169,6 @@ module.exports = {
132
169
. sort ( { eventDate : - 1 } )
133
170
. exec ( )
134
171
console . log ( 'Upcoming events ' , events )
135
- if ( events . length === 0 ) {
136
- return res . status ( HttpStatus . OK ) . json ( { msg : 'No Upcoming events exists!' } )
137
- }
138
172
return res . status ( HttpStatus . OK ) . json ( { events } )
139
173
} catch ( error ) {
140
174
HANDLER . handleError ( res , next )
@@ -147,9 +181,6 @@ module.exports = {
147
181
. sort ( { eventDate : - 1 } )
148
182
. populate ( 'createdBy' , '_id name.firstName name.lastName' )
149
183
. exec ( )
150
- if ( events . length === 0 ) {
151
- return res . status ( HttpStatus . OK ) . json ( { msg : 'No events posted by user!' } )
152
- }
153
184
return res . status ( HttpStatus . OK ) . json ( { events } )
154
185
} catch ( error ) {
155
186
HANDLER . handleError ( res , error )
0 commit comments