Spaces:
Sleeping
Sleeping
| const mongoose = require('mongoose'); | |
| const SourceText = require('./models/SourceText'); | |
| // MongoDB connection | |
| const MONGODB_URI = process.env.MONGODB_URI || 'mongodb+srv://nothingyu:wSg3lbO1PkHiRMq9@sandbox.ecysggv.mongodb.net/?retryWrites=true&w=majority&appName=sandbox'; | |
| // ENHANCED PROTECTION FUNCTIONS | |
| async function safeUpdateTask(taskId, updates, reason = 'No reason provided') { | |
| try { | |
| console.log('π Connecting to MongoDB...'); | |
| await mongoose.connect(MONGODB_URI); | |
| console.log('β Connected to MongoDB'); | |
| const task = await SourceText.findById(taskId); | |
| if (!task) { | |
| console.log(`β Task with ID ${taskId} not found`); | |
| return false; | |
| } | |
| if (task.isProtected) { | |
| console.log(`π« CANNOT UPDATE: Task "${task.title}" is PROTECTED`); | |
| console.log(` Reason: ${task.protectedReason}`); | |
| console.log(` To update this task, you must first unlock it with the special key`); | |
| return false; | |
| } | |
| // Safe to update | |
| const updatedTask = await SourceText.findByIdAndUpdate( | |
| taskId, | |
| { | |
| ...updates, | |
| lastModified: new Date(), | |
| modificationHistory: [ | |
| ...(task.modificationHistory || []), | |
| { | |
| action: 'UPDATED', | |
| timestamp: new Date(), | |
| reason: reason | |
| } | |
| ] | |
| }, | |
| { new: true } | |
| ); | |
| console.log(`β Updated task: ${updatedTask.title}`); | |
| return true; | |
| } catch (error) { | |
| console.error('β Error updating task:', error); | |
| return false; | |
| } finally { | |
| await mongoose.disconnect(); | |
| console.log('π Disconnected from MongoDB'); | |
| } | |
| } | |
| async function safeDeleteTask(taskId, reason = 'No reason provided') { | |
| try { | |
| console.log('π Connecting to MongoDB...'); | |
| await mongoose.connect(MONGODB_URI); | |
| console.log('β Connected to MongoDB'); | |
| const task = await SourceText.findById(taskId); | |
| if (!task) { | |
| console.log(`β Task with ID ${taskId} not found`); | |
| return false; | |
| } | |
| if (task.isProtected) { | |
| console.log(`π« CANNOT DELETE: Task "${task.title}" is PROTECTED`); | |
| console.log(` Reason: ${task.protectedReason}`); | |
| console.log(` To delete this task, you must first unlock it with the special key`); | |
| return false; | |
| } | |
| // Safe to delete | |
| const deletedTask = await SourceText.findByIdAndDelete(taskId); | |
| console.log(`β Deleted task: ${deletedTask.title}`); | |
| return true; | |
| } catch (error) { | |
| console.error('β Error deleting task:', error); | |
| return false; | |
| } finally { | |
| await mongoose.disconnect(); | |
| console.log('π Disconnected from MongoDB'); | |
| } | |
| } | |
| async function unlockProtectedTask(taskId, unlockKey) { | |
| try { | |
| console.log('π Connecting to MongoDB...'); | |
| await mongoose.connect(MONGODB_URI); | |
| console.log('β Connected to MongoDB'); | |
| // Simple unlock key - in production, use a more secure method | |
| const VALID_UNLOCK_KEY = 'UNLOCK_WEEK1_WEEK2_2024'; | |
| if (unlockKey !== VALID_UNLOCK_KEY) { | |
| console.log('β Invalid unlock key. Protected tasks cannot be unlocked.'); | |
| return false; | |
| } | |
| const task = await SourceText.findById(taskId); | |
| if (!task) { | |
| console.log(`β Task with ID ${taskId} not found`); | |
| return false; | |
| } | |
| if (!task.isProtected) { | |
| console.log(`β οΈ Task "${task.title}" is not protected`); | |
| return false; | |
| } | |
| const updatedTask = await SourceText.findByIdAndUpdate( | |
| taskId, | |
| { | |
| isProtected: false, | |
| protectedReason: null, | |
| lastModified: new Date(), | |
| modificationHistory: [ | |
| ...(task.modificationHistory || []), | |
| { | |
| action: 'UNLOCKED', | |
| timestamp: new Date(), | |
| reason: 'Task unlocked with valid key' | |
| } | |
| ] | |
| }, | |
| { new: true } | |
| ); | |
| console.log(`β Unlocked task: ${updatedTask.title}`); | |
| return true; | |
| } catch (error) { | |
| console.error('β Error unlocking task:', error); | |
| return false; | |
| } finally { | |
| await mongoose.disconnect(); | |
| console.log('π Disconnected from MongoDB'); | |
| } | |
| } | |
| async function showProtectedTasks() { | |
| try { | |
| console.log('π Connecting to MongoDB...'); | |
| await mongoose.connect(MONGODB_URI); | |
| console.log('β Connected to MongoDB'); | |
| const protectedTasks = await SourceText.find({ | |
| isProtected: true | |
| }).sort({ weekNumber: 1, title: 1 }); | |
| console.log(`\nπ PROTECTED TASKS (${protectedTasks.length} total):`); | |
| protectedTasks.forEach((task, index) => { | |
| console.log(`${index + 1}. Week ${task.weekNumber} - ${task.title}`); | |
| console.log(` ID: ${task._id}`); | |
| console.log(` Protected: ${task.isProtected ? 'YES' : 'NO'}`); | |
| console.log(` Reason: ${task.protectedReason}`); | |
| console.log(` Last Modified: ${task.lastModified}`); | |
| console.log('---'); | |
| }); | |
| } catch (error) { | |
| console.error('β Error showing protected tasks:', error); | |
| } finally { | |
| await mongoose.disconnect(); | |
| console.log('π Disconnected from MongoDB'); | |
| } | |
| } | |
| async function testEnhancedProtection() { | |
| try { | |
| console.log('π Connecting to MongoDB...'); | |
| await mongoose.connect(MONGODB_URI); | |
| console.log('β Connected to MongoDB'); | |
| // Find a protected task to test | |
| const protectedTask = await SourceText.findOne({ | |
| isProtected: true, | |
| weekNumber: 1 | |
| }); | |
| if (!protectedTask) { | |
| console.log('β No protected tasks found'); | |
| return; | |
| } | |
| console.log(`\nπ§ͺ TESTING ENHANCED PROTECTION SYSTEM:`); | |
| console.log(` Task: ${protectedTask.title}`); | |
| console.log(` ID: ${protectedTask._id}`); | |
| console.log(` Protected: ${protectedTask.isProtected ? 'YES' : 'NO'}`); | |
| console.log(` Reason: ${protectedTask.protectedReason}`); | |
| // Test 1: Try to update a protected task using safe function | |
| console.log('\nπ TEST 1: Attempting to update protected task using safe function...'); | |
| const updateResult = await safeUpdateTask( | |
| protectedTask._id, | |
| { content: 'This should not work!' }, | |
| 'Test update' | |
| ); | |
| if (!updateResult) { | |
| console.log('β PROTECTION WORKING: Update was prevented'); | |
| } else { | |
| console.log('β PROTECTION FAILED: Task was updated despite being protected!'); | |
| } | |
| // Test 2: Try to delete a protected task using safe function | |
| console.log('\nπ TEST 2: Attempting to delete protected task using safe function...'); | |
| const deleteResult = await safeDeleteTask( | |
| protectedTask._id, | |
| 'Test deletion' | |
| ); | |
| if (!deleteResult) { | |
| console.log('β PROTECTION WORKING: Deletion was prevented'); | |
| } else { | |
| console.log('β PROTECTION FAILED: Task was deleted despite being protected!'); | |
| } | |
| // Test 3: Show how to safely unlock a task | |
| console.log('\nπ TEST 3: Demonstrating unlock process...'); | |
| console.log(' To unlock a protected task, you need the special key:'); | |
| console.log(' Key: UNLOCK_WEEK1_WEEK2_2024'); | |
| console.log(' Usage: unlockProtectedTask(taskId, key)'); | |
| // Test 4: Show current protection status | |
| console.log('\nπ PROTECTION STATUS:'); | |
| const allProtectedTasks = await SourceText.find({ isProtected: true }); | |
| const week1Protected = allProtectedTasks.filter(t => t.weekNumber === 1).length; | |
| const week2Protected = allProtectedTasks.filter(t => t.weekNumber === 2).length; | |
| console.log(` Week 1 protected tasks: ${week1Protected}`); | |
| console.log(` Week 2 protected tasks: ${week2Protected}`); | |
| console.log(` Total protected tasks: ${allProtectedTasks.length}`); | |
| console.log('\nπ ENHANCED PROTECTION SYSTEM TEST COMPLETE'); | |
| console.log('\nπ PROTECTION SUMMARY:'); | |
| console.log(' β Week 1 and Week 2 tutorial tasks are LOCKED'); | |
| console.log(' β Protected tasks cannot be accidentally modified'); | |
| console.log(' β Special key required to unlock tasks'); | |
| console.log(' β Modification history is tracked'); | |
| console.log(' β Use safeUpdateTask() and safeDeleteTask() functions'); | |
| } catch (error) { | |
| console.error('β Error testing enhanced protection system:', error); | |
| } finally { | |
| await mongoose.disconnect(); | |
| console.log('π Disconnected from MongoDB'); | |
| } | |
| } | |
| // Export functions for safe operations | |
| module.exports = { | |
| safeUpdateTask, | |
| safeDeleteTask, | |
| unlockProtectedTask, | |
| showProtectedTasks, | |
| testEnhancedProtection | |
| }; | |
| // Run the enhanced test | |
| testEnhancedProtection(); |