Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion apps/contact/helpers/notion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Client, isFullPage } from "@notionhq/client";
import { notifyContactCreated } from "./slack";
import { notifyContactError, notifyContactCreated } from "./slack";

const { NOTION_TOKEN, MENTION_EMAILS, MENTION_IDS } = process.env;

Expand Down Expand Up @@ -120,12 +120,25 @@ const createContact = async (
createContactObject(id, email, name, content, databaseID, source),
);

// isFullPage checks if the response is type PageObjectResponse => contains url
if (response.id && isFullPage(response)) {
return {
id: response.id,
url: response.url,
};
// In case the page is created but the response is type PartialPageObjectResponse => doesn't contain url
} else if (response.id && !isFullPage(response)) {
// Notion allows navigation to the created page using only the id without '-'
// https://dev.to/adamcoster/change-a-url-without-breaking-existing-links-4m0d
const cleanId = response.id.replace(/-/g, "");
const pageUrl = `https://www.notion.so/${cleanId}`;
return {
id: response.id,
url: pageUrl,
};
}

await notifyContactError(name, email, content);
throw {
body: {
message: "Failed to create notion page",
Expand Down
59 changes: 59 additions & 0 deletions apps/contact/helpers/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ export const createPayload = (name: string, email: string, url: string) => ({
],
});

export const createErrorPayload = (
name: string,
email: string,
content: string,
) => ({
channel: SLACK_CHANNEL,
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: "An error ocured while creating a contact",
emoji: true,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `There was an error trying to create a contact for _${name}_ (_${email}_).`,
Comment thread
davidabram marked this conversation as resolved.
},
},
{
type: "section",
text: content,
},
],
});

export const notifyContactCreated = async (
name: string,
email: string,
Expand Down Expand Up @@ -71,3 +100,33 @@ export const notifyContactCreated = async (
}
}
};

export const notifyContactError = async (
name: string,
email: string,
content: string,
) => {
const payload = createErrorPayload(name, email, content);
const payloadStringify = JSON.stringify(payload);

if (IS_OFFLINE) {
console.log(payload);
} else {
const result = await fetch("https://slack.com/api/chat.postMessage", {
method: "POST",
body: payloadStringify,
headers: {
"Content-Type": "application/json; charset=utf-8",
"Content-Length": payloadStringify.length.toString(),
Authorization: `Bearer ${SLACK_BOT_TOKEN}`,
Accept: "application/json",
},
});
if (result.status !== 200) {
throw {
body: "Could not send notification message to Slack",
statusCode: result.status,
};
}
}
};