-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendarController.js
More file actions
53 lines (44 loc) · 1.65 KB
/
Copy pathcalendarController.js
File metadata and controls
53 lines (44 loc) · 1.65 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
const { db } = require('../config/firebase');
const syncCalendar = async (req, res) => {
try {
const userId = req.user.user_id;
const { access_token, calendar_id } = req.body;
// In production, this would use the Google Calendar API
// to sync schedule items to the user's Google Calendar.
// For now, we store the sync configuration.
const syncData = {
user_id: userId,
calendar_id: calendar_id || 'primary',
sync_enabled: true,
last_synced: new Date().toISOString(),
};
await db.collection('calendar_sync').doc(userId).set(syncData, { merge: true });
// Fetch current schedule items for sync
const today = new Date().toISOString().split('T')[0];
const schedulesSnapshot = await db.collection('schedules')
.where('user_id', '==', userId)
.where('schedule_date', '==', today)
.orderBy('created_at', 'desc')
.limit(1)
.get();
let syncedItems = 0;
if (!schedulesSnapshot.empty) {
const schedule = schedulesSnapshot.docs[0].data();
const itemsSnapshot = await db.collection('schedule_items')
.where('schedule_id', '==', schedule.schedule_id)
.get();
syncedItems = itemsSnapshot.size;
// TODO: In production, create Google Calendar events here
// using the googleapis package with the provided access_token
}
res.json({
message: 'Calendar sync configured successfully',
synced_items: syncedItems,
sync_data: syncData,
});
} catch (error) {
console.error('Calendar sync error:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
module.exports = { syncCalendar };