Skip to content

Commit

Permalink
fix: ensure right scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinsandilya committed May 2, 2024
1 parent 42dd10c commit 51ad66f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
7 changes: 6 additions & 1 deletion packages/app-store/hubspot-revert/api/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
teamId: Number(teamId),
});
const tenantId = teamId ? teamId : userId;
const scopes = ["crm.objects.contacts.read", "crm.objects.contacts.write"];
const scopes = [
"crm.objects.contacts.read",
"crm.objects.contacts.write",
"crm.objects.marketing_events.read",
"crm.objects.marketing_events.write",
];
res.status(200).json({
url: `https://app.hubspot.com/oauth/authorize?client_id=${
appKeys.client_id
Expand Down
40 changes: 18 additions & 22 deletions packages/app-store/hubspot-revert/lib/CalendarService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ContactCreateResult = {
};
};

export default class HubspotRevertCalendarService implements Calendar {
export default class HubSpotRevertCalendarService implements Calendar {
private log: typeof logger;
private tenantId: string;
private revertApiKey: string;
Expand Down Expand Up @@ -78,26 +78,22 @@ export default class HubspotRevertCalendarService implements Calendar {
};

private contactSearch = async (event: CalendarEvent) => {
const attendeeEmails = event.attendees.map((attendee) => attendee.email);

const headers = new Headers();
headers.append("x-revert-api-token", this.revertApiKey);
headers.append("x-revert-t-id", this.tenantId);
headers.append("Content-Type", "application/json");

const bodyRaw = JSON.stringify({
searchCriteria: {
filterGroups: [
{
filters: [
{
propertyName: "email",
operator: "IN",
values: attendeeEmails,
},
],
},
],
filterGroups: event.attendees.map((attendee) => ({
filters: [
{
value: attendee.email,
propertyName: "email",
operator: "EQ",
},
],
})),
},
});

Expand All @@ -124,7 +120,7 @@ export default class HubspotRevertCalendarService implements Calendar {
}`;
};

private createHubspotEvent = async (event: CalendarEvent, contacts: CalendarEvent["attendees"]) => {
private createHubSpotEvent = async (event: CalendarEvent, contacts: CalendarEvent["attendees"]) => {
const eventPayload = {
subject: event.title,
startDateTime: event.startTime,
Expand Down Expand Up @@ -187,7 +183,7 @@ export default class HubspotRevertCalendarService implements Calendar {
};

async handleEventCreation(event: CalendarEvent, contacts: CalendarEvent["attendees"]) {
const meetingEvent = await (await this.createHubspotEvent(event, contacts)).json();
const meetingEvent = await (await this.createHubSpotEvent(event, contacts)).json();
if (meetingEvent && meetingEvent.status === "ok") {
this.log.debug("event:creation:ok", { meetingEvent });
return Promise.resolve({
Expand All @@ -200,7 +196,7 @@ export default class HubspotRevertCalendarService implements Calendar {
});
}
this.log.debug("meeting:creation:notOk", { meetingEvent, event, contacts });
return Promise.reject("Something went wrong when creating a meeting in PipedriveCRM");
return Promise.reject("Something went wrong when creating a meeting in HubSpot CRM");
}

async createEvent(event: CalendarEvent): Promise<NewCalendarEventType> {
Expand All @@ -221,7 +217,7 @@ export default class HubspotRevertCalendarService implements Calendar {
});
return await this.handleEventCreation(event, existingPeople);
} else {
// Some attendees don't exist in PipedriveCRM
// Some attendees don't exist in HubSpot CRM
// Get the existing contacts' email to filter out
this.log.debug("contact:search:notAll", { event, contacts });
const existingContacts = contacts.results.map((contact) => contact.email);
Expand All @@ -231,7 +227,7 @@ export default class HubspotRevertCalendarService implements Calendar {
(attendee) => !existingContacts.includes(attendee.email)
);
this.log.debug("contact:filter:nonExisting", { nonExistingContacts });
// Only create contacts in PipedriveCRM that were not present in the previous contact search
// Only create contacts in HubSpot CRM that were not present in the previous contact search
const createdContacts = await this.createContacts(nonExistingContacts);
this.log.debug("contact:created", { createdContacts });
// Continue with event creation and association only when all contacts are present in hubspot
Expand Down Expand Up @@ -265,7 +261,7 @@ export default class HubspotRevertCalendarService implements Calendar {
return await this.handleEventCreation(event, allContacts);
}
return Promise.reject({
calError: "Something went wrong when creating non-existing attendees in PipedriveCRM",
calError: "Something went wrong when creating non-existing attendees in HubSpot CRM",
});
}
} else {
Expand All @@ -287,7 +283,7 @@ export default class HubspotRevertCalendarService implements Calendar {
}
}
return Promise.reject({
calError: "Something went wrong when searching/creating the attendees in PipedriveCRM",
calError: "Something went wrong when searching/creating the attendees in HubSpot CRM",
});
}

Expand Down Expand Up @@ -315,7 +311,7 @@ export default class HubspotRevertCalendarService implements Calendar {
});
}
this.log.debug("meeting:updation:notOk", { meetingEvent, event });
return Promise.reject("Something went wrong when updating a meeting in PipedriveCRM");
return Promise.reject("Something went wrong when updating a meeting in HubSpot CRM");
}

async deleteEvent(uid: string): Promise<void> {
Expand Down
1 change: 0 additions & 1 deletion packages/app-store/zohocrm-revert/api/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
teamId: Number(teamId),
});
const tenantId = teamId ? teamId : userId;
// @TODO check scopes before deployment
const scopes = ["ZohoCRM.modules.ALL", "ZohoCRM.users.READ", "AaaServer.profile.READ"];

const queryParams = {
Expand Down

0 comments on commit 51ad66f

Please sign in to comment.