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'; | |
| async function safeUpdateWeek2Images() { | |
| try { | |
| console.log('π Connecting to MongoDB...'); | |
| await mongoose.connect(MONGODB_URI); | |
| console.log('β Connected to MongoDB'); | |
| // First, let's see what Week 2 tutorial tasks currently exist | |
| console.log('π Current Week 2 tutorial tasks:'); | |
| const existingTasks = await SourceText.find({ | |
| category: 'tutorial', | |
| weekNumber: 2 | |
| }).sort({ title: 1 }); | |
| console.log(`Found ${existingTasks.length} existing Week 2 tutorial tasks:`); | |
| existingTasks.forEach((task, index) => { | |
| console.log(`${index + 1}. ${task.title}`); | |
| console.log(` Content: ${task.content}`); | |
| console.log(` Image: ${task.imageUrl || 'No image'}`); | |
| console.log(` ID: ${task._id}`); | |
| console.log('---'); | |
| }); | |
| // SAFE APPROACH: Only add new tasks or update existing ones | |
| console.log('\nπ SAFE UPDATE: Only adding new tasks or updating existing ones...'); | |
| // Example of how to safely add new tasks (without deleting existing ones) | |
| const newTasksToAdd = [ | |
| { | |
| title: 'Tutorial ST 13 - Week 2', // Use a new title to avoid conflicts | |
| content: 'Your content for new image 1', | |
| category: 'tutorial', | |
| weekNumber: 2, | |
| sourceLanguage: 'English', | |
| sourceCulture: 'Western', | |
| difficulty: 'intermediate', | |
| translationBrief: 'Translate this content considering the visual context of the image.', | |
| imageUrl: 'YOUR_NEW_IMAGE_URL_1', | |
| imageAlt: 'Description for new image 1' | |
| }, | |
| { | |
| title: 'Tutorial ST 14 - Week 2', // Use a new title to avoid conflicts | |
| content: 'Your content for new image 2', | |
| category: 'tutorial', | |
| weekNumber: 2, | |
| sourceLanguage: 'English', | |
| sourceCulture: 'Western', | |
| difficulty: 'intermediate', | |
| translationBrief: 'Translate this content considering the visual context of the image.', | |
| imageUrl: 'YOUR_NEW_IMAGE_URL_2', | |
| imageAlt: 'Description for new image 2' | |
| } | |
| ]; | |
| // SAFE: Only add new tasks (don't delete existing ones) | |
| let addedCount = 0; | |
| for (const newTask of newTasksToAdd) { | |
| // Check if a task with this title already exists | |
| const existingTask = await SourceText.findOne({ | |
| title: newTask.title, | |
| category: 'tutorial', | |
| weekNumber: 2 | |
| }); | |
| if (existingTask) { | |
| console.log(`β οΈ Task "${newTask.title}" already exists, skipping...`); | |
| } else { | |
| await SourceText.create(newTask); | |
| console.log(`β Added new task: ${newTask.title}`); | |
| addedCount++; | |
| } | |
| } | |
| console.log(`\nπ SAFE UPDATE COMPLETE: Added ${addedCount} new tasks`); | |
| console.log('π All Week 2 tutorial tasks (including existing ones):'); | |
| const allTasks = await SourceText.find({ | |
| category: 'tutorial', | |
| weekNumber: 2 | |
| }).sort({ title: 1 }); | |
| allTasks.forEach((task, index) => { | |
| console.log(`${index + 1}. ${task.title}`); | |
| console.log(` Image: ${task.imageUrl || 'No image'}`); | |
| }); | |
| console.log(`\nπ Total Week 2 tutorial tasks: ${allTasks.length}`); | |
| } catch (error) { | |
| console.error('β Error in safe update:', error); | |
| process.exit(1); | |
| } finally { | |
| await mongoose.disconnect(); | |
| console.log('π Disconnected from MongoDB'); | |
| } | |
| } | |
| // SAFE UPDATE FUNCTIONS | |
| async function updateExistingTaskImage(taskId, newImageUrl, newImageAlt) { | |
| try { | |
| console.log('π Connecting to MongoDB...'); | |
| await mongoose.connect(MONGODB_URI); | |
| console.log('β Connected to MongoDB'); | |
| const updatedTask = await SourceText.findByIdAndUpdate( | |
| taskId, | |
| { | |
| imageUrl: newImageUrl, | |
| imageAlt: newImageAlt | |
| }, | |
| { new: true } | |
| ); | |
| if (updatedTask) { | |
| console.log(`β Updated task: ${updatedTask.title}`); | |
| console.log(` New image: ${updatedTask.imageUrl}`); | |
| } else { | |
| console.log(`β Task with ID ${taskId} not found`); | |
| } | |
| } catch (error) { | |
| console.error('β Error updating task image:', error); | |
| } finally { | |
| await mongoose.disconnect(); | |
| console.log('π Disconnected from MongoDB'); | |
| } | |
| } | |
| async function addNewTask(taskData) { | |
| try { | |
| console.log('π Connecting to MongoDB...'); | |
| await mongoose.connect(MONGODB_URI); | |
| console.log('β Connected to MongoDB'); | |
| const newTask = await SourceText.create(taskData); | |
| console.log(`β Added new task: ${newTask.title}`); | |
| } catch (error) { | |
| console.error('β Error adding new task:', error); | |
| } finally { | |
| await mongoose.disconnect(); | |
| console.log('π Disconnected from MongoDB'); | |
| } | |
| } | |
| // Export functions for safe operations | |
| module.exports = { | |
| safeUpdateWeek2Images, | |
| updateExistingTaskImage, | |
| addNewTask | |
| }; | |
| // Run the safe update | |
| safeUpdateWeek2Images(); |