Fix sync token is no longer valid in calendar sync (#5563)
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._
This commit is contained in:
@ -117,7 +117,7 @@ export class CalendarChannelRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async updateSyncCursor(
|
public async updateSyncCursor(
|
||||||
syncCursor: string,
|
syncCursor: string | null,
|
||||||
calendarChannelId: string,
|
calendarChannelId: string,
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
transactionManager?: EntityManager,
|
transactionManager?: EntityManager,
|
||||||
@ -127,7 +127,7 @@ export class CalendarChannelRepository {
|
|||||||
|
|
||||||
await this.workspaceDataSourceService.executeRawQuery(
|
await this.workspaceDataSourceService.executeRawQuery(
|
||||||
`UPDATE ${dataSourceSchema}."calendarChannel" SET "syncCursor" = $1 WHERE "id" = $2`,
|
`UPDATE ${dataSourceSchema}."calendarChannel" SET "syncCursor" = $1 WHERE "id" = $2`,
|
||||||
[syncCursor, calendarChannelId],
|
[syncCursor || '', calendarChannelId],
|
||||||
workspaceId,
|
workspaceId,
|
||||||
transactionManager,
|
transactionManager,
|
||||||
);
|
);
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
|
|||||||
|
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { calendar_v3 as calendarV3 } from 'googleapis';
|
import { calendar_v3 as calendarV3 } from 'googleapis';
|
||||||
|
import { GaxiosError } from 'gaxios';
|
||||||
|
|
||||||
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
|
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
|
||||||
import { BlocklistRepository } from 'src/modules/connected-account/repositories/blocklist.repository';
|
import { BlocklistRepository } from 'src/modules/connected-account/repositories/blocklist.repository';
|
||||||
@ -320,14 +321,38 @@ export class GoogleCalendarSyncService {
|
|||||||
let hasMoreEvents = true;
|
let hasMoreEvents = true;
|
||||||
|
|
||||||
while (hasMoreEvents) {
|
while (hasMoreEvents) {
|
||||||
const googleCalendarEvents = await googleCalendarClient.events.list({
|
const googleCalendarEvents = await googleCalendarClient.events
|
||||||
calendarId: 'primary',
|
.list({
|
||||||
maxResults: 500,
|
calendarId: 'primary',
|
||||||
syncToken: emailOrDomainToReimport ? undefined : syncToken,
|
maxResults: 500,
|
||||||
pageToken: nextPageToken,
|
syncToken: emailOrDomainToReimport ? undefined : syncToken,
|
||||||
q: emailOrDomainToReimport,
|
pageToken: nextPageToken,
|
||||||
showDeleted: true,
|
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;
|
nextSyncToken = googleCalendarEvents.data.nextSyncToken;
|
||||||
nextPageToken = googleCalendarEvents.data.nextPageToken || undefined;
|
nextPageToken = googleCalendarEvents.data.nextPageToken || undefined;
|
||||||
|
|||||||
Reference in New Issue
Block a user