Skip to content

Add Delete functions for Background Sync #3132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 19, 2025
Merged
Changes from 3 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
57 changes: 54 additions & 3 deletions packages/trigger/google-calendar-sync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createAdminClient } from '@tuturuuu/supabase/next/server';
import { WorkspaceCalendarEvent } from '@tuturuuu/types/db';
import {
BACKGROUND_SYNC_RANGE,
updateLastUpsert,
Expand Down Expand Up @@ -114,10 +115,60 @@
});
if (error) {
console.error('Error upserting events:', error);
} else {
// Update lastUpsert timestamp after successful upsert
await updateLastUpsert(ws_id, supabase);
continue;

Check warning on line 118 in packages/trigger/google-calendar-sync.ts

View check run for this annotation

Codecov / codecov/patch

packages/trigger/google-calendar-sync.ts#L118

Added line #L118 was not covered by tests
}
console.log(
'Upserted events for wsId:',
ws_id,
formattedEvents.map((e) => e.title)
);

Check warning on line 124 in packages/trigger/google-calendar-sync.ts

View check run for this annotation

Codecov / codecov/patch

packages/trigger/google-calendar-sync.ts#L120-L124

Added lines #L120 - L124 were not covered by tests

// Google calendar not null
const { data: dbEventsAfterUpsert, error: dbError } = await supabase
.from('workspace_calendar_events')
.select('*')
.eq('ws_id', ws_id)
.not('google_event_id', 'is', null)
.gte('start_at', timeMin.toISOString())
.lte('start_at', timeMax.toISOString());

Check warning on line 133 in packages/trigger/google-calendar-sync.ts

View check run for this annotation

Codecov / codecov/patch

packages/trigger/google-calendar-sync.ts#L127-L133

Added lines #L127 - L133 were not covered by tests

if (dbError) {
console.error('Error fetching events after upsert:', dbError);
continue;
}

Check warning on line 138 in packages/trigger/google-calendar-sync.ts

View check run for this annotation

Codecov / codecov/patch

packages/trigger/google-calendar-sync.ts#L135-L138

Added lines #L135 - L138 were not covered by tests

const eventsToDelete: WorkspaceCalendarEvent[] = [];
for (const event of dbEventsAfterUpsert) {
if (
!formattedEvents.some(
(e) => e.google_event_id === event.google_event_id
)
) {
eventsToDelete.push(event);
}
}

Check warning on line 149 in packages/trigger/google-calendar-sync.ts

View check run for this annotation

Codecov / codecov/patch

packages/trigger/google-calendar-sync.ts#L140-L149

Added lines #L140 - L149 were not covered by tests

const { error: deleteError } = await supabase
.from('workspace_calendar_events')
.delete()
.in(
'id',
eventsToDelete.map((e) => e.id)
);

Check warning on line 157 in packages/trigger/google-calendar-sync.ts

View check run for this annotation

Codecov / codecov/patch

packages/trigger/google-calendar-sync.ts#L151-L157

Added lines #L151 - L157 were not covered by tests

if (deleteError) {
console.error('Error deleting events:', deleteError);
continue;
}

Check warning on line 162 in packages/trigger/google-calendar-sync.ts

View check run for this annotation

Codecov / codecov/patch

packages/trigger/google-calendar-sync.ts#L159-L162

Added lines #L159 - L162 were not covered by tests

console.log(
'Deleted events for wsId:',
ws_id,
eventsToDelete.map((e) => e.title)
);

Check warning on line 168 in packages/trigger/google-calendar-sync.ts

View check run for this annotation

Codecov / codecov/patch

packages/trigger/google-calendar-sync.ts#L164-L168

Added lines #L164 - L168 were not covered by tests

// Update lastUpsert timestamp after successful upsert
await updateLastUpsert(ws_id, supabase);

Check warning on line 171 in packages/trigger/google-calendar-sync.ts

View check run for this annotation

Codecov / codecov/patch

packages/trigger/google-calendar-sync.ts#L171

Added line #L171 was not covered by tests
} catch (error) {
console.error('Error fetching Google Calendar events:', error);
}
Expand Down