Skip to content

Commit

Permalink
Fix sync token is no longer valid in calendar sync (twentyhq#5563)
Browse files Browse the repository at this point in the history
Fix sync token is no longer valid in calendar sync.


https://developers.google.com/apps-script/add-ons/calendar/conferencing/sync-calendar-changes#implement_a_sync_trigger_function
_Caution: Occasionally sync tokens are invalidated by the server,
resulting in a Sync token is no longer valid error. When this happens,
your code should conduct a full sync and replace any stored sync tokens
you have._
  • Loading branch information
bosiraphael authored May 24, 2024
1 parent 87465b1 commit 3680647
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class CalendarChannelRepository {
}

public async updateSyncCursor(
syncCursor: string,
syncCursor: string | null,
calendarChannelId: string,
workspaceId: string,
transactionManager?: EntityManager,
Expand All @@ -127,7 +127,7 @@ export class CalendarChannelRepository {

await this.workspaceDataSourceService.executeRawQuery(
`UPDATE ${dataSourceSchema}."calendarChannel" SET "syncCursor" = $1 WHERE "id" = $2`,
[syncCursor, calendarChannelId],
[syncCursor || '', calendarChannelId],
workspaceId,
transactionManager,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';

import { Repository } from 'typeorm';
import { calendar_v3 as calendarV3 } from 'googleapis';
import { GaxiosError } from 'gaxios';

import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
import { BlocklistRepository } from 'src/modules/connected-account/repositories/blocklist.repository';
Expand Down Expand Up @@ -320,14 +321,38 @@ export class GoogleCalendarSyncService {
let hasMoreEvents = true;

while (hasMoreEvents) {
const googleCalendarEvents = await googleCalendarClient.events.list({
calendarId: 'primary',
maxResults: 500,
syncToken: emailOrDomainToReimport ? undefined : syncToken,
pageToken: nextPageToken,
q: emailOrDomainToReimport,
showDeleted: true,
});
const googleCalendarEvents = await googleCalendarClient.events
.list({
calendarId: 'primary',
maxResults: 500,
syncToken: emailOrDomainToReimport ? undefined : syncToken,
pageToken: nextPageToken,
q: emailOrDomainToReimport,
showDeleted: true,
})
.catch(async (error: GaxiosError) => {
if (error.response?.status !== 410) {
throw error;
}

await this.calendarChannelRepository.updateSyncCursor(
null,
connectedAccountId,
workspaceId,
);

this.logger.log(
`Sync token is no longer valid for connected account ${connectedAccountId} in workspace ${workspaceId}, resetting sync cursor.`,
);

return {
data: {
items: [],
nextSyncToken: undefined,
nextPageToken: undefined,
},
};
});

nextSyncToken = googleCalendarEvents.data.nextSyncToken;
nextPageToken = googleCalendarEvents.data.nextPageToken || undefined;
Expand Down

0 comments on commit 3680647

Please sign in to comment.