|
| 1 | +import { Client, isFullPage } from "@notionhq/client"; |
| 2 | +import { notifyContactCreated } from "./slack"; |
| 3 | + |
| 4 | +const { NOTION_TOKEN, MENTION_EMAILS, MENTION_IDS } = process.env; |
| 5 | + |
| 6 | +const notion = new Client({ auth: NOTION_TOKEN }); |
| 7 | + |
| 8 | +const mentionPerson = ({ id }: { id: string }) => [ |
| 9 | + { |
| 10 | + mention: { |
| 11 | + user: { |
| 12 | + id, |
| 13 | + }, |
| 14 | + }, |
| 15 | + plain_text: "", |
| 16 | + href: null, |
| 17 | + }, |
| 18 | + { |
| 19 | + text: { |
| 20 | + content: " ", |
| 21 | + }, |
| 22 | + }, |
| 23 | +]; |
| 24 | + |
| 25 | +const getMentions = () => { |
| 26 | + if (MENTION_EMAILS && MENTION_IDS) { |
| 27 | + const emails = MENTION_EMAILS.split(","); |
| 28 | + const ids = MENTION_IDS.split(","); |
| 29 | + |
| 30 | + if (emails.length && ids.length) { |
| 31 | + return ids.map((id, i) => ({ |
| 32 | + id, |
| 33 | + })); |
| 34 | + } |
| 35 | + } |
| 36 | + return []; |
| 37 | +}; |
| 38 | + |
| 39 | +const mentionPeople = () => { |
| 40 | + return getMentions().flatMap(mentionPerson); |
| 41 | +}; |
| 42 | + |
| 43 | +const createContactObject = ( |
| 44 | + id: string, |
| 45 | + email: string, |
| 46 | + name: string, |
| 47 | + content: string, |
| 48 | + databaseID: string, |
| 49 | + source: string, |
| 50 | +) => ({ |
| 51 | + parent: { |
| 52 | + database_id: databaseID, |
| 53 | + }, |
| 54 | + properties: { |
| 55 | + id: { |
| 56 | + title: [ |
| 57 | + { |
| 58 | + text: { |
| 59 | + content: id, |
| 60 | + }, |
| 61 | + }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + email: { |
| 65 | + email, |
| 66 | + }, |
| 67 | + name: { |
| 68 | + rich_text: [ |
| 69 | + { |
| 70 | + text: { |
| 71 | + content: name, |
| 72 | + }, |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + date: { |
| 77 | + date: { |
| 78 | + start: new Date().toISOString(), |
| 79 | + }, |
| 80 | + }, |
| 81 | + source: { |
| 82 | + rich_text: [ |
| 83 | + { |
| 84 | + text: { |
| 85 | + content: source, |
| 86 | + }, |
| 87 | + }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + }, |
| 91 | + children: [ |
| 92 | + { |
| 93 | + paragraph: { |
| 94 | + rich_text: [ |
| 95 | + { |
| 96 | + text: { |
| 97 | + content, |
| 98 | + }, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + paragraph: { |
| 105 | + rich_text: mentionPeople(), |
| 106 | + }, |
| 107 | + }, |
| 108 | + ], |
| 109 | +}); |
| 110 | + |
| 111 | +const createContact = async ( |
| 112 | + id: string, |
| 113 | + email: string, |
| 114 | + name: string, |
| 115 | + content: string, |
| 116 | + databaseID: string, |
| 117 | + source: string, |
| 118 | +) => { |
| 119 | + const response = await notion.pages.create( |
| 120 | + createContactObject(id, email, name, content, databaseID, source), |
| 121 | + ); |
| 122 | + |
| 123 | + if (response.id && isFullPage(response)) { |
| 124 | + return { |
| 125 | + id: response.id, |
| 126 | + url: response.url, |
| 127 | + }; |
| 128 | + } |
| 129 | + throw { |
| 130 | + body: { |
| 131 | + message: "Failed to create notion page", |
| 132 | + }, |
| 133 | + }; |
| 134 | +}; |
| 135 | + |
| 136 | +export const processContact = async (event: { |
| 137 | + id: string; |
| 138 | + email: string; |
| 139 | + name: string; |
| 140 | + message: string; |
| 141 | + databaseID: string; |
| 142 | + source: string; |
| 143 | +}) => { |
| 144 | + const { id, email, name, message, databaseID, source } = event; |
| 145 | + |
| 146 | + if (!id || !email || !name || !databaseID) { |
| 147 | + console.log({ event }); |
| 148 | + throw { |
| 149 | + body: { |
| 150 | + message: "Missing data in process contact event", |
| 151 | + }, |
| 152 | + }; |
| 153 | + } |
| 154 | + |
| 155 | + const { id: notionPageID, url } = await createContact( |
| 156 | + `Message from ${name} (${id})`, |
| 157 | + email, |
| 158 | + name, |
| 159 | + message, |
| 160 | + databaseID, |
| 161 | + source, |
| 162 | + ); |
| 163 | + |
| 164 | + await notifyContactCreated(name, email, url); |
| 165 | + return notionPageID; |
| 166 | +}; |
0 commit comments