import { Score } from '../shared/types.js'; export interface ConflictResult { accept: boolean; reason?: string; } /** * Determine whether an incoming score should be accepted over an existing one. * Uses last-write-wins strategy based on updatedAt timestamps. * * - If no existing score → accept * - If incoming.updatedAt > existing.updatedAt → accept (last-write-wins) * - If incoming.updatedAt <= existing.updatedAt → reject */ export function shouldAcceptScore( incoming: Score, existing: Score | undefined ): ConflictResult { if (!existing) { return { accept: true }; } if (incoming.updatedAt > existing.updatedAt) { return { accept: true }; } return { accept: false, reason: 'Newer score already exists' }; } /** * Resolve a score conflict with optional force override. * * - If force is true → always accept * - Otherwise delegate to shouldAcceptScore (last-write-wins) */ export function resolveConflict( incoming: Score, existing: Score | undefined, force?: boolean ): ConflictResult { if (force) { return { accept: true }; } return shouldAcceptScore(incoming, existing); }