Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion apps/sim/lib/webhooks/polling/gmail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function persistedHistoryIds() {
)
}

describe('gmailPollingHandler expired history cursor', () => {
describe('gmailPollingHandler history cursor', () => {
beforeEach(() => {
mockResolveOAuthCredential.mockResolvedValue('access-token')
mockProcessEvent.mockResolvedValue({ success: true })
Expand Down Expand Up @@ -180,4 +180,14 @@ describe('gmailPollingHandler expired history cursor', () => {

expect(persistedHistoryIds()).toEqual(['40100'])
})

it('leaves the stored cursor untouched when a search-mode poll finds no mail', async () => {
mailboxWithExpiredHistory({ profileHistoryId: '40100', inboxMessages: [] })

await expect(gmailPollingHandler.pollWebhook(context({}))).resolves.toBe('success')

expect(mockUpdateConfig).toHaveBeenCalledTimes(1)
const [, updates] = mockUpdateConfig.mock.calls[0]
expect(updates).not.toHaveProperty('historyId')
})
})
23 changes: 7 additions & 16 deletions apps/sim/lib/webhooks/polling/gmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,14 @@ export const gmailPollingHandler: PollingProviderHandler = {
requestId,
logger
)
const pollState = {
lastCheckedTimestamp: now.toISOString(),
// Omit unless this poll held a cursor, so it can't clear one an overlapping poll stored.
...(config.historyId || latestHistoryId ? { historyId: latestHistoryId } : {}),
}

if (!emails || !emails.length) {
await updateWebhookProviderConfig(
webhookId,
{
lastCheckedTimestamp: now.toISOString(),
historyId: latestHistoryId,
},
logger
)
await updateWebhookProviderConfig(webhookId, pollState, logger)
await markWebhookSuccess(webhookId, logger)
logger.info(`[${requestId}] No new emails found for webhook ${webhookId}`)
return 'success'
Expand All @@ -110,14 +108,7 @@ export const gmailPollingHandler: PollingProviderHandler = {
logger
)

await updateWebhookProviderConfig(
webhookId,
{
lastCheckedTimestamp: now.toISOString(),
historyId: latestHistoryId,
},
logger
)
await updateWebhookProviderConfig(webhookId, pollState, logger)

if (failedCount > 0 && processedCount === 0) {
await markWebhookFailed(webhookId, logger)
Expand Down
Loading