Skip to content

Commit

Permalink
fix: [CAL-3578] [CAL-2733] check events in the calendar when checking…
Browse files Browse the repository at this point in the history
… availability
  • Loading branch information
vachmara committed May 6, 2024
1 parent 994d8b0 commit d55aaea
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion packages/app-store/zohocalendar/lib/CalendarService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export default class ZohoCalendarService implements Calendar {

const token = await res.json();

// Revert if access_token is not present
if (!token.access_token) {
throw new Error("Invalid token response");
}

const key: ZohoAuthCredentials = {
access_token: token.access_token,
refresh_token: zohoCredentials.refresh_token,
Expand Down Expand Up @@ -260,6 +265,37 @@ export default class ZohoCalendarService implements Calendar {
);
}

private async getUnavailability(
range: { start: string; end: string },
calendarId: string
): Promise<Array<{ start: string; end: string }>> {
const query = stringify({
range: JSON.stringify(range),
});
this.log.debug("getUnavailability query", query);
try {
// List all events within the range
const response = await this.fetcher(`/calendars/${calendarId}/events?${query}`);
const data = await this.handleData(response, this.log);

// Check for no data scenario
if (!data.events || data.events.length === 0) return [];

return (
data.events
.filter((event: any) => event.isprivate === false)

Check warning on line 286 in packages/app-store/zohocalendar/lib/CalendarService.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app-store/zohocalendar/lib/CalendarService.ts#L286

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
.map((event: any) => {

Check warning on line 287 in packages/app-store/zohocalendar/lib/CalendarService.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app-store/zohocalendar/lib/CalendarService.ts#L287

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
const start = dayjs(event.dateandtime.start, "YYYYMMDD[T]HHmmssZ").utc().toISOString();
const end = dayjs(event.dateandtime.end, "YYYYMMDD[T]HHmmssZ").utc().toISOString();
return { start, end };
}) || []
);
} catch (error) {
this.log.error(error);
return [];
}
}

async getAvailability(
dateFrom: string,
dateTo: string,
Expand Down Expand Up @@ -296,7 +332,22 @@ export default class ZohoCalendarService implements Calendar {
originalEndDate.format("YYYYMMDD[T]HHmmss[Z]"),
userInfo.Email
);
return busyData;

const unavailabilityData = await Promise.all(
queryIds.map((calendarId) =>
this.getUnavailability(
{
start: originalStartDate.format("YYYYMMDD[T]HHmmss[Z]"),
end: originalEndDate.format("YYYYMMDD[T]HHmmss[Z]"),
},
calendarId
)
)
);

const unavailability = unavailabilityData.flat();

return busyData.concat(unavailability);
} else {
// Zoho only supports 31 days of freebusy data
const busyData = [];
Expand All @@ -317,6 +368,22 @@ export default class ZohoCalendarService implements Calendar {
))
);

const unavailabilityData = await Promise.all(
queryIds.map((calendarId) =>
this.getUnavailability(
{
start: startDate.format("YYYYMMDD[T]HHmmss[Z]"),
end: endDate.format("YYYYMMDD[T]HHmmss[Z]"),
},
calendarId
)
)
);

const unavailability = unavailabilityData.flat();

busyData.push(...unavailability);

startDate = endDate.add(1, "minutes");
endDate = startDate.add(30, "days");
}
Expand Down

0 comments on commit d55aaea

Please sign in to comment.