adding sendgrid contacts update functions

This commit is contained in:
Martina 2021-08-27 15:27:00 -07:00
parent 5563574dc3
commit 4e5f1d2d67
3 changed files with 70 additions and 0 deletions

View File

@ -95,10 +95,12 @@ import pruneVerifications from "~/node_common/data/methods/prune-verifications";
// NOTE(jim):
// one-offs
import createOrphan from "~/node_common/data/methods/create-orphan";
import getAllSendgridContacts from "~/node_common/data/methods/get-all-sendgrid-contacts";
export {
// NOTE(jim): One-offs
createOrphan,
getAllSendgridContacts,
// NOTE(jim): User operations
createUser,
updateUserById,

View File

@ -0,0 +1,33 @@
import * as Serializers from "~/node_common/serializers";
import { runQuery } from "~/node_common/data/utilities";
export default async () => {
return await runQuery({
label: "GET_ALL_SENDGRID_CONTACTS",
queryFn: async (DB) => {
let query = await DB.select("id", "username", "email").from("users").whereNotNull("email");
if (!query || query.error) {
return null;
}
let formatted = query.map((user) => {
return {
email: user.email,
custom_fields: {
w4_T: user.id,
w5_T: user.username,
},
};
});
return JSON.parse(JSON.stringify(formatted));
},
errorFn: async (e) => {
return {
error: true,
decorator: "GET_ALL_SENDGRID_CONTACTS",
};
},
});
};

View File

@ -0,0 +1,35 @@
import "isomorphic-fetch";
import * as Logging from "~/common/logging";
import * as Data from "~/node_common/data";
async function updateSendgridFields() {
let customFields = { name: "username", field_type: "Text" };
const fieldsResponse = await fetch("https://api.sendgrid.com/v3/marketing/field_definitions", {
method: "POST",
headers: new Headers({
Authorization: `Bearer ${process.env.SENDGRID_API_KEY}`,
"content-type": "application/json",
}),
body: JSON.stringify(customFields),
});
console.log(fieldsResponse);
let json = await fieldsResponse.json();
console.log(json);
}
async function updateSendgridContacts() {
let contacts = await Data.getAllSendgridContacts();
let data = { contacts };
const response = await fetch("https://api.sendgrid.com/v3/marketing/contacts", {
method: "PUT",
headers: new Headers({
Authorization: `Bearer ${process.env.SENDGRID_API_KEY}`,
"content-type": "application/json",
}),
body: JSON.stringify(data),
});
console.log(response);
let json = await response.json();
console.log(json);
}