-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzoomController.ts
More file actions
145 lines (138 loc) · 4.36 KB
/
Copy pathzoomController.ts
File metadata and controls
145 lines (138 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {Request, Response} from "express";
import dotenv from "dotenv";
dotenv.config();
// @desc Get a bearer token
// @route GET /api/zoom/token
// @access Public
async function getToken(){
try {
const credentials = btoa(`${process.env.ZOOM_CLIENT_ID}:${process.env.ZOOM_CLIENT_SECRET}`);
const token = await fetch(`https://zoom.us/oauth/token?grant_type=account_credentials&account_id=${process.env.ZOOM_ACCOUNT_ID}`, {
method: "POST",
headers: {
"Authorization": `Basic ${credentials}`,
"Content-Type": "application/x-www-form-urlencoded"
}
})
return (await token.json()).access_token
} catch (error) {
console.error(error);
return undefined
}
}
export const getMeetings = async (
req: Request,
res: Response
): Promise<void> => {
try {
await getToken().then(async (token) => {
const response = await fetch(`https://api.zoom.us/v2/users/${process.env.ZOOM_USER_ID}/meetings`, {
method: "GET",
headers: {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/x-www-form-urlencoded",
}
})
let meetings = await response.json()
res.status(200).json(meetings)
})
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Internal service error",
});
}
};
export const getWebinars = async (
req: Request,
res: Response
): Promise<void> => {
try {
await getToken().then(async (token) => {
const response = await fetch(`https://api.zoom.us/v2/users/${process.env.ZOOM_USER_ID}/webinars`, {
method: "GET",
headers: {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/x-www-form-urlencoded",
}
})
let webinars = (await response.json()).webinars
res.status(200).json({
webinars: webinars
})
})
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Internal service error",
});
}
};
export const createMeeting = async (
req: Request,
res: Response
): Promise<void> => {
const { topic, startTime, duration } = req.body;
try {
await getToken().then(async (token) => {
const response = await fetch(`https://api.zoom.us/v2/users/${process.env.ZOOM_USER_ID}/meetings`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
"Authorization": `Bearer ${token}`,
},
body: JSON.stringify({
topic,
start_time: startTime,
duration: duration
})
});
let meeting = await response.json()
console.log(meeting)
res.status(200).json({
meeting: meeting
})
})
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Internal service error",
});
}
};
export const createWebinar = async (
req: Request,
res: Response
): Promise<void> => {
const { topic, startTime, duration } = req.body;
try {
await getToken().then(async (token) => {
const response = await fetch(`https://api.zoom.us/v2/users/${process.env.ZOOM_USER_ID}/webinars`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
"Authorization": `Bearer ${token}`,
},
body: JSON.stringify({
topic,
start_time: startTime,
duration: duration
})
});
let webinar = await response.json()
console.log(webinar)
res.status(200).json({
webinar: webinar
})
})
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Internal service error",
});
}
};