-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Magnus Gule <[email protected]>
- Loading branch information
1 parent
8c37d8d
commit 2801e0b
Showing
24 changed files
with
868 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using Core.DomainModels; | ||
|
||
namespace Api.Consultants; | ||
|
||
public record ConsultantReadModel(int Id, string Name, string Email, List<string> Competences, string Department, | ||
List<BookedHoursPerWeek> Bookings, bool IsOccupied); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using Api.Options; | ||
using Core.DomainModels; | ||
using Core.Services; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Api.Consultants; | ||
|
||
public class ConsultantService | ||
{ | ||
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); | ||
|
||
var isOccupied = bookedHours.All(b => b.BookedHours >= GetHoursPrWeek() - tolerance); | ||
|
||
return new ConsultantReadModel( | ||
consultant.Id, | ||
consultant.Name, | ||
consultant.Email, | ||
consultant.Competences.Select(comp => comp.Name).ToList(), | ||
consultant.Department.Name, | ||
bookedHours, | ||
isOccupied | ||
); | ||
} | ||
|
||
public double GetBookedHours(Consultant consultant, int year, int week) | ||
{ | ||
var hoursPrWorkDay = _organizationOptions.HoursPerWorkday; | ||
|
||
var holidayHours = _holidayService.GetTotalHolidaysOfWeek(year, week) * hoursPrWorkDay; | ||
var vacationHours = consultant.Vacations.Count(v => DateService.DateIsInWeek(v.Date, year, week)) * | ||
hoursPrWorkDay; | ||
|
||
var plannedAbsenceHours = consultant.PlannedAbsences | ||
.Where(pa => pa.Year == year && pa.WeekNumber == week) | ||
.Select(pa => pa.Hours) | ||
.Sum(); | ||
|
||
var staffedHours = consultant.Staffings | ||
.Where(s => s.Year == year && s.Week == week) | ||
.Select(s => s.Hours) | ||
.Sum(); | ||
|
||
var bookedHours = holidayHours + vacationHours + plannedAbsenceHours + staffedHours; | ||
return Math.Min(bookedHours, 5 * hoursPrWorkDay); | ||
} | ||
|
||
public List<BookedHoursPerWeek> GetBookedHoursForWeeks(Consultant consultant, int weeksAhead) | ||
{ | ||
return Enumerable.Range(0, weeksAhead) | ||
.Select(offset => | ||
{ | ||
var year = DateTime.Today.AddDays(7 * offset).Year; | ||
var week = DateService.GetWeekAhead(offset); | ||
|
||
return new BookedHoursPerWeek( | ||
year, | ||
week, | ||
GetBookedHours(consultant, year, week) | ||
); | ||
}) | ||
.ToList(); | ||
} | ||
|
||
public double GetHoursPrWeek() | ||
{ | ||
return _organizationOptions.HoursPerWorkday * 5; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.