-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotion.ts
More file actions
196 lines (182 loc) · 3.69 KB
/
notion.ts
File metadata and controls
196 lines (182 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { Client, isFullPage } from "@notionhq/client";
import { notifyContactError, notifyContactCreated } from "./slack";
const { NOTION_TOKEN, MENTION_EMAILS, MENTION_IDS } = process.env;
const notion = new Client({ auth: NOTION_TOKEN });
const mentionPerson = ({ id }: { id: string }) => [
{
mention: {
user: {
id,
},
},
plain_text: "",
href: null,
},
{
text: {
content: " ",
},
},
];
const getMentions = () => {
if (MENTION_EMAILS && MENTION_IDS) {
const emails = MENTION_EMAILS.split(",");
const ids = MENTION_IDS.split(",");
if (emails.length && ids.length) {
return ids.map((id, i) => ({
id,
}));
}
}
return [];
};
const mentionPeople = () => {
return getMentions().flatMap(mentionPerson);
};
const createContactObject = (
id: string,
email: string,
name: string,
content: string,
databaseID: string,
source: string,
) => ({
parent: {
database_id: databaseID,
},
properties: {
id: {
title: [
{
text: {
content: id,
},
},
],
},
email: {
email,
},
name: {
rich_text: [
{
text: {
content: name,
},
},
],
},
date: {
date: {
start: new Date().toISOString(),
},
},
source: {
rich_text: [
{
text: {
content: source,
},
},
],
},
},
children: [
{
paragraph: {
rich_text: [
{
text: {
content,
},
},
],
},
},
{
paragraph: {
rich_text: mentionPeople(),
},
},
],
});
const createContact = async (
id: string,
email: string,
name: string,
content: string,
databaseID: string,
source: string,
) => {
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)) {
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 {
error: "Failed to create notion page",
};
} catch (e) {
return {
error: "Failed to create notion page",
};
}
};
export const processContact = async (event: {
id: string;
email: string;
name: string;
message: string;
databaseID: string;
source: string;
}) => {
const { id, email, name, message, databaseID, source } = event;
if (!id || !email || !name || !databaseID) {
console.log({ event });
throw {
body: {
message: "Missing data in process contact event",
},
};
}
const {
id: notionPageID,
url,
error: errorMessage,
} = await createContact(
`Message from ${name} (${id})`,
email,
name,
message,
databaseID,
source,
);
if (errorMessage) {
await notifyContactError(name, email, message);
throw {
body: {
message: errorMessage,
},
};
}
if (url) {
await notifyContactCreated(name, email, url);
}
return notionPageID;
};