1
+ import { Request , Response } from "express" ;
2
+ import { GroupService } from "../services/GroupService" ;
3
+
4
+ export class GroupController {
5
+ private groupService = new GroupService ( ) ;
6
+
7
+ async createGroup ( req : Request , res : Response ) {
8
+ try {
9
+ const userId = ( req as any ) . user ?. id ;
10
+ if ( ! userId ) {
11
+ return res . status ( 401 ) . json ( { error : "Unauthorized" } ) ;
12
+ }
13
+
14
+ const { name, description, participants } = req . body ;
15
+
16
+ const group = await this . groupService . createGroup ( {
17
+ name,
18
+ description,
19
+ owner : userId ,
20
+ admins : [ userId ] ,
21
+ participants : [ ]
22
+ } ) ;
23
+
24
+ // Add participants including the creator
25
+ const allParticipants = [ ...new Set ( [ userId , ...participants ] ) ] ;
26
+ for ( const participantId of allParticipants ) {
27
+ await this . groupService . addParticipantToGroup ( group . id , participantId ) ;
28
+ }
29
+
30
+ const fullGroup = await this . groupService . getGroupById ( group . id ) ;
31
+ res . status ( 201 ) . json ( fullGroup ) ;
32
+ } catch ( error ) {
33
+ console . error ( "Error creating group:" , error ) ;
34
+ res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
35
+ }
36
+ }
37
+
38
+ async getGroupById ( req : Request , res : Response ) {
39
+ try {
40
+ const { id } = req . params ;
41
+ const group = await this . groupService . getGroupById ( id ) ;
42
+
43
+ if ( ! group ) {
44
+ return res . status ( 404 ) . json ( { error : "Group not found" } ) ;
45
+ }
46
+
47
+ res . json ( group ) ;
48
+ } catch ( error ) {
49
+ console . error ( "Error getting group by id:" , error ) ;
50
+ res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
51
+ }
52
+ }
53
+
54
+ async updateGroup ( req : Request , res : Response ) {
55
+ try {
56
+ const { id } = req . params ;
57
+ const userId = ( req as any ) . user ?. id ;
58
+
59
+ if ( ! userId ) {
60
+ return res . status ( 401 ) . json ( { error : "Unauthorized" } ) ;
61
+ }
62
+
63
+ const group = await this . groupService . getGroupById ( id ) ;
64
+ if ( ! group ) {
65
+ return res . status ( 404 ) . json ( { error : "Group not found" } ) ;
66
+ }
67
+
68
+ // Check if user is owner or admin
69
+ const isOwner = group . owner === userId ;
70
+ const isAdmin = group . admins ?. includes ( userId ) ;
71
+
72
+ if ( ! isOwner && ! isAdmin ) {
73
+ return res . status ( 403 ) . json ( { error : "Access denied" } ) ;
74
+ }
75
+
76
+ const groupData = req . body ;
77
+ const updatedGroup = await this . groupService . updateGroup ( id , groupData ) ;
78
+ if ( ! updatedGroup ) {
79
+ return res . status ( 404 ) . json ( { error : "Group not found" } ) ;
80
+ }
81
+
82
+ res . json ( updatedGroup ) ;
83
+ } catch ( error ) {
84
+ console . error ( "Error updating group:" , error ) ;
85
+ res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
86
+ }
87
+ }
88
+
89
+ async updateCharter ( req : Request , res : Response ) {
90
+ try {
91
+ const { id } = req . params ;
92
+ const userId = ( req as any ) . user ?. id ;
93
+
94
+ if ( ! userId ) {
95
+ return res . status ( 401 ) . json ( { error : "Unauthorized" } ) ;
96
+ }
97
+
98
+ const group = await this . groupService . getGroupById ( id ) ;
99
+ if ( ! group ) {
100
+ return res . status ( 404 ) . json ( { error : "Group not found" } ) ;
101
+ }
102
+
103
+ // Check if user is a participant in the group
104
+ const isParticipant = group . participants ?. some ( p => p . id === userId ) ;
105
+ if ( ! isParticipant ) {
106
+ return res . status ( 403 ) . json ( { error : "Access denied - you must be a participant in this group" } ) ;
107
+ }
108
+
109
+ const { charter } = req . body ;
110
+ const updatedGroup = await this . groupService . updateGroup ( id , { charter } ) ;
111
+
112
+ if ( ! updatedGroup ) {
113
+ return res . status ( 404 ) . json ( { error : "Group not found" } ) ;
114
+ }
115
+
116
+ res . json ( updatedGroup ) ;
117
+ } catch ( error ) {
118
+ console . error ( "Error updating charter:" , error ) ;
119
+ res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
120
+ }
121
+ }
122
+
123
+ async deleteGroup ( req : Request , res : Response ) {
124
+ try {
125
+ const { id } = req . params ;
126
+ const success = await this . groupService . deleteGroup ( id ) ;
127
+
128
+ if ( ! success ) {
129
+ return res . status ( 404 ) . json ( { error : "Group not found" } ) ;
130
+ }
131
+
132
+ res . json ( { message : "Group deleted successfully" } ) ;
133
+ } catch ( error ) {
134
+ console . error ( "Error deleting group:" , error ) ;
135
+ res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
136
+ }
137
+ }
138
+
139
+ async getAllGroups ( req : Request , res : Response ) {
140
+ try {
141
+ const groups = await this . groupService . getAllGroups ( ) ;
142
+ res . json ( groups ) ;
143
+ } catch ( error ) {
144
+ console . error ( "Error getting all groups:" , error ) ;
145
+ res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
146
+ }
147
+ }
148
+
149
+ async getUserGroups ( req : Request , res : Response ) {
150
+ try {
151
+ const userId = ( req as any ) . user ?. id ;
152
+ if ( ! userId ) {
153
+ return res . status ( 401 ) . json ( { error : "Unauthorized" } ) ;
154
+ }
155
+
156
+ const groups = await this . groupService . getUserGroups ( userId ) ;
157
+ res . json ( groups ) ;
158
+ } catch ( error ) {
159
+ console . error ( "Error getting user groups:" , error ) ;
160
+ res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
161
+ }
162
+ }
163
+
164
+ async getGroup ( req : Request , res : Response ) {
165
+ try {
166
+ const { groupId } = req . params ;
167
+ const userId = ( req as any ) . user ?. id ;
168
+
169
+ if ( ! userId ) {
170
+ return res . status ( 401 ) . json ( { error : "Unauthorized" } ) ;
171
+ }
172
+
173
+ const group = await this . groupService . getGroupById ( groupId ) ;
174
+ if ( ! group ) {
175
+ return res . status ( 404 ) . json ( { error : "Group not found" } ) ;
176
+ }
177
+
178
+ // Check if user is a participant
179
+ const isParticipant = group . participants . some ( p => p . id === userId ) ;
180
+ if ( ! isParticipant ) {
181
+ return res . status ( 403 ) . json ( { error : "Access denied" } ) ;
182
+ }
183
+
184
+ res . json ( group ) ;
185
+ } catch ( error ) {
186
+ console . error ( "Error getting group:" , error ) ;
187
+ res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
188
+ }
189
+ }
190
+
191
+ async addParticipants ( req : Request , res : Response ) {
192
+ try {
193
+ const { groupId } = req . params ;
194
+ const { participants } = req . body ;
195
+ const userId = ( req as any ) . user ?. id ;
196
+
197
+ if ( ! userId ) {
198
+ return res . status ( 401 ) . json ( { error : "Unauthorized" } ) ;
199
+ }
200
+
201
+ const group = await this . groupService . getGroupById ( groupId ) ;
202
+ if ( ! group ) {
203
+ return res . status ( 404 ) . json ( { error : "Group not found" } ) ;
204
+ }
205
+
206
+ // Check if user is admin or owner
207
+ const isAdmin = group . admins ?. includes ( userId ) || group . owner === userId ;
208
+ if ( ! isAdmin ) {
209
+ return res . status ( 403 ) . json ( { error : "Access denied" } ) ;
210
+ }
211
+
212
+ for ( const participantId of participants ) {
213
+ await this . groupService . addParticipantToGroup ( groupId , participantId ) ;
214
+ }
215
+
216
+ const updatedGroup = await this . groupService . getGroupById ( groupId ) ;
217
+ res . json ( updatedGroup ) ;
218
+ } catch ( error ) {
219
+ console . error ( "Error adding participants:" , error ) ;
220
+ res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
221
+ }
222
+ }
223
+
224
+ async removeParticipant ( req : Request , res : Response ) {
225
+ try {
226
+ const { groupId, userId : participantId } = req . params ;
227
+ const userId = ( req as any ) . user ?. id ;
228
+
229
+ if ( ! userId ) {
230
+ return res . status ( 401 ) . json ( { error : "Unauthorized" } ) ;
231
+ }
232
+
233
+ const group = await this . groupService . getGroupById ( groupId ) ;
234
+ if ( ! group ) {
235
+ return res . status ( 404 ) . json ( { error : "Group not found" } ) ;
236
+ }
237
+
238
+ // Check if user is admin or owner
239
+ const isAdmin = group . admins ?. includes ( userId ) || group . owner === userId ;
240
+ if ( ! isAdmin ) {
241
+ return res . status ( 403 ) . json ( { error : "Access denied" } ) ;
242
+ }
243
+
244
+ await this . groupService . removeParticipantFromGroup ( groupId , participantId ) ;
245
+
246
+ const updatedGroup = await this . groupService . getGroupById ( groupId ) ;
247
+ res . json ( updatedGroup ) ;
248
+ } catch ( error ) {
249
+ console . error ( "Error removing participant:" , error ) ;
250
+ res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
251
+ }
252
+ }
253
+ }
0 commit comments