79 lines
2.1 KiB
JavaScript
79 lines
2.1 KiB
JavaScript
/**
|
|
* Calendar sync job script
|
|
* Run this periodically (e.g., every 15 minutes) to sync external calendars
|
|
*
|
|
* Usage:
|
|
* node scripts/sync-calendars.js
|
|
*
|
|
* Or via cron:
|
|
* */15 * * * * cd /path/to/NeahStable && node scripts/sync-calendars.js >> /var/log/calendar-sync.log 2>&1
|
|
*
|
|
* Or call the API endpoint:
|
|
* curl -X POST http://localhost:3000/api/calendars/sync/job -H "x-api-key: YOUR_API_KEY"
|
|
*/
|
|
|
|
// For now, this script calls the API endpoint
|
|
// In production, you can use this or call the API directly
|
|
|
|
const http = require('http');
|
|
const https = require('https');
|
|
|
|
const API_URL = process.env.CALENDAR_SYNC_API_URL || 'http://localhost:3000';
|
|
const API_KEY = process.env.CALENDAR_SYNC_API_KEY || '';
|
|
|
|
async function callSyncAPI() {
|
|
return new Promise((resolve, reject) => {
|
|
const url = new URL(`${API_URL}/api/calendars/sync/job`);
|
|
const client = url.protocol === 'https:' ? https : http;
|
|
|
|
const options = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'x-api-key': API_KEY,
|
|
},
|
|
};
|
|
|
|
const req = client.request(url, options, (res) => {
|
|
let data = '';
|
|
|
|
res.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
res.on('end', () => {
|
|
if (res.statusCode === 200) {
|
|
console.log(`[${new Date().toISOString()}] Sync completed successfully`);
|
|
resolve(JSON.parse(data));
|
|
} else {
|
|
console.error(`[${new Date().toISOString()}] Sync failed:`, data);
|
|
reject(new Error(`HTTP ${res.statusCode}: ${data}`));
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on('error', (error) => {
|
|
console.error(`[${new Date().toISOString()}] Request error:`, error);
|
|
reject(error);
|
|
});
|
|
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
async function runSync() {
|
|
try {
|
|
console.log(`[${new Date().toISOString()}] Starting calendar sync job`);
|
|
|
|
await callSyncAPI();
|
|
|
|
console.log(`[${new Date().toISOString()}] Calendar sync job completed successfully`);
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error(`[${new Date().toISOString()}] Error running calendar sync job:`, error.message);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
runSync();
|