-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-https-urls.js
More file actions
74 lines (60 loc) · 1.95 KB
/
fix-https-urls.js
File metadata and controls
74 lines (60 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Script to fix https:// URLs in database
// Run this to update existing det_graf URLs from https:// to https://
const mongoose = require('mongoose')
// MongoDB connection - adjust as needed
const MONGODB_URI = process.env.ME_CONFIG_MONGODB_URL || 'mongodb://localhost:27017/preveniusdbDev'
mongoose.connect(MONGODB_URI)
// Plan model
const PlanSchema = new mongoose.Schema({}, { strict: false, collection: 'planes' })
const Plan = mongoose.model('planes', PlanSchema)
async function fixHttpsUrls() {
console.log('🔧 Starting HTTPS URL fix process...')
try {
// Find all plans that have det_graf with https:// URLs
const plans = await Plan.find({
det_graf: {
$elemMatch: {
url: { $regex: /^https:\/\// }
}
}
})
console.log(`📊 Found ${plans.length} plans with https:// URLs to fix`)
let totalFixed = 0
for (const plan of plans) {
let hasChanges = false
// Fix each image URL
const fixedImages = plan.det_graf.map((img) => {
if (img.url && img.url.startsWith('https://')) {
hasChanges = true
totalFixed++
const fixedUrl = img.url.replace('https://', 'https://')
console.log(`🔄 Fixing URL: ${img.url.substring(0, 80)}... -> ${fixedUrl.substring(0, 80)}...`)
return {
...img,
url: fixedUrl
}
}
return img
})
// Update plan if changes were made
if (hasChanges) {
await Plan.updateOne(
{ _id: plan._id },
{
$set: {
det_graf: fixedImages
}
}
)
console.log(`✅ Updated plan: ${plan.nom_obra || plan._id}`)
}
}
console.log(`🎉 HTTPS URL fix complete! Fixed ${totalFixed} URLs in ${plans.length} plans.`)
} catch (error) {
console.error('❌ Error fixing HTTPS URLs:', error)
} finally {
await mongoose.disconnect()
}
}
// Run the fix
fixHttpsUrls()