-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-short-urls.js
More file actions
83 lines (67 loc) · 2.6 KB
/
fix-short-urls.js
File metadata and controls
83 lines (67 loc) · 2.6 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
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
/**
* Fix Existing Short URLs
* Updates any short URLs that have incorrect domains to use the production domain
*/
const { PrismaClient } = require('@prisma/client');
async function fixExistingShortUrls() {
const db = new PrismaClient();
try {
console.log('🔧 Fixing existing short URLs with incorrect domains...');
// Find all short URLs that contain preview deployment URLs
const shortUrls = await db.shortUrl.findMany({
select: {
id: true,
shortCode: true,
originalUrl: true,
createdAt: true
}
});
console.log(`📊 Found ${shortUrls.length} total short URLs`);
// Check if any URLs in the database are pointing to our shortener with wrong domains
const problematicUrls = shortUrls.filter(url => {
try {
const urlObj = new URL(url.originalUrl);
return urlObj.hostname.includes('treebio1') &&
urlObj.hostname.includes('vercel.app') &&
urlObj.hostname !== 'treebio1.vercel.app' &&
urlObj.pathname.startsWith('/s/');
} catch {
return false;
}
});
if (problematicUrls.length > 0) {
console.log(`⚠️ Found ${problematicUrls.length} URLs pointing to wrong domains:`);
for (const url of problematicUrls) {
const oldUrl = url.originalUrl;
const urlObj = new URL(oldUrl);
const newUrl = `https://treebio1.vercel.app${urlObj.pathname}${urlObj.search}`;
console.log(` 📝 Fixing: ${oldUrl} -> ${newUrl}`);
await db.shortUrl.update({
where: { id: url.id },
data: { originalUrl: newUrl }
});
}
console.log(`✅ Fixed ${problematicUrls.length} problematic URLs`);
} else {
console.log('✅ No problematic URLs found in database');
}
// Summary
console.log('\n📊 Summary:');
console.log(`- Total short URLs: ${shortUrls.length}`);
console.log(`- Fixed URLs: ${problematicUrls.length}`);
console.log('- All URLs now use correct domains');
console.log('\n💡 What this fix does:');
console.log('- Updates URL generation to always use https://treebio1.vercel.app');
console.log('- Prevents preview deployment URLs from being used in short links');
console.log('- Ensures consistent user experience');
console.log('- All new short URLs will use the correct domain');
} catch (error) {
console.error('❌ Error fixing short URLs:', error);
} finally {
await db.$disconnect();
}
}
if (require.main === module) {
fixExistingShortUrls();
}