Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { EventStorage, SessionEvent } from "@hypr/store";
import { format, safeParseDate, TZDate } from "@hypr/utils";

import { id } from "../../../../utils";
import {
Expand Down Expand Up @@ -97,8 +98,13 @@ export function syncSessionEmbeddedEvents(
timezone?: string,
): void {
const incomingByKey = new Map<string, IncomingEvent>();
const incomingByTrackingId = new Map<string, IncomingEvent[]>();
for (const event of incoming) {
incomingByKey.set(eventMatchingKey(event, timezone), event);
const tid = event.tracking_id_event;
const arr = incomingByTrackingId.get(tid) ?? [];
arr.push(event);
incomingByTrackingId.set(tid, arr);
}

ctx.store.transaction(() => {
Expand All @@ -107,7 +113,14 @@ export function syncSessionEmbeddedEvents(
if (!sessionEvent) return;
const key = sessionEventMatchingKey(sessionEvent, timezone);

const incomingEvent = incomingByKey.get(key);
let incomingEvent = incomingByKey.get(key);
if (!incomingEvent) {
incomingEvent = findIncomingByTrackingIdAndDay(
incomingByTrackingId,
sessionEvent,
timezone,
);
}
if (!incomingEvent) return;

const calendarId =
Expand All @@ -134,3 +147,26 @@ export function syncSessionEmbeddedEvents(
});
});
}

function dayFromDateLocal(
dateStr: string | null | undefined,
timezone?: string,
): string {
const parsed = safeParseDate(dateStr);
if (!parsed) return "1970-01-01";
const date = timezone ? new TZDate(parsed, timezone) : parsed;
return format(date, "yyyy-MM-dd");
}

function findIncomingByTrackingIdAndDay(
incomingByTrackingId: Map<string, IncomingEvent[]>,
sessionEvent: SessionEvent,
timezone?: string,
): IncomingEvent | undefined {
const candidates = incomingByTrackingId.get(sessionEvent.tracking_id);
if (!candidates) return undefined;
const sessionDay = dayFromDateLocal(sessionEvent.started_at, timezone);
return candidates.find(
(e) => dayFromDateLocal(e.started_at, timezone) === sessionDay,
);
}
23 changes: 23 additions & 0 deletions apps/desktop/src/utils/session-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,34 @@ export function findSessionByKey(
if (!sessionEvent) return;
if (sessionEventMatchingKey(sessionEvent, timezone) === key) {
found = rowId;
return;
}
if (matchesWithFallback(sessionEvent, key, timezone)) {
found = rowId;
}
});
return found;
}

function matchesWithFallback(
sessionEvent: SessionEvent,
targetKey: string,
timezone?: string,
): boolean {
const colonIdx = targetKey.indexOf(":");
if (colonIdx === -1) {
return false;
}

const targetTrackingId = targetKey.substring(0, colonIdx);
const targetDay = targetKey.substring(colonIdx + 1);
if (sessionEvent.tracking_id !== targetTrackingId) {
return false;
}
const sessionDay = dayFromDate(sessionEvent.started_at, timezone);
return sessionDay === targetDay;
}

export function findSessionByTrackingId(
store: Store,
trackingId: string,
Expand Down
Loading