first commit
This commit is contained in:
8
calcom/packages/app-store/intercom/DESCRIPTION.md
Normal file
8
calcom/packages/app-store/intercom/DESCRIPTION.md
Normal file
@@ -0,0 +1,8 @@
|
||||
---
|
||||
items:
|
||||
- 1.png
|
||||
- 2.png
|
||||
- 3.png
|
||||
---
|
||||
|
||||
{DESCRIPTION}
|
||||
30
calcom/packages/app-store/intercom/api/add.ts
Normal file
30
calcom/packages/app-store/intercom/api/add.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { stringify } from "querystring";
|
||||
|
||||
import { WEBAPP_URL_FOR_OAUTH } from "@calcom/lib/constants";
|
||||
|
||||
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
||||
import { encodeOAuthState } from "../../_utils/oauth/encodeOAuthState";
|
||||
|
||||
let client_id = "";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "GET") {
|
||||
const appKeys = await getAppKeysFromSlug("intercom");
|
||||
if (typeof appKeys.client_id === "string") client_id = appKeys.client_id;
|
||||
if (!client_id) return res.status(400).json({ message: "Intercom client_id missing." });
|
||||
|
||||
const state = encodeOAuthState(req);
|
||||
|
||||
const params = {
|
||||
client_id,
|
||||
redirect_uri: `${WEBAPP_URL_FOR_OAUTH}/api/integrations/intercom/callback`,
|
||||
state,
|
||||
response_type: "code",
|
||||
};
|
||||
|
||||
const authUrl = `https://app.intercom.com/oauth?${stringify(params)}`;
|
||||
|
||||
res.status(200).json({ url: authUrl });
|
||||
}
|
||||
}
|
||||
95
calcom/packages/app-store/intercom/api/callback.ts
Normal file
95
calcom/packages/app-store/intercom/api/callback.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
||||
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
||||
import createOAuthAppCredential from "../../_utils/oauth/createOAuthAppCredential";
|
||||
import { decodeOAuthState } from "../../_utils/oauth/decodeOAuthState";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: [`[[intercom/api/callback]`] });
|
||||
|
||||
let client_id = "";
|
||||
let client_secret = "";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { code } = req.query;
|
||||
|
||||
if (code && typeof code !== "string") {
|
||||
res.status(400).json({ message: "`code` must be a string" });
|
||||
return;
|
||||
}
|
||||
if (!req.session?.user?.id) {
|
||||
return res.status(401).json({ message: "You must be logged in to do this" });
|
||||
}
|
||||
|
||||
const appKeys = await getAppKeysFromSlug("intercom");
|
||||
|
||||
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) return res.status(400).json({ message: "Intercom client_id missing." });
|
||||
if (!client_secret) return res.status(400).json({ message: "Intercom client_secret missing." });
|
||||
|
||||
const response = await fetch(`https://api.intercom.io/auth/eagle/token`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
code,
|
||||
client_id,
|
||||
client_secret,
|
||||
}),
|
||||
});
|
||||
|
||||
const responseBody = await response.json();
|
||||
|
||||
if (response.status !== 200) {
|
||||
log.error("get user_access_token failed", responseBody);
|
||||
return res.redirect(`/apps/installed?error=${JSON.stringify(responseBody)}`);
|
||||
}
|
||||
|
||||
// Find the admin id from the accompte thanks to access_token and store it
|
||||
const admin = await fetch(`https://api.intercom.io/me`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${responseBody.access_token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const adminBody = await admin.json();
|
||||
|
||||
if (admin.status !== 200) {
|
||||
log.error("get admin_id failed", adminBody);
|
||||
return res.redirect(`/apps/installed?error=${JSON.stringify(adminBody)}`);
|
||||
}
|
||||
|
||||
const adminId = adminBody.id;
|
||||
|
||||
// Remove the previous credential if admin id was already linked
|
||||
await prisma.credential.deleteMany({
|
||||
where: {
|
||||
type: "intercom_automation",
|
||||
key: {
|
||||
string_contains: adminId,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
createOAuthAppCredential(
|
||||
{ appId: "intercom", type: "intercom_automation" },
|
||||
JSON.stringify({ access_token: responseBody.access_token, admin_id: adminId }),
|
||||
req
|
||||
);
|
||||
|
||||
const state = decodeOAuthState(req);
|
||||
|
||||
res.redirect(
|
||||
getSafeRedirectUrl(`${WEBAPP_URL}/apps/installed/automation?hl=intercom`) ??
|
||||
getInstalledAppPath({ variant: "automation", slug: "intercom" })
|
||||
);
|
||||
}
|
||||
136
calcom/packages/app-store/intercom/api/configure.ts
Normal file
136
calcom/packages/app-store/intercom/api/configure.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { WEBSITE_URL } from "@calcom/lib/constants";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import type {
|
||||
NewCanvas,
|
||||
ListComponent,
|
||||
ListItem,
|
||||
SpacerComponent,
|
||||
TextComponent,
|
||||
InputComponent,
|
||||
} from "../lib";
|
||||
import { isValidCalURL } from "../lib/isValidCalURL";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { admin, input_values, component_id } = req.body;
|
||||
|
||||
let isValid: boolean | TextComponent = true;
|
||||
if (component_id || input_values?.submit_booking_url) {
|
||||
const url = component_id === "submit_booking_url" ? input_values?.submit_booking_url : component_id;
|
||||
isValid = await isValidCalURL(url);
|
||||
|
||||
if (isValid === true) return res.status(200).json({ results: { submit_booking_url: url } });
|
||||
}
|
||||
|
||||
const input: InputComponent = {
|
||||
type: "input",
|
||||
id: "submit_booking_url",
|
||||
label: "Enter your Cal.com link",
|
||||
placeholder: "https://cal.com/valentinchmara/30min",
|
||||
save_state: "unsaved",
|
||||
action: {
|
||||
type: "submit",
|
||||
},
|
||||
aria_label: "Enter your Cal.com link",
|
||||
};
|
||||
|
||||
const defaultCanvasData: NewCanvas = {
|
||||
canvas: {
|
||||
content: {
|
||||
components: isValid === true ? [input] : [isValid, input],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (!admin?.id) return res.status(200).json(defaultCanvasData);
|
||||
|
||||
const credential = await prisma.credential.findFirst({
|
||||
where: {
|
||||
appId: "intercom",
|
||||
key: {
|
||||
string_contains: admin.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!credential) return res.status(200).json(defaultCanvasData);
|
||||
|
||||
const team = credential.teamId
|
||||
? await prisma.team.findUnique({
|
||||
where: {
|
||||
id: credential.teamId,
|
||||
},
|
||||
})
|
||||
: null;
|
||||
|
||||
const userId = credential.userId;
|
||||
|
||||
const user = userId
|
||||
? await prisma.user.findUnique({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
})
|
||||
: null;
|
||||
|
||||
const eventTypes = await prisma.eventType.findMany({
|
||||
where: {
|
||||
userId,
|
||||
hidden: false,
|
||||
},
|
||||
});
|
||||
|
||||
if (!eventTypes) return res.status(200).json(defaultCanvasData);
|
||||
if (!user && !team) return res.status(200).json(defaultCanvasData);
|
||||
|
||||
const list: ListItem[] = eventTypes.map((eventType) => {
|
||||
let slug;
|
||||
if (team && team.slug) {
|
||||
slug = `team/${team.slug}`;
|
||||
} else if (user && user.username) {
|
||||
slug = user.username;
|
||||
}
|
||||
|
||||
return {
|
||||
id: `${WEBSITE_URL}/${slug}/${eventType.slug}`,
|
||||
type: "item",
|
||||
title: eventType.title,
|
||||
subtitle: `${slug}/${eventType.slug}`,
|
||||
rounded_image: false,
|
||||
disabled: false,
|
||||
action: {
|
||||
type: "submit",
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const components: ListComponent = {
|
||||
type: "list",
|
||||
items: list,
|
||||
};
|
||||
|
||||
const spacer: SpacerComponent = {
|
||||
type: "spacer",
|
||||
size: "m",
|
||||
};
|
||||
|
||||
const text: TextComponent = {
|
||||
type: "text",
|
||||
text: "Or choose another Cal.com link:",
|
||||
style: "muted",
|
||||
align: "left",
|
||||
};
|
||||
|
||||
const canvasData: NewCanvas = {
|
||||
canvas: {
|
||||
content: {
|
||||
components:
|
||||
isValid === true ? [components, spacer, text, input] : [components, spacer, text, input, isValid],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return res.status(200).json(canvasData);
|
||||
}
|
||||
4
calcom/packages/app-store/intercom/api/index.ts
Normal file
4
calcom/packages/app-store/intercom/api/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export { default as add } from "./add";
|
||||
export { default as callback } from "./callback";
|
||||
export { default as initialize } from "./initialize";
|
||||
export { default as configure } from "./configure";
|
||||
32
calcom/packages/app-store/intercom/api/initialize.ts
Normal file
32
calcom/packages/app-store/intercom/api/initialize.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import type { NewCanvas } from "../lib";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { card_creation_options } = req.body;
|
||||
|
||||
if (!card_creation_options) return res.status(400).json({ message: "Missing card_creation_options" });
|
||||
|
||||
const URL = card_creation_options.submit_booking_url;
|
||||
|
||||
const canvasData: NewCanvas = {
|
||||
canvas: {
|
||||
content: {
|
||||
components: [
|
||||
{
|
||||
type: "button",
|
||||
id: "submit-issue-form",
|
||||
label: "Book a meeting",
|
||||
style: "primary",
|
||||
action: {
|
||||
type: "sheet",
|
||||
url: URL,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return res.status(200).json(canvasData);
|
||||
}
|
||||
18
calcom/packages/app-store/intercom/config.json
Normal file
18
calcom/packages/app-store/intercom/config.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"/*": "Don't modify slug - If required, do it using cli edit command",
|
||||
"name": "Intercom",
|
||||
"slug": "intercom",
|
||||
"type": "intercom_automation",
|
||||
"logo": "icon.svg",
|
||||
"url": "https://github.com/vachmara",
|
||||
"variant": "automation",
|
||||
"categories": ["automation"],
|
||||
"publisher": "Valentin Chmara",
|
||||
"email": "valentinchmara@gmail.com",
|
||||
"description": "Enhance your scheduling and appointment management experience with the Intercom Integration for Cal.com.",
|
||||
"isTemplate": false,
|
||||
"__createdUsingCli": true,
|
||||
"__template": "basic",
|
||||
"dirName": "intercom",
|
||||
"isOAuth": true
|
||||
}
|
||||
1
calcom/packages/app-store/intercom/index.ts
Normal file
1
calcom/packages/app-store/intercom/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * as api from "./api";
|
||||
66
calcom/packages/app-store/intercom/lib/index.ts
Normal file
66
calcom/packages/app-store/intercom/lib/index.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
export interface CanvasComponent {
|
||||
type: string;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
export interface InputComponent extends CanvasComponent {
|
||||
type: "input";
|
||||
id: string;
|
||||
label: string;
|
||||
placeholder: string;
|
||||
save_state: "unsaved" | "saved";
|
||||
action: {
|
||||
type: "submit";
|
||||
};
|
||||
aria_label: string;
|
||||
}
|
||||
|
||||
interface ButtonComponent extends CanvasComponent {
|
||||
type: "button";
|
||||
id: string;
|
||||
label: string;
|
||||
style: "primary" | "secondary" | "link";
|
||||
action: {
|
||||
type: "submit" | "sheet" | "url";
|
||||
url?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface SpacerComponent extends CanvasComponent {
|
||||
type: "spacer";
|
||||
size: "s" | "m" | "l";
|
||||
}
|
||||
|
||||
export interface TextComponent extends CanvasComponent {
|
||||
type: "text";
|
||||
text: string;
|
||||
style: "header" | "body" | "error" | "muted";
|
||||
align: "left" | "center" | "right";
|
||||
}
|
||||
|
||||
export interface ListItem {
|
||||
id: string;
|
||||
type: "item";
|
||||
title: string;
|
||||
subtitle: string;
|
||||
rounded_image: boolean;
|
||||
disabled: boolean;
|
||||
action: {
|
||||
type: "submit";
|
||||
};
|
||||
}
|
||||
|
||||
export interface ListComponent extends CanvasComponent {
|
||||
type: "list";
|
||||
items: ListItem[];
|
||||
}
|
||||
|
||||
export interface CanvasContent {
|
||||
components: (InputComponent | SpacerComponent | TextComponent | ListComponent | ButtonComponent)[];
|
||||
}
|
||||
|
||||
export interface NewCanvas {
|
||||
canvas: {
|
||||
content: CanvasContent;
|
||||
};
|
||||
}
|
||||
87
calcom/packages/app-store/intercom/lib/isValidCalURL.ts
Normal file
87
calcom/packages/app-store/intercom/lib/isValidCalURL.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { WEBSITE_URL } from "@calcom/lib/constants";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import type { TextComponent } from "../lib";
|
||||
|
||||
/**
|
||||
* Check if the url is a valid cal.com url
|
||||
* @param url
|
||||
* @returns boolean
|
||||
*/
|
||||
export async function isValidCalURL(url: string) {
|
||||
const regex = new RegExp(`^${WEBSITE_URL}/`, `i`);
|
||||
|
||||
const error: TextComponent = {
|
||||
type: "text",
|
||||
text: `This is not a valid ${WEBSITE_URL.replace("https://", "")} link`,
|
||||
style: "error",
|
||||
align: "left",
|
||||
};
|
||||
|
||||
if (!regex.test(url)) return error;
|
||||
|
||||
const urlWithoutCal = url.replace(regex, "");
|
||||
|
||||
const urlParts = urlWithoutCal.split("/");
|
||||
const usernameOrTeamSlug = urlParts[0];
|
||||
const eventTypeSlug = urlParts[1];
|
||||
|
||||
if (!usernameOrTeamSlug || !eventTypeSlug) return error;
|
||||
|
||||
// Find all potential users with the given username
|
||||
const potentialUsers = await prisma.user.findMany({
|
||||
where: {
|
||||
username: usernameOrTeamSlug,
|
||||
},
|
||||
include: {
|
||||
eventTypes: {
|
||||
where: {
|
||||
slug: eventTypeSlug,
|
||||
hidden: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Find all potential teams with the given slug
|
||||
const potentialTeams = await prisma.team.findMany({
|
||||
where: {
|
||||
slug: usernameOrTeamSlug,
|
||||
},
|
||||
include: {
|
||||
eventTypes: {
|
||||
where: {
|
||||
slug: eventTypeSlug,
|
||||
hidden: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Check if any user has the matching eventTypeSlug
|
||||
const matchingUser = potentialUsers.find((user) => user.eventTypes.length > 0);
|
||||
|
||||
// Check if any team has the matching eventTypeSlug
|
||||
const matchingTeam = potentialTeams.find((team) => team.eventTypes.length > 0);
|
||||
|
||||
if (!matchingUser && !matchingTeam) return error;
|
||||
|
||||
const userOrTeam = matchingUser || matchingTeam;
|
||||
|
||||
if (!userOrTeam) return error;
|
||||
|
||||
// Retrieve the correct user or team
|
||||
const userOrTeamId = userOrTeam.id;
|
||||
|
||||
const eventType = await prisma.eventType.findFirst({
|
||||
where: {
|
||||
userId: userOrTeamId,
|
||||
slug: eventTypeSlug,
|
||||
hidden: false,
|
||||
},
|
||||
});
|
||||
|
||||
if (!eventType) return error;
|
||||
|
||||
return true;
|
||||
}
|
||||
14
calcom/packages/app-store/intercom/package.json
Normal file
14
calcom/packages/app-store/intercom/package.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/package.json",
|
||||
"private": true,
|
||||
"name": "@calcom/intercom",
|
||||
"version": "0.0.0",
|
||||
"main": "./index.ts",
|
||||
"dependencies": {
|
||||
"@calcom/lib": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@calcom/types": "*"
|
||||
},
|
||||
"description": "Enhance your scheduling and appointment management experience with the Intercom Integration for Cal.com."
|
||||
}
|
||||
BIN
calcom/packages/app-store/intercom/static/1.png
Normal file
BIN
calcom/packages/app-store/intercom/static/1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.6 MiB |
BIN
calcom/packages/app-store/intercom/static/2.png
Normal file
BIN
calcom/packages/app-store/intercom/static/2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 MiB |
BIN
calcom/packages/app-store/intercom/static/3.png
Normal file
BIN
calcom/packages/app-store/intercom/static/3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 MiB |
1
calcom/packages/app-store/intercom/static/icon.svg
Normal file
1
calcom/packages/app-store/intercom/static/icon.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="36" height="36" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid"><path d="M221.867 140.748a8.534 8.534 0 0 1-17.067 0V64a8.534 8.534 0 0 1 17.067 0v76.748zm-2.978 53.413c-1.319 1.129-32.93 27.655-90.889 27.655-57.958 0-89.568-26.527-90.887-27.656a8.535 8.535 0 0 1-.925-12.033 8.53 8.53 0 0 1 12.013-.942c.501.42 28.729 23.563 79.8 23.563 51.712 0 79.503-23.31 79.778-23.545 3.571-3.067 8.968-2.655 12.033.925a8.534 8.534 0 0 1-.923 12.033zM34.133 64A8.534 8.534 0 0 1 51.2 64v76.748a8.534 8.534 0 0 1-17.067 0V64zm42.668-17.067a8.534 8.534 0 0 1 17.066 0v114.001a8.534 8.534 0 0 1-17.066 0v-114zm42.666-4.318A8.532 8.532 0 0 1 128 34.082a8.532 8.532 0 0 1 8.534 8.533v123.733a8.534 8.534 0 0 1-17.067 0V42.615zm42.667 4.318a8.534 8.534 0 0 1 17.066 0v114.001a8.534 8.534 0 0 1-17.066 0v-114zM224 0H32C14.327 0 0 14.327 0 32v192c0 17.672 14.327 32 32 32h192c17.673 0 32-14.328 32-32V32c0-17.673-14.327-32-32-32z" fill="#1F8DED"/></svg>
|
||||
|
After Width: | Height: | Size: 987 B |
8
calcom/packages/app-store/intercom/zod.ts
Normal file
8
calcom/packages/app-store/intercom/zod.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const appDataSchema = z.object({});
|
||||
|
||||
export const appKeysSchema = z.object({
|
||||
client_id: z.string(),
|
||||
client_secret: z.string(),
|
||||
});
|
||||
Reference in New Issue
Block a user