powr-sdk-api 2.5.1 → 2.5.3
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 +88 -27
- package/package.json +1 -1
package/dist/routes/chat.js
CHANGED
|
@@ -14,7 +14,7 @@ router.get('/conversations', async (req, res) => {
|
|
|
14
14
|
try {
|
|
15
15
|
const userId = req.user.powrId;
|
|
16
16
|
const userAccess = req.user.access;
|
|
17
|
-
const projectId = req.projectId;
|
|
17
|
+
const projectId = req.query.projectId || req.projectId;
|
|
18
18
|
console.log('Current user ID:', userId, 'Access:', userAccess, 'Project:', projectId);
|
|
19
19
|
const db = await getDb();
|
|
20
20
|
const conversations = await db.collection('conversations').find({
|
|
@@ -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
|
|
|
@@ -78,7 +84,7 @@ router.get('/conversations/:conversationId/messages', async (req, res) => {
|
|
|
78
84
|
conversationId
|
|
79
85
|
} = req.params;
|
|
80
86
|
const userId = req.user.powrId;
|
|
81
|
-
const projectId = req.projectId;
|
|
87
|
+
const projectId = req.query.projectId || req.projectId;
|
|
82
88
|
const db = await getDb();
|
|
83
89
|
|
|
84
90
|
// Verify user is part of conversation
|
|
@@ -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
|
|
|
@@ -129,12 +141,16 @@ router.post('/conversations/:conversationId/messages', async (req, res) => {
|
|
|
129
141
|
conversationId
|
|
130
142
|
} = req.params;
|
|
131
143
|
const {
|
|
132
|
-
content
|
|
144
|
+
content,
|
|
145
|
+
projectId: bodyProjectId
|
|
133
146
|
} = req.body;
|
|
134
147
|
const userId = req.user.powrId;
|
|
135
|
-
const projectId = req.projectId;
|
|
148
|
+
const projectId = bodyProjectId || req.projectId;
|
|
136
149
|
if (!content || content.trim() === '') {
|
|
137
|
-
return res.
|
|
150
|
+
return res.status(400).json({
|
|
151
|
+
success: false,
|
|
152
|
+
message: "Message content is required"
|
|
153
|
+
});
|
|
138
154
|
}
|
|
139
155
|
const db = await getDb();
|
|
140
156
|
|
|
@@ -145,7 +161,10 @@ router.post('/conversations/:conversationId/messages', async (req, res) => {
|
|
|
145
161
|
projectId: projectId
|
|
146
162
|
});
|
|
147
163
|
if (!conversation) {
|
|
148
|
-
return res.
|
|
164
|
+
return res.status(404).json({
|
|
165
|
+
success: false,
|
|
166
|
+
message: "Conversation not found"
|
|
167
|
+
});
|
|
149
168
|
}
|
|
150
169
|
|
|
151
170
|
// Get current user's name from database
|
|
@@ -182,10 +201,16 @@ router.post('/conversations/:conversationId/messages', async (req, res) => {
|
|
|
182
201
|
timestamp: message.createdAt,
|
|
183
202
|
isOwn: true
|
|
184
203
|
};
|
|
185
|
-
res.
|
|
204
|
+
return res.status(201).json({
|
|
205
|
+
success: true,
|
|
206
|
+
data: responseMessage
|
|
207
|
+
});
|
|
186
208
|
} catch (error) {
|
|
187
209
|
console.error('Error sending message:', error);
|
|
188
|
-
res.
|
|
210
|
+
return res.status(500).json({
|
|
211
|
+
success: false,
|
|
212
|
+
message: "Failed to send message."
|
|
213
|
+
});
|
|
189
214
|
}
|
|
190
215
|
});
|
|
191
216
|
|
|
@@ -193,13 +218,17 @@ router.post('/conversations/:conversationId/messages', async (req, res) => {
|
|
|
193
218
|
router.post('/conversations', async (req, res) => {
|
|
194
219
|
try {
|
|
195
220
|
const {
|
|
196
|
-
participantId
|
|
221
|
+
participantId,
|
|
222
|
+
projectId: bodyProjectId
|
|
197
223
|
} = req.body;
|
|
198
224
|
const userId = req.user.powrId;
|
|
199
225
|
const userAccess = req.user.access;
|
|
200
|
-
const projectId = req.projectId;
|
|
226
|
+
const projectId = bodyProjectId || req.projectId;
|
|
201
227
|
if (!participantId) {
|
|
202
|
-
return res.
|
|
228
|
+
return res.status(400).json({
|
|
229
|
+
success: false,
|
|
230
|
+
message: "Participant ID is required"
|
|
231
|
+
});
|
|
203
232
|
}
|
|
204
233
|
const db = await getDb();
|
|
205
234
|
|
|
@@ -212,7 +241,10 @@ router.post('/conversations', async (req, res) => {
|
|
|
212
241
|
}
|
|
213
242
|
});
|
|
214
243
|
if (!participant) {
|
|
215
|
-
return res.
|
|
244
|
+
return res.status(404).json({
|
|
245
|
+
success: false,
|
|
246
|
+
message: "Participant not found"
|
|
247
|
+
});
|
|
216
248
|
}
|
|
217
249
|
|
|
218
250
|
// Validate access permissions
|
|
@@ -228,7 +260,10 @@ router.post('/conversations', async (req, res) => {
|
|
|
228
260
|
hasPermission = participant.access === 100 || participant.access === null;
|
|
229
261
|
}
|
|
230
262
|
if (!hasPermission) {
|
|
231
|
-
return res.
|
|
263
|
+
return res.status(403).json({
|
|
264
|
+
success: false,
|
|
265
|
+
message: "You do not have permission to chat with this user"
|
|
266
|
+
});
|
|
232
267
|
}
|
|
233
268
|
|
|
234
269
|
// Check if conversation already exists
|
|
@@ -239,9 +274,12 @@ router.post('/conversations', async (req, res) => {
|
|
|
239
274
|
projectId: projectId
|
|
240
275
|
});
|
|
241
276
|
if (existingConversation) {
|
|
242
|
-
return res.
|
|
243
|
-
|
|
244
|
-
|
|
277
|
+
return res.json({
|
|
278
|
+
success: true,
|
|
279
|
+
data: {
|
|
280
|
+
id: existingConversation._id.toString(),
|
|
281
|
+
message: 'Conversation already exists'
|
|
282
|
+
}
|
|
245
283
|
});
|
|
246
284
|
}
|
|
247
285
|
const conversation = {
|
|
@@ -251,13 +289,19 @@ router.post('/conversations', async (req, res) => {
|
|
|
251
289
|
updatedAt: new Date()
|
|
252
290
|
};
|
|
253
291
|
const result = await db.collection('conversations').insertOne(conversation);
|
|
254
|
-
res.
|
|
255
|
-
|
|
256
|
-
|
|
292
|
+
return res.status(201).json({
|
|
293
|
+
success: true,
|
|
294
|
+
data: {
|
|
295
|
+
id: result.insertedId.toString(),
|
|
296
|
+
message: 'Conversation created successfully'
|
|
297
|
+
}
|
|
257
298
|
});
|
|
258
299
|
} catch (error) {
|
|
259
300
|
console.error('Error creating conversation:', error);
|
|
260
|
-
res.
|
|
301
|
+
return res.status(500).json({
|
|
302
|
+
success: false,
|
|
303
|
+
message: "Failed to create conversation."
|
|
304
|
+
});
|
|
261
305
|
}
|
|
262
306
|
});
|
|
263
307
|
|
|
@@ -266,7 +310,7 @@ router.get('/users', async (req, res) => {
|
|
|
266
310
|
try {
|
|
267
311
|
const userId = req.user.powrId;
|
|
268
312
|
const userAccess = req.user.access;
|
|
269
|
-
const projectId = req.projectId;
|
|
313
|
+
const projectId = req.query.projectId || req.projectId;
|
|
270
314
|
console.log('Current user access level:', userAccess, 'Project:', projectId);
|
|
271
315
|
const db = await getDb();
|
|
272
316
|
|
|
@@ -274,7 +318,18 @@ router.get('/users', async (req, res) => {
|
|
|
274
318
|
const projectUsers = await db.collection('profiles').find({
|
|
275
319
|
projectId: projectId
|
|
276
320
|
}).toArray();
|
|
321
|
+
console.log('Project users found:', projectUsers.length, 'for projectId:', projectId);
|
|
277
322
|
const userIds = projectUsers.map(profile => profile.userId);
|
|
323
|
+
console.log('User IDs extracted:', userIds);
|
|
324
|
+
|
|
325
|
+
// If no users found for this project, return empty array
|
|
326
|
+
if (userIds.length === 0) {
|
|
327
|
+
console.log('No users found for project:', projectId);
|
|
328
|
+
return res.json({
|
|
329
|
+
success: true,
|
|
330
|
+
data: []
|
|
331
|
+
});
|
|
332
|
+
}
|
|
278
333
|
let userQuery = {
|
|
279
334
|
_id: {
|
|
280
335
|
$in: userIds.map(id => new ObjectId(id))
|
|
@@ -323,10 +378,16 @@ router.get('/users', async (req, res) => {
|
|
|
323
378
|
access: user.access
|
|
324
379
|
}));
|
|
325
380
|
console.log('Available users for chat:', formattedUsers);
|
|
326
|
-
res.
|
|
381
|
+
return res.json({
|
|
382
|
+
success: true,
|
|
383
|
+
data: formattedUsers
|
|
384
|
+
});
|
|
327
385
|
} catch (error) {
|
|
328
386
|
console.error('Error fetching users:', error);
|
|
329
|
-
res.
|
|
387
|
+
return res.status(500).json({
|
|
388
|
+
success: false,
|
|
389
|
+
message: "Failed to fetch users."
|
|
390
|
+
});
|
|
330
391
|
}
|
|
331
392
|
});
|
|
332
393
|
module.exports = router;
|