TransHub_backend / safe-update-week2-images.js
linguabot's picture
Upload folder using huggingface_hub
da819ac verified
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();