2022-08-15 20:45:15 +00:00
|
|
|
import logger from "@calcom/lib/logger";
|
|
|
|
|
2022-06-18 00:58:27 +00:00
|
|
|
export type CloseComLead = {
|
2022-08-26 21:10:12 +00:00
|
|
|
companyName?: string | null | undefined;
|
2022-06-18 00:58:27 +00:00
|
|
|
contactName?: string;
|
|
|
|
contactEmail?: string;
|
2022-08-15 20:45:15 +00:00
|
|
|
description?: string;
|
|
|
|
};
|
|
|
|
|
2022-08-26 21:10:12 +00:00
|
|
|
export type CloseComFieldOptions = [string, string, boolean, boolean][];
|
|
|
|
|
2022-08-15 20:45:15 +00:00
|
|
|
export type CloseComLeadCreateResult = {
|
|
|
|
status_id: string;
|
|
|
|
status_label: string;
|
|
|
|
display_name: string;
|
|
|
|
addresses: { [key: string]: any }[];
|
|
|
|
name: string;
|
|
|
|
contacts: { [key: string]: any }[];
|
|
|
|
[key: CloseComCustomActivityCustomField<string>]: string;
|
|
|
|
id: string;
|
2022-06-18 00:58:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type CloseComStatus = {
|
|
|
|
id: string;
|
|
|
|
organization_id: string;
|
|
|
|
label: string;
|
|
|
|
};
|
|
|
|
|
2022-08-15 20:45:15 +00:00
|
|
|
export type CloseComCustomActivityTypeCreate = {
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type CloseComContactSearch = {
|
|
|
|
data: {
|
|
|
|
__object_type: "contact";
|
|
|
|
emails: {
|
|
|
|
email: string;
|
|
|
|
type: string;
|
|
|
|
}[];
|
|
|
|
id: string;
|
|
|
|
lead_id: string;
|
|
|
|
name: string;
|
|
|
|
}[];
|
|
|
|
cursor: null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type CloseComCustomActivityTypeGet = {
|
|
|
|
data: {
|
|
|
|
api_create_only: boolean;
|
|
|
|
created_by: string;
|
|
|
|
date_created: string;
|
|
|
|
date_updated: string;
|
|
|
|
description: string;
|
|
|
|
editable_with_roles: string[];
|
|
|
|
fields: CloseComCustomActivityFieldGet["data"][number][];
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
organization_id: string;
|
|
|
|
updated_by: string;
|
|
|
|
}[];
|
|
|
|
cursor: null;
|
|
|
|
};
|
|
|
|
|
2022-08-26 21:10:12 +00:00
|
|
|
export type CloseComCustomActivityFieldCreate = CloseComCustomContactFieldCreate & {
|
2022-08-15 20:45:15 +00:00
|
|
|
custom_activity_type_id: string;
|
2022-08-26 21:10:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type CloseComCustomContactFieldGet = {
|
|
|
|
data: {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
type: string;
|
|
|
|
choices?: string[];
|
|
|
|
accepts_multiple_values: boolean;
|
|
|
|
editable_with_roles: string[];
|
|
|
|
date_created: string;
|
|
|
|
date_updated: string;
|
|
|
|
created_by: string;
|
|
|
|
updated_by: string;
|
|
|
|
organization_id: string;
|
|
|
|
}[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export type CloseComCustomContactFieldCreate = {
|
2022-08-15 20:45:15 +00:00
|
|
|
name: string;
|
2022-08-26 21:10:12 +00:00
|
|
|
description?: string;
|
2022-08-15 20:45:15 +00:00
|
|
|
type: string;
|
|
|
|
required: boolean;
|
|
|
|
accepts_multiple_values: boolean;
|
|
|
|
editable_with_roles: string[];
|
2022-08-26 21:10:12 +00:00
|
|
|
choices?: string[];
|
2022-08-15 20:45:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type CloseComCustomActivityFieldGet = {
|
|
|
|
data: {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
type: string;
|
2022-08-26 21:10:12 +00:00
|
|
|
choices?: string[];
|
2022-08-15 20:45:15 +00:00
|
|
|
accepts_multiple_values: boolean;
|
|
|
|
editable_with_roles: string[];
|
|
|
|
date_created: string;
|
|
|
|
date_updated: string;
|
2022-08-26 21:10:12 +00:00
|
|
|
created_by: string;
|
|
|
|
updated_by: string;
|
2022-08-15 20:45:15 +00:00
|
|
|
organization_id: string;
|
2022-08-26 21:10:12 +00:00
|
|
|
custom_activity_type_id: string;
|
2022-08-15 20:45:15 +00:00
|
|
|
}[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export type CloseComCustomActivityCreate = {
|
|
|
|
custom_activity_type_id: string;
|
|
|
|
lead_id: string;
|
|
|
|
[key: CloseComCustomActivityCustomField<string>]: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type typeCloseComCustomActivityGet = {
|
|
|
|
organization_id: string;
|
|
|
|
contact_id: any;
|
|
|
|
date_updated: string;
|
|
|
|
user_name: string;
|
|
|
|
created_by_name: "Bruce Wayne";
|
|
|
|
id: string;
|
|
|
|
created_by: string;
|
|
|
|
status: string;
|
|
|
|
user_id: string;
|
|
|
|
users: any[];
|
|
|
|
lead_id: string;
|
|
|
|
_type: string;
|
|
|
|
updated_by: string;
|
|
|
|
custom_activity_type_id: string;
|
|
|
|
date_created: string;
|
|
|
|
updated_by_name: string;
|
|
|
|
[key: CloseComCustomActivityCustomField<string>]: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type CloseComCustomActivityCustomField<T extends string> = `custom.${T}`;
|
|
|
|
|
|
|
|
const environmentApiKey = process.env.CLOSECOM_API_KEY || "";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class to instance communicating to Close.com 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 Close.com is just for
|
|
|
|
* one account only, not configurable by any user at any moment.
|
|
|
|
*/
|
2022-06-18 00:58:27 +00:00
|
|
|
export default class CloseCom {
|
2022-08-15 20:45:15 +00:00
|
|
|
private apiUrl = "https://api.close.com/api/v1";
|
2022-06-18 00:58:27 +00:00
|
|
|
private apiKey: string | undefined = undefined;
|
2022-08-15 20:45:15 +00:00
|
|
|
private log: typeof logger;
|
2022-06-18 00:58:27 +00:00
|
|
|
|
2022-08-15 20:45:15 +00:00
|
|
|
constructor(providedApiKey = "") {
|
2022-08-26 21:10:12 +00:00
|
|
|
this.log = logger.getChildLogger({ prefix: [`[[lib] close.com`] });
|
2022-08-15 20:45:15 +00:00
|
|
|
if (!providedApiKey && !environmentApiKey) throw Error("Close.com Api Key not present");
|
|
|
|
this.apiKey = providedApiKey || environmentApiKey;
|
2022-06-18 00:58:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 20:45:15 +00:00
|
|
|
public me = async () => {
|
|
|
|
return this._get({ urlPath: "/me/" });
|
|
|
|
};
|
|
|
|
|
2022-06-18 00:58:27 +00:00
|
|
|
public contact = {
|
2022-08-15 20:45:15 +00:00
|
|
|
search: async ({ emails }: { emails: string[] }): Promise<CloseComContactSearch> => {
|
2022-06-18 00:58:27 +00:00
|
|
|
return this._post({
|
2022-08-15 20:45:15 +00:00
|
|
|
urlPath: "/data/search/",
|
|
|
|
data: closeComQueries.contact.search(emails),
|
2022-06-18 00:58:27 +00:00
|
|
|
});
|
|
|
|
},
|
2022-08-15 20:45:15 +00:00
|
|
|
create: async (data: {
|
2022-08-26 21:10:12 +00:00
|
|
|
person: { name: string | null; email: string };
|
2022-08-15 20:45:15 +00:00
|
|
|
leadId: string;
|
|
|
|
}): Promise<CloseComContactSearch["data"][number]> => {
|
|
|
|
return this._post({ urlPath: "/contact/", data: closeComQueries.contact.create(data) });
|
|
|
|
},
|
2022-08-26 21:10:12 +00:00
|
|
|
update: async ({
|
|
|
|
contactId,
|
|
|
|
...data
|
|
|
|
}: {
|
|
|
|
person: { name: string; email: string };
|
|
|
|
contactId: string;
|
|
|
|
leadId?: string;
|
|
|
|
}): Promise<CloseComContactSearch["data"][number]> => {
|
|
|
|
return this._put({
|
|
|
|
urlPath: `/contact/${contactId}/`,
|
|
|
|
data: closeComQueries.contact.update(data),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
delete: async (contactId: string) => {
|
|
|
|
return this._delete({
|
|
|
|
urlPath: `/contact/${contactId}/`,
|
|
|
|
});
|
|
|
|
},
|
2022-06-18 00:58:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public lead = {
|
2022-08-15 20:45:15 +00:00
|
|
|
list: async ({
|
|
|
|
query,
|
|
|
|
}: {
|
|
|
|
query: { [key: string]: any };
|
|
|
|
}): Promise<{ data: { [key: string]: any }[] }> => {
|
|
|
|
return this._get({ urlPath: "/lead", query });
|
|
|
|
},
|
2022-06-18 00:58:27 +00:00
|
|
|
status: async () => {
|
2022-08-15 20:45:15 +00:00
|
|
|
return this._get({ urlPath: `/status/lead/` });
|
2022-06-18 00:58:27 +00:00
|
|
|
},
|
2022-08-26 21:10:12 +00:00
|
|
|
update: async (leadId: string, data: CloseComLead): Promise<CloseComLeadCreateResult> => {
|
|
|
|
return this._put({
|
|
|
|
urlPath: `/lead/${leadId}`,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
},
|
2022-08-15 20:45:15 +00:00
|
|
|
create: async (data: CloseComLead): Promise<CloseComLeadCreateResult> => {
|
2022-06-18 00:58:27 +00:00
|
|
|
return this._post({
|
2022-08-15 20:45:15 +00:00
|
|
|
urlPath: "/lead/",
|
|
|
|
data: closeComQueries.lead.create(data),
|
2022-06-18 00:58:27 +00:00
|
|
|
});
|
|
|
|
},
|
2022-08-26 21:10:12 +00:00
|
|
|
delete: async (leadId: string) => {
|
|
|
|
return this._delete({ urlPath: `/lead/${leadId}/` });
|
|
|
|
},
|
2022-06-18 00:58:27 +00:00
|
|
|
};
|
|
|
|
|
2022-08-15 20:45:15 +00:00
|
|
|
public customActivity = {
|
|
|
|
type: {
|
|
|
|
create: async (
|
|
|
|
data: CloseComCustomActivityTypeCreate
|
|
|
|
): Promise<CloseComCustomActivityTypeGet["data"][number]> => {
|
|
|
|
return this._post({
|
|
|
|
urlPath: "/custom_activity",
|
|
|
|
data: closeComQueries.customActivity.type.create(data),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
get: async (): Promise<CloseComCustomActivityTypeGet> => {
|
|
|
|
return this._get({ urlPath: "/custom_activity" });
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
public customField = {
|
|
|
|
activity: {
|
|
|
|
create: async (
|
|
|
|
data: CloseComCustomActivityFieldCreate
|
|
|
|
): Promise<CloseComCustomActivityFieldGet["data"][number]> => {
|
|
|
|
return this._post({ urlPath: "/custom_field/activity/", data });
|
|
|
|
},
|
|
|
|
get: async ({ query }: { query: { [key: string]: any } }): Promise<CloseComCustomActivityFieldGet> => {
|
|
|
|
return this._get({ urlPath: "/custom_field/activity/", query });
|
|
|
|
},
|
|
|
|
},
|
2022-08-26 21:10:12 +00:00
|
|
|
contact: {
|
|
|
|
create: async (
|
|
|
|
data: CloseComCustomContactFieldCreate
|
|
|
|
): Promise<CloseComCustomContactFieldGet["data"][number]> => {
|
|
|
|
return this._post({ urlPath: "/custom_field/contact/", data });
|
|
|
|
},
|
|
|
|
get: async ({ query }: { query: { [key: string]: any } }): Promise<CloseComCustomContactFieldGet> => {
|
|
|
|
return this._get({ urlPath: "/custom_field/contact/", query });
|
|
|
|
},
|
|
|
|
},
|
|
|
|
shared: {
|
|
|
|
get: async ({ query }: { query: { [key: string]: any } }): Promise<CloseComCustomContactFieldGet> => {
|
|
|
|
return this._get({ urlPath: "/custom_field/shared/", query });
|
|
|
|
},
|
|
|
|
},
|
2022-08-15 20:45:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public activity = {
|
|
|
|
custom: {
|
|
|
|
create: async (
|
|
|
|
data: CloseComCustomActivityCreate
|
|
|
|
): Promise<CloseComCustomActivityTypeGet["data"][number]> => {
|
|
|
|
return this._post({ urlPath: "/activity/custom/", data });
|
|
|
|
},
|
|
|
|
delete: async (uuid: string) => {
|
|
|
|
return this._delete({ urlPath: `/activity/custom/${uuid}/` });
|
|
|
|
},
|
|
|
|
update: async (
|
|
|
|
uuid: string,
|
|
|
|
data: Partial<CloseComCustomActivityCreate>
|
|
|
|
): Promise<CloseComCustomActivityTypeGet["data"][number]> => {
|
|
|
|
return this._put({ urlPath: `/activity/custom/${uuid}/`, data });
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
private _get = async ({ urlPath, query }: { urlPath: string; query?: { [key: string]: any } }) => {
|
|
|
|
return await this._request({ urlPath, method: "get", query });
|
2022-06-18 00:58:27 +00:00
|
|
|
};
|
2022-08-15 20:45:15 +00:00
|
|
|
private _post = async ({ urlPath, data }: { urlPath: string; data: Record<string, unknown> }) => {
|
|
|
|
return this._request({ urlPath, method: "post", data });
|
2022-06-18 00:58:27 +00:00
|
|
|
};
|
2022-08-15 20:45:15 +00:00
|
|
|
private _put = async ({ urlPath, data }: { urlPath: string; data: Record<string, unknown> }) => {
|
|
|
|
return this._request({ urlPath, method: "put", data });
|
|
|
|
};
|
|
|
|
private _delete = async ({ urlPath }: { urlPath: string }) => {
|
|
|
|
return this._request({ urlPath, method: "delete" });
|
2022-06-18 00:58:27 +00:00
|
|
|
};
|
|
|
|
private _request = async ({
|
2022-08-15 20:45:15 +00:00
|
|
|
urlPath,
|
2022-06-18 00:58:27 +00:00
|
|
|
data,
|
2022-08-15 20:45:15 +00:00
|
|
|
method,
|
|
|
|
query,
|
2022-06-18 00:58:27 +00:00
|
|
|
}: {
|
2022-08-15 20:45:15 +00:00
|
|
|
urlPath: string;
|
2022-06-18 00:58:27 +00:00
|
|
|
method: string;
|
2022-08-15 20:45:15 +00:00
|
|
|
query?: { [key: string]: any };
|
2022-06-18 00:58:27 +00:00
|
|
|
data?: Record<string, unknown>;
|
|
|
|
}) => {
|
2022-08-15 20:45:15 +00:00
|
|
|
this.log.debug(method, urlPath, query, data);
|
2022-06-18 00:58:27 +00:00
|
|
|
const credentials = Buffer.from(`${this.apiKey}:`).toString("base64");
|
|
|
|
const headers = {
|
|
|
|
Authorization: `Basic ${credentials}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
};
|
2022-08-15 20:45:15 +00:00
|
|
|
const queryString = query ? `?${new URLSearchParams(query).toString()}` : "";
|
|
|
|
return await fetch(`${this.apiUrl}${urlPath}${queryString}`, {
|
|
|
|
headers,
|
|
|
|
method,
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
}).then(async (response) => {
|
2022-06-18 00:58:27 +00:00
|
|
|
if (!response.ok) {
|
2022-08-15 20:45:15 +00:00
|
|
|
const message = `[Close.com app] An error has occured: ${response.status}`;
|
|
|
|
this.log.error(await response.json());
|
2022-06-18 00:58:27 +00:00
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
return await response.json();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const closeComQueries = {
|
|
|
|
contact: {
|
2022-08-15 20:45:15 +00:00
|
|
|
search(contactEmails: string[]) {
|
2022-06-18 00:58:27 +00:00
|
|
|
return {
|
|
|
|
limit: null,
|
|
|
|
_fields: {
|
2022-08-15 20:45:15 +00:00
|
|
|
contact: ["id", "name", "emails"],
|
2022-06-18 00:58:27 +00:00
|
|
|
},
|
|
|
|
query: {
|
|
|
|
negate: false,
|
|
|
|
queries: [
|
|
|
|
{
|
|
|
|
negate: false,
|
|
|
|
object_type: "contact",
|
|
|
|
type: "object_type",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
negate: false,
|
|
|
|
queries: [
|
|
|
|
{
|
|
|
|
negate: false,
|
|
|
|
related_object_type: "contact_email",
|
|
|
|
related_query: {
|
|
|
|
negate: false,
|
|
|
|
queries: contactEmails.map((contactEmail) => ({
|
|
|
|
condition: {
|
|
|
|
mode: "full_words",
|
|
|
|
type: "text",
|
|
|
|
value: contactEmail,
|
|
|
|
},
|
|
|
|
field: {
|
|
|
|
field_name: "email",
|
|
|
|
object_type: "contact_email",
|
|
|
|
type: "regular_field",
|
|
|
|
},
|
|
|
|
negate: false,
|
|
|
|
type: "field_condition",
|
|
|
|
})),
|
|
|
|
type: "or",
|
|
|
|
},
|
|
|
|
this_object_type: "contact",
|
|
|
|
type: "has_related",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
type: "and",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
type: "and",
|
|
|
|
},
|
|
|
|
results_limit: null,
|
|
|
|
sort: [],
|
|
|
|
};
|
|
|
|
},
|
2022-08-26 21:10:12 +00:00
|
|
|
create(data: { person: { name: string | null; email: string }; leadId: string }) {
|
2022-08-15 20:45:15 +00:00
|
|
|
return {
|
|
|
|
lead_id: data.leadId,
|
2022-08-26 21:10:12 +00:00
|
|
|
name: data.person.name ?? data.person.email,
|
|
|
|
emails: [{ email: data.person.email, type: "office" }],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
update({ person, leadId, ...rest }: { person: { name: string; email: string }; leadId?: string }) {
|
|
|
|
return {
|
|
|
|
...(leadId && { lead_id: leadId }),
|
|
|
|
name: person.name ?? person.email,
|
|
|
|
emails: [{ email: person.email, type: "office" }],
|
|
|
|
...rest,
|
2022-08-15 20:45:15 +00:00
|
|
|
};
|
|
|
|
},
|
2022-06-18 00:58:27 +00:00
|
|
|
},
|
|
|
|
lead: {
|
2022-08-15 20:45:15 +00:00
|
|
|
create({ companyName, contactEmail, contactName, description }: CloseComLead) {
|
2022-06-18 00:58:27 +00:00
|
|
|
return {
|
|
|
|
name: companyName,
|
2022-08-15 20:45:15 +00:00
|
|
|
...(description ? { description } : {}),
|
|
|
|
...(contactEmail && contactName
|
|
|
|
? {
|
|
|
|
contacts: [
|
|
|
|
{
|
|
|
|
name: contactName,
|
|
|
|
email: contactEmail,
|
|
|
|
emails: [
|
|
|
|
{
|
|
|
|
type: "office",
|
|
|
|
email: contactEmail,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
: {}),
|
2022-06-18 00:58:27 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
2022-08-15 20:45:15 +00:00
|
|
|
customActivity: {
|
|
|
|
type: {
|
|
|
|
create({ name, description }: CloseComCustomActivityTypeCreate) {
|
|
|
|
return {
|
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
api_create_only: false,
|
|
|
|
editable_with_roles: ["admin"],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
customField: {
|
|
|
|
activity: {
|
|
|
|
create({
|
|
|
|
custom_activity_type_id,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
required,
|
|
|
|
accepts_multiple_values,
|
|
|
|
}: CloseComCustomActivityFieldCreate) {
|
|
|
|
return {
|
|
|
|
custom_activity_type_id,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
required,
|
|
|
|
accepts_multiple_values,
|
|
|
|
editable_with_roles: [],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-06-18 00:58:27 +00:00
|
|
|
};
|