Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Orbit.Application/Auth/Commands/SendCodeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public async Task<Result> Handle(SendCodeCommand request, CancellationToken canc
var email = request.Email.Trim().ToLowerInvariant();
var cacheKey = $"verify:{email}";

// Reviewer test account: skip email, store fixed code
var reviewerEmail = Environment.GetEnvironmentVariable("REVIEWER_TEST_EMAIL")?.ToLowerInvariant();
var reviewerCode = Environment.GetEnvironmentVariable("REVIEWER_TEST_CODE");
if (reviewerEmail is not null && reviewerCode is not null && email == reviewerEmail)
{
var testEntry = new VerificationEntry(reviewerCode, 0, DateTime.UtcNow);
cache.Set(cacheKey, testEntry, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30)
});
return Result.Success();
}

// Rate limit: no new code within 60 seconds
if (cache.TryGetValue(cacheKey, out VerificationEntry? existing) && existing is not null)
{
Expand Down
23 changes: 19 additions & 4 deletions src/Orbit.Application/Habits/Queries/GetHabitScheduleQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,25 @@ public record GetHabitScheduleQuery(
int PageSize = 50) : IRequest<PaginatedResponse<HabitScheduleItem>>;

public class GetHabitScheduleQueryHandler(
IGenericRepository<Habit> habitRepository) : IRequestHandler<GetHabitScheduleQuery, PaginatedResponse<HabitScheduleItem>>
IGenericRepository<Habit> habitRepository,
IUserDateService userDateService,
IUnitOfWork unitOfWork) : IRequestHandler<GetHabitScheduleQuery, PaginatedResponse<HabitScheduleItem>>
{
public async Task<PaginatedResponse<HabitScheduleItem>> Handle(GetHabitScheduleQuery request, CancellationToken cancellationToken)
{
// Advance stale bad habit DueDates so they show on the correct next scheduled day
var today = await userDateService.GetUserTodayAsync(request.UserId, cancellationToken);
var staleBadHabits = await habitRepository.FindTrackedAsync(
h => h.UserId == request.UserId && h.IsBadHabit && h.FrequencyUnit != null && h.DueDate < today,
cancellationToken);

if (staleBadHabits.Count > 0)
{
foreach (var habit in staleBadHabits)
habit.AdvanceDueDate(today);
await unitOfWork.SaveChangesAsync(cancellationToken);
}

var allHabits = await habitRepository.FindAsync(
h => h.UserId == request.UserId,
q => q.Include(h => h.Tags),
Expand Down Expand Up @@ -127,7 +142,7 @@ bool HasDescendantWithTag(Guid parentId)
var scheduledDates = HabitScheduleService.GetScheduledDates(habit, request.DateFrom, request.DateTo);
var isOverdue = false;

if (request.IncludeOverdue && !habit.IsCompleted && habit.DueDate < request.DateFrom)
if (request.IncludeOverdue && !habit.IsCompleted && !habit.IsBadHabit && habit.DueDate < request.DateFrom)
{
isOverdue = true;
}
Expand Down Expand Up @@ -193,7 +208,7 @@ private static bool HasAnyDescendantDue(Guid parentId, ILookup<Guid?, Habit> loo
{
if (HabitScheduleService.GetScheduledDates(child, dateFrom, dateTo).Count > 0)
return true;
if (!child.IsCompleted && child.DueDate < dateFrom)
if (!child.IsCompleted && !child.IsBadHabit && child.DueDate < dateFrom)
return true;
if (HasAnyDescendantDue(child.Id, lookup, dateFrom, dateTo))
return true;
Expand All @@ -205,7 +220,7 @@ private static List<HabitScheduleChildItem> MapChildren(Guid parentId, ILookup<G
lookup[parentId]
.Where(c => HabitScheduleService.GetScheduledDates(c, dateFrom, dateTo).Count > 0
|| c.IsCompleted
|| (!c.IsCompleted && c.DueDate < dateFrom)
|| (!c.IsCompleted && !c.IsBadHabit && c.DueDate < dateFrom)
|| HasAnyDescendantDue(c.Id, lookup, dateFrom, dateTo))
.OrderBy(c => c.Position ?? int.MaxValue)
.ThenBy(c => c.CreatedAtUtc)
Expand Down
2 changes: 1 addition & 1 deletion src/Orbit.Domain/Entities/Habit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Result<HabitLog> Log(DateOnly date, string? note = null)
return Result.Success(log);
}

private void AdvanceDueDate(DateOnly today)
public void AdvanceDueDate(DateOnly today)
{
do
{
Expand Down
1 change: 1 addition & 0 deletions src/Orbit.Domain/Interfaces/IGenericRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface IGenericRepository<T> where T : Entity
Task<IReadOnlyList<T>> FindAsync(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default);
Task<IReadOnlyList<T>> FindAsync(Expression<Func<T, bool>> predicate, Func<IQueryable<T>, IQueryable<T>>? includes, CancellationToken cancellationToken = default);
Task<T?> FindOneTrackedAsync(Expression<Func<T, bool>> predicate, Func<IQueryable<T>, IQueryable<T>>? includes = null, CancellationToken cancellationToken = default);
Task<IReadOnlyList<T>> FindTrackedAsync(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default);
Task AddAsync(T entity, CancellationToken cancellationToken = default);
void Update(T entity);
void Remove(T entity);
Expand Down
7 changes: 7 additions & 0 deletions src/Orbit.Infrastructure/Persistence/GenericRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public async Task<IReadOnlyList<T>> FindAsync(
return await query.FirstOrDefaultAsync(predicate, cancellationToken);
}

public async Task<IReadOnlyList<T>> FindTrackedAsync(
Expression<Func<T, bool>> predicate,
CancellationToken cancellationToken = default)
{
return await _dbSet.Where(predicate).ToListAsync(cancellationToken);
}

public async Task AddAsync(T entity, CancellationToken cancellationToken = default)
{
await _dbSet.AddAsync(entity, cancellationToken);
Expand Down