powr-sdk-api 2.5.1 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/routes/chat.js +68 -20
- package/package.json +1 -1
package/dist/routes/chat.js
CHANGED
|
@@ -64,10 +64,16 @@ router.get('/conversations', async (req, res) => {
|
|
|
64
64
|
isOnline: false // Simple implementation - could be enhanced later
|
|
65
65
|
};
|
|
66
66
|
}));
|
|
67
|
-
res.
|
|
67
|
+
return res.json({
|
|
68
|
+
success: true,
|
|
69
|
+
data: conversationsWithUsers
|
|
70
|
+
});
|
|
68
71
|
} catch (error) {
|
|
69
72
|
console.error('Error fetching conversations:', error);
|
|
70
|
-
res.
|
|
73
|
+
return res.status(500).json({
|
|
74
|
+
success: false,
|
|
75
|
+
message: "Failed to fetch conversations."
|
|
76
|
+
});
|
|
71
77
|
}
|
|
72
78
|
});
|
|
73
79
|
|
|
@@ -115,10 +121,16 @@ router.get('/conversations/:conversationId/messages', async (req, res) => {
|
|
|
115
121
|
timestamp: msg.createdAt,
|
|
116
122
|
isOwn: msg.senderId === userId
|
|
117
123
|
}));
|
|
118
|
-
res.
|
|
124
|
+
return res.json({
|
|
125
|
+
success: true,
|
|
126
|
+
data: messagesWithOwnership
|
|
127
|
+
});
|
|
119
128
|
} catch (error) {
|
|
120
129
|
console.error('Error fetching messages:', error);
|
|
121
|
-
res.
|
|
130
|
+
return res.status(500).json({
|
|
131
|
+
success: false,
|
|
132
|
+
message: "Failed to fetch messages."
|
|
133
|
+
});
|
|
122
134
|
}
|
|
123
135
|
});
|
|
124
136
|
|
|
@@ -134,7 +146,10 @@ router.post('/conversations/:conversationId/messages', async (req, res) => {
|
|
|
134
146
|
const userId = req.user.powrId;
|
|
135
147
|
const projectId = req.projectId;
|
|
136
148
|
if (!content || content.trim() === '') {
|
|
137
|
-
return res.
|
|
149
|
+
return res.status(400).json({
|
|
150
|
+
success: false,
|
|
151
|
+
message: "Message content is required"
|
|
152
|
+
});
|
|
138
153
|
}
|
|
139
154
|
const db = await getDb();
|
|
140
155
|
|
|
@@ -145,7 +160,10 @@ router.post('/conversations/:conversationId/messages', async (req, res) => {
|
|
|
145
160
|
projectId: projectId
|
|
146
161
|
});
|
|
147
162
|
if (!conversation) {
|
|
148
|
-
return res.
|
|
163
|
+
return res.status(404).json({
|
|
164
|
+
success: false,
|
|
165
|
+
message: "Conversation not found"
|
|
166
|
+
});
|
|
149
167
|
}
|
|
150
168
|
|
|
151
169
|
// Get current user's name from database
|
|
@@ -182,10 +200,16 @@ router.post('/conversations/:conversationId/messages', async (req, res) => {
|
|
|
182
200
|
timestamp: message.createdAt,
|
|
183
201
|
isOwn: true
|
|
184
202
|
};
|
|
185
|
-
res.
|
|
203
|
+
return res.status(201).json({
|
|
204
|
+
success: true,
|
|
205
|
+
data: responseMessage
|
|
206
|
+
});
|
|
186
207
|
} catch (error) {
|
|
187
208
|
console.error('Error sending message:', error);
|
|
188
|
-
res.
|
|
209
|
+
return res.status(500).json({
|
|
210
|
+
success: false,
|
|
211
|
+
message: "Failed to send message."
|
|
212
|
+
});
|
|
189
213
|
}
|
|
190
214
|
});
|
|
191
215
|
|
|
@@ -199,7 +223,10 @@ router.post('/conversations', async (req, res) => {
|
|
|
199
223
|
const userAccess = req.user.access;
|
|
200
224
|
const projectId = req.projectId;
|
|
201
225
|
if (!participantId) {
|
|
202
|
-
return res.
|
|
226
|
+
return res.status(400).json({
|
|
227
|
+
success: false,
|
|
228
|
+
message: "Participant ID is required"
|
|
229
|
+
});
|
|
203
230
|
}
|
|
204
231
|
const db = await getDb();
|
|
205
232
|
|
|
@@ -212,7 +239,10 @@ router.post('/conversations', async (req, res) => {
|
|
|
212
239
|
}
|
|
213
240
|
});
|
|
214
241
|
if (!participant) {
|
|
215
|
-
return res.
|
|
242
|
+
return res.status(404).json({
|
|
243
|
+
success: false,
|
|
244
|
+
message: "Participant not found"
|
|
245
|
+
});
|
|
216
246
|
}
|
|
217
247
|
|
|
218
248
|
// Validate access permissions
|
|
@@ -228,7 +258,10 @@ router.post('/conversations', async (req, res) => {
|
|
|
228
258
|
hasPermission = participant.access === 100 || participant.access === null;
|
|
229
259
|
}
|
|
230
260
|
if (!hasPermission) {
|
|
231
|
-
return res.
|
|
261
|
+
return res.status(403).json({
|
|
262
|
+
success: false,
|
|
263
|
+
message: "You do not have permission to chat with this user"
|
|
264
|
+
});
|
|
232
265
|
}
|
|
233
266
|
|
|
234
267
|
// Check if conversation already exists
|
|
@@ -239,9 +272,12 @@ router.post('/conversations', async (req, res) => {
|
|
|
239
272
|
projectId: projectId
|
|
240
273
|
});
|
|
241
274
|
if (existingConversation) {
|
|
242
|
-
return res.
|
|
243
|
-
|
|
244
|
-
|
|
275
|
+
return res.json({
|
|
276
|
+
success: true,
|
|
277
|
+
data: {
|
|
278
|
+
id: existingConversation._id.toString(),
|
|
279
|
+
message: 'Conversation already exists'
|
|
280
|
+
}
|
|
245
281
|
});
|
|
246
282
|
}
|
|
247
283
|
const conversation = {
|
|
@@ -251,13 +287,19 @@ router.post('/conversations', async (req, res) => {
|
|
|
251
287
|
updatedAt: new Date()
|
|
252
288
|
};
|
|
253
289
|
const result = await db.collection('conversations').insertOne(conversation);
|
|
254
|
-
res.
|
|
255
|
-
|
|
256
|
-
|
|
290
|
+
return res.status(201).json({
|
|
291
|
+
success: true,
|
|
292
|
+
data: {
|
|
293
|
+
id: result.insertedId.toString(),
|
|
294
|
+
message: 'Conversation created successfully'
|
|
295
|
+
}
|
|
257
296
|
});
|
|
258
297
|
} catch (error) {
|
|
259
298
|
console.error('Error creating conversation:', error);
|
|
260
|
-
res.
|
|
299
|
+
return res.status(500).json({
|
|
300
|
+
success: false,
|
|
301
|
+
message: "Failed to create conversation."
|
|
302
|
+
});
|
|
261
303
|
}
|
|
262
304
|
});
|
|
263
305
|
|
|
@@ -323,10 +365,16 @@ router.get('/users', async (req, res) => {
|
|
|
323
365
|
access: user.access
|
|
324
366
|
}));
|
|
325
367
|
console.log('Available users for chat:', formattedUsers);
|
|
326
|
-
res.
|
|
368
|
+
return res.json({
|
|
369
|
+
success: true,
|
|
370
|
+
data: formattedUsers
|
|
371
|
+
});
|
|
327
372
|
} catch (error) {
|
|
328
373
|
console.error('Error fetching users:', error);
|
|
329
|
-
res.
|
|
374
|
+
return res.status(500).json({
|
|
375
|
+
success: false,
|
|
376
|
+
message: "Failed to fetch users."
|
|
377
|
+
});
|
|
330
378
|
}
|
|
331
379
|
});
|
|
332
380
|
module.exports = router;
|