1- import type { Store } from "../../../store/tinybase/main " ;
1+ import type { Ctx } from "../ctx " ;
22import type { ExistingEvent , IncomingEvent } from "../fetch/types" ;
33import type { SyncInput , SyncOutput } from "./types" ;
44import { 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