2023-02-16 22:39:57 +00:00
|
|
|
import type { Prisma } from "@prisma/client";
|
2022-03-23 22:00:30 +00:00
|
|
|
|
|
|
|
import { handleErrorsJson, handleErrorsRaw } from "@calcom/lib/errors";
|
2022-05-02 23:13:34 +00:00
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
2022-03-23 22:00:30 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { CredentialPayload } from "@calcom/types/Credential";
|
2022-03-23 22:00:30 +00:00
|
|
|
import type { PartialReference } from "@calcom/types/EventManager";
|
|
|
|
import type { VideoApiAdapter, VideoCallData } from "@calcom/types/VideoApiAdapter";
|
|
|
|
|
2022-05-02 23:13:34 +00:00
|
|
|
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
|
|
|
|
|
|
|
let client_id = "";
|
|
|
|
let client_secret = "";
|
2022-03-23 22:00:30 +00:00
|
|
|
|
|
|
|
/** @link https://docs.microsoft.com/en-us/graph/api/application-post-onlinemeetings?view=graph-rest-1.0&tabs=http#response */
|
|
|
|
export interface TeamsEventResult {
|
|
|
|
creationDateTime: string;
|
|
|
|
startDateTime: string;
|
|
|
|
endDateTime: string;
|
|
|
|
id: string;
|
|
|
|
joinWebUrl: string;
|
|
|
|
subject: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface O365AuthCredentials {
|
|
|
|
email: string;
|
|
|
|
scope: string;
|
|
|
|
token_type: string;
|
|
|
|
expiry_date: number;
|
|
|
|
access_token: string;
|
|
|
|
refresh_token: string;
|
|
|
|
ext_expires_in: number;
|
|
|
|
}
|
|
|
|
|
2022-10-31 22:06:03 +00:00
|
|
|
interface ITokenResponse {
|
|
|
|
expiry_date: number;
|
|
|
|
expires_in?: number;
|
|
|
|
token_type: string;
|
|
|
|
scope: string;
|
|
|
|
access_token: string;
|
|
|
|
refresh_token: string;
|
|
|
|
error?: string;
|
|
|
|
error_description?: string;
|
|
|
|
}
|
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
// Checks to see if our O365 user token is valid or if we need to refresh
|
2022-10-31 22:06:03 +00:00
|
|
|
const o365Auth = async (credential: CredentialPayload) => {
|
2022-05-02 23:13:34 +00:00
|
|
|
const appKeys = await getAppKeysFromSlug("msteams");
|
|
|
|
if (typeof appKeys.client_id === "string") client_id = appKeys.client_id;
|
|
|
|
if (typeof appKeys.client_secret === "string") client_secret = appKeys.client_secret;
|
|
|
|
if (!client_id) throw new HttpError({ statusCode: 400, message: "MS teams client_id missing." });
|
|
|
|
if (!client_secret) throw new HttpError({ statusCode: 400, message: "MS teams client_secret missing." });
|
|
|
|
|
2023-01-23 22:36:24 +00:00
|
|
|
const isExpired = (expiryDate: number) => expiryDate < Math.round(+new Date());
|
2022-03-23 22:00:30 +00:00
|
|
|
|
|
|
|
const o365AuthCredentials = credential.key as unknown as O365AuthCredentials;
|
|
|
|
|
2022-05-02 23:13:34 +00:00
|
|
|
const refreshAccessToken = async (refreshToken: string) => {
|
|
|
|
const response = await fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token", {
|
2022-03-23 22:00:30 +00:00
|
|
|
method: "POST",
|
|
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
|
|
body: new URLSearchParams({
|
2022-05-02 23:13:34 +00:00
|
|
|
client_id,
|
2022-03-23 22:00:30 +00:00
|
|
|
refresh_token: refreshToken,
|
|
|
|
grant_type: "refresh_token",
|
2022-05-02 23:13:34 +00:00
|
|
|
client_secret,
|
2022-03-23 22:00:30 +00:00
|
|
|
}),
|
2022-05-02 23:13:34 +00:00
|
|
|
});
|
2022-10-31 22:06:03 +00:00
|
|
|
|
|
|
|
const responseBody = await handleErrorsJson<ITokenResponse>(response);
|
2023-01-23 22:36:24 +00:00
|
|
|
|
2022-10-31 22:06:03 +00:00
|
|
|
if (responseBody?.error) {
|
2022-10-07 17:59:58 +00:00
|
|
|
console.error(responseBody);
|
2023-01-23 22:36:24 +00:00
|
|
|
throw new HttpError({ statusCode: 500, message: `Error contacting MS Teams: ${responseBody.error}` });
|
2022-10-07 17:59:58 +00:00
|
|
|
}
|
2022-05-02 23:13:34 +00:00
|
|
|
// set expiry date as offset from current time.
|
2022-10-31 22:06:03 +00:00
|
|
|
responseBody.expiry_date = Math.round(Date.now() + (responseBody?.expires_in || 0) * 1000);
|
2022-05-02 23:13:34 +00:00
|
|
|
delete responseBody.expires_in;
|
|
|
|
// Store new tokens in database.
|
|
|
|
await prisma.credential.update({
|
|
|
|
where: {
|
|
|
|
id: credential.id,
|
|
|
|
},
|
|
|
|
data: {
|
2022-10-31 22:06:03 +00:00
|
|
|
// @NOTE: prisma doesn't know key its a JSON so do as responseBody
|
|
|
|
key: responseBody as unknown as Prisma.InputJsonValue,
|
2022-05-02 23:13:34 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
o365AuthCredentials.expiry_date = responseBody.expiry_date;
|
|
|
|
o365AuthCredentials.access_token = responseBody.access_token;
|
|
|
|
return o365AuthCredentials.access_token;
|
2022-03-23 22:00:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
getToken: () =>
|
2023-01-23 22:36:24 +00:00
|
|
|
isExpired(o365AuthCredentials.expiry_date)
|
|
|
|
? refreshAccessToken(o365AuthCredentials.refresh_token)
|
|
|
|
: Promise.resolve(o365AuthCredentials.access_token),
|
2022-03-23 22:00:30 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-31 22:06:03 +00:00
|
|
|
const TeamsVideoApiAdapter = (credential: CredentialPayload): VideoApiAdapter => {
|
2022-03-23 22:00:30 +00:00
|
|
|
const auth = o365Auth(credential);
|
|
|
|
|
|
|
|
const translateEvent = (event: CalendarEvent) => {
|
|
|
|
return {
|
|
|
|
startDateTime: event.startTime,
|
|
|
|
endDateTime: event.endTime,
|
|
|
|
subject: event.title,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Since the meeting link is not tied to an event we only need the create and update functions
|
|
|
|
return {
|
|
|
|
getAvailability: () => {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
},
|
|
|
|
updateMeeting: async (bookingRef: PartialReference, event: CalendarEvent) => {
|
2022-05-02 23:13:34 +00:00
|
|
|
const accessToken = await (await auth).getToken();
|
2022-03-23 22:00:30 +00:00
|
|
|
|
|
|
|
const resultString = await fetch("https://graph.microsoft.com/v1.0/me/onlineMeetings", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(translateEvent(event)),
|
|
|
|
}).then(handleErrorsRaw);
|
|
|
|
|
|
|
|
const resultObject = JSON.parse(resultString);
|
|
|
|
|
|
|
|
return Promise.resolve({
|
|
|
|
type: "office365_video",
|
|
|
|
id: resultObject.id,
|
|
|
|
password: "",
|
|
|
|
url: resultObject.joinUrl,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
deleteMeeting: () => {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
},
|
|
|
|
createMeeting: async (event: CalendarEvent): Promise<VideoCallData> => {
|
2022-05-02 23:13:34 +00:00
|
|
|
const accessToken = await (await auth).getToken();
|
2022-03-23 22:00:30 +00:00
|
|
|
|
|
|
|
const resultString = await fetch("https://graph.microsoft.com/v1.0/me/onlineMeetings", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(translateEvent(event)),
|
|
|
|
}).then(handleErrorsRaw);
|
|
|
|
|
|
|
|
const resultObject = JSON.parse(resultString);
|
|
|
|
|
2022-12-07 19:56:15 +00:00
|
|
|
if (!resultObject.id || !resultObject.joinUrl || !resultObject.joinWebUrl) {
|
2023-01-12 21:43:40 +00:00
|
|
|
throw new HttpError({
|
|
|
|
statusCode: 500,
|
|
|
|
message: `Error creating MS Teams meeting: ${resultObject.error.message}`,
|
|
|
|
});
|
2022-11-18 17:41:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
return Promise.resolve({
|
|
|
|
type: "office365_video",
|
|
|
|
id: resultObject.id,
|
|
|
|
password: "",
|
2022-12-07 19:56:15 +00:00
|
|
|
url: resultObject.joinWebUrl || resultObject.joinUrl,
|
2022-03-23 22:00:30 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TeamsVideoApiAdapter;
|