Files
formbricks-formbricks/packages/lib/notion/service.ts
T
Dhruwang Jariwala ab80bc1bf2 feat: language switch (#2692)
Co-authored-by: Johannes <johannes@formbricks.com>
Co-authored-by: Matti Nannt <mail@matthiasnannt.com>
2024-06-12 14:10:22 +00:00

72 lines
1.9 KiB
TypeScript

import {
TIntegrationNotion,
TIntegrationNotionConfig,
TIntegrationNotionDatabase,
} from "@formbricks/types/integration/notion";
import { ENCRYPTION_KEY } from "../constants";
import { symmetricDecrypt } from "../crypto";
import { getIntegrationByType } from "../integration/service";
const fetchPages = async (config: TIntegrationNotionConfig) => {
try {
const res = await fetch("https://api.notion.com/v1/search", {
headers: getHeaders(config),
method: "POST",
body: JSON.stringify({
page_size: 100,
filter: {
value: "database",
property: "object",
},
}),
});
return (await res.json()).results;
} catch (error) {
throw error;
}
};
export const getNotionDatabases = async (environmentId: string): Promise<TIntegrationNotionDatabase[]> => {
let results: TIntegrationNotionDatabase[] = [];
try {
const notionIntegration = (await getIntegrationByType(environmentId, "notion")) as TIntegrationNotion;
if (notionIntegration && notionIntegration.config?.key.bot_id) {
results = await fetchPages(notionIntegration.config);
}
return results;
} catch (error) {
throw error;
}
};
export const writeData = async (
databaseId: string,
properties: Record<string, Object>,
config: TIntegrationNotionConfig
) => {
try {
await fetch(`https://api.notion.com/v1/pages`, {
headers: getHeaders(config),
method: "POST",
body: JSON.stringify({
parent: {
database_id: databaseId,
},
properties: properties,
}),
});
} catch (error) {
throw error;
}
};
const getHeaders = (config: TIntegrationNotionConfig) => {
const decryptedToken = symmetricDecrypt(config.key.access_token, ENCRYPTION_KEY!);
return {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${decryptedToken}`,
"Notion-Version": "2022-06-28",
};
};