mirror of
https://github.com/urosran/cally.git
synced 2025-07-15 09:45:20 +00:00
Syncing rework
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
import React, {useMemo} from "react";
|
import React from "react";
|
||||||
import {Drawer} from "expo-router/drawer";
|
import {Drawer} from "expo-router/drawer";
|
||||||
import {useSignOut} from "@/hooks/firebase/useSignOut";
|
import {useSignOut} from "@/hooks/firebase/useSignOut";
|
||||||
import {DrawerContentScrollView, DrawerNavigationOptions, DrawerNavigationProp} from "@react-navigation/drawer";
|
import {DrawerContentScrollView, DrawerNavigationOptions, DrawerNavigationProp} from "@react-navigation/drawer";
|
||||||
@ -64,46 +64,42 @@ export default function TabLayout() {
|
|||||||
const setUserView = useSetAtom(userSettingsView);
|
const setUserView = useSetAtom(userSettingsView);
|
||||||
const setToDosIndex = useSetAtom(toDosPageIndex);
|
const setToDosIndex = useSetAtom(toDosPageIndex);
|
||||||
|
|
||||||
const screenOptions = useMemo(
|
const screenOptions = ({
|
||||||
() =>
|
navigation,
|
||||||
({
|
route,
|
||||||
navigation,
|
}: {
|
||||||
route,
|
navigation: DrawerNavigationProp<DrawerParamList>;
|
||||||
}: {
|
route: RouteProp<DrawerParamList>;
|
||||||
navigation: DrawerNavigationProp<DrawerParamList>;
|
}): DrawerNavigationOptions => ({
|
||||||
route: RouteProp<DrawerParamList>;
|
headerShown: true,
|
||||||
}): DrawerNavigationOptions => ({
|
headerTitleAlign: Device.deviceType === DeviceType.TABLET ? "left" : "center",
|
||||||
headerShown: true,
|
headerTitleStyle: {
|
||||||
headerTitleAlign: Device.deviceType === DeviceType.TABLET ? "left" : "center",
|
fontFamily: "Manrope_600SemiBold",
|
||||||
headerTitleStyle: {
|
fontSize: Device.deviceType === DeviceType.TABLET ? 22 : 17,
|
||||||
fontFamily: "Manrope_600SemiBold",
|
},
|
||||||
fontSize: Device.deviceType === DeviceType.TABLET ? 22 : 17,
|
headerLeft: () => (
|
||||||
},
|
<TouchableOpacity
|
||||||
headerLeft: () => (
|
onPress={navigation.toggleDrawer}
|
||||||
<TouchableOpacity
|
style={{marginLeft: 16}}
|
||||||
onPress={navigation.toggleDrawer}
|
>
|
||||||
style={{marginLeft: 16}}
|
<DrawerIcon/>
|
||||||
>
|
</TouchableOpacity>
|
||||||
<DrawerIcon/>
|
),
|
||||||
</TouchableOpacity>
|
headerRight: () => {
|
||||||
),
|
const showViewSwitch = ["calendar", "todos", "index"].includes(route.name);
|
||||||
headerRight: () => {
|
|
||||||
const showViewSwitch = ["calendar", "todos", "index"].includes(route.name);
|
|
||||||
|
|
||||||
if (Device.deviceType !== DeviceType.TABLET || !showViewSwitch) {
|
if (Device.deviceType !== DeviceType.TABLET || !showViewSwitch) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return <MemoizedViewSwitch navigation={navigation}/>;
|
return <MemoizedViewSwitch navigation={navigation}/>;
|
||||||
},
|
},
|
||||||
drawerStyle: {
|
drawerStyle: {
|
||||||
width: Device.deviceType === DeviceType.TABLET ? "30%" : "90%",
|
width: Device.deviceType === DeviceType.TABLET ? "30%" : "90%",
|
||||||
backgroundColor: "#f9f8f7",
|
backgroundColor: "#f9f8f7",
|
||||||
height: "100%",
|
height: "100%",
|
||||||
},
|
},
|
||||||
}),
|
})
|
||||||
[]
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Drawer
|
<Drawer
|
||||||
|
@ -58,7 +58,7 @@ export default function Screen() {
|
|||||||
]}
|
]}
|
||||||
tintColor={colorMap.pink}
|
tintColor={colorMap.pink}
|
||||||
progressBackgroundColor={"white"}
|
progressBackgroundColor={"white"}
|
||||||
refreshing={refreshing || isSyncing}
|
refreshing={isSyncing}
|
||||||
onRefresh={onRefresh}
|
onRefresh={onRefresh}
|
||||||
style={{
|
style={{
|
||||||
position: "absolute",
|
position: "absolute",
|
||||||
@ -72,7 +72,7 @@ export default function Screen() {
|
|||||||
}
|
}
|
||||||
bounces={true}
|
bounces={true}
|
||||||
showsVerticalScrollIndicator={false}
|
showsVerticalScrollIndicator={false}
|
||||||
pointerEvents={refreshing || isSyncing ? "auto" : "none"}
|
pointerEvents={isSyncing ? "auto" : "none"}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
|
@ -1,21 +1,34 @@
|
|||||||
import React, {memo} from "react";
|
import React, {memo} from "react";
|
||||||
import {Button, Picker, PickerModes, SegmentedControl, Text, View,} from "react-native-ui-lib";
|
import {Button, Picker, PickerModes, SegmentedControl, Text, View,} from "react-native-ui-lib";
|
||||||
import {MaterialIcons} from "@expo/vector-icons";
|
import {MaterialIcons} from "@expo/vector-icons";
|
||||||
import {modeMap, months} from "./constants";
|
import {months} from "./constants";
|
||||||
import {StyleSheet} from "react-native";
|
import {StyleSheet} from "react-native";
|
||||||
import {useAtom} from "jotai";
|
import {useAtom} from "jotai";
|
||||||
import {modeAtom, selectedDateAtom} from "@/components/pages/calendar/atoms";
|
import {modeAtom, selectedDateAtom} from "@/components/pages/calendar/atoms";
|
||||||
import {format, isSameDay} from "date-fns";
|
import {format, isSameDay} from "date-fns";
|
||||||
|
import * as Device from "expo-device";
|
||||||
|
import {Mode} from "react-native-big-calendar";
|
||||||
|
|
||||||
export const CalendarHeader = memo(() => {
|
export const CalendarHeader = memo(() => {
|
||||||
const [selectedDate, setSelectedDate] = useAtom(selectedDateAtom);
|
const [selectedDate, setSelectedDate] = useAtom(selectedDateAtom);
|
||||||
const [mode, setMode] = useAtom(modeAtom);
|
const [mode, setMode] = useAtom(modeAtom);
|
||||||
|
const isTablet = Device.deviceType === Device.DeviceType.TABLET;
|
||||||
|
|
||||||
|
const segments = isTablet
|
||||||
|
? [{label: "D"}, {label: "W"}, {label: "M"}] // Tablet segments
|
||||||
|
: [{label: "D"}, {label: "3D"}, {label: "M"}]; // Phone segments
|
||||||
|
|
||||||
const handleSegmentChange = (index: number) => {
|
const handleSegmentChange = (index: number) => {
|
||||||
const selectedMode = modeMap.get(index);
|
let selectedMode: Mode;
|
||||||
|
if (isTablet) {
|
||||||
|
selectedMode = ["day", "week", "month"][index] as Mode;
|
||||||
|
} else {
|
||||||
|
selectedMode = ["day", "3days", "month"][index] as Mode;
|
||||||
|
}
|
||||||
|
|
||||||
if (selectedMode) {
|
if (selectedMode) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
setMode(selectedMode as "day" | "week" | "month");
|
setMode(selectedMode as "day" | "week" | "month" | "3days");
|
||||||
}, 150);
|
}, 150);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -31,6 +44,34 @@ export const CalendarHeader = memo(() => {
|
|||||||
|
|
||||||
const isSelectedDateToday = isSameDay(selectedDate, new Date());
|
const isSelectedDateToday = isSameDay(selectedDate, new Date());
|
||||||
|
|
||||||
|
const getInitialIndex = () => {
|
||||||
|
if (isTablet) {
|
||||||
|
// Tablet index mapping
|
||||||
|
switch (mode) {
|
||||||
|
case "day":
|
||||||
|
return 0;
|
||||||
|
case "week":
|
||||||
|
return 1;
|
||||||
|
case "month":
|
||||||
|
return 2;
|
||||||
|
default:
|
||||||
|
return 1; // Default to week view for tablets
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Phone index mapping
|
||||||
|
switch (mode) {
|
||||||
|
case "day":
|
||||||
|
return 0;
|
||||||
|
case "3days":
|
||||||
|
return 1;
|
||||||
|
case "month":
|
||||||
|
return 2;
|
||||||
|
default:
|
||||||
|
return 1; // Default to 3day view for phones
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View
|
<View
|
||||||
style={{
|
style={{
|
||||||
@ -96,7 +137,7 @@ export const CalendarHeader = memo(() => {
|
|||||||
|
|
||||||
<View>
|
<View>
|
||||||
<SegmentedControl
|
<SegmentedControl
|
||||||
segments={[{label: "D"}, {label: "W"}, {label: "M"}]}
|
segments={segments}
|
||||||
backgroundColor="#ececec"
|
backgroundColor="#ececec"
|
||||||
inactiveColor="#919191"
|
inactiveColor="#919191"
|
||||||
activeBackgroundColor="#ea156c"
|
activeBackgroundColor="#ea156c"
|
||||||
@ -105,7 +146,7 @@ export const CalendarHeader = memo(() => {
|
|||||||
outlineWidth={3}
|
outlineWidth={3}
|
||||||
segmentLabelStyle={styles.segmentslblStyle}
|
segmentLabelStyle={styles.segmentslblStyle}
|
||||||
onChangeIndex={handleSegmentChange}
|
onChangeIndex={handleSegmentChange}
|
||||||
initialIndex={mode === "day" ? 0 : mode === "week" ? 1 : 2}
|
initialIndex={getInitialIndex()}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
|
@ -33,7 +33,7 @@ const getTotalMinutes = () => {
|
|||||||
|
|
||||||
export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
||||||
({calendarHeight}) => {
|
({calendarHeight}) => {
|
||||||
const {data: events, isLoading, refetch} = useGetEvents();
|
const {data: events, isLoading} = useGetEvents();
|
||||||
const {profileData, user} = useAuthContext();
|
const {profileData, user} = useAuthContext();
|
||||||
const [selectedDate, setSelectedDate] = useAtom(selectedDateAtom);
|
const [selectedDate, setSelectedDate] = useAtom(selectedDateAtom);
|
||||||
const [mode, setMode] = useAtom(modeAtom);
|
const [mode, setMode] = useAtom(modeAtom);
|
||||||
@ -52,9 +52,8 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
|||||||
|
|
||||||
const handlePressEvent = useCallback(
|
const handlePressEvent = useCallback(
|
||||||
(event: CalendarEvent) => {
|
(event: CalendarEvent) => {
|
||||||
if (mode === "day" || mode === "week") {
|
if (mode === "day" || mode === "week" || mode === "3days") {
|
||||||
setEditVisible(true);
|
setEditVisible(true);
|
||||||
// console.log({event});
|
|
||||||
setEventForEdit(event);
|
setEventForEdit(event);
|
||||||
} else {
|
} else {
|
||||||
setMode("day");
|
setMode("day");
|
||||||
@ -66,7 +65,7 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
|||||||
|
|
||||||
const handlePressCell = useCallback(
|
const handlePressCell = useCallback(
|
||||||
(date: Date) => {
|
(date: Date) => {
|
||||||
if (mode === "day" || mode === "week") {
|
if (mode === "day" || mode === "week" || mode === "3days") {
|
||||||
setSelectedNewEndDate(date);
|
setSelectedNewEndDate(date);
|
||||||
} else {
|
} else {
|
||||||
setMode("day");
|
setMode("day");
|
||||||
@ -83,9 +82,8 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
|||||||
setSelectedNewEndDate(date);
|
setSelectedNewEndDate(date);
|
||||||
setEditVisible(true);
|
setEditVisible(true);
|
||||||
}
|
}
|
||||||
if (mode === 'week') {
|
if (mode === 'week' || mode === '3days') {
|
||||||
setSelectedDate(date)
|
setSelectedDate(date)
|
||||||
|
|
||||||
setMode("day")
|
setMode("day")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -102,7 +100,7 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
|||||||
const memoizedEventCellStyle = useCallback(
|
const memoizedEventCellStyle = useCallback(
|
||||||
(event: CalendarEvent) => {
|
(event: CalendarEvent) => {
|
||||||
let eventColor = event.eventColor;
|
let eventColor = event.eventColor;
|
||||||
if (!isFamilyView && (event.attendees?.includes(user?.uid) || event.creatorId === user?.uid)) {
|
if (!isFamilyView && (event.attendees?.includes(user?.uid!) || event.creatorId! === user?.uid)) {
|
||||||
eventColor = profileData?.eventColor ?? colorMap.teal;
|
eventColor = profileData?.eventColor ?? colorMap.teal;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,7 +127,7 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
|||||||
}, [selectedDate, mode]);
|
}, [selectedDate, mode]);
|
||||||
|
|
||||||
const dateStyle = useMemo(() => {
|
const dateStyle = useMemo(() => {
|
||||||
if (mode === "week") return undefined;
|
if (mode === "week" || mode === "3days") return undefined;
|
||||||
return isSameDate(todaysDate, selectedDate) && mode === "day"
|
return isSameDate(todaysDate, selectedDate) && mode === "day"
|
||||||
? styles.dayHeader
|
? styles.dayHeader
|
||||||
: styles.otherDayHeader;
|
: styles.otherDayHeader;
|
||||||
@ -138,7 +136,7 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
|||||||
const memoizedHeaderContentStyle = useMemo(() => {
|
const memoizedHeaderContentStyle = useMemo(() => {
|
||||||
if (mode === "day") {
|
if (mode === "day") {
|
||||||
return styles.dayModeHeader;
|
return styles.dayModeHeader;
|
||||||
} else if (mode === "week") {
|
} else if (mode === "week" || mode === "3days") {
|
||||||
return styles.weekModeHeader;
|
return styles.weekModeHeader;
|
||||||
} else if (mode === "month") {
|
} else if (mode === "month") {
|
||||||
return styles.monthModeHeader;
|
return styles.monthModeHeader;
|
||||||
@ -146,12 +144,11 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}, [mode]);
|
}, [mode]);
|
||||||
|
|
||||||
const {enrichedEvents, filteredEvents} = useMemo(() => {
|
const {enrichedEvents, filteredEvents} = useMemo(() => {
|
||||||
const startTime = Date.now(); // Start timer
|
const startTime = Date.now();
|
||||||
|
|
||||||
const startOffset = mode === "month" ? 40 : mode === "week" ? 10 : 1;
|
const startOffset = mode === "month" ? 40 : (mode === "week" || mode === "3days") ? 10 : 1;
|
||||||
const endOffset = mode === "month" ? 40 : mode === "week" ? 10 : 1;
|
const endOffset = mode === "month" ? 40 : (mode === "week" || mode === "3days") ? 10 : 1;
|
||||||
|
|
||||||
const filteredEvents =
|
const filteredEvents =
|
||||||
events?.filter(
|
events?.filter(
|
||||||
@ -177,7 +174,7 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
|||||||
overlapCount: 0,
|
overlapCount: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
acc[dateKey].sort((a, b) => compareAsc(a.start, b.start));
|
acc[dateKey].sort((a: { start: any; }, b: { start: any; }) => compareAsc(a.start, b.start));
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
}, {} as Record<string, CalendarEvent[]>);
|
}, {} as Record<string, CalendarEvent[]>);
|
||||||
@ -230,7 +227,7 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
|||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
[todaysDate, currentDateStyle, defaultStyle] // dependencies
|
[todaysDate, currentDateStyle, defaultStyle]
|
||||||
);
|
);
|
||||||
|
|
||||||
return renderDate(date);
|
return renderDate(date);
|
||||||
@ -249,8 +246,6 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log(enrichedEvents, filteredEvents)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{isSyncing && (
|
{isSyncing && (
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
import { atom } from "jotai";
|
import { atom } from "jotai";
|
||||||
|
import * as Device from "expo-device";
|
||||||
import { CalendarEvent } from "@/components/pages/calendar/interfaces";
|
import { CalendarEvent } from "@/components/pages/calendar/interfaces";
|
||||||
|
|
||||||
|
const getDefaultMode = () => {
|
||||||
|
const isTablet = Device.deviceType === Device.DeviceType.TABLET;
|
||||||
|
return isTablet ? "week" : "3days";
|
||||||
|
};
|
||||||
|
|
||||||
export const editVisibleAtom = atom<boolean>(false);
|
export const editVisibleAtom = atom<boolean>(false);
|
||||||
export const isAllDayAtom = atom<boolean>(false);
|
export const isAllDayAtom = atom<boolean>(false);
|
||||||
export const eventForEditAtom = atom<CalendarEvent | undefined>(undefined);
|
export const eventForEditAtom = atom<CalendarEvent | undefined>(undefined);
|
||||||
export const isFamilyViewAtom = atom<boolean>(false);
|
export const isFamilyViewAtom = atom<boolean>(false);
|
||||||
export const modeAtom = atom<"week" | "month" | "day">("week");
|
export const modeAtom = atom<"week" | "month" | "day" | "3days">(getDefaultMode());
|
||||||
export const selectedDateAtom = atom<Date>(new Date());
|
export const selectedDateAtom = atom<Date>(new Date());
|
||||||
export const selectedNewEventDateAtom = atom<Date | undefined>(undefined);
|
export const selectedNewEventDateAtom = atom<Date | undefined>(undefined);
|
||||||
export const settingsPageIndex = atom<number>(0);
|
export const settingsPageIndex = atom<number>(0);
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
export const modeMap = new Map([
|
export const modeMap = new Map([
|
||||||
[0, "day"],
|
[0, "day"],
|
||||||
[1, "week"],
|
[1, "3days"],
|
||||||
[2, "month"],
|
[2, "week"],
|
||||||
|
[3, "month"]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
export const months = [
|
export const months = [
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
export interface CalendarEvent {
|
export interface CalendarEvent {
|
||||||
id?: number | string; // Unique identifier for the event
|
id?: number | string; // Unique identifier for the event
|
||||||
user?: string;
|
user?: string;
|
||||||
|
creatorId?: string;
|
||||||
title: string; // Event title or name
|
title: string; // Event title or name
|
||||||
description?: string; // Optional description for the event
|
description?: string; // Optiional description for the event
|
||||||
start: Date; // Start date and time of the event
|
start: Date; // Start date and time of the event
|
||||||
end: Date; // End date and time of the event
|
end: Date; // End date and time of the event
|
||||||
location?: string; // Optional event location
|
location?: string; // Optional event location
|
||||||
|
@ -190,7 +190,10 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
|||||||
.document('Events/{eventId}')
|
.document('Events/{eventId}')
|
||||||
.onCreate(async (snapshot, context) => {
|
.onCreate(async (snapshot, context) => {
|
||||||
const eventData = snapshot.data();
|
const eventData = snapshot.data();
|
||||||
const {familyId, creatorId, email, title, externalOrigin} = eventData;
|
const familyId = eventData.familyId;
|
||||||
|
const creatorId = eventData.creatorId;
|
||||||
|
const title = eventData.title || '';
|
||||||
|
const externalOrigin = eventData.externalOrigin || false;
|
||||||
|
|
||||||
if (!familyId || !creatorId) {
|
if (!familyId || !creatorId) {
|
||||||
console.error('Missing familyId or creatorId in event data');
|
console.error('Missing familyId or creatorId in event data');
|
||||||
@ -209,10 +212,10 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
|||||||
transaction.set(batchRef, {
|
transaction.set(batchRef, {
|
||||||
familyId,
|
familyId,
|
||||||
creatorId,
|
creatorId,
|
||||||
externalOrigin,
|
externalOrigin: externalOrigin || false,
|
||||||
events: [{
|
events: [{
|
||||||
id: context.params.eventId,
|
id: context.params.eventId,
|
||||||
title: title,
|
title: title || '',
|
||||||
timestamp: new Date().toISOString()
|
timestamp: new Date().toISOString()
|
||||||
}],
|
}],
|
||||||
createdAt: admin.firestore.FieldValue.serverTimestamp(),
|
createdAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||||
@ -224,7 +227,7 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
|||||||
transaction.update(batchRef, {
|
transaction.update(batchRef, {
|
||||||
events: [...existingEvents, {
|
events: [...existingEvents, {
|
||||||
id: context.params.eventId,
|
id: context.params.eventId,
|
||||||
title: title,
|
title: title || '',
|
||||||
timestamp: new Date().toISOString()
|
timestamp: new Date().toISOString()
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
@ -256,7 +259,7 @@ exports.processEventBatches = functions.pubsub
|
|||||||
try {
|
try {
|
||||||
const pushTokens = await getPushTokensForFamily(
|
const pushTokens = await getPushTokensForFamily(
|
||||||
familyId,
|
familyId,
|
||||||
externalOrigin ? null : creatorId
|
creatorId
|
||||||
);
|
);
|
||||||
|
|
||||||
if (pushTokens.length) {
|
if (pushTokens.length) {
|
||||||
@ -269,24 +272,23 @@ exports.processEventBatches = functions.pubsub
|
|||||||
: `${events.length} new events have been added to the family calendar.`;
|
: `${events.length} new events have been added to the family calendar.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all([
|
await sendNotifications(pushTokens, {
|
||||||
sendNotifications(pushTokens, {
|
title: 'New Family Calendar Events',
|
||||||
title: externalOrigin ? 'Calendar Sync Complete' : 'New Family Calendar Events',
|
body: notificationMessage,
|
||||||
body: notificationMessage,
|
data: {
|
||||||
data: {
|
|
||||||
type: externalOrigin ? 'sync' : 'manual',
|
|
||||||
count: events.length
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
storeNotification({
|
|
||||||
type: externalOrigin ? 'sync' : 'manual',
|
type: externalOrigin ? 'sync' : 'manual',
|
||||||
familyId,
|
count: events.length
|
||||||
content: notificationMessage,
|
}
|
||||||
timestamp: admin.firestore.FieldValue.serverTimestamp(),
|
});
|
||||||
creatorId,
|
|
||||||
eventCount: events.length
|
await storeNotification({
|
||||||
})
|
type: externalOrigin ? 'sync' : 'manual',
|
||||||
]);
|
familyId,
|
||||||
|
content: notificationMessage,
|
||||||
|
timestamp: admin.firestore.FieldValue.serverTimestamp(),
|
||||||
|
creatorId,
|
||||||
|
eventCount: events.length
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await doc.ref.update({
|
await doc.ref.update({
|
||||||
@ -307,71 +309,6 @@ exports.processEventBatches = functions.pubsub
|
|||||||
await Promise.all(processPromises);
|
await Promise.all(processPromises);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
exports.processEventBatchesRealtime = functions.firestore
|
|
||||||
.document('EventBatches/{batchId}')
|
|
||||||
.onWrite(async (change, context) => {
|
|
||||||
const batchData = change.after.data();
|
|
||||||
|
|
||||||
|
|
||||||
if (!batchData || batchData.processed) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const {familyId, creatorId, externalOrigin, events} = batchData;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const pushTokens = await getPushTokensForFamily(
|
|
||||||
familyId,
|
|
||||||
externalOrigin ? null : creatorId
|
|
||||||
);
|
|
||||||
|
|
||||||
if (pushTokens.length) {
|
|
||||||
let notificationMessage;
|
|
||||||
if (externalOrigin) {
|
|
||||||
notificationMessage = `Calendar sync completed: ${events.length} ${events.length === 1 ? 'event has' : 'events have'} been added.`;
|
|
||||||
} else {
|
|
||||||
notificationMessage = events.length === 1
|
|
||||||
? `New event "${events[0].title}" has been added to the family calendar.`
|
|
||||||
: `${events.length} new events have been added to the family calendar.`;
|
|
||||||
}
|
|
||||||
|
|
||||||
await Promise.all([
|
|
||||||
sendNotifications(pushTokens, {
|
|
||||||
title: externalOrigin ? 'Calendar Sync Complete' : 'New Family Calendar Events',
|
|
||||||
body: notificationMessage,
|
|
||||||
data: {
|
|
||||||
type: externalOrigin ? 'sync' : 'manual',
|
|
||||||
count: events.length
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
storeNotification({
|
|
||||||
type: externalOrigin ? 'sync' : 'manual',
|
|
||||||
familyId,
|
|
||||||
content: notificationMessage,
|
|
||||||
timestamp: admin.firestore.FieldValue.serverTimestamp(),
|
|
||||||
creatorId,
|
|
||||||
eventCount: events.length
|
|
||||||
})
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
await change.after.ref.update({
|
|
||||||
processed: true,
|
|
||||||
processedAt: admin.firestore.FieldValue.serverTimestamp()
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error processing batch ${context.params.batchId}:`, error);
|
|
||||||
await change.after.ref.update({
|
|
||||||
processed: true,
|
|
||||||
processedAt: admin.firestore.FieldValue.serverTimestamp(),
|
|
||||||
error: error.message
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
async function addToUpdateBatch(eventData, eventId) {
|
async function addToUpdateBatch(eventData, eventId) {
|
||||||
const timeWindow = Math.floor(Date.now() / 2000);
|
const timeWindow = Math.floor(Date.now() / 2000);
|
||||||
const batchId = `${timeWindow}_${eventData.familyId}_${eventData.lastModifiedBy}`;
|
const batchId = `${timeWindow}_${eventData.familyId}_${eventData.lastModifiedBy}`;
|
||||||
@ -1126,85 +1063,16 @@ exports.renewGoogleCalendarWatch = functions.pubsub
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
exports.sendSyncNotification = functions.https.onRequest(async (req, res) => {
|
const tokenRefreshInProgress = new Map();
|
||||||
const userId = req.query.userId;
|
|
||||||
const calendarId = req.body.resourceId;
|
|
||||||
|
|
||||||
console.log(`Received notification for user ${userId} with calendar ID ${calendarId}`);
|
exports.cleanupTokenRefreshFlags = functions.pubsub
|
||||||
|
.schedule('every 5 minutes')
|
||||||
|
.onRun(() => {
|
||||||
|
tokenRefreshInProgress.clear();
|
||||||
|
console.log('[CLEANUP] Cleared all token refresh flags');
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
|
||||||
const userDoc = await db.collection("Profiles").doc(userId).get();
|
|
||||||
const userData = userDoc.data();
|
|
||||||
|
|
||||||
const {googleAccounts} = userData;
|
|
||||||
const email = Object.keys(googleAccounts || {})[0];
|
|
||||||
const accountData = googleAccounts[email] || {};
|
|
||||||
const token = accountData.accessToken;
|
|
||||||
const refreshToken = accountData.refreshToken;
|
|
||||||
const familyId = userData.familyId;
|
|
||||||
|
|
||||||
|
|
||||||
if (userData.pushToken) {
|
|
||||||
await sendNotifications(
|
|
||||||
Array.isArray(userData.pushToken) ? userData.pushToken : [userData.pushToken],
|
|
||||||
{
|
|
||||||
title: "Calendar Sync",
|
|
||||||
body: "Calendar sync in progress...",
|
|
||||||
data: {type: 'sync_started'}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Starting calendar sync...");
|
|
||||||
const eventCount = await calendarSync({userId, email, token, refreshToken, familyId});
|
|
||||||
console.log("Calendar sync completed.");
|
|
||||||
|
|
||||||
|
|
||||||
if (userData.pushToken) {
|
|
||||||
const syncMessage = `Calendar sync completed: ${eventCount} ${eventCount === 1 ? 'event has' : 'events have'} been synced.`;
|
|
||||||
|
|
||||||
await Promise.all([
|
|
||||||
|
|
||||||
sendNotifications(
|
|
||||||
Array.isArray(userData.pushToken) ? userData.pushToken : [userData.pushToken],
|
|
||||||
{
|
|
||||||
title: "Calendar Sync",
|
|
||||||
body: syncMessage,
|
|
||||||
data: {
|
|
||||||
type: 'sync_completed',
|
|
||||||
count: eventCount
|
|
||||||
}
|
|
||||||
}
|
|
||||||
),
|
|
||||||
|
|
||||||
storeNotification({
|
|
||||||
type: 'sync',
|
|
||||||
familyId,
|
|
||||||
content: syncMessage,
|
|
||||||
timestamp: admin.firestore.FieldValue.serverTimestamp(),
|
|
||||||
creatorId: userId,
|
|
||||||
date: new Date()
|
|
||||||
})
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200).send("Sync completed successfully.");
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error in sendSyncNotification for user ${userId}:`, error.message);
|
|
||||||
|
|
||||||
if (userData?.pushToken) {
|
|
||||||
await sendNotifications(
|
|
||||||
Array.isArray(userData.pushToken) ? userData.pushToken : [userData.pushToken],
|
|
||||||
{
|
|
||||||
title: "Calendar Sync Error",
|
|
||||||
body: "There was an error syncing your calendar. Please try again later.",
|
|
||||||
data: {type: 'sync_error'}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
res.status(500).send("Failed to send sync notification.");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
async function fetchAndSaveGoogleEvents({token, refreshToken, email, familyId, creatorId}) {
|
async function fetchAndSaveGoogleEvents({token, refreshToken, email, familyId, creatorId}) {
|
||||||
const baseDate = new Date();
|
const baseDate = new Date();
|
||||||
@ -1216,7 +1084,7 @@ async function fetchAndSaveGoogleEvents({token, refreshToken, email, familyId, c
|
|||||||
const batchSize = 50;
|
const batchSize = 50;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`Fetching events for user: ${email}`);
|
console.log(`[FETCH] Starting event fetch for user: ${email}`);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let events = [];
|
let events = [];
|
||||||
@ -1227,19 +1095,32 @@ async function fetchAndSaveGoogleEvents({token, refreshToken, email, familyId, c
|
|||||||
url.searchParams.set("maxResults", batchSize.toString());
|
url.searchParams.set("maxResults", batchSize.toString());
|
||||||
if (pageToken) url.searchParams.set("pageToken", pageToken);
|
if (pageToken) url.searchParams.set("pageToken", pageToken);
|
||||||
|
|
||||||
const response = await fetch(url.toString(), {
|
console.log(`[FETCH] Making request with token: ${token.substring(0, 10)}...`);
|
||||||
|
|
||||||
|
let response = await fetch(url.toString(), {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.status === 401 && refreshToken) {
|
if (response.status === 401 && refreshToken) {
|
||||||
console.log(`Token expired for user: ${email}, attempting to refresh`);
|
console.log(`[TOKEN] Token expired during fetch, refreshing for ${email}`);
|
||||||
const refreshedToken = await refreshGoogleToken(refreshToken);
|
const {refreshedGoogleToken} = await refreshGoogleToken(refreshToken);
|
||||||
token = refreshedToken;
|
if (refreshedGoogleToken) {
|
||||||
|
console.log(`[TOKEN] Token refreshed successfully during fetch`);
|
||||||
|
token = refreshedGoogleToken;
|
||||||
|
|
||||||
if (token) {
|
// Update token in Firestore
|
||||||
return fetchAndSaveGoogleEvents({token, refreshToken, email, familyId, creatorId});
|
await db.collection("Profiles").doc(creatorId).update({
|
||||||
|
[`googleAccounts.${email}.accessToken`]: refreshedGoogleToken
|
||||||
|
});
|
||||||
|
|
||||||
|
// Retry the request with new token
|
||||||
|
response = await fetch(url.toString(), {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${refreshedGoogleToken}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1267,8 +1148,8 @@ async function fetchAndSaveGoogleEvents({token, refreshToken, email, familyId, c
|
|||||||
events.push(googleEvent);
|
events.push(googleEvent);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
if (events.length > 0) {
|
if (events.length > 0) {
|
||||||
|
console.log(`[FETCH] Saving batch of ${events.length} events`);
|
||||||
await saveEventsToFirestore(events);
|
await saveEventsToFirestore(events);
|
||||||
totalEvents += events.length;
|
totalEvents += events.length;
|
||||||
}
|
}
|
||||||
@ -1276,9 +1157,10 @@ async function fetchAndSaveGoogleEvents({token, refreshToken, email, familyId, c
|
|||||||
pageToken = data.nextPageToken;
|
pageToken = data.nextPageToken;
|
||||||
} while (pageToken);
|
} while (pageToken);
|
||||||
|
|
||||||
|
console.log(`[FETCH] Completed with ${totalEvents} total events`);
|
||||||
return totalEvents;
|
return totalEvents;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error fetching Google Calendar events for ${email}:`, error);
|
console.error(`[ERROR] Failed fetching events for ${email}:`, error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1293,8 +1175,20 @@ async function saveEventsToFirestore(events) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function calendarSync({userId, email, token, refreshToken, familyId}) {
|
async function calendarSync({userId, email, token, refreshToken, familyId}) {
|
||||||
console.log(`Starting calendar sync for user ${userId} with email ${email}`);
|
console.log(`[SYNC] Starting calendar sync for user ${userId} with email ${email}`);
|
||||||
try {
|
try {
|
||||||
|
if (refreshToken) {
|
||||||
|
console.log(`[TOKEN] Initial token refresh for ${email}`);
|
||||||
|
const {refreshedGoogleToken} = await refreshGoogleToken(refreshToken);
|
||||||
|
if (refreshedGoogleToken) {
|
||||||
|
console.log(`[TOKEN] Token refreshed successfully for ${email}`);
|
||||||
|
token = refreshedGoogleToken;
|
||||||
|
await db.collection("Profiles").doc(userId).update({
|
||||||
|
[`googleAccounts.${email}.accessToken`]: refreshedGoogleToken
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const eventCount = await fetchAndSaveGoogleEvents({
|
const eventCount = await fetchAndSaveGoogleEvents({
|
||||||
token,
|
token,
|
||||||
refreshToken,
|
refreshToken,
|
||||||
@ -1302,10 +1196,11 @@ async function calendarSync({userId, email, token, refreshToken, familyId}) {
|
|||||||
familyId,
|
familyId,
|
||||||
creatorId: userId,
|
creatorId: userId,
|
||||||
});
|
});
|
||||||
console.log("Calendar events synced successfully.");
|
|
||||||
|
console.log(`[SYNC] Calendar sync completed. Processed ${eventCount} events`);
|
||||||
return eventCount;
|
return eventCount;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error syncing calendar for user ${userId}:`, error);
|
console.error(`[ERROR] Calendar sync failed for user ${userId}:`, error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1314,41 +1209,83 @@ exports.sendSyncNotification = functions.https.onRequest(async (req, res) => {
|
|||||||
const userId = req.query.userId;
|
const userId = req.query.userId;
|
||||||
const calendarId = req.body.resourceId;
|
const calendarId = req.body.resourceId;
|
||||||
|
|
||||||
console.log(`Received notification for user ${userId} with calendar ID ${calendarId}`);
|
console.log(`[SYNC START] Received notification for user ${userId} with calendar ID ${calendarId}`);
|
||||||
|
console.log('Request headers:', req.headers);
|
||||||
|
console.log('Request body:', req.body);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log(`[PROFILE] Fetching user profile for ${userId}`);
|
||||||
const userDoc = await db.collection("Profiles").doc(userId).get();
|
const userDoc = await db.collection("Profiles").doc(userId).get();
|
||||||
|
|
||||||
|
if (!userDoc.exists) {
|
||||||
|
console.error(`[ERROR] No profile found for user ${userId}`);
|
||||||
|
return res.status(404).send("User profile not found");
|
||||||
|
}
|
||||||
|
|
||||||
const userData = userDoc.data();
|
const userData = userDoc.data();
|
||||||
|
console.log(`[PROFILE] Found profile data for user ${userId}:`, {
|
||||||
let pushTokens = [];
|
hasGoogleAccounts: !!userData.googleAccounts,
|
||||||
if (userData && userData.pushToken) {
|
familyId: userData.familyId
|
||||||
pushTokens = Array.isArray(userData.pushToken) ? userData.pushToken : [userData.pushToken];
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (pushTokens.length === 0) {
|
|
||||||
console.log(`No push tokens found for user ${userId}`);
|
|
||||||
res.status(200).send("No push tokens found for user.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const {googleAccounts} = userData;
|
const {googleAccounts} = userData;
|
||||||
const email = Object.keys(googleAccounts || {})[0];
|
const email = Object.keys(googleAccounts || {})[0];
|
||||||
|
|
||||||
|
if (!email) {
|
||||||
|
console.error(`[ERROR] No Google account found for user ${userId}`);
|
||||||
|
return res.status(400).send("No Google account found");
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[GOOGLE] Using Google account: ${email}`);
|
||||||
|
|
||||||
const accountData = googleAccounts[email] || {};
|
const accountData = googleAccounts[email] || {};
|
||||||
const token = accountData.accessToken;
|
const token = accountData.accessToken;
|
||||||
const refreshToken = accountData.refreshToken;
|
const refreshToken = accountData.refreshToken;
|
||||||
const familyId = userData.familyId;
|
const familyId = userData.familyId;
|
||||||
|
|
||||||
console.log("Starting calendar sync...");
|
if (!familyId) {
|
||||||
await calendarSync({userId, email, token, refreshToken, familyId});
|
console.error(`[ERROR] No family ID found for user ${userId}`);
|
||||||
console.log("Calendar sync completed.");
|
return res.status(400).send("No family ID found");
|
||||||
|
}
|
||||||
|
|
||||||
res.status(200).send("Sync notification sent.");
|
console.log(`[SYNC] Starting calendar sync for user ${userId} (family: ${familyId})`);
|
||||||
|
const syncStartTime = Date.now();
|
||||||
|
await calendarSync({userId, email, token, refreshToken, familyId});
|
||||||
|
console.log(`[SYNC] Calendar sync completed in ${Date.now() - syncStartTime}ms`);
|
||||||
|
|
||||||
|
console.log(`[HOUSEHOLDS] Fetching households for family ${familyId}`);
|
||||||
|
const querySnapshot = await db.collection('Households')
|
||||||
|
.where("familyId", "==", familyId)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
console.log(`[HOUSEHOLDS] Found ${querySnapshot.size} households to update`);
|
||||||
|
|
||||||
|
const batch = db.batch();
|
||||||
|
querySnapshot.docs.forEach((doc) => {
|
||||||
|
console.log(`[HOUSEHOLDS] Adding household ${doc.id} to update batch`);
|
||||||
|
batch.update(doc.ref, {
|
||||||
|
lastSyncTimestamp: admin.firestore.FieldValue.serverTimestamp()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`[HOUSEHOLDS] Committing batch update for ${querySnapshot.size} households`);
|
||||||
|
const batchStartTime = Date.now();
|
||||||
|
await batch.commit();
|
||||||
|
console.log(`[HOUSEHOLDS] Batch update completed in ${Date.now() - batchStartTime}ms`);
|
||||||
|
|
||||||
|
console.log(`[SYNC COMPLETE] Successfully processed sync for user ${userId}`);
|
||||||
|
res.status(200).send("Sync completed successfully.");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error in sendSyncNotification for user ${userId}:`, error.message);
|
console.error(`[ERROR] Error in sendSyncNotification for user ${userId}:`, {
|
||||||
|
errorMessage: error.message,
|
||||||
|
errorStack: error.stack,
|
||||||
|
errorCode: error.code
|
||||||
|
});
|
||||||
res.status(500).send("Failed to send sync notification.");
|
res.status(500).send("Failed to send sync notification.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
async function refreshMicrosoftToken(refreshToken) {
|
async function refreshMicrosoftToken(refreshToken) {
|
||||||
try {
|
try {
|
||||||
console.log("Refreshing Microsoft token...");
|
console.log("Refreshing Microsoft token...");
|
||||||
@ -1557,3 +1494,256 @@ exports.microsoftCalendarWebhook = functions.https.onRequest(async (req, res) =>
|
|||||||
res.status(500).send();
|
res.status(500).send();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
exports.triggerGoogleSync = functions.https.onCall(async (data, context) => {
|
||||||
|
if (!context.auth) {
|
||||||
|
throw new functions.https.HttpsError(
|
||||||
|
'unauthenticated',
|
||||||
|
'Authentication required'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const {email} = data;
|
||||||
|
const userId = context.auth.uid;
|
||||||
|
|
||||||
|
const userDoc = await db.collection("Profiles").doc(userId).get();
|
||||||
|
const userData = userDoc.data();
|
||||||
|
|
||||||
|
if (!userData?.googleAccounts?.[email]) {
|
||||||
|
throw new functions.https.HttpsError(
|
||||||
|
'failed-precondition',
|
||||||
|
'No valid Google account found'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const accountData = userData.googleAccounts[email];
|
||||||
|
const eventCount = await calendarSync({
|
||||||
|
userId,
|
||||||
|
email,
|
||||||
|
token: accountData.accessToken,
|
||||||
|
refreshToken: accountData.refreshToken,
|
||||||
|
familyId: userData.familyId
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
eventCount,
|
||||||
|
message: "Google calendar sync completed successfully"
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Google sync error:', error);
|
||||||
|
throw new functions.https.HttpsError('internal', error.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
exports.triggerMicrosoftSync = functions.https.onCall(async (data, context) => {
|
||||||
|
if (!context.auth) {
|
||||||
|
throw new functions.https.HttpsError(
|
||||||
|
'unauthenticated',
|
||||||
|
'Authentication required'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const {email} = data;
|
||||||
|
if (!email) {
|
||||||
|
throw new functions.https.HttpsError(
|
||||||
|
'invalid-argument',
|
||||||
|
'Email is required'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Starting Microsoft sync for:', {userId: context.auth.uid, email});
|
||||||
|
|
||||||
|
const userDoc = await db.collection("Profiles").doc(context.auth.uid).get();
|
||||||
|
if (!userDoc.exists) {
|
||||||
|
throw new functions.https.HttpsError(
|
||||||
|
'not-found',
|
||||||
|
'User profile not found'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const userData = userDoc.data();
|
||||||
|
const accountData = userData.microsoftAccounts?.[email];
|
||||||
|
|
||||||
|
if (!accountData) {
|
||||||
|
throw new functions.https.HttpsError(
|
||||||
|
'failed-precondition',
|
||||||
|
'Microsoft account not found'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let {accessToken, refreshToken} = accountData;
|
||||||
|
|
||||||
|
// Try to refresh token if it exists
|
||||||
|
if (refreshToken) {
|
||||||
|
try {
|
||||||
|
const refreshedTokens = await refreshMicrosoftToken(refreshToken);
|
||||||
|
accessToken = refreshedTokens.accessToken;
|
||||||
|
refreshToken = refreshedTokens.refreshToken || refreshToken;
|
||||||
|
|
||||||
|
// Update the stored tokens
|
||||||
|
await db.collection("Profiles").doc(context.auth.uid).update({
|
||||||
|
[`microsoftAccounts.${email}`]: {
|
||||||
|
...accountData,
|
||||||
|
accessToken,
|
||||||
|
refreshToken,
|
||||||
|
lastRefresh: admin.firestore.FieldValue.serverTimestamp()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (refreshError) {
|
||||||
|
console.error('Token refresh failed:', refreshError);
|
||||||
|
throw new functions.https.HttpsError(
|
||||||
|
'failed-precondition',
|
||||||
|
'Failed to refresh Microsoft token. Please reconnect your account.',
|
||||||
|
{requiresReauth: true}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else if (!accessToken) {
|
||||||
|
throw new functions.https.HttpsError(
|
||||||
|
'failed-precondition',
|
||||||
|
'Microsoft account requires authentication. Please reconnect your account.',
|
||||||
|
{requiresReauth: true}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log('Fetching Microsoft events with token');
|
||||||
|
const eventCount = await fetchAndSaveMicrosoftEvents({
|
||||||
|
token: accessToken,
|
||||||
|
refreshToken,
|
||||||
|
email,
|
||||||
|
familyId: userData.familyId,
|
||||||
|
creatorId: context.auth.uid
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Microsoft sync completed successfully:', {eventCount});
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
eventCount,
|
||||||
|
message: "Microsoft calendar sync completed successfully"
|
||||||
|
};
|
||||||
|
} catch (syncError) {
|
||||||
|
// Check if the error is due to invalid token
|
||||||
|
if (syncError.message?.includes('401') ||
|
||||||
|
syncError.message?.includes('unauthorized') ||
|
||||||
|
syncError.message?.includes('invalid_grant')) {
|
||||||
|
throw new functions.https.HttpsError(
|
||||||
|
'unauthenticated',
|
||||||
|
'Microsoft authentication expired. Please reconnect your account.',
|
||||||
|
{requiresReauth: true}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw new functions.https.HttpsError(
|
||||||
|
'internal',
|
||||||
|
syncError.message || 'Failed to sync Microsoft calendar',
|
||||||
|
{originalError: syncError}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Microsoft sync function error:', error);
|
||||||
|
if (error instanceof functions.https.HttpsError) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
throw new functions.https.HttpsError(
|
||||||
|
'internal',
|
||||||
|
error.message || 'Unknown error occurred',
|
||||||
|
{originalError: error}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
exports.updateHouseholdTimestampOnEventCreate = functions.firestore
|
||||||
|
.document('Events/{eventId}')
|
||||||
|
.onCreate(async (snapshot, context) => {
|
||||||
|
const eventData = snapshot.data();
|
||||||
|
const familyId = eventData.familyId;
|
||||||
|
const eventId = context.params.eventId;
|
||||||
|
|
||||||
|
console.log(`[HOUSEHOLD_UPDATE] Event created - Processing timestamp updates`, {
|
||||||
|
eventId,
|
||||||
|
familyId,
|
||||||
|
eventTitle: eventData.title || 'Untitled'
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const householdsSnapshot = await db.collection('Households')
|
||||||
|
.where('familyId', '==', familyId)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
console.log(`[HOUSEHOLD_UPDATE] Found ${householdsSnapshot.size} households to update for family ${familyId}`);
|
||||||
|
|
||||||
|
const batch = db.batch();
|
||||||
|
householdsSnapshot.docs.forEach((doc) => {
|
||||||
|
console.log(`[HOUSEHOLD_UPDATE] Adding household ${doc.id} to update batch`);
|
||||||
|
batch.update(doc.ref, {
|
||||||
|
lastUpdateTimestamp: admin.firestore.FieldValue.serverTimestamp()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const batchStartTime = Date.now();
|
||||||
|
await batch.commit();
|
||||||
|
console.log(`[HOUSEHOLD_UPDATE] Batch update completed in ${Date.now() - batchStartTime}ms`, {
|
||||||
|
familyId,
|
||||||
|
householdsUpdated: householdsSnapshot.size,
|
||||||
|
eventId
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[HOUSEHOLD_UPDATE] Error updating households for event creation`, {
|
||||||
|
eventId,
|
||||||
|
familyId,
|
||||||
|
error: error.message,
|
||||||
|
stack: error.stack
|
||||||
|
});
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
exports.updateHouseholdTimestampOnEventUpdate = functions.firestore
|
||||||
|
.document('Events/{eventId}')
|
||||||
|
.onUpdate(async (change, context) => {
|
||||||
|
const eventData = change.after.data();
|
||||||
|
const familyId = eventData.familyId;
|
||||||
|
const eventId = context.params.eventId;
|
||||||
|
|
||||||
|
console.log(`[HOUSEHOLD_UPDATE] Event updated - Processing timestamp updates`, {
|
||||||
|
eventId,
|
||||||
|
familyId,
|
||||||
|
eventTitle: eventData.title || 'Untitled'
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const householdsSnapshot = await db.collection('Households')
|
||||||
|
.where('familyId', '==', familyId)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
console.log(`[HOUSEHOLD_UPDATE] Found ${householdsSnapshot.size} households to update for family ${familyId}`);
|
||||||
|
|
||||||
|
const batch = db.batch();
|
||||||
|
householdsSnapshot.docs.forEach((doc) => {
|
||||||
|
console.log(`[HOUSEHOLD_UPDATE] Adding household ${doc.id} to update batch`);
|
||||||
|
batch.update(doc.ref, {
|
||||||
|
lastUpdateTimestamp: admin.firestore.FieldValue.serverTimestamp()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const batchStartTime = Date.now();
|
||||||
|
await batch.commit();
|
||||||
|
console.log(`[HOUSEHOLD_UPDATE] Batch update completed in ${Date.now() - batchStartTime}ms`, {
|
||||||
|
familyId,
|
||||||
|
householdsUpdated: householdsSnapshot.size,
|
||||||
|
eventId
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[HOUSEHOLD_UPDATE] Error updating households for event update`, {
|
||||||
|
eventId,
|
||||||
|
familyId,
|
||||||
|
error: error.message,
|
||||||
|
stack: error.stack
|
||||||
|
});
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
@ -1,10 +1,29 @@
|
|||||||
import {ProfileType} from "@/contexts/AuthContext";
|
export type ProfileType = 'parent' | 'child';
|
||||||
|
|
||||||
export interface User {
|
export interface CalendarAccount {
|
||||||
uid: string;
|
accessToken: string;
|
||||||
email: string | null;
|
refreshToken?: string;
|
||||||
|
resourceId?: string;
|
||||||
|
email?: string;
|
||||||
|
expiresAt?: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface GoogleAccount extends CalendarAccount {
|
||||||
|
scope?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MicrosoftAccount extends CalendarAccount {
|
||||||
|
subscriptionId?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AppleAccount extends CalendarAccount {
|
||||||
|
identityToken?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type CalendarAccounts = {
|
||||||
|
[email: string]: GoogleAccount | MicrosoftAccount | AppleAccount;
|
||||||
|
};
|
||||||
|
|
||||||
export interface UserProfile {
|
export interface UserProfile {
|
||||||
userType: ProfileType;
|
userType: ProfileType;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
@ -21,23 +40,7 @@ export interface UserProfile {
|
|||||||
eventColor?: string | null;
|
eventColor?: string | null;
|
||||||
timeZone?: string | null;
|
timeZone?: string | null;
|
||||||
firstDayOfWeek?: string | null;
|
firstDayOfWeek?: string | null;
|
||||||
googleAccounts?: Object;
|
googleAccounts?: { [email: string]: GoogleAccount };
|
||||||
microsoftAccounts?: Object;
|
microsoftAccounts?: { [email: string]: MicrosoftAccount };
|
||||||
appleAccounts?: Object;
|
appleAccounts?: { [email: string]: AppleAccount };
|
||||||
}
|
|
||||||
|
|
||||||
export interface ParentProfile extends UserProfile {
|
|
||||||
userType: ProfileType.PARENT;
|
|
||||||
childrenIds: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ChildProfile extends UserProfile {
|
|
||||||
userType: ProfileType.CHILD;
|
|
||||||
birthday: Date;
|
|
||||||
parentId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface CaregiverProfile extends UserProfile {
|
|
||||||
userType: ProfileType.CAREGIVER;
|
|
||||||
contact: string;
|
|
||||||
}
|
}
|
@ -1,51 +1,82 @@
|
|||||||
import {useQuery} from "react-query";
|
import {useQuery, useQueryClient} from "react-query";
|
||||||
import firestore from "@react-native-firebase/firestore";
|
import firestore from "@react-native-firebase/firestore";
|
||||||
import {useAuthContext} from "@/contexts/AuthContext";
|
import {useAuthContext} from "@/contexts/AuthContext";
|
||||||
import {useAtomValue} from "jotai";
|
import {useAtomValue} from "jotai";
|
||||||
import {isFamilyViewAtom} from "@/components/pages/calendar/atoms";
|
import {isFamilyViewAtom} from "@/components/pages/calendar/atoms";
|
||||||
import {colorMap} from "@/constants/colorMap";
|
import {colorMap} from "@/constants/colorMap";
|
||||||
import {uuidv4} from "@firebase/util";
|
import {uuidv4} from "@firebase/util";
|
||||||
|
import {useEffect} from "react";
|
||||||
|
|
||||||
export const useGetEvents = () => {
|
export const useGetEvents = () => {
|
||||||
const {user, profileData} = useAuthContext();
|
const {user, profileData} = useAuthContext();
|
||||||
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!profileData?.familyId) return;
|
||||||
|
|
||||||
|
console.log(`[SYNC] Setting up sync listener for family: ${profileData.familyId}`);
|
||||||
|
|
||||||
|
const unsubscribe = firestore()
|
||||||
|
.collection('Households')
|
||||||
|
.where("familyId", "==", profileData.familyId)
|
||||||
|
.onSnapshot((snapshot) => {
|
||||||
|
snapshot.docChanges().forEach((change) => {
|
||||||
|
if (change.type === 'modified') {
|
||||||
|
const data = change.doc.data();
|
||||||
|
if (data?.lastSyncTimestamp) {
|
||||||
|
console.log(`[SYNC] Change detected at ${data.lastSyncTimestamp.toDate()}`);
|
||||||
|
console.log(`[SYNC] Household ${change.doc.id} triggered refresh`);
|
||||||
|
queryClient.invalidateQueries(["events", user?.uid, isFamilyView]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, (error) => {
|
||||||
|
console.error('[SYNC] Listener error:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
console.log('[SYNC] Cleaning up sync listener');
|
||||||
|
unsubscribe();
|
||||||
|
};
|
||||||
|
}, [profileData?.familyId, user?.uid, isFamilyView, queryClient]);
|
||||||
|
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: ["events", user?.uid, isFamilyView],
|
queryKey: ["events", user?.uid, isFamilyView],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
|
console.log(`Fetching events - Family View: ${isFamilyView}, User: ${user?.uid}`);
|
||||||
|
|
||||||
const db = firestore();
|
const db = firestore();
|
||||||
const userId = user?.uid;
|
const userId = user?.uid;
|
||||||
const familyId = profileData?.familyId;
|
const familyId = profileData?.familyId;
|
||||||
|
|
||||||
let allEvents = [];
|
let allEvents = [];
|
||||||
|
|
||||||
if (isFamilyView) {
|
if (isFamilyView) {
|
||||||
// Get public family events
|
|
||||||
const publicFamilyEvents = await db.collection("Events")
|
const publicFamilyEvents = await db.collection("Events")
|
||||||
.where("familyId", "==", familyId)
|
.where("familyId", "==", familyId)
|
||||||
.where("private", "==", false)
|
.where("private", "==", false)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
// Get private events where user is creator
|
|
||||||
const privateCreatorEvents = await db.collection("Events")
|
const privateCreatorEvents = await db.collection("Events")
|
||||||
.where("familyId", "==", familyId)
|
.where("familyId", "==", familyId)
|
||||||
.where("private", "==", true)
|
.where("private", "==", true)
|
||||||
.where("creatorId", "==", userId)
|
.where("creatorId", "==", userId)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
// Get private events where user is attendee
|
|
||||||
const privateAttendeeEvents = await db.collection("Events")
|
const privateAttendeeEvents = await db.collection("Events")
|
||||||
.where("private", "==", true)
|
.where("private", "==", true)
|
||||||
.where("attendees", "array-contains", userId)
|
.where("attendees", "array-contains", userId)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
|
console.log(`Found ${publicFamilyEvents.size} public events, ${privateCreatorEvents.size} private creator events, ${privateAttendeeEvents.size} private attendee events`);
|
||||||
|
|
||||||
allEvents = [
|
allEvents = [
|
||||||
...publicFamilyEvents.docs.map(doc => doc.data()),
|
...publicFamilyEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
|
||||||
...privateCreatorEvents.docs.map(doc => doc.data()),
|
...privateCreatorEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
|
||||||
...privateAttendeeEvents.docs.map(doc => doc.data())
|
...privateAttendeeEvents.docs.map(doc => ({...doc.data(), id: doc.id}))
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
// Personal view: Only show events where user is creator or attendee
|
|
||||||
const [creatorEvents, attendeeEvents] = await Promise.all([
|
const [creatorEvents, attendeeEvents] = await Promise.all([
|
||||||
db.collection("Events")
|
db.collection("Events")
|
||||||
.where("creatorId", "==", userId)
|
.where("creatorId", "==", userId)
|
||||||
@ -55,24 +86,28 @@ export const useGetEvents = () => {
|
|||||||
.get()
|
.get()
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
console.log(`Found ${creatorEvents.size} creator events, ${attendeeEvents.size} attendee events`);
|
||||||
|
|
||||||
allEvents = [
|
allEvents = [
|
||||||
...creatorEvents.docs.map(doc => doc.data()),
|
...creatorEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
|
||||||
...attendeeEvents.docs.map(doc => doc.data())
|
...attendeeEvents.docs.map(doc => ({...doc.data(), id: doc.id}))
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure uniqueness
|
|
||||||
const uniqueEventsMap = new Map();
|
const uniqueEventsMap = new Map();
|
||||||
allEvents.forEach(event => {
|
allEvents.forEach(event => {
|
||||||
if (event.id) {
|
if (event.id) {
|
||||||
uniqueEventsMap.set(event.id, event);
|
uniqueEventsMap.set(event.id, event);
|
||||||
} else {
|
} else {
|
||||||
uniqueEventsMap.set(uuidv4(), event);
|
const newId = uuidv4();
|
||||||
|
console.log(`Generated new ID for event without ID: ${newId}`);
|
||||||
|
uniqueEventsMap.set(newId, {...event, id: newId});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Map events with creator colors
|
console.log(`Processing ${uniqueEventsMap.size} unique events`);
|
||||||
return await Promise.all(
|
|
||||||
|
const processedEvents = await Promise.all(
|
||||||
Array.from(uniqueEventsMap.values()).map(async (event) => {
|
Array.from(uniqueEventsMap.values()).map(async (event) => {
|
||||||
const profileSnapshot = await db
|
const profileSnapshot = await db
|
||||||
.collection("Profiles")
|
.collection("Profiles")
|
||||||
@ -96,9 +131,15 @@ export const useGetEvents = () => {
|
|||||||
};
|
};
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log(`Events processing completed, returning ${processedEvents.length} events`);
|
||||||
|
return processedEvents;
|
||||||
},
|
},
|
||||||
staleTime: Infinity,
|
staleTime: 5 * 60 * 1000,
|
||||||
cacheTime: Infinity,
|
cacheTime: 30 * 60 * 1000,
|
||||||
keepPreviousData: true,
|
keepPreviousData: true,
|
||||||
|
onError: (error) => {
|
||||||
|
console.error('Error fetching events:', error);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
@ -2,7 +2,7 @@ import {useAuthContext} from "@/contexts/AuthContext";
|
|||||||
import {useEffect} from "react";
|
import {useEffect} from "react";
|
||||||
import {useUpdateUserData} from "@/hooks/firebase/useUpdateUserData";
|
import {useUpdateUserData} from "@/hooks/firebase/useUpdateUserData";
|
||||||
import {useFetchAndSaveGoogleEvents} from "@/hooks/useFetchAndSaveGoogleEvents";
|
import {useFetchAndSaveGoogleEvents} from "@/hooks/useFetchAndSaveGoogleEvents";
|
||||||
import {useFetchAndSaveOutlookEvents} from "@/hooks/useFetchAndSaveOutlookEvents";
|
import {useFetchAndSaveMicrosoftEvents} from "@/hooks/useFetchAndSaveOutlookEvents";
|
||||||
import {useFetchAndSaveAppleEvents} from "@/hooks/useFetchAndSaveAppleEvents";
|
import {useFetchAndSaveAppleEvents} from "@/hooks/useFetchAndSaveAppleEvents";
|
||||||
import * as WebBrowser from "expo-web-browser";
|
import * as WebBrowser from "expo-web-browser";
|
||||||
import * as Google from "expo-auth-session/providers/google";
|
import * as Google from "expo-auth-session/providers/google";
|
||||||
@ -10,14 +10,12 @@ import * as AuthSession from "expo-auth-session";
|
|||||||
import * as AppleAuthentication from "expo-apple-authentication";
|
import * as AppleAuthentication from "expo-apple-authentication";
|
||||||
import * as Notifications from 'expo-notifications';
|
import * as Notifications from 'expo-notifications';
|
||||||
import {useQueryClient} from "react-query";
|
import {useQueryClient} from "react-query";
|
||||||
|
import {AppleAccount, GoogleAccount, MicrosoftAccount} from "@/hooks/firebase/types/profileTypes";
|
||||||
|
|
||||||
const googleConfig = {
|
const googleConfig = {
|
||||||
androidClientId:
|
androidClientId: "406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
||||||
"406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
iosClientId: "406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
||||||
iosClientId:
|
webClientId: "406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
||||||
"406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
|
||||||
webClientId:
|
|
||||||
"406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
|
||||||
scopes: [
|
scopes: [
|
||||||
"email",
|
"email",
|
||||||
"profile",
|
"profile",
|
||||||
@ -39,18 +37,32 @@ const microsoftConfig = {
|
|||||||
"Calendars.ReadWrite",
|
"Calendars.ReadWrite",
|
||||||
"User.Read",
|
"User.Read",
|
||||||
],
|
],
|
||||||
authorizationEndpoint:
|
authorizationEndpoint: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
|
||||||
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
|
|
||||||
tokenEndpoint: "https://login.microsoftonline.com/common/oauth2/v2.0/token",
|
tokenEndpoint: "https://login.microsoftonline.com/common/oauth2/v2.0/token",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface SyncResponse {
|
||||||
|
success: boolean;
|
||||||
|
eventCount?: number;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CalendarSyncResult {
|
||||||
|
data: {
|
||||||
|
success: boolean;
|
||||||
|
eventCount: number;
|
||||||
|
message?: string;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const useCalSync = () => {
|
export const useCalSync = () => {
|
||||||
const {profileData} = useAuthContext();
|
const {profileData} = useAuthContext();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
const {mutateAsync: updateUserData} = useUpdateUserData();
|
const {mutateAsync: updateUserData} = useUpdateUserData();
|
||||||
const {mutateAsync: fetchAndSaveGoogleEvents, isLoading: isSyncingGoogle} = useFetchAndSaveGoogleEvents();
|
const {mutateAsync: fetchAndSaveGoogleEvents, isLoading: isSyncingGoogle} = useFetchAndSaveGoogleEvents();
|
||||||
const {mutateAsync: fetchAndSaveOutlookEvents, isLoading: isSyncingOutlook} = useFetchAndSaveOutlookEvents();
|
const {mutateAsync: fetchAndSaveOutlookEvents, isLoading: isSyncingOutlook} = useFetchAndSaveMicrosoftEvents();
|
||||||
const {mutateAsync: fetchAndSaveAppleEvents, isLoading: isSyncingApple} = useFetchAndSaveAppleEvents();
|
const {mutateAsync: fetchAndSaveAppleEvents, isLoading: isSyncingApple} = useFetchAndSaveAppleEvents();
|
||||||
|
|
||||||
WebBrowser.maybeCompleteAuthSession();
|
WebBrowser.maybeCompleteAuthSession();
|
||||||
@ -72,134 +84,106 @@ export const useCalSync = () => {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(response)
|
|
||||||
const userInfo = await userInfoResponse.json();
|
const userInfo = await userInfoResponse.json();
|
||||||
const googleMail = userInfo.email;
|
const googleMail = userInfo.email;
|
||||||
|
|
||||||
let googleAccounts = profileData?.googleAccounts || {};
|
const googleAccount: GoogleAccount = {
|
||||||
const updatedGoogleAccounts = {
|
accessToken,
|
||||||
...googleAccounts,
|
refreshToken,
|
||||||
[googleMail]: {accessToken, refreshToken},
|
email: googleMail,
|
||||||
|
expiresAt: new Date(Date.now() + 3600 * 1000),
|
||||||
|
scope: googleConfig.scopes.join(' ')
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log({refreshToken})
|
|
||||||
|
|
||||||
await updateUserData({
|
await updateUserData({
|
||||||
newUserData: {googleAccounts: updatedGoogleAccounts},
|
newUserData: {
|
||||||
|
googleAccounts: {
|
||||||
|
...profileData?.googleAccounts,
|
||||||
|
[googleMail]: googleAccount
|
||||||
|
}
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await fetchAndSaveGoogleEvents({
|
await fetchAndSaveGoogleEvents({email: googleMail});
|
||||||
token: accessToken,
|
|
||||||
refreshToken: refreshToken,
|
|
||||||
email: googleMail,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error during Google sign-in:", error);
|
console.error("Error during Google sign-in:", error);
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleMicrosoftSignIn = async () => {
|
const handleMicrosoftSignIn = async () => {
|
||||||
try {
|
try {
|
||||||
console.log("Starting Microsoft sign-in...");
|
|
||||||
|
|
||||||
const authRequest = new AuthSession.AuthRequest({
|
const authRequest = new AuthSession.AuthRequest({
|
||||||
clientId: microsoftConfig.clientId,
|
clientId: microsoftConfig.clientId,
|
||||||
scopes: microsoftConfig.scopes,
|
scopes: microsoftConfig.scopes,
|
||||||
redirectUri: microsoftConfig.redirectUri,
|
redirectUri: microsoftConfig.redirectUri,
|
||||||
responseType: AuthSession.ResponseType.Code,
|
responseType: AuthSession.ResponseType.Code,
|
||||||
usePKCE: true, // Enable PKCE
|
usePKCE: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Auth request created:", authRequest);
|
|
||||||
|
|
||||||
const authResult = await authRequest.promptAsync({
|
const authResult = await authRequest.promptAsync({
|
||||||
authorizationEndpoint: microsoftConfig.authorizationEndpoint,
|
authorizationEndpoint: microsoftConfig.authorizationEndpoint,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Auth result:", authResult);
|
|
||||||
|
|
||||||
if (authResult.type === "success" && authResult.params?.code) {
|
if (authResult.type === "success" && authResult.params?.code) {
|
||||||
const code = authResult.params.code;
|
const code = authResult.params.code;
|
||||||
console.log("Authorization code received:", code);
|
|
||||||
|
|
||||||
// Exchange authorization code for tokens
|
|
||||||
const tokenResponse = await fetch(microsoftConfig.tokenEndpoint, {
|
const tokenResponse = await fetch(microsoftConfig.tokenEndpoint, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
},
|
},
|
||||||
body: `client_id=${
|
body: `client_id=${microsoftConfig.clientId}&redirect_uri=${encodeURIComponent(
|
||||||
microsoftConfig.clientId
|
|
||||||
}&redirect_uri=${encodeURIComponent(
|
|
||||||
microsoftConfig.redirectUri
|
microsoftConfig.redirectUri
|
||||||
)}&grant_type=authorization_code&code=${code}&code_verifier=${
|
)}&grant_type=authorization_code&code=${code}&code_verifier=${
|
||||||
authRequest.codeVerifier
|
authRequest.codeVerifier
|
||||||
}&scope=${encodeURIComponent(
|
}&scope=${encodeURIComponent(microsoftConfig.scopes.join(' '))}`,
|
||||||
"https://graph.microsoft.com/Calendars.ReadWrite offline_access User.Read"
|
|
||||||
)}`,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Token response status:", tokenResponse.status);
|
|
||||||
|
|
||||||
if (!tokenResponse.ok) {
|
if (!tokenResponse.ok) {
|
||||||
const errorText = await tokenResponse.text();
|
throw new Error(await tokenResponse.text());
|
||||||
console.error("Token exchange failed:", errorText);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const tokenData = await tokenResponse.json();
|
const tokenData = await tokenResponse.json();
|
||||||
console.log("Token data received:", tokenData);
|
const userInfoResponse = await fetch(
|
||||||
|
"https://graph.microsoft.com/v1.0/me",
|
||||||
if (tokenData?.access_token) {
|
{
|
||||||
console.log("Access token received, fetching user info...");
|
headers: {
|
||||||
|
Authorization: `Bearer ${tokenData.access_token}`,
|
||||||
// Fetch user info from Microsoft Graph API to get the email
|
},
|
||||||
const userInfoResponse = await fetch(
|
|
||||||
"https://graph.microsoft.com/v1.0/me",
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${tokenData.access_token}`,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const userInfo = await userInfoResponse.json();
|
|
||||||
console.log("User info received:", userInfo);
|
|
||||||
|
|
||||||
if (userInfo.error) {
|
|
||||||
console.error("Error fetching user info:", userInfo.error);
|
|
||||||
} else {
|
|
||||||
const outlookMail = userInfo.mail || userInfo.userPrincipalName;
|
|
||||||
|
|
||||||
let microsoftAccounts = profileData?.microsoftAccounts;
|
|
||||||
const updatedMicrosoftAccounts = microsoftAccounts
|
|
||||||
? {...microsoftAccounts, [outlookMail]: tokenData.access_token}
|
|
||||||
: {[outlookMail]: tokenData.access_token};
|
|
||||||
|
|
||||||
await updateUserData({
|
|
||||||
newUserData: {microsoftAccounts: updatedMicrosoftAccounts},
|
|
||||||
});
|
|
||||||
|
|
||||||
await fetchAndSaveOutlookEvents(
|
|
||||||
tokenData.access_token,
|
|
||||||
outlookMail
|
|
||||||
);
|
|
||||||
console.log("User data updated successfully.");
|
|
||||||
}
|
}
|
||||||
}
|
);
|
||||||
} else {
|
|
||||||
console.warn("Authentication was not successful:", authResult);
|
const userInfo = await userInfoResponse.json();
|
||||||
|
const outlookMail = userInfo.mail || userInfo.userPrincipalName;
|
||||||
|
|
||||||
|
const microsoftAccount: MicrosoftAccount = {
|
||||||
|
accessToken: tokenData.access_token,
|
||||||
|
refreshToken: tokenData.refresh_token,
|
||||||
|
email: outlookMail,
|
||||||
|
expiresAt: new Date(Date.now() + tokenData.expires_in * 1000),
|
||||||
|
};
|
||||||
|
|
||||||
|
await updateUserData({
|
||||||
|
newUserData: {
|
||||||
|
microsoftAccounts: {
|
||||||
|
...profileData?.microsoftAccounts,
|
||||||
|
[outlookMail]: microsoftAccount
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetchAndSaveOutlookEvents({email: outlookMail});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error during Microsoft sign-in:", error);
|
console.error("Error during Microsoft sign-in:", error);
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleAppleSignIn = async () => {
|
const handleAppleSignIn = async () => {
|
||||||
try {
|
try {
|
||||||
console.log("Starting Apple Sign-in...");
|
|
||||||
|
|
||||||
const credential = await AppleAuthentication.signInAsync({
|
const credential = await AppleAuthentication.signInAsync({
|
||||||
requestedScopes: [
|
requestedScopes: [
|
||||||
AppleAuthentication.AppleAuthenticationScope.EMAIL,
|
AppleAuthentication.AppleAuthenticationScope.EMAIL,
|
||||||
@ -207,117 +191,124 @@ export const useCalSync = () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Apple sign-in result:", credential);
|
|
||||||
|
|
||||||
alert(JSON.stringify(credential))
|
|
||||||
|
|
||||||
const appleToken = credential.identityToken;
|
const appleToken = credential.identityToken;
|
||||||
const appleMail = credential.email!;
|
const appleMail = credential.email!;
|
||||||
|
|
||||||
|
|
||||||
if (appleToken) {
|
if (appleToken) {
|
||||||
console.log("Apple ID token received. Fetch user info if needed...");
|
const appleAccount: AppleAccount = {
|
||||||
|
accessToken: appleToken,
|
||||||
|
email: appleMail,
|
||||||
|
identityToken: credential.identityToken!,
|
||||||
|
expiresAt: new Date(Date.now() + 3600 * 1000)
|
||||||
|
};
|
||||||
|
|
||||||
let appleAcounts = profileData?.appleAccounts;
|
const updatedAppleAccounts = {
|
||||||
const updatedAppleAccounts = appleAcounts
|
...profileData?.appleAccounts,
|
||||||
? {...appleAcounts, [appleMail]: appleToken}
|
[appleMail]: appleAccount
|
||||||
: {[appleMail]: appleToken};
|
};
|
||||||
|
|
||||||
await updateUserData({
|
await updateUserData({
|
||||||
newUserData: {appleAccounts: updatedAppleAccounts},
|
newUserData: {appleAccounts: updatedAppleAccounts},
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("User data updated with Apple ID token.");
|
await fetchAndSaveAppleEvents({email: appleMail});
|
||||||
await fetchAndSaveAppleEvents({token: appleToken, email: appleMail!});
|
|
||||||
} else {
|
|
||||||
console.warn(
|
|
||||||
"Apple authentication was not successful or email was hidden."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error during Apple Sign-in:", error);
|
console.error("Error during Apple Sign-in:", error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const resyncAllCalendars = async (): Promise<void> => {
|
const resyncAllCalendars = async (): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const syncPromises: Promise<void>[] = [];
|
const results: SyncResponse[] = [];
|
||||||
|
|
||||||
if (profileData?.googleAccounts) {
|
if (profileData?.googleAccounts) {
|
||||||
console.log(profileData.googleAccounts)
|
for (const email of Object.keys(profileData.googleAccounts)) {
|
||||||
for (const [email, emailAcc] of Object.entries(profileData.googleAccounts)) {
|
try {
|
||||||
if(emailAcc?.accessToken) {
|
const result = await fetchAndSaveGoogleEvents({email});
|
||||||
syncPromises.push(fetchAndSaveGoogleEvents({ token: emailAcc?.accessToken, refreshToken: emailAcc?.refreshToken, email }));
|
results.push({
|
||||||
|
success: result.success,
|
||||||
|
eventCount: result.eventCount
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error(`Failed to sync Google calendar ${email}:`, error);
|
||||||
|
results.push({
|
||||||
|
success: false,
|
||||||
|
error: error.message
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (profileData?.microsoftAccounts) {
|
if (profileData?.microsoftAccounts) {
|
||||||
for (const [email, accessToken] of Object.entries(profileData.microsoftAccounts)) {
|
for (const email of Object.keys(profileData.microsoftAccounts)) {
|
||||||
syncPromises.push(fetchAndSaveOutlookEvents(accessToken, email));
|
try {
|
||||||
|
const result = await fetchAndSaveOutlookEvents({email});
|
||||||
|
results.push({
|
||||||
|
success: result.success,
|
||||||
|
eventCount: result.eventCount
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error(`Failed to sync Microsoft calendar ${email}:`, error);
|
||||||
|
results.push({
|
||||||
|
success: false,
|
||||||
|
error: error.message
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (profileData?.appleAccounts) {
|
if (profileData?.appleAccounts) {
|
||||||
for (const [email, token] of Object.entries(profileData.appleAccounts)) {
|
for (const email of Object.keys(profileData.appleAccounts)) {
|
||||||
syncPromises.push(fetchAndSaveAppleEvents({ token, email }));
|
try {
|
||||||
|
const result = await fetchAndSaveAppleEvents({email});
|
||||||
|
results.push({
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error(`Failed to sync Apple calendar ${email}:`, error);
|
||||||
|
results.push({
|
||||||
|
success: false,
|
||||||
|
error: error.message
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(syncPromises);
|
const successCount = results.filter(r => r.success).length;
|
||||||
console.log("All calendars have been resynced.");
|
const failCount = results.filter(r => !r.success).length;
|
||||||
|
const totalEvents = results.reduce((sum, r) => sum + (r.eventCount || 0), 0);
|
||||||
|
|
||||||
|
if (failCount > 0) {
|
||||||
|
console.error(`${failCount} calendar syncs failed, ${successCount} succeeded`);
|
||||||
|
results.filter(r => !r.success).forEach(r => {
|
||||||
|
console.error('Sync failed:', r.error);
|
||||||
|
});
|
||||||
|
} else if (successCount > 0) {
|
||||||
|
console.log(`Successfully synced ${successCount} calendars with ${totalEvents} total events`);
|
||||||
|
} else {
|
||||||
|
console.log("No calendars to sync");
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error resyncing calendars:", error);
|
console.error("Error in resyncAllCalendars:", error);
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let isConnectedToGoogle = false;
|
|
||||||
if (profileData?.googleAccounts) {
|
|
||||||
Object.values(profileData?.googleAccounts).forEach((item) => {
|
|
||||||
if (item !== null) {
|
|
||||||
isConnectedToGoogle = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let isConnectedToMicrosoft = false;
|
|
||||||
const microsoftAccounts = profileData?.microsoftAccounts;
|
|
||||||
if (microsoftAccounts) {
|
|
||||||
Object.values(profileData?.microsoftAccounts).forEach((item) => {
|
|
||||||
if (item !== null) {
|
|
||||||
isConnectedToMicrosoft = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let isConnectedToApple = false;
|
|
||||||
if (profileData?.appleAccounts) {
|
|
||||||
Object.values(profileData?.appleAccounts).forEach((item) => {
|
|
||||||
if (item !== null) {
|
|
||||||
isConnectedToApple = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const isConnectedToGoogle = Object.values(profileData?.googleAccounts || {}).some(account => !!account);
|
||||||
|
const isConnectedToMicrosoft = Object.values(profileData?.microsoftAccounts || {}).some(account => !!account);
|
||||||
|
const isConnectedToApple = Object.values(profileData?.appleAccounts || {}).some(account => !!account);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleNotification = async (notification: Notifications.Notification) => {
|
const handleNotification = async (notification: Notifications.Notification) => {
|
||||||
const eventId = notification?.request?.content?.data?.eventId;
|
|
||||||
|
|
||||||
// await resyncAllCalendars();
|
|
||||||
queryClient.invalidateQueries(["events"]);
|
queryClient.invalidateQueries(["events"]);
|
||||||
};
|
};
|
||||||
|
|
||||||
const sub = Notifications.addNotificationReceivedListener(handleNotification);
|
const sub = Notifications.addNotificationReceivedListener(handleNotification);
|
||||||
|
|
||||||
return () => sub.remove();
|
return () => sub.remove();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
handleAppleSignIn,
|
handleAppleSignIn,
|
||||||
handleMicrosoftSignIn,
|
handleMicrosoftSignIn,
|
||||||
@ -334,5 +325,5 @@ export const useCalSync = () => {
|
|||||||
isSyncingApple,
|
isSyncingApple,
|
||||||
resyncAllCalendars,
|
resyncAllCalendars,
|
||||||
isSyncing: isSyncingApple || isSyncingOutlook || isSyncingGoogle
|
isSyncing: isSyncingApple || isSyncingOutlook || isSyncingGoogle
|
||||||
}
|
};
|
||||||
}
|
};
|
@ -1,107 +1,37 @@
|
|||||||
import { useMutation, useQueryClient } from "react-query";
|
import {useMutation, useQueryClient} from "react-query";
|
||||||
import { fetchGoogleCalendarEvents } from "@/calendar-integration/google-calendar-utils";
|
import {useAuthContext} from "@/contexts/AuthContext";
|
||||||
import { useAuthContext } from "@/contexts/AuthContext";
|
import functions from "@react-native-firebase/functions";
|
||||||
import { useCreateEventsFromProvider } from "@/hooks/firebase/useCreateEvent";
|
|
||||||
import { useClearTokens } from "@/hooks/firebase/useClearTokens";
|
interface SyncResponse {
|
||||||
import { useUpdateUserData } from "@/hooks/firebase/useUpdateUserData";
|
success: boolean;
|
||||||
|
eventCount: number;
|
||||||
|
message?: string;
|
||||||
|
}
|
||||||
|
|
||||||
export const useFetchAndSaveGoogleEvents = () => {
|
export const useFetchAndSaveGoogleEvents = () => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const { profileData } = useAuthContext();
|
const { profileData } = useAuthContext();
|
||||||
const { mutateAsync: createEventsFromProvider } = useCreateEventsFromProvider();
|
|
||||||
const { mutateAsync: clearToken } = useClearTokens();
|
|
||||||
const { mutateAsync: updateUserData } = useUpdateUserData();
|
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationKey: ["fetchAndSaveGoogleEvents", "sync"],
|
mutationKey: ["fetchAndSaveGoogleEvents", "sync"],
|
||||||
mutationFn: async ({ token, email, date, refreshToken }: { token?: string; refreshToken?: string; email?: string; date?: Date }) => {
|
mutationFn: async ({ email }: { email?: string }) => {
|
||||||
const baseDate = date || new Date();
|
if (!email || !profileData?.googleAccounts?.[email]) {
|
||||||
const timeMin = new Date(baseDate.setMonth(baseDate.getMonth() - 1)).toISOString().slice(0, -5) + "Z";
|
throw new Error("No valid Google account found");
|
||||||
const timeMax = new Date(baseDate.setMonth(baseDate.getMonth() + 2)).toISOString().slice(0, -5) + "Z";
|
}
|
||||||
|
|
||||||
console.log("Token: ", token);
|
try {
|
||||||
|
const response = await functions()
|
||||||
|
.httpsCallable('triggerGoogleSync')({ email });
|
||||||
|
|
||||||
const tryFetchEvents = async (isRetry = false) => {
|
return response.data as SyncResponse;
|
||||||
try {
|
} catch (error: any) {
|
||||||
const response = await fetchGoogleCalendarEvents(
|
console.error("Error initiating Google Calendar sync:", error);
|
||||||
token,
|
throw new Error(error.details?.message || error.message || "Failed to sync calendar");
|
||||||
email,
|
}
|
||||||
profileData?.familyId,
|
|
||||||
timeMin,
|
|
||||||
timeMax
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!response.success) {
|
|
||||||
await clearToken({ email: email!, provider: "google" });
|
|
||||||
return; // Stop refetching if clearing the token
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Google Calendar events fetched:", response);
|
|
||||||
|
|
||||||
const items = response?.googleEvents?.map((item) => {
|
|
||||||
if (item.allDay) {
|
|
||||||
item.startDate = new Date(item.startDate.setHours(0, 0, 0, 0));
|
|
||||||
item.endDate = item.startDate;
|
|
||||||
}
|
|
||||||
return item;
|
|
||||||
}) || [];
|
|
||||||
|
|
||||||
await createEventsFromProvider(items);
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error fetching Google Calendar events:", error);
|
|
||||||
|
|
||||||
if (!isRetry) {
|
|
||||||
const refreshedToken = await handleRefreshToken(email, refreshToken);
|
|
||||||
if (refreshedToken) {
|
|
||||||
await updateUserData({
|
|
||||||
newUserData: {
|
|
||||||
googleAccounts: {
|
|
||||||
...profileData.googleAccounts,
|
|
||||||
[email!]: { ...profileData.googleAccounts[email!], accessToken: refreshedToken },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return tryFetchEvents(true); // Retry once after refreshing
|
|
||||||
} else {
|
|
||||||
await clearToken({ email: email!, provider: "google" });
|
|
||||||
console.error(`Token refresh failed; token cleared for ${email}`);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.error(`Retry failed after refreshing token for user ${profileData?.email}:`, error.message);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return tryFetchEvents();
|
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
onSuccess: (data) => {
|
||||||
queryClient.invalidateQueries(["events"]);
|
queryClient.invalidateQueries(["events"]);
|
||||||
},
|
console.log(`Successfully synced ${data.eventCount} events`);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
async function handleRefreshToken(email: string, refreshToken: string) {
|
|
||||||
if (!refreshToken) return null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch('https://oauth2.googleapis.com/token', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
grant_type: 'refresh_token',
|
|
||||||
refresh_token: refreshToken,
|
|
||||||
client_id: "406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
return data.access_token;
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error refreshing Google token:", error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +1,144 @@
|
|||||||
import {useMutation, useQueryClient} from "react-query";
|
import { useMutation, useQueryClient } from "react-query";
|
||||||
import {useAuthContext} from "@/contexts/AuthContext";
|
import { useAuthContext } from "@/contexts/AuthContext";
|
||||||
import {useCreateEventsFromProvider} from "@/hooks/firebase/useCreateEvent";
|
import { useSetUserData } from "@/hooks/firebase/useSetUserData";
|
||||||
import {fetchMicrosoftCalendarEvents} from "@/calendar-integration/microsoft-calendar-utils";
|
import functions from '@react-native-firebase/functions';
|
||||||
|
import * as AuthSession from 'expo-auth-session';
|
||||||
|
|
||||||
export const useFetchAndSaveOutlookEvents = () => {
|
interface SyncResponse {
|
||||||
const queryClient = useQueryClient()
|
success: boolean;
|
||||||
const {profileData} = useAuthContext();
|
eventCount: number;
|
||||||
const {mutateAsync: createEventsFromProvider} = useCreateEventsFromProvider();
|
message?: string;
|
||||||
|
}
|
||||||
|
|
||||||
return useMutation({
|
interface SyncError extends Error {
|
||||||
|
code?: string;
|
||||||
|
details?: {
|
||||||
|
requiresReauth?: boolean;
|
||||||
|
message?: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const microsoftConfig = {
|
||||||
|
clientId: "13c79071-1066-40a9-9f71-b8c4b138b4af",
|
||||||
|
scopes: [
|
||||||
|
"openid",
|
||||||
|
"profile",
|
||||||
|
"email",
|
||||||
|
"offline_access",
|
||||||
|
"Calendars.ReadWrite",
|
||||||
|
"User.Read",
|
||||||
|
],
|
||||||
|
redirectUri: AuthSession.makeRedirectUri({path: "settings"})
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useFetchAndSaveMicrosoftEvents = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const { profileData } = useAuthContext();
|
||||||
|
const { mutateAsync: setUserData } = useSetUserData();
|
||||||
|
|
||||||
|
const handleReauth = async (email: string) => {
|
||||||
|
try {
|
||||||
|
const authRequest = new AuthSession.AuthRequest({
|
||||||
|
clientId: microsoftConfig.clientId,
|
||||||
|
scopes: microsoftConfig.scopes,
|
||||||
|
redirectUri: microsoftConfig.redirectUri,
|
||||||
|
responseType: AuthSession.ResponseType.Code,
|
||||||
|
usePKCE: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await authRequest.promptAsync({
|
||||||
|
authorizationEndpoint: 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.type === 'success' && result.params?.code) {
|
||||||
|
const tokenResponse = await fetch('https://login.microsoftonline.com/common/oauth2/v2.0/token', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
},
|
||||||
|
body: new URLSearchParams({
|
||||||
|
client_id: microsoftConfig.clientId,
|
||||||
|
scope: microsoftConfig.scopes.join(' '),
|
||||||
|
code: result.params.code,
|
||||||
|
redirect_uri: microsoftConfig.redirectUri,
|
||||||
|
grant_type: 'authorization_code',
|
||||||
|
code_verifier: authRequest.codeVerifier || '',
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const tokens = await tokenResponse.json();
|
||||||
|
|
||||||
|
await setUserData({
|
||||||
|
newUserData: {
|
||||||
|
microsoftAccounts: {
|
||||||
|
...profileData?.microsoftAccounts,
|
||||||
|
[email]: {
|
||||||
|
accessToken: tokens.access_token,
|
||||||
|
refreshToken: tokens.refresh_token,
|
||||||
|
email,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Microsoft reauth error:', error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return useMutation<SyncResponse, SyncError, { email?: string }>({
|
||||||
mutationKey: ["fetchAndSaveOutlookEvents", "sync"],
|
mutationKey: ["fetchAndSaveOutlookEvents", "sync"],
|
||||||
mutationFn: async ({token, email, date}: { token?: string; email?: string, date?: Date }) => {
|
mutationFn: async ({ email }: { email?: string }) => {
|
||||||
const baseDate = date || new Date();
|
if (!email) {
|
||||||
const timeMin = new Date(new Date(baseDate).setFullYear(new Date(baseDate).getMonth() - 1));
|
throw new Error("Email is required");
|
||||||
const timeMax = new Date(new Date(baseDate).setFullYear(new Date(baseDate).getMonth() + 1));
|
}
|
||||||
|
|
||||||
console.log("Token: ", token ?? profileData?.microsoftToken);
|
if (!profileData?.microsoftAccounts?.[email]) {
|
||||||
|
throw new Error("No valid Microsoft account found");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetchMicrosoftCalendarEvents(
|
const response = await functions()
|
||||||
token ?? profileData?.microsoftToken,
|
.httpsCallable('triggerMicrosoftSync')({ email });
|
||||||
email ?? profileData?.outlookMail,
|
|
||||||
profileData?.familyId,
|
return response.data as SyncResponse;
|
||||||
timeMin.toISOString().slice(0, -5) + "Z",
|
} catch (error: any) {
|
||||||
timeMax.toISOString().slice(0, -5) + "Z"
|
console.error("Microsoft sync error:", error);
|
||||||
);
|
|
||||||
|
// Check if we need to reauthenticate
|
||||||
|
if (error.details?.requiresReauth ||
|
||||||
|
error.code === 'functions/failed-precondition' ||
|
||||||
|
error.code === 'functions/unauthenticated') {
|
||||||
|
|
||||||
|
console.log('Attempting Microsoft reauth...');
|
||||||
|
const reauthSuccessful = await handleReauth(email);
|
||||||
|
|
||||||
|
if (reauthSuccessful) {
|
||||||
|
// Retry the sync with new tokens
|
||||||
|
console.log('Retrying sync after reauth...');
|
||||||
|
const retryResponse = await functions()
|
||||||
|
.httpsCallable('triggerMicrosoftSync')({ email });
|
||||||
|
return retryResponse.data as SyncResponse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
console.log(response);
|
|
||||||
const items = response ?? [];
|
|
||||||
await createEventsFromProvider(items);
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error fetching and saving Outlook events: ", error);
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
onSuccess: (data) => {
|
||||||
queryClient.invalidateQueries(["events"])
|
queryClient.invalidateQueries(["events"]);
|
||||||
|
console.log(`Successfully synced ${data.eventCount} Microsoft events`);
|
||||||
},
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
console.error('Microsoft sync failed:', {
|
||||||
|
message: error.message,
|
||||||
|
code: error.code,
|
||||||
|
details: error.details
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
@ -3,10 +3,16 @@ import { useAuthContext } from "@/contexts/AuthContext";
|
|||||||
import { useAtomValue } from "jotai";
|
import { useAtomValue } from "jotai";
|
||||||
import { useFetchAndSaveGoogleEvents } from "./useFetchAndSaveGoogleEvents";
|
import { useFetchAndSaveGoogleEvents } from "./useFetchAndSaveGoogleEvents";
|
||||||
import { useFetchAndSaveAppleEvents } from "./useFetchAndSaveAppleEvents";
|
import { useFetchAndSaveAppleEvents } from "./useFetchAndSaveAppleEvents";
|
||||||
import { useFetchAndSaveOutlookEvents } from "./useFetchAndSaveOutlookEvents";
|
import { useFetchAndSaveMicrosoftEvents } from "./useFetchAndSaveOutlookEvents";
|
||||||
import { selectedDateAtom } from "@/components/pages/calendar/atoms";
|
import { selectedDateAtom } from "@/components/pages/calendar/atoms";
|
||||||
import { addDays, subDays, isBefore, isAfter, format } from "date-fns";
|
import { addDays, subDays, isBefore, isAfter, format } from "date-fns";
|
||||||
|
|
||||||
|
interface SyncResponse {
|
||||||
|
success: boolean;
|
||||||
|
eventCount?: number;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
export const useSyncEvents = () => {
|
export const useSyncEvents = () => {
|
||||||
const { profileData } = useAuthContext();
|
const { profileData } = useAuthContext();
|
||||||
const selectedDate = useAtomValue(selectedDateAtom);
|
const selectedDate = useAtomValue(selectedDateAtom);
|
||||||
@ -15,12 +21,18 @@ export const useSyncEvents = () => {
|
|||||||
const [lowerBoundDate, setLowerBoundDate] = useState<Date>(subDays(selectedDate, 6 * 30));
|
const [lowerBoundDate, setLowerBoundDate] = useState<Date>(subDays(selectedDate, 6 * 30));
|
||||||
const [upperBoundDate, setUpperBoundDate] = useState<Date>(addDays(selectedDate, 6 * 30));
|
const [upperBoundDate, setUpperBoundDate] = useState<Date>(addDays(selectedDate, 6 * 30));
|
||||||
const [isSyncing, setIsSyncing] = useState(false);
|
const [isSyncing, setIsSyncing] = useState(false);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState<Error | null>(null);
|
||||||
|
const [syncStats, setSyncStats] = useState<{
|
||||||
|
total: number;
|
||||||
|
success: number;
|
||||||
|
failed: number;
|
||||||
|
events: number;
|
||||||
|
}>({ total: 0, success: 0, failed: 0, events: 0 });
|
||||||
|
|
||||||
const syncedRanges = useState<Set<string>>(new Set())[0];
|
const syncedRanges = useState<Set<string>>(new Set())[0];
|
||||||
|
|
||||||
const { mutateAsync: fetchAndSaveGoogleEvents } = useFetchAndSaveGoogleEvents();
|
const { mutateAsync: fetchAndSaveGoogleEvents } = useFetchAndSaveGoogleEvents();
|
||||||
const { mutateAsync: fetchAndSaveOutlookEvents } = useFetchAndSaveOutlookEvents();
|
const { mutateAsync: fetchAndSaveOutlookEvents } = useFetchAndSaveMicrosoftEvents();
|
||||||
const { mutateAsync: fetchAndSaveAppleEvents } = useFetchAndSaveAppleEvents();
|
const { mutateAsync: fetchAndSaveAppleEvents } = useFetchAndSaveAppleEvents();
|
||||||
|
|
||||||
const generateRangeKey = (startDate: Date, endDate: Date) => {
|
const generateRangeKey = (startDate: Date, endDate: Date) => {
|
||||||
@ -41,26 +53,71 @@ export const useSyncEvents = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isBefore(selectedDate, lowerBoundDate) || isAfter(selectedDate, upperBoundDate)) {
|
if (isBefore(selectedDate, lowerBoundDate) || isAfter(selectedDate, upperBoundDate)) {
|
||||||
|
const results: SyncResponse[] = [];
|
||||||
|
const stats = { total: 0, success: 0, failed: 0, events: 0 };
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const googleEvents = Object.entries(profileData?.googleAccounts || {}).map(([email, { accessToken }]) =>
|
if (profileData?.googleAccounts) {
|
||||||
fetchAndSaveGoogleEvents({ token: accessToken, email, date: selectedDate })
|
for (const [email] of Object.entries(profileData.googleAccounts)) {
|
||||||
);
|
try {
|
||||||
|
stats.total++;
|
||||||
|
const result = await fetchAndSaveGoogleEvents({ email }) as SyncResponse;
|
||||||
|
if (result.success) {
|
||||||
|
stats.success++;
|
||||||
|
stats.events += result.eventCount || 0;
|
||||||
|
} else {
|
||||||
|
stats.failed++;
|
||||||
|
}
|
||||||
|
results.push(result);
|
||||||
|
} catch (err) {
|
||||||
|
stats.failed++;
|
||||||
|
console.error(`Failed to sync Google calendar for ${email}:`, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const outlookEvents = Object.entries(profileData?.microsoftAccounts || {}).map(([email, token]) =>
|
if (profileData?.microsoftAccounts) {
|
||||||
fetchAndSaveOutlookEvents({ token, email, date: selectedDate })
|
for (const [email] of Object.entries(profileData.microsoftAccounts)) {
|
||||||
);
|
try {
|
||||||
|
stats.total++;
|
||||||
|
const result = await fetchAndSaveOutlookEvents({ email });
|
||||||
|
if (result.success) {
|
||||||
|
stats.success++;
|
||||||
|
stats.events += result.eventCount || 0;
|
||||||
|
} else {
|
||||||
|
stats.failed++;
|
||||||
|
}
|
||||||
|
results.push(result);
|
||||||
|
} catch (err) {
|
||||||
|
stats.failed++;
|
||||||
|
console.error(`Failed to sync Microsoft calendar for ${email}:`, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const appleEvents = Object.entries(profileData?.appleAccounts || {}).map(([email, token]) =>
|
if (profileData?.appleAccounts) {
|
||||||
fetchAndSaveAppleEvents({ token, email, date: selectedDate })
|
for (const [email] of Object.entries(profileData.appleAccounts)) {
|
||||||
);
|
try {
|
||||||
|
stats.total++;
|
||||||
await Promise.all([...googleEvents, ...outlookEvents, ...appleEvents]);
|
const result = await fetchAndSaveAppleEvents({ email });
|
||||||
|
} catch (err) {
|
||||||
|
stats.failed++;
|
||||||
|
console.error(`Failed to sync Apple calendar for ${email}:`, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setSyncStats(stats);
|
||||||
setLastSyncDate(selectedDate);
|
setLastSyncDate(selectedDate);
|
||||||
setLowerBoundDate(newLowerBound);
|
setLowerBoundDate(newLowerBound);
|
||||||
setUpperBoundDate(newUpperBound);
|
setUpperBoundDate(newUpperBound);
|
||||||
syncedRanges.add(rangeKey);
|
syncedRanges.add(rangeKey);
|
||||||
} catch (err) {
|
|
||||||
|
if (stats.failed > 0) {
|
||||||
|
throw new Error(`Failed to sync ${stats.failed} calendars`);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (err: any) {
|
||||||
console.error("Error syncing events:", err);
|
console.error("Error syncing events:", err);
|
||||||
setError(err);
|
setError(err);
|
||||||
} finally {
|
} finally {
|
||||||
@ -69,7 +126,16 @@ export const useSyncEvents = () => {
|
|||||||
} else {
|
} else {
|
||||||
setIsSyncing(false);
|
setIsSyncing(false);
|
||||||
}
|
}
|
||||||
}, [selectedDate, lowerBoundDate, upperBoundDate, profileData, fetchAndSaveGoogleEvents, fetchAndSaveOutlookEvents, fetchAndSaveAppleEvents, syncedRanges]);
|
}, [
|
||||||
|
selectedDate,
|
||||||
|
lowerBoundDate,
|
||||||
|
upperBoundDate,
|
||||||
|
profileData,
|
||||||
|
fetchAndSaveGoogleEvents,
|
||||||
|
fetchAndSaveOutlookEvents,
|
||||||
|
fetchAndSaveAppleEvents,
|
||||||
|
syncedRanges
|
||||||
|
]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
syncEvents();
|
syncEvents();
|
||||||
@ -81,5 +147,6 @@ export const useSyncEvents = () => {
|
|||||||
lastSyncDate,
|
lastSyncDate,
|
||||||
lowerBoundDate,
|
lowerBoundDate,
|
||||||
upperBoundDate,
|
upperBoundDate,
|
||||||
|
syncStats,
|
||||||
};
|
};
|
||||||
};
|
};
|
Reference in New Issue
Block a user