Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -480,7 +480,9 @@ app.post('/api/admin/messages', async (req, res) => {
|
|
| 480 |
try {
|
| 481 |
const { userId, content } = req.body;
|
| 482 |
const db = await getDb();
|
| 483 |
-
|
|
|
|
|
|
|
| 484 |
res.json({ success: true });
|
| 485 |
} catch (error) {
|
| 486 |
console.error('Error sending message:', error);
|
|
@@ -492,7 +494,9 @@ app.delete('/api/admin/messages/:messageId', async (req, res) => {
|
|
| 492 |
const { messageId } = req.params;
|
| 493 |
try {
|
| 494 |
const db = await getDb();
|
| 495 |
-
|
|
|
|
|
|
|
| 496 |
res.json({ success: true });
|
| 497 |
} catch (error) {
|
| 498 |
console.error('Error deleting message:', error);
|
|
@@ -554,17 +558,25 @@ app.post('/api/admin/submissions/:submissionId/status', async (req, res) => {
|
|
| 554 |
|
| 555 |
try {
|
| 556 |
const db = await getDb();
|
|
|
|
|
|
|
| 557 |
// First check if submission exists
|
| 558 |
-
const checkResult = await db.collection('submissions').findOne({ _id:
|
| 559 |
|
| 560 |
if (!checkResult) {
|
| 561 |
return res.status(404).json({ success: false, error: 'Submission not found' });
|
| 562 |
}
|
| 563 |
|
| 564 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
const updateResult = await db.collection('submissions').updateOne(
|
| 566 |
-
{ _id:
|
| 567 |
-
{ $set:
|
| 568 |
);
|
| 569 |
|
| 570 |
if (updateResult.matchedCount === 0) {
|
|
@@ -594,7 +606,9 @@ app.get('/api/admin/submissions/:submissionId', async (req, res) => {
|
|
| 594 |
const { submissionId } = req.params;
|
| 595 |
try {
|
| 596 |
const db = await getDb();
|
| 597 |
-
|
|
|
|
|
|
|
| 598 |
|
| 599 |
if (!submission) {
|
| 600 |
return res.status(404).json({ success: false, error: 'Submission not found' });
|
|
|
|
| 480 |
try {
|
| 481 |
const { userId, content } = req.body;
|
| 482 |
const db = await getDb();
|
| 483 |
+
// Convert userId to ObjectId
|
| 484 |
+
const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
|
| 485 |
+
const result = await db.collection('messages').insertOne({ user_id: userIdObj, content, sender: 'admin', created_at: new Date() });
|
| 486 |
res.json({ success: true });
|
| 487 |
} catch (error) {
|
| 488 |
console.error('Error sending message:', error);
|
|
|
|
| 494 |
const { messageId } = req.params;
|
| 495 |
try {
|
| 496 |
const db = await getDb();
|
| 497 |
+
// Convert messageId to ObjectId
|
| 498 |
+
const messageIdObj = ObjectId.isValid(messageId) ? new ObjectId(messageId) : messageId;
|
| 499 |
+
await db.collection('messages').deleteOne({ _id: messageIdObj });
|
| 500 |
res.json({ success: true });
|
| 501 |
} catch (error) {
|
| 502 |
console.error('Error deleting message:', error);
|
|
|
|
| 558 |
|
| 559 |
try {
|
| 560 |
const db = await getDb();
|
| 561 |
+
// Convert submissionId to ObjectId
|
| 562 |
+
const submissionIdObj = ObjectId.isValid(submissionId) ? new ObjectId(submissionId) : submissionId;
|
| 563 |
// First check if submission exists
|
| 564 |
+
const checkResult = await db.collection('submissions').findOne({ _id: submissionIdObj });
|
| 565 |
|
| 566 |
if (!checkResult) {
|
| 567 |
return res.status(404).json({ success: false, error: 'Submission not found' });
|
| 568 |
}
|
| 569 |
|
| 570 |
+
// If status is 'closed', mask the gc_code
|
| 571 |
+
let updateFields = { status: status, updated_at: new Date() };
|
| 572 |
+
if (status === 'closed' && checkResult.gc_code) {
|
| 573 |
+
updateFields.gc_code = 'x'.repeat(checkResult.gc_code.length);
|
| 574 |
+
}
|
| 575 |
+
|
| 576 |
+
// Update submission status (and gc_code if closed)
|
| 577 |
const updateResult = await db.collection('submissions').updateOne(
|
| 578 |
+
{ _id: submissionIdObj },
|
| 579 |
+
{ $set: updateFields }
|
| 580 |
);
|
| 581 |
|
| 582 |
if (updateResult.matchedCount === 0) {
|
|
|
|
| 606 |
const { submissionId } = req.params;
|
| 607 |
try {
|
| 608 |
const db = await getDb();
|
| 609 |
+
// Convert submissionId to ObjectId
|
| 610 |
+
const submissionIdObj = ObjectId.isValid(submissionId) ? new ObjectId(submissionId) : submissionId;
|
| 611 |
+
const submission = await db.collection('submissions').findOne({ _id: submissionIdObj });
|
| 612 |
|
| 613 |
if (!submission) {
|
| 614 |
return res.status(404).json({ success: false, error: 'Submission not found' });
|