fix/connected calendar list different states (#4821)

* Display error if trpc query failed, catch error on getting calendar and cleanup to don't leak credentials to client

* Add calendar connection broke error

* Edit style on alert and disconnecting button

* Ts errors and added translation

* Type fix

* Key and text change for broken calendar connection

Co-authored-by: Joe Au-Yeung <[email protected]>
Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
authored and GitHub committed 2022-10-04 14:56:38 +00:00
1 parent d43d8700d0
commit 62fea25eb9
3 files changed
+115 -33

No files matched your search

+66 -27
View File
@@ -8,6 +8,7 @@ import getApps from "@calcom/app-store/utils";
import { getUid } from "@calcom/lib/CalEventParser";
import logger from "@calcom/lib/logger";
import { performance } from "@calcom/lib/server/perfObserver";
import { App } from "@calcom/types/App";
import type { CalendarEvent, EventBusyDate, NewCalendarEventType } from "@calcom/types/Calendar";
import type { EventResult } from "@calcom/types/EventManager";
@@ -33,47 +34,85 @@ export const getConnectedCalendars = async (
) => {
const connectedCalendars = await Promise.all(
calendarCredentials.map(async (item) => {
const { calendar, integration, credential } = item;
const credentialId = credential.id;
if (!calendar) {
try {
const { calendar, integration, credential } = item;
// Don't leak credentials to the client
const credentialId = credential.id;
if (!calendar) {
return {
integration,
credentialId,
};
}
const cals = await calendar.listCalendars();
const calendars = _(cals)
.map((cal) => ({
...cal,
readOnly: cal.readOnly || false,
primary: cal.primary || null,
isSelected: selectedCalendars.some((selected) => selected.externalId === cal.externalId),
credentialId,
}))
.sortBy(["primary"])
.value();
const primary = calendars.find((item) => item.primary) ?? calendars.find((cal) => cal !== undefined);
if (!primary) {
return {
integration,
credentialId,
error: {
message: "No primary calendar found",
},
};
}
return {
integration,
integration: cleanIntegrationKeys(integration),
credentialId,
primary,
calendars,
};
}
const cals = await calendar.listCalendars();
const calendars = _(cals)
.map((cal) => ({
...cal,
readOnly: cal.readOnly || false,
primary: cal.primary || null,
isSelected: selectedCalendars.some((selected) => selected.externalId === cal.externalId),
credentialId,
}))
.sortBy(["primary"])
.value();
const primary = calendars.find((item) => item.primary) ?? calendars.find((cal) => cal !== undefined);
if (!primary) {
} catch (error) {
let errorMessage = "Could not get connected calendars";
// Here you can expect for specific errors
if (error instanceof Error) {
if (error.message === "invalid_grant") {
errorMessage = "Access token expired or revoked";
}
}
return {
integration,
credentialId,
integration: cleanIntegrationKeys(item.integration),
credentialId: item.credential.id,
error: {
message: "No primary calendar found",
message: errorMessage,
},
};
}
return {
integration,
credentialId,
primary,
calendars,
};
})
);
return connectedCalendars;
};
/**
* Important function to prevent leaking credentials to the client
* @param appIntegration
* @returns App
*/
const cleanIntegrationKeys = (
appIntegration: ReturnType<typeof getCalendarCredentials>[number]["integration"] & {
credentials?: Array<Credential>;
credential: Credential;
}
) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { credentials, credential, ...rest } = appIntegration;
return rest;
};
const CACHING_TIME = 30_000; // 30 seconds
const getCachedResults = async (