using AttendanceHub.Sync.Domain; namespace AttendanceHub.Sync.Domain; /// Canonical raw punch identity. punch_type is metadata and MUST NOT appear here. public readonly record struct PunchIdentity(string DeviceId, string PersonnelCode, DateTimeOffset PunchTime) { public string Key => $"{DeviceId}|{PersonnelCode}|{PunchTime.UtcDateTime:yyyy-MM-dd HH:mm:ss}"; } public enum PunchSource { Realtime = 0, Reconciliation = 1 } /// /// Future / skew classification for a punch_time relative to host clock + tolerance. /// public enum PunchTimeStatus { Acceptable = 0, FutureHold = 1, Invalid = 2, /// /// punch_time is older than the ingest retention window. The outbox keeps only a rolling /// retention of history (matching the reporting window); older punches are already durable in /// MySQL, and re-ingesting them after a prune would create an endless re-upload cycle because a /// full-buffer device read re-delivers the whole device history on every pass. Excluded at the /// ingest path so the ledger stays bounded without ever re-adding pruned history. /// Expired = 3 } /// /// Normalized durable punch. Identity is (DeviceId, PersonnelCode, PunchTime) only. /// public sealed record NormalizedPunch( PunchIdentity Identity, PunchSource Source, int AttendanceState, int VerifyMethod, int WorkCode, bool IsInvalid, int SessionGeneration, string SourceApi, DateTimeOffset FirstSeenAt, DateTimeOffset LastSeenAt, int OccurrenceCount, PunchTimeStatus TimeStatus, int? DeviceYear, int? DeviceMonth, int? DeviceDay, int? DeviceHour, int? DeviceMinute, int? DeviceSecond) { public string DeviceId => Identity.DeviceId; public string PersonnelCode => Identity.PersonnelCode; public DateTimeOffset PunchTime => Identity.PunchTime; public static PunchIdentity MakeIdentity(string deviceId, string personnelCode, DateTimeOffset punchTime) => new(deviceId, personnelCode, punchTime); public static DateTimeOffset ComposePunchTime(int year, int month, int day, int hour, int minute, int second) => new(year, month, day, hour, minute, second, TimeSpan.Zero); public static PunchTimeStatus ClassifyTime( DateTimeOffset punchTime, DateTimeOffset hostNow, TimeSpan futureTolerance, TimeSpan? ingestRetention = null) { var retention = ingestRetention ?? RetentionPolicy.PunchRetention; if (punchTime == default) return PunchTimeStatus.Invalid; if (punchTime.Year < 2000 || punchTime.Year > 2100) return PunchTimeStatus.Invalid; if (punchTime > hostNow.Add(futureTolerance)) return PunchTimeStatus.FutureHold; // Ingest retention: the outbox holds a rolling window of history only. Anything the device // re-delivers that is older than this window is already in MySQL, so drop it here instead of // persisting + uploading it again. Bounds the ledger without an endless prune/re-upload loop. if (punchTime < hostNow - retention) return PunchTimeStatus.Expired; return PunchTimeStatus.Acceptable; } } /// Maps a lightweight realtime/read DTO into a NormalizedPunch (no I/O). public static class PunchNormalizer { public static NormalizedPunch FromRaw( RawAttendanceEvent raw, string deviceId, PunchSource source, DateTimeOffset hostNow, TimeSpan futureTolerance, TimeSpan? ingestRetention = null) { var punchTime = NormalizedPunch.ComposePunchTime(raw.Year, raw.Month, raw.Day, raw.Hour, raw.Minute, raw.Second); var id = NormalizedPunch.MakeIdentity(deviceId, raw.PersonnelCode ?? "", punchTime); var status = NormalizedPunch.ClassifyTime(punchTime, hostNow, futureTolerance, ingestRetention); return new NormalizedPunch( id, source, raw.AttendanceState, raw.VerifyMethod, raw.WorkCode, raw.IsInvalid, raw.Generation, raw.SourceApi, hostNow, hostNow, OccurrenceCount: 1, status, raw.Year, raw.Month, raw.Day, raw.Hour, raw.Minute, raw.Second); } }