Files
vip-coordinator/update-departments.js

125 lines
3.9 KiB
JavaScript

// Script to add department field to existing VIPs and drivers
const API_BASE = 'http://localhost:3000/api';
async function updateVipsWithDepartments() {
console.log('🔄 Updating VIPs with department field...');
try {
// Get all VIPs
const vipsResponse = await fetch(`${API_BASE}/vips`);
const vips = await vipsResponse.json();
console.log(`📋 Found ${vips.length} VIPs to update`);
// Update each VIP with department field
for (const vip of vips) {
if (!vip.department) {
// Assign departments based on organization or name patterns
let department = 'Office of Development'; // Default
// Simple logic to assign departments
if (vip.organization && (
vip.organization.toLowerCase().includes('admin') ||
vip.organization.toLowerCase().includes('system') ||
vip.organization.toLowerCase().includes('tech')
)) {
department = 'Admin';
}
const updateData = {
...vip,
department: department
};
const updateResponse = await fetch(`${API_BASE}/vips/${vip.id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updateData)
});
if (updateResponse.ok) {
console.log(`✅ Updated ${vip.name}${department}`);
} else {
console.log(`❌ Failed to update ${vip.name}`);
}
} else {
console.log(`⏭️ ${vip.name} already has department: ${vip.department}`);
}
}
} catch (error) {
console.error('❌ Error updating VIPs:', error);
}
}
async function updateDriversWithDepartments() {
console.log('\n🔄 Updating drivers with department field...');
try {
// Get all drivers
const driversResponse = await fetch(`${API_BASE}/drivers`);
const drivers = await driversResponse.json();
console.log(`🚗 Found ${drivers.length} drivers to update`);
// Update each driver with department field
for (let i = 0; i < drivers.length; i++) {
const driver = drivers[i];
if (!driver.department) {
// Alternate between departments for variety
const department = i % 2 === 0 ? 'Office of Development' : 'Admin';
const updateData = {
...driver,
department: department
};
const updateResponse = await fetch(`${API_BASE}/drivers/${driver.id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updateData)
});
if (updateResponse.ok) {
console.log(`✅ Updated ${driver.name}${department}`);
} else {
console.log(`❌ Failed to update ${driver.name}`);
}
} else {
console.log(`⏭️ ${driver.name} already has department: ${driver.department}`);
}
}
} catch (error) {
console.error('❌ Error updating drivers:', error);
}
}
async function main() {
console.log('🚀 Starting department field updates...\n');
// Check if API is available
try {
const healthCheck = await fetch(`${API_BASE}/health`);
if (!healthCheck.ok) {
console.error('❌ API is not responding. Make sure the backend is running on port 3000');
return;
}
} catch (error) {
console.error('❌ Cannot connect to API. Make sure the backend is running on port 3000');
return;
}
await updateVipsWithDepartments();
await updateDriversWithDepartments();
console.log('\n✅ Department field updates completed!');
console.log('\n🎯 Department assignments:');
console.log('🏢 Office of Development - Main VIP coordination department');
console.log('⚙️ Admin - Administrative and technical support department');
console.log('\nYou can now filter and organize VIPs and drivers by their departments!');
}
// Run the script
main().catch(console.error);