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

57 lines
2.8 KiB
TypeScript

// AK-P16a · conflict resolution decision (PURE) — Last-Write-Wins (M25 Block F).
//
// When a calendar event changed on BOTH sides between two syncs, HiveGrid resolves
// with Last-Write-Wins and tells the user in Klartext (DoD 4). This module holds
// the DECISION only (no DB, no network) so it is unit-tested in isolation; the
// ConflictResolver service applies the decision (persist + notify + conflict-log).
//
// Default tie-breaker: the EXTERNAL provider wins (M25 Block F-1) — the user can
// then manually re-apply their HiveGrid edit from the Klartext notification.
/** Mirror of the Prisma enum CalendarConflictResolution (kept in sync). */
export type ConflictResolution = "external_won" | "hivegrid_won" | "external_deleted" | "hivegrid_deleted";
export interface ConflictInput {
/** Last HiveGrid-side modification (null = unknown/never). */
readonly internalLastModified: Date | null;
/** Last external modification reported by the provider (null = unknown). */
readonly externalLastModified: Date | null;
/** The HiveGrid-internal event was deleted. */
readonly internalDeleted: boolean;
/** The external mirror was deleted/cancelled at the provider. */
readonly externalDeleted: boolean;
}
export interface ConflictDecision {
readonly resolution: ConflictResolution;
/** Which side's state should be applied. `none` = no content overwrite. */
readonly winner: "external" | "hivegrid" | "none";
/** i18n key for the Klartext conflict-log entry + notification (M002/M003). */
readonly descriptionKey: string;
}
/**
* Decide a single event conflict. Deletion beats edit; otherwise the most-recent
* write wins, with the external provider winning ties (authoritative default).
*/
export function resolveConflict(input: ConflictInput): ConflictDecision {
// Deletion cases first — a delete is a terminal state, not a timestamp race.
if (input.externalDeleted && !input.internalDeleted) {
// External mirror gone; the HiveGrid event stays (e.g. a Booking still exists).
return { resolution: "external_deleted", winner: "none", descriptionKey: "calendar.conflict.externalDeleted" };
}
if (input.internalDeleted) {
// HiveGrid event deleted → push the delete to the external mirror.
return { resolution: "hivegrid_deleted", winner: "hivegrid", descriptionKey: "calendar.conflict.hivegridDeleted" };
}
const internalAt = input.internalLastModified?.getTime() ?? 0;
const externalAt = input.externalLastModified?.getTime() ?? 0;
if (internalAt > externalAt) {
return { resolution: "hivegrid_won", winner: "hivegrid", descriptionKey: "calendar.conflict.hivegridWon" };
}
// external newer OR tie → external wins (authoritative default, M25 Block F-1).
return { resolution: "external_won", winner: "external", descriptionKey: "calendar.conflict.externalWon" };
}