From ce17ae2199dfb03d4e1391fd4af15534890e486a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ante=20Kocei=C4=87?= Date: Fri, 19 Sep 2025 13:27:47 +0200 Subject: [PATCH 1/4] Added sending message if notion page is not created, generating alternative URL for notion page --- apps/contact/helpers/notion.ts | 15 ++++++++- apps/contact/helpers/slack.ts | 59 ++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/apps/contact/helpers/notion.ts b/apps/contact/helpers/notion.ts index 51fa3074..e240c29e 100644 --- a/apps/contact/helpers/notion.ts +++ b/apps/contact/helpers/notion.ts @@ -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; @@ -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://notion.so/${cleanId}`; + return { + id: response.id, + url: pageUrl, + }; } + + await notifyContactError(name, email, content); throw { body: { message: "Failed to create notion page", diff --git a/apps/contact/helpers/slack.ts b/apps/contact/helpers/slack.ts index 8114d6d9..a1fcdffc 100644 --- a/apps/contact/helpers/slack.ts +++ b/apps/contact/helpers/slack.ts @@ -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}_).`, + }, + }, + { + type: "section", + text: content, + }, + ], +}); + export const notifyContactCreated = async ( name: string, email: string, @@ -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, + }; + } + } +}; From e5682bbc0aeabeb459565aafd96264046bae4793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ante=20Kocei=C4=87?= Date: Fri, 19 Sep 2025 15:53:31 +0200 Subject: [PATCH 2/4] Fixed URL --- apps/contact/helpers/notion.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/contact/helpers/notion.ts b/apps/contact/helpers/notion.ts index e240c29e..a0037fb3 100644 --- a/apps/contact/helpers/notion.ts +++ b/apps/contact/helpers/notion.ts @@ -131,7 +131,7 @@ const createContact = async ( // 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://notion.so/${cleanId}`; + const pageUrl = `https://www.notion.so/${cleanId}`; return { id: response.id, url: pageUrl, From 0ab7d73d7e932940b4e25f004f7bfa57cfbb088e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ante=20Kocei=C4=87?= Date: Mon, 22 Sep 2025 14:25:23 +0200 Subject: [PATCH 3/4] Added try catch for creating notion page --- apps/contact/helpers/notion.ts | 65 +++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/apps/contact/helpers/notion.ts b/apps/contact/helpers/notion.ts index a0037fb3..503c613e 100644 --- a/apps/contact/helpers/notion.ts +++ b/apps/contact/helpers/notion.ts @@ -116,34 +116,36 @@ const createContact = async ( databaseID: string, source: string, ) => { - const response = await notion.pages.create( - createContactObject(id, email, name, content, databaseID, source), - ); + try { + const response = await notion.pages.create( + createContactObject(id, email, name, content, databaseID, source), + ); - // isFullPage checks if the response is type PageObjectResponse => contains url - if (response.id && isFullPage(response)) { + // 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, + }; + } return { - id: response.id, - url: response.url, + message: "Failed to create notion page", }; - // 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}`; + } catch (error) { return { - id: response.id, - url: pageUrl, + message: "Failed to create notion page", }; } - - await notifyContactError(name, email, content); - throw { - body: { - message: "Failed to create notion page", - }, - }; }; export const processContact = async (event: { @@ -165,7 +167,11 @@ export const processContact = async (event: { }; } - const { id: notionPageID, url } = await createContact( + const { + id: notionPageID, + url, + message: errorMessage, + } = await createContact( `Message from ${name} (${id})`, email, name, @@ -174,6 +180,17 @@ export const processContact = async (event: { source, ); - await notifyContactCreated(name, email, url); + if (errorMessage) { + await notifyContactError(name, email, message); + throw { + body: { + message: errorMessage, + }, + }; + } + + if (url) { + await notifyContactCreated(name, email, url); + } return notionPageID; }; From 8b79fd9543ea55b233ec54f78c9d65584da576e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ante=20Kocei=C4=87?= Date: Mon, 22 Sep 2025 14:31:06 +0200 Subject: [PATCH 4/4] Better naming for return --- apps/contact/helpers/notion.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/contact/helpers/notion.ts b/apps/contact/helpers/notion.ts index 503c613e..31a27f2d 100644 --- a/apps/contact/helpers/notion.ts +++ b/apps/contact/helpers/notion.ts @@ -139,11 +139,11 @@ const createContact = async ( }; } return { - message: "Failed to create notion page", + error: "Failed to create notion page", }; - } catch (error) { + } catch (e) { return { - message: "Failed to create notion page", + error: "Failed to create notion page", }; } }; @@ -170,7 +170,7 @@ export const processContact = async (event: { const { id: notionPageID, url, - message: errorMessage, + error: errorMessage, } = await createContact( `Message from ${name} (${id})`, email,