|
| 1 | +using Taskdeck.Application.Interfaces; |
| 2 | +using Taskdeck.Domain.Common; |
| 3 | +using Taskdeck.Domain.Entities; |
| 4 | +using Taskdeck.Domain.Exceptions; |
| 5 | + |
| 6 | +namespace Taskdeck.Application.Services; |
| 7 | + |
| 8 | +public class DailySealService : IDailySealService |
| 9 | +{ |
| 10 | + private readonly IUnitOfWork _unitOfWork; |
| 11 | + |
| 12 | + public DailySealService(IUnitOfWork unitOfWork) |
| 13 | + { |
| 14 | + _unitOfWork = unitOfWork; |
| 15 | + } |
| 16 | + |
| 17 | + public async Task<Result<DailySealResponse>> SealDayAsync(Guid userId, DateOnly date, CancellationToken cancellationToken = default) |
| 18 | + { |
| 19 | + if (userId == Guid.Empty) |
| 20 | + return Result.Failure<DailySealResponse>(ErrorCodes.ValidationError, "UserId cannot be empty"); |
| 21 | + |
| 22 | + var now = DateTimeOffset.UtcNow; |
| 23 | + |
| 24 | + if (date > DateOnly.FromDateTime(now.UtcDateTime)) |
| 25 | + return Result.Failure<DailySealResponse>(ErrorCodes.ValidationError, "Cannot seal a future date"); |
| 26 | + |
| 27 | + var snapshot = await _unitOfWork.DailySnapshots.GetByUserAndDateAsync(userId, date, cancellationToken); |
| 28 | + |
| 29 | + var wasAlreadySealed = false; |
| 30 | + |
| 31 | + if (snapshot is null) |
| 32 | + { |
| 33 | + snapshot = new DailySnapshot(userId, date, now); |
| 34 | + snapshot.Seal(now); |
| 35 | + await _unitOfWork.DailySnapshots.AddAsync(snapshot, cancellationToken); |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + wasAlreadySealed = snapshot.IsSealed; |
| 40 | + snapshot.Seal(now); |
| 41 | + } |
| 42 | + |
| 43 | + await _unitOfWork.SaveChangesAsync(cancellationToken); |
| 44 | + |
| 45 | + // Re-fetch to ensure accurate response after potential concurrent seal resolution. |
| 46 | + // If TryResolveDuplicateDailySnapshotConflicts detached our entity, the persisted |
| 47 | + // snapshot will have a different Id — meaning another request won the race. |
| 48 | + var persisted = await _unitOfWork.DailySnapshots.GetByUserAndDateAsync(userId, date, cancellationToken); |
| 49 | + if (persisted != null && persisted.Id != snapshot.Id) |
| 50 | + { |
| 51 | + return Result.Success(new DailySealResponse(persisted.SealedAt!.Value, WasAlreadySealed: true)); |
| 52 | + } |
| 53 | + |
| 54 | + return Result.Success(new DailySealResponse(snapshot.SealedAt!.Value, wasAlreadySealed)); |
| 55 | + } |
| 56 | + |
| 57 | + public async Task<Result<DailySealStatusResponse>> GetSealStatusAsync(Guid userId, DateOnly date, CancellationToken cancellationToken = default) |
| 58 | + { |
| 59 | + if (userId == Guid.Empty) |
| 60 | + return Result.Failure<DailySealStatusResponse>(ErrorCodes.ValidationError, "UserId cannot be empty"); |
| 61 | + |
| 62 | + var snapshot = await _unitOfWork.DailySnapshots.GetByUserAndDateAsync(userId, date, cancellationToken); |
| 63 | + |
| 64 | + if (snapshot is null) |
| 65 | + return Result.Success(new DailySealStatusResponse(date, false, null)); |
| 66 | + |
| 67 | + return Result.Success(new DailySealStatusResponse(date, snapshot.IsSealed, snapshot.SealedAt)); |
| 68 | + } |
| 69 | +} |
0 commit comments