refactor: platform managed user org admin access (#22597)

* refactor: examples app setup

* wip: view org event types as org administrator

* fix: create event type has org admin

* fix: create event type has org admin

* fix: allow org admin team event type access

* fix: uncaught error

* chore: bump platform libs

* refactor: getting event type for org admin

* add test

* chore: libraries
This commit is contained in:
Lauris Skraucis authored and GitHub committed 2025-07-28 13:50:12 +02:00
1 parent 30061c0d58
commit 19d7764e9d
15 files changed
+863 -234

No files matched your search

@@ -12,6 +12,49 @@ type Data = {
accessToken: string;
};
async function createUserWithDefaultSchedule(email: string, name: string, avatarUrl: string) {
const localUser = await prisma.user.create({
data: {
email,
},
});
const managedUserResponse = await fetch(
// eslint-disable-next-line turbo/no-undeclared-env-vars
`${process.env.NEXT_PUBLIC_CALCOM_API_URL ?? ""}/oauth-clients/${process.env.NEXT_PUBLIC_X_CAL_ID}/users`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
// eslint-disable-next-line turbo/no-undeclared-env-vars
[X_CAL_SECRET_KEY]: process.env.X_CAL_SECRET_KEY ?? "",
origin: "http://localhost:4321",
},
body: JSON.stringify({
email,
name,
avatarUrl,
}),
}
);
const managedUserResponseBody = await managedUserResponse.json();
await prisma.user.update({
data: {
refreshToken: (managedUserResponseBody.data?.refreshToken as string) ?? "",
accessToken: (managedUserResponseBody.data?.accessToken as string) ?? "",
calcomUserId: managedUserResponseBody.data?.user.id,
calcomUsername: (managedUserResponseBody.data?.user.username as string) ?? "",
},
where: { id: localUser.id },
});
await createDefaultSchedule(managedUserResponseBody.data?.accessToken as string);
return managedUserResponseBody.data;
}
// example endpoint to create a managed cal.com user
export default async function handler(req: NextApiRequest, res: NextApiResponse<Data>) {
const { emails } = JSON.parse(req.body);
@@ -31,195 +74,31 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
});
}
const localUserOne = await prisma.user.create({
data: {
email: emailOne,
},
});
const localUserTwo = await prisma.user.create({
data: {
email: emailTwo,
},
});
const localUserThree = await prisma.user.create({
data: {
email: emailThree,
},
});
const localUserFour = await prisma.user.create({
data: {
email: emailFour,
},
});
const localUserFive = await prisma.user.create({
data: {
email: emailFive,
},
});
const response = await fetch(
// eslint-disable-next-line turbo/no-undeclared-env-vars
`${process.env.NEXT_PUBLIC_CALCOM_API_URL ?? ""}/oauth-clients/${process.env.NEXT_PUBLIC_X_CAL_ID}/users`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
// eslint-disable-next-line turbo/no-undeclared-env-vars
[X_CAL_SECRET_KEY]: process.env.X_CAL_SECRET_KEY ?? "",
origin: "http://localhost:4321",
},
body: JSON.stringify({
email: emailOne,
name: "John Jones",
avatarUrl:
"https://images.unsplash.com/photo-1527980965255-d3b416303d12?q=80&w=3023&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
}),
}
const managedUserResponseOne = await createUserWithDefaultSchedule(
emailOne,
"Keith",
"https://images.unsplash.com/photo-1527980965255-d3b416303d12?q=80&w=3023&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
);
const body = await response.json();
await prisma.user.update({
data: {
refreshToken: (body.data?.refreshToken as string) ?? "",
accessToken: (body.data?.accessToken as string) ?? "",
calcomUserId: body.data?.user.id,
calcomUsername: (body.data?.user.username as string) ?? "",
},
where: { id: localUserOne.id },
});
const responseTwo = await fetch(
// eslint-disable-next-line turbo/no-undeclared-env-vars
`${process.env.NEXT_PUBLIC_CALCOM_API_URL ?? ""}/oauth-clients/${process.env.NEXT_PUBLIC_X_CAL_ID}/users`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
// eslint-disable-next-line turbo/no-undeclared-env-vars
[X_CAL_SECRET_KEY]: process.env.X_CAL_SECRET_KEY ?? "",
origin: "http://localhost:4321",
},
body: JSON.stringify({
email: emailTwo,
name: "Jane Doe",
avatarUrl:
"https://plus.unsplash.com/premium_photo-1668319915476-5cc7717e00f1?q=80&w=3164&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
}),
}
const managedUserResponseTwo = await createUserWithDefaultSchedule(
emailTwo,
"Somay",
"https://plus.unsplash.com/premium_photo-1668319915476-5cc7717e00f1?q=80&w=3164&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
);
const bodyTwo = await responseTwo.json();
await prisma.user.update({
data: {
refreshToken: (bodyTwo.data?.refreshToken as string) ?? "",
accessToken: (bodyTwo.data?.accessToken as string) ?? "",
calcomUserId: bodyTwo.data?.user.id,
calcomUsername: (bodyTwo.data?.user.username as string) ?? "",
},
where: { id: localUserTwo.id },
});
const responseThree = await fetch(
// eslint-disable-next-line turbo/no-undeclared-env-vars
`${process.env.NEXT_PUBLIC_CALCOM_API_URL ?? ""}/oauth-clients/${process.env.NEXT_PUBLIC_X_CAL_ID}/users`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
// eslint-disable-next-line turbo/no-undeclared-env-vars
[X_CAL_SECRET_KEY]: process.env.X_CAL_SECRET_KEY ?? "",
origin: "http://localhost:4321",
},
body: JSON.stringify({
email: emailThree,
name: "Rajiv",
avatarUrl:
"https://plus.unsplash.com/premium_photo-1668319915476-5cc7717e00f1?q=80&w=3164&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
}),
}
const managedUserResponseThree = await createUserWithDefaultSchedule(
emailThree,
"Rajiv",
"https://plus.unsplash.com/premium_photo-1668319915476-5cc7717e00f1?q=80&w=3164&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
);
const bodyThree = await responseThree.json();
await prisma.user.update({
data: {
refreshToken: (bodyThree.data?.refreshToken as string) ?? "",
accessToken: (bodyThree.data?.accessToken as string) ?? "",
calcomUserId: bodyThree.data?.user.id,
calcomUsername: (bodyThree.data?.user.username as string) ?? "",
},
where: { id: localUserThree.id },
});
const responseFour = await fetch(
// eslint-disable-next-line turbo/no-undeclared-env-vars
`${process.env.NEXT_PUBLIC_CALCOM_API_URL ?? ""}/oauth-clients/${process.env.NEXT_PUBLIC_X_CAL_ID}/users`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
// eslint-disable-next-line turbo/no-undeclared-env-vars
[X_CAL_SECRET_KEY]: process.env.X_CAL_SECRET_KEY ?? "",
origin: "http://localhost:4321",
},
body: JSON.stringify({
email: emailFour,
name: "Morgan",
avatarUrl:
"https://plus.unsplash.com/premium_photo-1668319915476-5cc7717e00f1?q=80&w=3164&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
}),
}
const managedUserResponseFour = await createUserWithDefaultSchedule(
emailFour,
"Morgan",
"https://plus.unsplash.com/premium_photo-1668319915476-5cc7717e00f1?q=80&w=3164&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
);
const bodyFour = await responseFour.json();
await prisma.user.update({
data: {
refreshToken: (bodyFour.data?.refreshToken as string) ?? "",
accessToken: (bodyFour.data?.accessToken as string) ?? "",
calcomUserId: bodyFour.data?.user.id,
calcomUsername: (bodyFour.data?.user.username as string) ?? "",
},
where: { id: localUserFour.id },
});
const responseFive = await fetch(
// eslint-disable-next-line turbo/no-undeclared-env-vars
`${process.env.NEXT_PUBLIC_CALCOM_API_URL ?? ""}/oauth-clients/${process.env.NEXT_PUBLIC_X_CAL_ID}/users`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
// eslint-disable-next-line turbo/no-undeclared-env-vars
[X_CAL_SECRET_KEY]: process.env.X_CAL_SECRET_KEY ?? "",
origin: "http://localhost:4321",
},
body: JSON.stringify({
email: emailFive,
name: "Lauris",
avatarUrl:
"https://plus.unsplash.com/premium_photo-1668319915476-5cc7717e00f1?q=80&w=3164&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
}),
}
const managedUserResponseFive = await createUserWithDefaultSchedule(
emailFive,
"Lauris",
"https://plus.unsplash.com/premium_photo-1668319915476-5cc7717e00f1?q=80&w=3164&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
);
const bodyFive = await responseFive.json();
await prisma.user.update({
data: {
refreshToken: (bodyFive.data?.refreshToken as string) ?? "",
accessToken: (bodyFive.data?.accessToken as string) ?? "",
calcomUserId: bodyFive.data?.user.id,
calcomUsername: (bodyFive.data?.user.username as string) ?? "",
},
where: { id: localUserFive.id },
});
await createDefaultSchedule(body.data?.accessToken as string);
await createDefaultSchedule(bodyTwo.data?.accessToken as string);
await createDefaultSchedule(bodyThree.data?.accessToken as string);
await createDefaultSchedule(bodyFour.data?.accessToken as string);
await createDefaultSchedule(bodyFive.data?.accessToken as string);
// eslint-disable-next-line turbo/no-undeclared-env-vars
const organizationId = process.env.ORGANIZATION_ID;
@@ -227,27 +106,37 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
throw new Error("Organization ID is not set");
}
const team = await createTeam(+organizationId, "Team Doe");
const team = await createTeam(+organizationId, `Platform devs - ${Date.now()}`);
if (!team) {
throw new Error("Failed to create team. Probably your platform team does not have required plan.");
}
await createMembership(+organizationId, team.id, body.data?.user.id);
await createMembership(+organizationId, team.id, bodyTwo.data?.user.id);
await createMembership(+organizationId, team.id, bodyThree.data?.user.id);
await createOrgTeamMembershipMember(+organizationId, team.id, managedUserResponseOne.user.id);
await createOrgTeamMembershipMember(+organizationId, team.id, managedUserResponseTwo.user.id);
await createOrgTeamMembershipMember(+organizationId, team.id, managedUserResponseThree.user.id);
await createOrgTeamMembershipMember(+organizationId, team.id, managedUserResponseFour.user.id);
await createCollectiveEventType(+organizationId, team.id, [
body.data?.user.id,
bodyTwo.data?.user.id,
bodyThree.data?.user.id,
bodyFour.data?.user.id,
bodyFive.data?.user.id,
managedUserResponseOne.user.id,
managedUserResponseTwo.user.id,
managedUserResponseThree.user.id,
managedUserResponseFour.user.id,
]);
await createRoundRobinEventType(+organizationId, team.id, [
managedUserResponseOne.user.id,
managedUserResponseTwo.user.id,
managedUserResponseThree.user.id,
managedUserResponseFour.user.id,
]);
await createOrgMembershipAdmin(+organizationId, managedUserResponseFive.user.id);
return res.status(200).json({
id: body?.data?.user?.id,
email: (body.data?.user.email as string) ?? "",
username: (body.data?.username as string) ?? "",
accessToken: (body.data?.accessToken as string) ?? "",
id: managedUserResponseOne?.user?.id,
email: (managedUserResponseOne.user.email as string) ?? "",
username: (managedUserResponseOne.user.username as string) ?? "",
accessToken: (managedUserResponseOne.accessToken as string) ?? "",
});
}
@@ -276,10 +165,33 @@ async function createTeam(orgId: number, name: string) {
return body.data;
}
async function createMembership(orgId: number, teamId: number, userId: number) {
async function createOrgTeamMembershipMember(orgId: number, teamId: number, userId: number) {
await fetch(
// eslint-disable-next-line turbo/no-undeclared-env-vars
`${process.env.NEXT_PUBLIC_CALCOM_API_URL ?? ""}/organizations/${orgId}/teams/${teamId}/memberships`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
// eslint-disable-next-line turbo/no-undeclared-env-vars
[X_CAL_SECRET_KEY]: process.env.X_CAL_SECRET_KEY ?? "",
// eslint-disable-next-line turbo/no-undeclared-env-vars
[X_CAL_CLIENT_ID]: process.env.NEXT_PUBLIC_X_CAL_ID ?? "",
origin: "http://localhost:4321",
},
body: JSON.stringify({
userId,
accepted: true,
role: "MEMBER",
}),
}
);
}
async function createOrgMembershipAdmin(orgId: number, userId: number) {
await fetch(
// eslint-disable-next-line turbo/no-undeclared-env-vars
`${process.env.NEXT_PUBLIC_CALCOM_API_URL ?? ""}/organizations/${orgId}/memberships`,
{
method: "POST",
headers: {
@@ -315,9 +227,34 @@ async function createCollectiveEventType(orgId: number, teamId: number, userIds:
},
body: JSON.stringify({
lengthInMinutes: 60,
title: "Doe collective",
slug: "doe-collective",
schedulingType: "COLLECTIVE",
title: "Platform example collective",
slug: "platform-example-collective",
schedulingType: "collective",
hosts: userIds.map((userId) => ({ userId })),
}),
}
);
}
async function createRoundRobinEventType(orgId: number, teamId: number, userIds: number[]) {
await fetch(
// eslint-disable-next-line turbo/no-undeclared-env-vars
`${process.env.NEXT_PUBLIC_CALCOM_API_URL ?? ""}/organizations/${orgId}/teams/${teamId}/event-types`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
// eslint-disable-next-line turbo/no-undeclared-env-vars
[X_CAL_SECRET_KEY]: process.env.X_CAL_SECRET_KEY ?? "",
// eslint-disable-next-line turbo/no-undeclared-env-vars
[X_CAL_CLIENT_ID]: process.env.NEXT_PUBLIC_X_CAL_ID ?? "",
origin: "http://localhost:4321",
},
body: JSON.stringify({
lengthInMinutes: 60,
title: "Platform example round robin",
slug: "platform-example-round-robin",
schedulingType: "roundRobin",
hosts: userIds.map((userId) => ({ userId })),
}),
}