Skip to content

Commit

Permalink
Revert "* Moved HolidayService to be Consultant dependant."
Browse files Browse the repository at this point in the history
This reverts commit b17033f.
  • Loading branch information
yelodevopsi committed Oct 20, 2023
1 parent b17033f commit d628719
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 82 deletions.
30 changes: 15 additions & 15 deletions backend/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.10"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.10"/>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.10" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.16.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.8" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.10" />
<PackageReference Include="PublicHoliday" Version="2.31.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.10"/>
<PackageReference Include="Microsoft.Identity.Web" Version="1.16.0"/>
<PackageReference Include="Microsoft.OpenApi" Version="1.6.8"/>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.10"/>
<PackageReference Include="PublicHoliday" Version="2.31.0"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.11"/>
</ItemGroup>


<ItemGroup>
<ProjectReference Include="..\Database\Database.csproj" />
<ProjectReference Include="..\Database\Database.csproj"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Database\Database.csproj" />
<ProjectReference Include="..\Database\Database.csproj"/>
</ItemGroup>

<ItemGroup>
<_ContentIncludedByDefault Remove="Routes\obj\Api.csproj.nuget.dgspec.json" />
<_ContentIncludedByDefault Remove="Routes\obj\project.assets.json" />
<_ContentIncludedByDefault Remove="Routes\obj\project.packagespec.json" />
<_ContentIncludedByDefault Remove="Routes\obj\Api.csproj.nuget.dgspec.json"/>
<_ContentIncludedByDefault Remove="Routes\obj\project.assets.json"/>
<_ContentIncludedByDefault Remove="Routes\obj\project.packagespec.json"/>
</ItemGroup>


Expand Down
6 changes: 4 additions & 2 deletions backend/Api/Consultants/ConsultantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ namespace Api.Consultants;
public class ConsultantController : ControllerBase
{
private readonly IMemoryCache _cache;
private readonly ConsultantService _consultantService;
private readonly ApplicationContext _context;

public ConsultantController(ApplicationContext context, IMemoryCache cache)
public ConsultantController(ApplicationContext context, IMemoryCache cache, ConsultantService consultantService)
{
_context = context;
_cache = cache;
_consultantService = consultantService;
}

[HttpGet]
Expand Down Expand Up @@ -79,7 +81,7 @@ private List<ConsultantReadModel> GetConsultantsWithAvailability(string orgUrlKe
}

var consultants = LoadConsultantAvailability(orgUrlKey, numberOfWeeks)
.Select(c => c.MapConsultantToReadModel(numberOfWeeks)).ToList();
.Select(c => _consultantService.MapConsultantToReadModel(c, numberOfWeeks)).ToList();

_cache.Set($"{orgUrlKey}/{CacheKeys.ConsultantAvailability8Weeks}", consultants);
return consultants;
Expand Down
26 changes: 19 additions & 7 deletions backend/Api/Consultants/ConsultantService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
using Api.Options;
using Core.DomainModels;
using Core.Services;
using Microsoft.Extensions.Options;

namespace Api.Consultants;

public static class ConsultantService
public class ConsultantService
{
public static ConsultantReadModel MapConsultantToReadModel(this Consultant consultant, int weeks)
private readonly HolidayService _holidayService;
private readonly OrganizationOptions _organizationOptions;

public ConsultantService(IOptions<OrganizationOptions> orgOptions, HolidayService holidayService)
{
_organizationOptions = orgOptions.Value;
_holidayService = holidayService;
}

public ConsultantReadModel MapConsultantToReadModel(Consultant consultant, int weeks)
{
const double tolerance = 0.1;
var bookedHours = GetBookedHoursForWeeks(consultant, weeks);
Expand All @@ -23,11 +34,12 @@ public static ConsultantReadModel MapConsultantToReadModel(this Consultant consu
);
}

public static double GetBookedHours(this Consultant consultant, int year, int week)
public double GetBookedHours(Consultant consultant, int year, int week)
{
var hoursPrWorkDay = consultant.Department.Organization.HoursPerWorkday;
// var hoursPrWorkDay = _organizationOptions.HoursPerWorkday;

var holidayHours = consultant.GetTotalHolidaysOfWeek(year, week) * hoursPrWorkDay;
var holidayHours = _holidayService.GetTotalHolidaysOfWeek(year, week) * hoursPrWorkDay;
var vacationHours = consultant.Vacations.Count(v => DateService.DateIsInWeek(v.Date, year, week)) *
hoursPrWorkDay;

Expand All @@ -45,7 +57,7 @@ public static double GetBookedHours(this Consultant consultant, int year, int we
return Math.Min(bookedHours, 5 * hoursPrWorkDay);
}

private static List<BookedHoursPerWeek> GetBookedHoursForWeeks(this Consultant consultant, int weeksAhead)
public List<BookedHoursPerWeek> GetBookedHoursForWeeks(Consultant consultant, int weeksAhead)
{
return Enumerable.Range(0, weeksAhead)
.Select(offset =>
Expand All @@ -62,8 +74,8 @@ private static List<BookedHoursPerWeek> GetBookedHoursForWeeks(this Consultant c
.ToList();
}

private static double GetHoursPrWeek(this Consultant consultant)
public double GetHoursPrWeek(Consultant consultant)
{
return consultant.Department.Organization.HoursPerWorkday * 5;
}
}
}
30 changes: 19 additions & 11 deletions backend/Api/Consultants/HolidayService.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
using Core.DomainModels;
using Api.Options;
using Core.Services;
using Microsoft.Extensions.Options;
using PublicHoliday;

namespace Api.Consultants;

public static class HolidayService
public class HolidayService
{
public static int GetTotalHolidaysOfWeek(this Consultant consultant, int year, int week)
private readonly OrganizationOptions _organizationOptions;

public HolidayService(IOptions<OrganizationOptions> options)
{
_organizationOptions = options.Value;
}

public int GetTotalHolidaysOfWeek(int year, int week)
{
var datesOfThisWeek = DateService.GetDatesInWorkWeek(year, week);
return datesOfThisWeek.Count(consultant.IsHoliday);
return datesOfThisWeek.Count(IsHoliday);
}

private static bool IsHoliday(this Consultant consultant, DateOnly day)
private bool IsHoliday(DateOnly day)
{
var holidayCountry = consultant.GetHolidayCountry();
var holidayCountry = GetHolidayCountry();
var isPublicHoliday = holidayCountry.IsPublicHoliday(day.ToDateTime(TimeOnly.MinValue));
return isPublicHoliday || consultant.IsVariantChristmasHoliday(day);
return isPublicHoliday || IsVariantChristmasHoliday(day);
}

private static PublicHolidayBase GetHolidayCountry(this Consultant consultant)
private PublicHolidayBase GetHolidayCountry()
{
var country = consultant.Department.Organization.Country;
var country = _organizationOptions.Country;

return country switch
{
Expand All @@ -31,9 +39,9 @@ private static PublicHolidayBase GetHolidayCountry(this Consultant consultant)
};
}

private static bool IsVariantChristmasHoliday(this Consultant consultant, DateOnly date)
private bool IsVariantChristmasHoliday(DateOnly date)
{
if (consultant.Department.Organization.HasVacationInChristmas) return false;
if (!_organizationOptions.HasVacationInChristmas) return false;

var startDate = new DateOnly(date.Year, 12, 24);
var endDate = new DateOnly(date.Year, 12, 31);
Expand Down
9 changes: 9 additions & 0 deletions backend/Api/Options/OrganizationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Api.Options;

public class OrganizationOptions
{
public double HoursPerWorkday { get; set; }
public bool HasVacationInChristmas { get; set; }
public int NumberOfVacationDaysInYear { get; set; }
public string Country { get; set; }

Check warning on line 8 in backend/Api/Options/OrganizationOptions.cs

View workflow job for this annotation

GitHub Actions / dotnet_core_project_tests (7.x.x)

Non-nullable property 'Country' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
6 changes: 6 additions & 0 deletions backend/Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Api.BuildHelpers;
using Api.Consultants;
using Api.Options;
using Database.DatabaseContext;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Web;
using Microsoft.OpenApi.Models;
Expand All @@ -20,6 +22,10 @@

builder.Services.AddMemoryCache();


builder.Services.Configure<OrganizationOptions>(builder.Configuration.GetSection("OrganizationSettings"));
builder.Services.AddSingleton<HolidayService>();
builder.Services.AddSingleton<ConsultantService>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

Expand Down
66 changes: 19 additions & 47 deletions backend/Tests/AbsenceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,13 @@ public class Tests
[TestCase(0, 0, 0, 37.5, 37.5)]
[TestCase(0, 0, 0, 30, 30)]
[TestCase(0, 7.5, 0, 22.5, 30)]
[Ignore("Ignored until organizations has been rewritten into Holiday-logic and Consultant")]

public void AvailabilityCalculation(int vacationDays, double plannedAbsenceHours, int numberOfHolidays,
double staffedHours,
double expectedBookedHours)
{
var org = new Organization
{
Id = "konsulent-as",
Name = "Konsulent as",
UrlKey = "konsulent-as",
Country = "norway",
NumberOfVacationDaysInYear = 25,
HoursPerWorkday = 7.5,
Departments = new List<Department>(),
HasVacationInChristmas = false,
Customers = new List<Customer>()
};

var department = new Department
{
Id = "barteby",
Name = "Barteby",
Hotkey = 1,
Organization = org,
Consultants = Substitute.For<List<Consultant>>()
};


var department = Substitute.For<Department>();
var consultant = new Consultant
{
Id = 1,
Expand Down Expand Up @@ -99,35 +79,22 @@ public void AvailabilityCalculation(int vacationDays, double plannedAbsenceHours
Hours = staffedHours
});

var bookedHours = consultant.GetBookedHours(year, week);
var organization = new OrganizationOptions();
organization.HoursPerWorkday = 7.5;
organization.HasVacationInChristmas = true;
var orgOptions = Options.Create(organization);
var holidayService = new HolidayService(orgOptions);

var bookedHours =
new ConsultantService(orgOptions, holidayService).GetBookedHours(consultant, year, week);
Assert.That(bookedHours, Is.EqualTo(expectedBookedHours));
}

[Test]
[Ignore("Ignored until organizations has been rewritten into Holiday-logic and Consultant")]
public void MultiplePlannedAbsences()
{
var org = new Organization
{
Id = "konsulent-as",
Name = "Konsulent as",
UrlKey = "konsulent-as",
Country = "norway",
NumberOfVacationDaysInYear = 25,
HoursPerWorkday = 7.5,
Departments = new List<Department>(),
HasVacationInChristmas = false,
Customers = new List<Customer>()
};

var department = new Department
{
Id = "barteby",
Name = "Barteby",
Hotkey = 1,
Organization = org,
Consultants = Substitute.For<List<Consultant>>()
};

var department = Substitute.For<Department>();
var consultant = new Consultant
{
Id = 1,
Expand Down Expand Up @@ -156,8 +123,13 @@ public void MultiplePlannedAbsences()
Hours = 15
});

var organization = new OrganizationOptions();
organization.HoursPerWorkday = 7.5;
var orgOptions = Options.Create(organization);
var holidayService = new HolidayService(orgOptions);

var bookedHours =
consultant.GetBookedHours(year, week);
new ConsultantService(orgOptions, holidayService).GetBookedHours(consultant, year, week);

Assert.That(bookedHours, Is.EqualTo(30));
}
Expand Down

0 comments on commit d628719

Please sign in to comment.