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
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public CreateHabitCommandValidator()

SharedHabitRules.AddOneTimeTaskEndDateRules(this,
x => x.Options != null ? x.Options.EndDate : null,
x => x.FrequencyUnit,
x => x.IsGeneral);
x => x.FrequencyUnit);

RuleFor(x => x.SubHabits)
.Must(subs => subs is null || subs.Count <= AppConstants.MaxSubHabits)
Expand Down
9 changes: 3 additions & 6 deletions src/Orbit.Application/Habits/Validators/SharedHabitRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,13 @@ public static void AddDaysRules<T>(
public static void AddOneTimeTaskEndDateRules<T>(
AbstractValidator<T> validator,
System.Linq.Expressions.Expression<Func<T, DateOnly?>> endDateExpr,
System.Linq.Expressions.Expression<Func<T, Domain.Enums.FrequencyUnit?>> freqUnitExpr,
System.Linq.Expressions.Expression<Func<T, bool>> isGeneralExpr)
System.Linq.Expressions.Expression<Func<T, Domain.Enums.FrequencyUnit?>> freqUnitExpr)
{
var freqUnitFunc = freqUnitExpr.Compile();
var isGeneralFunc = isGeneralExpr.Compile();

validator.RuleFor(endDateExpr)
.Must((command, endDate) =>
!endDate.HasValue || freqUnitFunc(command) is not null || isGeneralFunc(command))
.WithMessage("A one-time task cannot have an end date");
!endDate.HasValue || freqUnitFunc(command) is not null)
.WithMessage("An end date requires a recurring habit");
}

public static void AddGeneralHabitRules<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public UpdateHabitCommandValidator(IGenericRepository<Habit> habitRepository)

SharedHabitRules.AddOneTimeTaskEndDateRules(this,
x => x.Options != null ? x.Options.EndDate : null,
x => x.FrequencyUnit,
x => x.IsGeneral != false);
x => x.FrequencyUnit);

SharedHabitRules.AddGeneralHabitRules(this,
x => x.IsGeneral,
Expand Down
1 change: 1 addition & 0 deletions src/Orbit.Domain/Common/DomainErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static class DomainErrors
public static readonly AppError DaysRequireQuantityOne = new("DAYS_REQUIRE_QUANTITY_ONE", "Days can only be set when frequency quantity is 1.");
public static readonly AppError EndTimeBeforeStartTime = new("END_TIME_BEFORE_START", "End time must be after start time.");
public static readonly AppError OneTimeTaskHasEndDate = new("ONE_TIME_TASK_HAS_END_DATE", "One-time tasks cannot have an end date.");
public static readonly AppError GeneralHabitHasEndDate = new("GENERAL_HABIT_HAS_END_DATE", "General habits cannot have an end date.");
public static readonly AppError EndDateBeforeStartDate = new("END_DATE_BEFORE_START", "End date must be on or after the start date.");
public static readonly AppError MaxScheduledReminders = new("MAX_SCHEDULED_REMINDERS", "A habit can have at most {0} scheduled reminders.");
public static readonly AppError MaxReminderTimes = new("MAX_REMINDER_TIMES", "A habit can have at most {0} reminder times.");
Expand Down
10 changes: 6 additions & 4 deletions src/Orbit.Domain/Entities/Habit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@
{
IsCompleted = true;
}
else if (FrequencyUnit is not null && !IsFlexible && advanceDueDate)
else if (FrequencyUnit is not null)
{
AdvanceDueDate(date, weekStartDay);
if (!IsFlexible && advanceDueDate)
AdvanceDueDate(date, weekStartDay);

if (ChecklistItems.Count > 0)
ChecklistItems = ChecklistItems.Select(i => i with { IsChecked = false }).ToList();
Expand Down Expand Up @@ -478,7 +479,8 @@

var dateValidation = HabitInvariants.ValidateDateOptions(
p.DueTime ?? DueTime, p.DueEndTime ?? DueEndTime,
p.ClearEndDate == true ? null : (p.EndDate ?? EndDate),
effectiveIsGeneral && p.EndDate.HasValue ? p.EndDate
: p.ClearEndDate == true || effectiveIsGeneral ? null : (p.EndDate ?? EndDate),

Check warning on line 483 in src/Orbit.Domain/Entities/Habit.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Extract this nested ternary operation into an independent statement.

Check warning on line 483 in src/Orbit.Domain/Entities/Habit.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=thomasluizon_orbit-api&issues=AaDecYSzRgBRhcf6mjjt&open=AaDecYSzRgBRhcf6mjjt&pullRequest=586
p.FrequencyUnit, effectiveIsGeneral, p.DueDate ?? DueDate);
if (dateValidation is not null)
return dateValidation;
Expand Down Expand Up @@ -548,7 +550,7 @@
if (p.ScheduledReminders is not null)
ScheduledReminders = p.ScheduledReminders;

if (p.ClearEndDate == true)
if (p.ClearEndDate == true || IsGeneral)
EndDate = null;
else if (p.EndDate.HasValue)
EndDate = p.EndDate.Value;
Expand Down
4 changes: 2 additions & 2 deletions src/Orbit.Domain/Entities/HabitInvariants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// </summary>
internal static class HabitInvariants
{
public static AppError? ValidateScheduleOptions(

Check warning on line 15 in src/Orbit.Domain/Entities/HabitInvariants.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Refactor this method to reduce its Cognitive Complexity from 18 to the 15 allowed.
bool isGeneral, bool isFlexible, bool isBadHabit,
FrequencyUnit? frequencyUnit, int? frequencyQuantity,
IReadOnlyList<System.DayOfWeek>? days,
Expand Down Expand Up @@ -53,8 +53,8 @@
if (dueEndTime.HasValue && dueTime.HasValue && dueEndTime.Value <= dueTime.Value)
return DomainErrors.EndTimeBeforeStartTime;

if (endDate.HasValue && frequencyUnit is null && !isGeneral)
return DomainErrors.OneTimeTaskHasEndDate;
if (endDate.HasValue && frequencyUnit is null)
return isGeneral ? DomainErrors.GeneralHabitHasEndDate : DomainErrors.OneTimeTaskHasEndDate;

if (endDate.HasValue && endDate.Value < dueDate)
return DomainErrors.EndDateBeforeStartDate;
Expand Down
Loading
Loading