- Implemented connecting multiple accounts for microsoft

This commit is contained in:
Dejan
2024-10-20 19:16:48 +02:00
parent d6d1bcf3cf
commit be28266696
3 changed files with 90 additions and 43 deletions

View File

@ -174,9 +174,12 @@ const CalendarSettingsPage = (props: {
} else { } else {
const outlookMail = userInfo.mail || userInfo.userPrincipalName; const outlookMail = userInfo.mail || userInfo.userPrincipalName;
let microsoftAccounts = profileData?.microsoftAccounts;
const updatedMicrosoftAccounts = microsoftAccounts ? {...microsoftAccounts, [outlookMail]: tokenData.access_token} : {[outlookMail]: tokenData.access_token};
// Update user data with Microsoft token and email // Update user data with Microsoft token and email
await updateUserData({ await updateUserData({
newUserData: {microsoftToken: tokenData.access_token, outlookMail: outlookMail}, newUserData: {microsoftAccounts: updatedMicrosoftAccounts},
}); });
await fetchAndSaveOutlookEvents(tokenData.access_token, outlookMail) await fetchAndSaveOutlookEvents(tokenData.access_token, outlookMail)
@ -275,8 +278,11 @@ const CalendarSettingsPage = (props: {
newUserData.googleAccounts = googleAccounts; newUserData.googleAccounts = googleAccounts;
} }
} else if (provider === "outlook") { } else if (provider === "outlook") {
newUserData.microsoftToken = null; let microsoftAccounts = profileData?.microsoftAccounts;
newUserData.outlookMail = null; if (microsoftAccounts) {
microsoftAccounts[email] = null;
newUserData.microsoftAccounts = microsoftAccounts;
}
} else if (provider === "apple") { } else if (provider === "apple") {
newUserData.appleToken = null; newUserData.appleToken = null;
newUserData.appleMail = null; newUserData.appleMail = null;
@ -294,6 +300,17 @@ const CalendarSettingsPage = (props: {
}); });
} }
let isConnectedToMicrosoft = false;
const microsoftAccounts = profileData?.microsoftAccounts;
if (microsoftAccounts) {
Object.values(profileData?.microsoftAccounts).forEach((item) => {
if (item !== null) {
isConnectedToMicrosoft = true;
return;
}
});
}
return ( return (
<ScrollView> <ScrollView>
<View marginH-30 marginB-30> <View marginH-30 marginB-30>
@ -446,9 +463,10 @@ const CalendarSettingsPage = (props: {
color="black" color="black"
text70BL text70BL
/> />
<Button <Button
onPress={() => !profileData?.microsoftToken ? handleMicrosoftSignIn() : clearToken("outlook")} onPress={() => handleMicrosoftSignIn()}
label={profileData?.microsoftToken ? `Disconnect ${profileData.outlookMail}` : "Connect Outlook"} label={"Connect Outlook"}
labelStyle={styles.addCalLbl} labelStyle={styles.addCalLbl}
labelProps={{ labelProps={{
numberOfLines: 2 numberOfLines: 2
@ -462,8 +480,27 @@ const CalendarSettingsPage = (props: {
color="black" color="black"
text70BL text70BL
/> />
{profileData?.microsoftAccounts ? Object.keys(profileData?.microsoftAccounts)?.map((microsoftEmail) => {
const microsoftToken = profileData?.microsoftAccounts?.[microsoftEmail];
return microsoftToken && <Button
onPress={() => clearToken("outlook", microsoftEmail)}
label={`Disconnect ${microsoftEmail}`}
labelStyle={styles.addCalLbl}
labelProps={{
numberOfLines: 2
}}
iconSource={() => (
<View marginR-15>
<OutlookIcon/>
</View>
)}
style={styles.addCalBtn}
color="black"
text70BL
/>
}) : null}
{(isConnectedToGoogle || profileData?.outlookMail || profileData?.appleMail) && ( {(isConnectedToGoogle || isConnectedToMicrosoft || profileData?.appleMail) && (
<> <>
<Text style={styles.subTitle} marginT-30 marginB-20> <Text style={styles.subTitle} marginT-30 marginB-20>
Connected Calendars Connected Calendars
@ -539,21 +576,23 @@ const CalendarSettingsPage = (props: {
</TouchableOpacity> </TouchableOpacity>
)} )}
{!!profileData?.outlookMail && ( {Object.keys(profileData?.microsoftAccounts)?.map((microsoftEmail) => {
const microsoftToken = profileData?.microsoftAccounts?.[microsoftEmail];
return microsoftToken && (
<TouchableOpacity <TouchableOpacity
onPress={() => fetchAndSaveOutlookEvents({ onPress={() => fetchAndSaveOutlookEvents({
token: profileData?.microsoftToken!, token: microsoftToken,
email: profileData?.outlookMail! email: microsoftEmail
})} })}
> >
<View row paddingR-20 center> <View row paddingR-20 center>
<Button <Button
disabled={isSyncingOutlook} disabled={isSyncingOutlook}
onPress={() => fetchAndSaveOutlookEvents({ onPress={() => fetchAndSaveOutlookEvents({
token: profileData?.microsoftToken!, token: microsoftToken,
email: profileData?.outlookMail! email: microsoftEmail
})} })}
label={`Sync ${profileData?.outlookMail}`} label={`Sync ${microsoftEmail}`}
labelStyle={styles.addCalLbl} labelStyle={styles.addCalLbl}
labelProps={{numberOfLines: 3}} labelProps={{numberOfLines: 3}}
iconSource={() => ( iconSource={() => (
@ -572,7 +611,8 @@ const CalendarSettingsPage = (props: {
)} )}
</View> </View>
</TouchableOpacity> </TouchableOpacity>
)} )
})}
</View> </View>
</View> </View>
</> </>

View File

@ -204,11 +204,17 @@ exports.refreshTokens = functions.pubsub.schedule('every 12 hours').onRun(async
} }
} }
if (profileData.microsoftToken) { if (profileData.microsoftAccounts) {
try { try {
const refreshedMicrosoftToken = await refreshMicrosoftToken(profileData.microsoftToken); for (const microsoftEmail of Object.keys(profileData?.microsoftAccounts)) {
await profileDoc.ref.update({microsoftToken: refreshedMicrosoftToken}); const microsoftToken = profileData?.microsoftAccounts?.[microsoftEmail];
if (microsoftToken) {
const refreshedMicrosoftToken = await refreshMicrosoftToken(microsoftToken);
const updatedMicrosoftAccounts = {...profileData.microsoftAccounts, [microsoftEmail]: refreshedMicrosoftToken};
await profileDoc.ref.update({microsoftAccounts: updatedMicrosoftAccounts});
console.log(`Microsoft token updated for user ${profileDoc.id}`); console.log(`Microsoft token updated for user ${profileDoc.id}`);
}
}
} catch (error) { } catch (error) {
console.error(`Error refreshing Microsoft token for user ${profileDoc.id}:`, error.message); console.error(`Error refreshing Microsoft token for user ${profileDoc.id}:`, error.message);
} }

View File

@ -26,6 +26,7 @@ export interface UserProfile {
timeZone?: string | null; timeZone?: string | null;
firstDayOfWeek?: string | null; firstDayOfWeek?: string | null;
googleAccounts?: Object; googleAccounts?: Object;
microsoftAccounts?: Object;
} }
export interface ParentProfile extends UserProfile { export interface ParentProfile extends UserProfile {