|
1 |
| -import z from "zod"; |
2 |
| - |
3 |
| -import { handleErrorsJson } from "@calcom/lib/errors"; |
4 |
| -import { randomString } from "@calcom/lib/random"; |
| 1 | +import logger from "@calcom/lib/logger"; |
| 2 | +import type { CalendarEvent } from "@calcom/types/Calendar"; |
| 3 | +import type { CredentialPayload } from "@calcom/types/Credential"; |
5 | 4 | import type { PartialReference } from "@calcom/types/EventManager";
|
6 |
| -import type { VideoApiAdapter, VideoCallData } from "@calcom/types/VideoApiAdapter"; |
| 5 | +import type { VideoApiAdapter } from "@calcom/types/VideoApiAdapter"; |
| 6 | + |
| 7 | +import { getHuddle01APIKey, getHuddle01Credential } from "../utils/storage"; |
| 8 | + |
| 9 | +const API_END_POINT = "https://platform-api.huddle01.workers.dev/api/v2/calendar"; |
| 10 | + |
| 11 | +const fetchHuddleAPI = async (userId: number) => { |
| 12 | + const { identityToken } = await getHuddle01Credential(userId); |
| 13 | + const { apiKey } = await getHuddle01APIKey(); |
| 14 | + |
| 15 | + const headers = { |
| 16 | + "x-api-key": apiKey, |
| 17 | + "x-identity-token": identityToken, |
| 18 | + }; |
| 19 | + return ( |
| 20 | + endpoint: "subdomains" | "createMeeting" | "deleteMeeting" | "updateMeeting", |
| 21 | + option?: RequestInit |
| 22 | + ) => { |
| 23 | + return fetch(`${API_END_POINT}/${endpoint}`, { |
| 24 | + ...option, |
| 25 | + headers: { |
| 26 | + ...option?.headers, |
| 27 | + ...headers, |
| 28 | + }, |
| 29 | + }); |
| 30 | + }; |
| 31 | +}; |
7 | 32 |
|
8 |
| -const huddle01Schema = z.object({ url: z.string().url(), roomId: z.string() }); |
| 33 | +const log = logger.getSubLogger({ prefix: ["app-store/huddle01video/lib/VideoApiAdapter"] }); |
9 | 34 |
|
10 |
| -const Huddle01VideoApiAdapter = (): VideoApiAdapter => { |
| 35 | +const Huddle01ApiAdapter = (credential: CredentialPayload): VideoApiAdapter => { |
11 | 36 | return {
|
12 |
| - getAvailability: () => { |
13 |
| - return Promise.resolve([]); |
14 |
| - }, |
15 |
| - createMeeting: async (): Promise<VideoCallData> => { |
16 |
| - const res = await fetch( |
17 |
| - "https://wpss2zlpb9.execute-api.us-east-1.amazonaws.com/new-meeting?utmCampaign=cal.com&utmSource=partner&utmMedium=calendar" |
18 |
| - ); |
19 |
| - |
20 |
| - const json = await handleErrorsJson<{ url: string }>(res); |
21 |
| - const { url } = huddle01Schema.parse(json); |
22 |
| - if (url) { |
23 |
| - return Promise.resolve({ |
| 37 | + createMeeting: async (e: CalendarEvent) => { |
| 38 | + if (!credential.userId) { |
| 39 | + log.error("[Huddle01 Error] -> User is not logged in"); |
| 40 | + throw new Error("User is not logged in"); |
| 41 | + } |
| 42 | + |
| 43 | + try { |
| 44 | + const fetch = await fetchHuddleAPI(credential.userId); |
| 45 | + |
| 46 | + const res = await fetch("createMeeting", { |
| 47 | + method: "POST", |
| 48 | + body: JSON.stringify({ |
| 49 | + title: e.title, |
| 50 | + startTime: e.startTime, |
| 51 | + endTime: e.endTime, |
| 52 | + }), |
| 53 | + headers: { |
| 54 | + "Content-Type": "application/json", |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + const data = (await res.json()) as { |
| 59 | + roomId: string; |
| 60 | + meetingLink: string; |
| 61 | + }; |
| 62 | + |
| 63 | + return { |
24 | 64 | type: "huddle01_video",
|
25 |
| - id: randomString(21), |
| 65 | + id: data.roomId, |
26 | 66 | password: "",
|
27 |
| - url, |
28 |
| - }); |
| 67 | + url: data.meetingLink, |
| 68 | + }; |
| 69 | + } catch (e) { |
| 70 | + log.error("[Huddle01 Error] -> Error while creating meeeting", e); |
| 71 | + throw Error("Error while creating meeting"); |
29 | 72 | }
|
30 |
| - return Promise.reject("Url was not received in response body."); |
31 | 73 | },
|
32 |
| - deleteMeeting: async (): Promise<void> => { |
33 |
| - Promise.resolve(); |
34 |
| - }, |
35 |
| - updateMeeting: (bookingRef: PartialReference): Promise<VideoCallData> => { |
36 |
| - return Promise.resolve({ |
| 74 | + updateMeeting: async (bookingRef: PartialReference, e: CalendarEvent) => { |
| 75 | + if (!credential.userId) { |
| 76 | + log.error("[Huddle01 Error] -> User is not logged in"); |
| 77 | + throw new Error("User is not logged in"); |
| 78 | + } |
| 79 | + |
| 80 | + const fetch = await fetchHuddleAPI(credential.userId); |
| 81 | + |
| 82 | + const res = await fetch("updateMeeting", { |
| 83 | + method: "PUT", |
| 84 | + body: JSON.stringify({ |
| 85 | + title: e.title, |
| 86 | + startTime: e.startTime, |
| 87 | + endTime: e.endTime, |
| 88 | + meetingId: bookingRef.uid, |
| 89 | + }), |
| 90 | + headers: { |
| 91 | + "Content-Type": "application/json", |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + const data = (await res.json()) as { |
| 96 | + roomId: string; |
| 97 | + meetingLink: string; |
| 98 | + }; |
| 99 | + |
| 100 | + return { |
37 | 101 | type: "huddle01_video",
|
38 |
| - id: bookingRef.meetingId as string, |
39 |
| - password: bookingRef.meetingPassword as string, |
40 |
| - url: bookingRef.meetingUrl as string, |
| 102 | + id: data.roomId, |
| 103 | + password: "", |
| 104 | + url: data.meetingLink, |
| 105 | + }; |
| 106 | + }, |
| 107 | + deleteMeeting: async (meetingId: string) => { |
| 108 | + if (!credential.userId) { |
| 109 | + log.error("[Huddle01 Error] -> User is not logged in"); |
| 110 | + throw new Error("User is not logged in"); |
| 111 | + } |
| 112 | + |
| 113 | + const fetch = await fetchHuddleAPI(credential.userId); |
| 114 | + |
| 115 | + const res = await fetch("deleteMeeting", { |
| 116 | + method: "DELETE", |
| 117 | + body: JSON.stringify({ |
| 118 | + meetingId, |
| 119 | + }), |
| 120 | + headers: { |
| 121 | + "Content-Type": "application/json", |
| 122 | + }, |
41 | 123 | });
|
| 124 | + |
| 125 | + const data = (await res.json()) as { |
| 126 | + roomId: string; |
| 127 | + meetingLink: string; |
| 128 | + }; |
| 129 | + |
| 130 | + return { |
| 131 | + type: "huddle01_video", |
| 132 | + id: data.roomId, |
| 133 | + password: "", |
| 134 | + url: data.meetingLink, |
| 135 | + }; |
| 136 | + }, |
| 137 | + getAvailability: async () => { |
| 138 | + return []; |
42 | 139 | },
|
43 | 140 | };
|
44 | 141 | };
|
45 | 142 |
|
46 |
| -export default Huddle01VideoApiAdapter; |
| 143 | +export default Huddle01ApiAdapter; |
0 commit comments