Skip to content

Commit e02291f

Browse files
committed
remove orphan event when doing apple calendar sync (#2508)
1 parent c52b35f commit e02291f

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

apps/desktop/src/services/apple-calendar/fetch/existing.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import type { Ctx } from "../ctx";
22
import type { ExistingEvent } from "./types";
33

44
export function fetchExistingEvents(ctx: Ctx): Array<ExistingEvent> {
5-
const enabledCalendarIds = ctx.calendarIds;
65
const events: Array<ExistingEvent> = [];
76

87
ctx.store.forEachRow("events", (rowId, _forEachCell) => {
98
const event = ctx.store.getRow("events", rowId);
109
if (!event) return;
1110

1211
const calendarId = event.calendar_id as string | undefined;
13-
if (!calendarId || !enabledCalendarIds.has(calendarId)) {
12+
if (!calendarId) {
1413
return;
1514
}
1615

apps/desktop/src/services/apple-calendar/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export async function syncCalendarEvents(
1313
): Promise<void> {
1414
await Promise.all([
1515
new Promise((resolve) => setTimeout(resolve, 250)),
16-
runSyncPipeline(store, queries),
16+
run(store, queries),
1717
]);
1818
}
1919

20-
async function runSyncPipeline(store: Store, queries: Queries<Schemas>) {
20+
async function run(store: Store, queries: Queries<Schemas>) {
2121
const ctx = createCtx(store, queries);
2222
if (!ctx) {
2323
return null;
@@ -26,6 +26,6 @@ async function runSyncPipeline(store: Store, queries: Queries<Schemas>) {
2626
const incoming = await fetchIncomingEvents(ctx);
2727
const existing = fetchExistingEvents(ctx);
2828

29-
const out = sync(ctx.store, { incoming, existing });
29+
const out = sync(ctx, { incoming, existing });
3030
execute(ctx.store, out);
3131
}

apps/desktop/src/services/apple-calendar/process/sync.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import type { Store } from "../../../store/tinybase/main";
1+
import type { Ctx } from "../ctx";
22
import type { ExistingEvent, IncomingEvent } from "../fetch/types";
33
import type { SyncInput, SyncOutput } from "./types";
44
import { getSessionForEvent, isSessionEmpty } from "./utils";
55

6-
export function sync(
7-
store: Store,
8-
{ incoming, existing }: SyncInput,
9-
): SyncOutput {
6+
export function sync(ctx: Ctx, { incoming, existing }: SyncInput): SyncOutput {
107
const out: SyncOutput = {
118
toDelete: [],
129
toUpdate: [],
@@ -17,6 +14,17 @@ export function sync(
1714
const handledIncomingEventIds = new Set<string>();
1815

1916
for (const storeEvent of existing) {
17+
const sessionId = getSessionForEvent(ctx.store, storeEvent.id);
18+
const hasNonEmptySession =
19+
sessionId && !isSessionEmpty(ctx.store, sessionId);
20+
21+
if (!ctx.calendarIds.has(storeEvent.calendar_id!)) {
22+
if (!hasNonEmptySession) {
23+
out.toDelete.push(storeEvent.id);
24+
}
25+
continue;
26+
}
27+
2028
const matchingIncomingEvent = incomingEventMap.get(storeEvent.id);
2129

2230
if (matchingIncomingEvent) {
@@ -30,18 +38,19 @@ export function sync(
3038
continue;
3139
}
3240

33-
const sessionId = getSessionForEvent(store, storeEvent.id);
34-
const hasNonEmptySession = sessionId && !isSessionEmpty(store, sessionId);
35-
3641
if (hasNonEmptySession) {
3742
continue;
3843
}
3944

4045
const rescheduledEvent = findRescheduledEvent(storeEvent, incoming);
4146

4247
if (rescheduledEvent && !handledIncomingEventIds.has(rescheduledEvent.id)) {
43-
out.toDelete.push(storeEvent.id);
44-
out.toAdd.push(rescheduledEvent);
48+
out.toUpdate.push({
49+
...rescheduledEvent,
50+
id: storeEvent.id,
51+
user_id: storeEvent.user_id,
52+
created_at: storeEvent.created_at,
53+
});
4554
handledIncomingEventIds.add(rescheduledEvent.id);
4655
continue;
4756
}

0 commit comments

Comments
 (0)