* 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>
142 lines
5.5 KiB
TypeScript
142 lines
5.5 KiB
TypeScript
import { MembershipRole } from "@prisma/client";
|
|
|
|
import CloseCom, { CloseComFieldOptions, CloseComLead } from "@calcom/lib/CloseCom";
|
|
import { getCloseComContactIds, getCloseComLeadId, getCustomFieldsIds } from "@calcom/lib/CloseComeUtils";
|
|
import logger from "@calcom/lib/logger";
|
|
import SyncServiceCore, { TeamInfoType } from "@calcom/lib/sync/ISyncService";
|
|
import ISyncService, { ConsoleUserInfoType, WebUserInfoType } from "@calcom/lib/sync/ISyncService";
|
|
|
|
// Cal.com Custom Contact Fields
|
|
const calComCustomContactFields: CloseComFieldOptions = [
|
|
// Field name, field type, required?, multiple values?
|
|
["Username", "text", false, false],
|
|
["Plan", "text", true, false],
|
|
["Last booking", "date", false, false],
|
|
["Created at", "date", true, false],
|
|
];
|
|
|
|
const calComSharedFields: CloseComFieldOptions = [["Contact Role", "text", false, false]];
|
|
|
|
const serviceName = "closecom_service";
|
|
|
|
export default class CloseComService extends SyncServiceCore implements ISyncService {
|
|
protected declare service: CloseCom;
|
|
|
|
constructor() {
|
|
super(serviceName, CloseCom, logger.getChildLogger({ prefix: [`[[sync] ${serviceName}`] }));
|
|
}
|
|
|
|
upsertAnyUser = async (
|
|
user: WebUserInfoType | ConsoleUserInfoType,
|
|
leadInfo?: CloseComLead,
|
|
role?: string
|
|
) => {
|
|
this.log.debug("sync:closecom:user", { user });
|
|
// Get Cal.com Lead
|
|
const leadId = await getCloseComLeadId(this.service, leadInfo);
|
|
this.log.debug("sync:closecom:user:leadId", { leadId });
|
|
// Get Contacts ids: already creates contacts
|
|
const [contactId] = await getCloseComContactIds([user], this.service, leadId);
|
|
this.log.debug("sync:closecom:user:contactsIds", { contactId });
|
|
// Get Custom Contact fields ids
|
|
const customFieldsIds = await getCustomFieldsIds("contact", calComCustomContactFields, this.service);
|
|
this.log.debug("sync:closecom:user:customFieldsIds", { customFieldsIds });
|
|
// Get shared fields ids
|
|
const sharedFieldsIds = await getCustomFieldsIds("shared", calComSharedFields, this.service);
|
|
this.log.debug("sync:closecom:user:sharedFieldsIds", { sharedFieldsIds });
|
|
const allFields = customFieldsIds.concat(sharedFieldsIds);
|
|
this.log.debug("sync:closecom:user:allFields", { allFields });
|
|
const lastBooking = "email" in user ? await this.getUserLastBooking(user) : null;
|
|
this.log.debug("sync:closecom:user:lastBooking", { lastBooking });
|
|
const username = "username" in user ? user.username : null;
|
|
// Prepare values for each Custom Contact Fields
|
|
const allFieldsValues = [
|
|
username, // Username
|
|
user.plan, // Plan
|
|
lastBooking && lastBooking.booking
|
|
? new Date(lastBooking.booking.createdAt).toLocaleDateString("en-US")
|
|
: null, // Last Booking
|
|
user.createdDate,
|
|
role === MembershipRole.OWNER ? "Point of Contact" : "",
|
|
];
|
|
this.log.debug("sync:closecom:contact:allFieldsValues", { allFieldsValues });
|
|
// Preparing Custom Activity Instance data for Close.com
|
|
const person = Object.assign(
|
|
{},
|
|
{
|
|
person: user,
|
|
lead_id: leadId,
|
|
contactId,
|
|
},
|
|
...allFields.map((fieldId: string, index: number) => {
|
|
return {
|
|
[`custom.${fieldId}`]: allFieldsValues[index],
|
|
};
|
|
})
|
|
);
|
|
// Create Custom Activity type instance
|
|
return await this.service.contact.update(person);
|
|
};
|
|
|
|
public console = {
|
|
user: {
|
|
upsert: async (consoleUser: ConsoleUserInfoType) => {
|
|
return this.upsertAnyUser(consoleUser);
|
|
},
|
|
},
|
|
};
|
|
|
|
public web = {
|
|
user: {
|
|
upsert: async (webUser: WebUserInfoType) => {
|
|
return this.upsertAnyUser(webUser);
|
|
},
|
|
delete: async (webUser: WebUserInfoType) => {
|
|
this.log.debug("sync:closecom:web:user:delete", { webUser });
|
|
const [contactId] = await getCloseComContactIds([webUser], this.service);
|
|
this.log.debug("sync:closecom:web:user:delete:contactId", { contactId });
|
|
if (contactId) {
|
|
return this.service.contact.delete(contactId);
|
|
} else {
|
|
throw Error("Web user not found in service");
|
|
}
|
|
},
|
|
},
|
|
team: {
|
|
create: async (team: TeamInfoType, webUser: WebUserInfoType, role: MembershipRole) => {
|
|
return this.upsertAnyUser(
|
|
webUser,
|
|
{
|
|
companyName: team.name,
|
|
},
|
|
role
|
|
);
|
|
},
|
|
delete: async (team: TeamInfoType) => {
|
|
this.log.debug("sync:closecom:web:team:delete", { team });
|
|
const leadId = await getCloseComLeadId(this.service, { companyName: team.name });
|
|
this.log.debug("sync:closecom:web:team:delete:leadId", { leadId });
|
|
this.service.lead.delete(leadId);
|
|
},
|
|
update: async (prevTeam: TeamInfoType, updatedTeam: TeamInfoType) => {
|
|
this.log.debug("sync:closecom:web:team:update", { prevTeam, updatedTeam });
|
|
const leadId = await getCloseComLeadId(this.service, { companyName: prevTeam.name });
|
|
this.log.debug("sync:closecom:web:team:update:leadId", { leadId });
|
|
this.service.lead.update(leadId, { companyName: updatedTeam.name });
|
|
},
|
|
},
|
|
membership: {
|
|
delete: async (webUser: WebUserInfoType) => {
|
|
this.log.debug("sync:closecom:web:membership:delete", { webUser });
|
|
const [contactId] = await getCloseComContactIds([webUser], this.service);
|
|
this.log.debug("sync:closecom:web:membership:delete:contactId", { contactId });
|
|
if (contactId) {
|
|
return this.service.contact.delete(contactId);
|
|
} else {
|
|
throw Error("Web user not found in service");
|
|
}
|
|
},
|
|
},
|
|
};
|
|
}
|