-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils-server.js
181 lines (164 loc) · 4.29 KB
/
utils-server.js
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
import * as postmark from "postmark";
import { arrayToHasuraList } from "utils-common";
const graphqlUrl = process.env.NEXT_PUBLIC_GRAPHQL_API;
const hasuraAdminSecret = process.env.HASURA_ADMIN_SECRET;
const postmarkSecretKey = process.env.POSTMARK_KEY;
var postmarkClient = new postmark.ServerClient(postmarkSecretKey);
export const waitlistEmailQuery = `
query WaitlistByEmail($email: String!) {
waitlist (where: {email: {_eq: $email}}) {
id
created_at
email
lists
confirmed
updated_at
donations
}
}
`;
export const insertWaitlistQuery = `
mutation InsertWaitlist($email: String!, $lists: _text, $donations:jsonb) {
insert_waitlist_one(object: {email: $email, lists: $lists, donations: $donations}) {
id
email
lists
confirmed
donations
}
}
`;
export const deleteInsertWaitlistQuery = `
mutation DeleteInsertWaitlist($email: String!, $lists: _text, $donations: jsonb, $existingId: uuid!) {
delete_waitlist_by_pk(id: $existingId) {
id
}
insert_waitlist_one(object: {email: $email, lists: $lists, donations: $donations}) {
id
}
}
`;
export const appendDonationWaitlistQuery = `
mutation AppendWaitlistDonation($id: uuid!, $donation: jsonb) {
update_waitlist_by_pk(pk_columns: {id: $id}, _append: {donations: $donation}) {
id
}
}
`;
export const updateDonationsWaitlistQuery = `
mutation UpdateWaitlistDonations($id: uuid!, $donations: jsonb) {
update_waitlist_by_pk(pk_columns: {id: $id}, _set: {donations: $donations}) {
id
created_at
updated_at
confirmed
donations
email
lists
}
}
`;
export async function fetchAdminGraphQL(
operationsDoc,
operationName,
variables
) {
const result = await fetch(graphqlUrl, {
method: "POST",
body: JSON.stringify({
query: operationsDoc,
variables: variables,
operationName: operationName,
}),
headers: new Headers({
"x-hasura-admin-secret": hasuraAdminSecret,
}),
});
return await result.json();
}
export function fetchEmailWaitlist(email) {
return fetchAdminGraphQL(waitlistEmailQuery, "WaitlistByEmail", { email });
}
export function insertUnconfirmedWaitlist(email, lists = [], donations = []) {
return fetchAdminGraphQL(insertWaitlistQuery, "InsertWaitlist", {
email,
lists: arrayToHasuraList(lists),
donations,
});
}
export function deleteExistingInsertNewWaitlist({
existingId,
email,
lists,
donations = [],
}) {
return fetchAdminGraphQL(deleteInsertWaitlistQuery, "DeleteInsertWaitlist", {
email,
lists: arrayToHasuraList(lists),
donations,
existingId,
});
}
export function appendWaitlistDonation({ id, donation }) {
return fetchAdminGraphQL(
appendDonationWaitlistQuery,
"AppendWaitlistDonation",
{
id,
donation,
}
);
}
export function updateWaitlistDonations({ id, donations }) {
return fetchAdminGraphQL(
updateDonationsWaitlistQuery,
"UpdateWaitlistDonations",
{
id,
donations,
}
);
}
export async function sendEmail(template, email, jwt, linkDomain) {
if (linkDomain.includes("localhost")) {
const link = `http://${linkDomain}/roadmap#waitlist?token=${jwt}`;
console.log({ link, template, email });
return { ok: true };
} else {
const link = `https://${linkDomain}/roadmap#waitlist?token=${jwt}`;
return await postmarkClient.sendEmail({
From: "[email protected]",
To: email,
Subject: "Please Confirm your Address!",
HtmlBody: `<strong>Template:</strong> ${template}, <a href="${link}" target="_blank">link!</a>`,
TextBody: `template: ${template}, link: ${link}`,
MessageStream: "outbound",
});
}
}
export async function sendTemplateEmail(
template,
email,
jwt,
linkDomain,
templateModel
) {
const linkUrl = `${linkDomain}/roadmap?token=${jwt}#waitlist`;
if (linkDomain.includes("localhost")) {
const link = `http://${linkUrl}`;
console.log({ link, template, email });
return { ok: true };
} else {
const link = `https://${linkUrl}`;
return await postmarkClient.sendEmailWithTemplate({
TemplateAlias: template,
TemplateModel: {
action_url: link,
...templateModel,
},
From: "[email protected]",
To: email,
MessageStream: "outbound",
});
}
}