Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/modules/calendar/services/eventMapping.ts
T
2026-07-08 16:15:37 +02:00

73 lines
2.6 KiB
TypeScript

// AK-P16a · external-event → cache-row mapping (PURE).
//
// Turns a provider-neutral NeutralEvent (from the CalendarAdapter) into the field
// set persisted in CalendarEvent for the external-synced cache. Pure + decoupled
// from Prisma's generated input types so it can be unit-tested with plain objects;
// the db wrapper spreads the result into `db.calendarEvent.upsert`.
import type { NeutralEvent } from "./adapters/CalendarAdapter";
export interface EventOwnerContext {
readonly tenantId: string;
readonly userId: string;
readonly connectionId: string;
readonly provider: "google";
readonly externalCalendarId: string;
}
/** The persisted field set for an external-synced CalendarEvent row. */
export interface ExternalEventFields {
readonly tenantId: string;
readonly userId: string;
readonly connectionId: string;
readonly source: "external_synced";
readonly provider: "google";
readonly externalEventId: string;
readonly externalCalendarId: string;
readonly title: string | null;
readonly description: string | null;
readonly location: string | null;
readonly startsAt: Date;
readonly endsAt: Date;
readonly isAllDay: boolean;
readonly timeZone: string | null;
readonly recurrenceRule: string | null;
readonly attendees: ReadonlyArray<{ email: string; status: string | null }>;
readonly etag: string | null;
readonly lastModifiedExternal: Date | null;
readonly isBusyForAvailability: boolean;
/** Set when the provider cancelled/deleted the event (soft-delete the cache). */
readonly archivedAt: Date | null;
}
/**
* Map one external event to its cache-row fields. A cancelled/deleted provider
* event (`isActive=false`) is soft-archived (`archivedAt`) and forced non-busy so
* it drops out of availability immediately.
*/
export function toExternalEventFields(event: NeutralEvent, ctx: EventOwnerContext, now: Date): ExternalEventFields {
const cancelled = !event.isActive;
return {
tenantId: ctx.tenantId,
userId: ctx.userId,
connectionId: ctx.connectionId,
source: "external_synced",
provider: ctx.provider,
externalEventId: event.externalId,
externalCalendarId: ctx.externalCalendarId,
title: event.title,
description: event.description,
location: event.location,
startsAt: event.startsAt,
endsAt: event.endsAt,
isAllDay: event.isAllDay,
timeZone: event.timeZone,
recurrenceRule: event.recurrenceRule,
attendees: event.attendees,
etag: event.etag,
lastModifiedExternal: event.lastModifiedExternal,
isBusyForAvailability: cancelled ? false : event.isBusy,
archivedAt: cancelled ? now : null,
};
}