forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calendly & SavvyCal import (calcom#1512)
* Calendly & SavvyCal import * added string keys to import * Update pages/api/import/savvycal.ts Co-authored-by: Omar López <[email protected]> * Update pages/api/import/savvycal.ts Co-authored-by: Omar López <[email protected]> * Update pages/getting-started.tsx Co-authored-by: Omar López <[email protected]> * fixed string * prettier Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Omar López <[email protected]> Co-authored-by: Peer Richelsen <[email protected]>
- Loading branch information
1 parent
b5569c6
commit 3369419
Showing
4 changed files
with
274 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { getSession } from "@lib/auth"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const session = await getSession({ req }); | ||
const authenticatedUser = await prisma.user.findFirst({ | ||
rejectOnNotFound: true, | ||
where: { | ||
id: session?.user.id, | ||
}, | ||
select: { | ||
id: true, | ||
}, | ||
}); | ||
if (req.method == "POST") { | ||
const userResult = await fetch("https://api.calendly.com/users/me", { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: "Bearer " + req.body.token, | ||
}, | ||
}); | ||
|
||
if (userResult.status == 200) { | ||
const userData = await userResult.json(); | ||
|
||
await prisma.user.update({ | ||
where: { | ||
id: authenticatedUser.id, | ||
}, | ||
data: { | ||
name: userData.resource.name, | ||
}, | ||
}); | ||
|
||
const eventTypesResult = await fetch( | ||
"https://api.calendly.com/event_types?user=" + userData.resource.uri, | ||
{ | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: "Bearer " + req.body.token, | ||
}, | ||
} | ||
); | ||
|
||
const eventTypesData = await eventTypesResult.json(); | ||
|
||
eventTypesData.collection.forEach(async (eventType: any) => { | ||
await prisma.eventType.create({ | ||
data: { | ||
title: eventType.name, | ||
slug: eventType.slug, | ||
length: eventType.duration, | ||
description: eventType.description_plain, | ||
hidden: eventType.secret, | ||
users: { | ||
connect: { | ||
id: authenticatedUser.id, | ||
}, | ||
}, | ||
userId: authenticatedUser.id, | ||
}, | ||
}); | ||
}); | ||
|
||
res.status(201).end(); | ||
} else { | ||
res.status(500).end(); | ||
} | ||
} else { | ||
res.status(405).end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { getSession } from "@lib/auth"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const session = await getSession({ req }); | ||
const authenticatedUser = await prisma.user.findFirst({ | ||
rejectOnNotFound: true, | ||
where: { | ||
id: session?.user.id, | ||
}, | ||
select: { | ||
id: true, | ||
}, | ||
}); | ||
if (req.method === "POST") { | ||
const userResult = await fetch("https://api.savvycal.com/v1/me", { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: "Bearer " + req.body.token, | ||
}, | ||
}); | ||
|
||
if (userResult.status === 200) { | ||
const userData = await userResult.json(); | ||
|
||
await prisma.user.update({ | ||
where: { | ||
id: authenticatedUser.id, | ||
}, | ||
data: { | ||
name: userData.display_name, | ||
timeZone: userData.time_zone, | ||
weekStart: userData.first_day_of_week === 0 ? "Sunday" : "Monday", | ||
avatar: userData.avatar_url, | ||
}, | ||
}); | ||
|
||
const eventTypesResult = await fetch("https://api.savvycal.com/v1/links?limit=100", { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: "Bearer " + req.body.token, | ||
}, | ||
}); | ||
|
||
const eventTypesData = await eventTypesResult.json(); | ||
|
||
eventTypesData.entries.forEach(async (eventType: any) => { | ||
await prisma.eventType.create({ | ||
data: { | ||
title: eventType.name, | ||
slug: eventType.slug, | ||
length: eventType.durations[0], | ||
description: eventType.description.replace(/<[^>]*>?/gm, ""), | ||
hidden: eventType.state === "active" ? true : false, | ||
users: { | ||
connect: { | ||
id: authenticatedUser.id, | ||
}, | ||
}, | ||
userId: authenticatedUser.id, | ||
}, | ||
}); | ||
}); | ||
|
||
res.status(201).end(); | ||
} else { | ||
res.status(500).end(); | ||
} | ||
} else { | ||
res.status(405).end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters