cal.pub0.org/packages/lib/Sendgrid.ts

130 lines
4.1 KiB
TypeScript
Raw Normal View History

Bringing back sendgrid app to review (#5501) * Sendgrid app and code simplification * Applying app-store-cli + impl * Fixing types * Adding features to readme * Fixing unit tests * A few last tweaks regarding UX and env vars * Applying feedback * Using calcom icons * Renaming and applying feedback * Testing user/type page fix * Standarizing Sendgrid client usage * Removing types * Reverting CloseCom changes * Stop relying on sendgrid client pkg * Fixing button and more reverting closecom changes * Revert "Stop relying on sendgrid client pkg" This reverts commit dd61851572a17a1e4051b133683af85c934bc2d0. * Revert "Removing types" This reverts commit 1ec5ed8de2f3139bbe84f867f229bc5759256806. * Is this it? * Standardizing apis * Fixing path * Fixing throwing errors the standard way * Stop relying on getInstalledAppPath * Removing seemingly troubling code * Returning error and avoiding any outer reference * Revert "Returning error and avoiding any outer reference" This reverts commit 7d32e30154423c95f54ebae81a76ab16a1c7bc94. * Revert "Removing seemingly troubling code" This reverts commit eaae772abcd04c8f046e4960116f42c5aaf87adf. * Revert "Stop relying on getInstalledAppPath" This reverts commit bcc70fc337bbe7fb5e74609abaeee7cd3ede90a3. * Revert "Fixing throwing errors the standard way" This reverts commit bb1bb410fac6f8c6ad14c3163a8433d125f7a885. * Revert "Fixing path" This reverts commit a7bd83c4fb7597594d0470cb530378c826b45481. * Revert "Standardizing apis" This reverts commit 0258a182298af3ebad321854ef4f34a65f4c700a. * Revert "Is this it?" This reverts commit 70b3f7b98e3003dfa225dc539e02a1e17abdd840. * Converting APIs to legacy style * Missing reverted CloseCom test mock * Needed for the renaming * Reverting Closecom and yarn unneeded changes * Ununsed type * Testing rearranging exports * Update apps/web/components/apps/OmniInstallAppButton.tsx Co-authored-by: Omar López <zomars@me.com> * Standardizing APIs * Fixing wrong toast message on app page Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Omar López <zomars@me.com>
2022-11-17 21:38:34 +00:00
import client from "@sendgrid/client";
import type { ClientRequest } from "@sendgrid/client/src/request";
import type { ClientResponse } from "@sendgrid/client/src/response";
Bringing back sendgrid app to review (#5501) * Sendgrid app and code simplification * Applying app-store-cli + impl * Fixing types * Adding features to readme * Fixing unit tests * A few last tweaks regarding UX and env vars * Applying feedback * Using calcom icons * Renaming and applying feedback * Testing user/type page fix * Standarizing Sendgrid client usage * Removing types * Reverting CloseCom changes * Stop relying on sendgrid client pkg * Fixing button and more reverting closecom changes * Revert "Stop relying on sendgrid client pkg" This reverts commit dd61851572a17a1e4051b133683af85c934bc2d0. * Revert "Removing types" This reverts commit 1ec5ed8de2f3139bbe84f867f229bc5759256806. * Is this it? * Standardizing apis * Fixing path * Fixing throwing errors the standard way * Stop relying on getInstalledAppPath * Removing seemingly troubling code * Returning error and avoiding any outer reference * Revert "Returning error and avoiding any outer reference" This reverts commit 7d32e30154423c95f54ebae81a76ab16a1c7bc94. * Revert "Removing seemingly troubling code" This reverts commit eaae772abcd04c8f046e4960116f42c5aaf87adf. * Revert "Stop relying on getInstalledAppPath" This reverts commit bcc70fc337bbe7fb5e74609abaeee7cd3ede90a3. * Revert "Fixing throwing errors the standard way" This reverts commit bb1bb410fac6f8c6ad14c3163a8433d125f7a885. * Revert "Fixing path" This reverts commit a7bd83c4fb7597594d0470cb530378c826b45481. * Revert "Standardizing apis" This reverts commit 0258a182298af3ebad321854ef4f34a65f4c700a. * Revert "Is this it?" This reverts commit 70b3f7b98e3003dfa225dc539e02a1e17abdd840. * Converting APIs to legacy style * Missing reverted CloseCom test mock * Needed for the renaming * Reverting Closecom and yarn unneeded changes * Ununsed type * Testing rearranging exports * Update apps/web/components/apps/OmniInstallAppButton.tsx Co-authored-by: Omar López <zomars@me.com> * Standardizing APIs * Fixing wrong toast message on app page Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Omar López <zomars@me.com>
2022-11-17 21:38:34 +00:00
import logger from "@calcom/lib/logger";
export type SendgridFieldOptions = [string, string][];
type SendgridUsernameResult = {
username: string;
user_id: number;
};
export type SendgridCustomField = {
id: string;
name: string;
field_type: string;
_metadata: {
self: string;
};
};
export type SendgridContact = {
id: string;
first_name: string;
last_name: string;
email: string;
};
export type SendgridSearchResult = {
result: SendgridContact[];
};
export type SendgridFieldDefinitions = {
custom_fields: SendgridCustomField[];
};
export type SendgridNewContact = {
job_id: string;
};
const environmentApiKey = process.env.SENDGRID_SYNC_API_KEY || "";
/**
* This class to instance communicating to Sendgrid APIs requires an API Key.
*
* You can either pass to the constructor an API Key or have one defined as an
* environment variable in case the communication to Sendgrid is just for
* one account only, not configurable by any user at any moment.
*/
export default class Sendgrid {
private log: typeof logger;
constructor(providedApiKey = "") {
this.log = logger.getChildLogger({ prefix: [`[[lib] sendgrid`] });
if (!providedApiKey && !environmentApiKey) throw Error("Sendgrid Api Key not present");
client.setApiKey(providedApiKey || environmentApiKey);
}
public username = async () => {
const username = await this.sendgridRequest<SendgridUsernameResult>({
url: `/v3/user/username`,
method: "GET",
});
return username;
};
public async sendgridRequest<R = ClientResponse>(data: ClientRequest): Promise<R> {
this.log.debug("sendgridRequest:request", data);
const results = await client.request(data);
this.log.debug("sendgridRequest:results", results);
if (results[1].errors) throw Error(`Sendgrid request error: ${results[1].errors}`);
return results[1];
}
public async getSendgridContactId(email: string) {
const search = await this.sendgridRequest<SendgridSearchResult>({
url: `/v3/marketing/contacts/search`,
method: "POST",
body: {
query: `email LIKE '${email}'`,
},
});
this.log.debug("sync:sendgrid:getSendgridContactId:search", search);
return search.result || [];
}
public async getSendgridCustomFieldsIds(customFields: SendgridFieldOptions) {
// Get Custom Activity Fields
const allFields = await this.sendgridRequest<SendgridFieldDefinitions>({
url: `/v3/marketing/field_definitions`,
method: "GET",
});
allFields.custom_fields = allFields.custom_fields ?? [];
this.log.debug("sync:sendgrid:getCustomFieldsIds:allFields", allFields);
const customFieldsNames = allFields.custom_fields.map((fie) => fie.name);
this.log.debug("sync:sendgrid:getCustomFieldsIds:customFieldsNames", customFieldsNames);
const customFieldsExist = customFields.map((cusFie) => customFieldsNames.includes(cusFie[0]));
this.log.debug("sync:sendgrid:getCustomFieldsIds:customFieldsExist", customFieldsExist);
return await Promise.all(
customFieldsExist.map(async (exist, idx) => {
if (!exist) {
const [name, field_type] = customFields[idx];
const created = await this.sendgridRequest<SendgridCustomField>({
url: `/v3/marketing/field_definitions`,
method: "POST",
body: {
name,
field_type,
},
});
this.log.debug("sync:sendgrid:getCustomFieldsIds:customField:created", created);
return created.id;
} else {
const index = customFieldsNames.findIndex((val) => val === customFields[idx][0]);
if (index >= 0) {
this.log.debug(
"sync:sendgrid:getCustomFieldsIds:customField:existed",
allFields.custom_fields[index].id
);
return allFields.custom_fields[index].id;
} else {
throw Error("Couldn't find the field index");
}
}
})
);
}
}