From 5d4ba00d24934f08ef0acc95c35515f6b330d411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B8ralt?= <61310258+jonasbjoralt@users.noreply.github.com> Date: Tue, 8 Oct 2024 15:21:44 +0200 Subject: [PATCH] refactor: move holiday into core logic, remove LoadHolidays from StorageService (#528) --- backend/Api/Common/StorageService.cs | 51 ------------- .../OrganisationHolidayExtensions.cs | 71 ------------------- .../VacationsController.cs | 13 ++-- backend/Core/Organizations/Organization.cs | 65 +++++++++++++++++ 4 files changed, 73 insertions(+), 127 deletions(-) delete mode 100644 backend/Api/Organisation/OrganisationHolidayExtensions.cs diff --git a/backend/Api/Common/StorageService.cs b/backend/Api/Common/StorageService.cs index 4d57200b..a716c5dd 100644 --- a/backend/Api/Common/StorageService.cs +++ b/backend/Api/Common/StorageService.cs @@ -1,5 +1,4 @@ using Api.Consultants; -using Api.Organisation; using Core.Consultants; using Core.Customers; using Core.DomainModels; @@ -424,51 +423,11 @@ public Customer UpdateOrCreateCustomer(Organization org, string customerName, st return customer; } - public Engagement UpdateProjectState(int engagementId, EngagementState projectState, string orgUrlKey) - { - var engagement = _dbContext.Project - .Include(p => p.Customer) - .Include(p => p.Staffings) - .Include(p => p.Consultants) - .SingleOrDefault(p => p.Id == engagementId); - var similarEngagement = _dbContext.Project - //.Include(p => p.Customer) - .Include(p => p.Staffings) - //.Include(p=> p.Consultants) - .SingleOrDefault( - p => p.Customer == engagement.Customer && p.Name == engagement.Name && p.Id != engagementId); - - if (similarEngagement is not null && similarEngagement.State == projectState) - { - similarEngagement.MergeEngagement(engagement); - //_dbContext.SaveChanges(); - //foreach (var staffing in engagement.Staffings) _dbContext.Remove(staffing); - _dbContext.Project.Remove(_dbContext.Project.Find(engagement.Id)); - _dbContext.SaveChanges(); - - //engagement = similarEngagement; - } - else - { - engagement.State = projectState; - _dbContext.SaveChanges(); - } - - _cache.Remove($"{ConsultantCacheKey}/{orgUrlKey}"); - - return engagement; - } - public Engagement? GetProjectById(int id) { return _dbContext.Project.Find(id); } - public Engagement? GetProjectWithCustumerById(int id) - { - return _dbContext.Project.Include(p => p.Customer).SingleOrDefault(p => p.Id == id); - } - public Engagement GetProjectWithOrganisationById(int id) { return _dbContext.Project @@ -492,16 +451,6 @@ public List LoadConsultantVacation(int consultantId) return _dbContext.Vacation.Where(v => v.ConsultantId == consultantId).ToList(); } - public List? LoadPublicHolidays(string orgUrlKey) - { - var org = _dbContext.Organization.SingleOrDefault(org => org.UrlKey == orgUrlKey); - if (org is null) return null; - var year = DateOnly.FromDateTime(DateTime.Now).Year; - //Get the public holidays for the next three years, and return them as a list - return org.GetPublicHolidays(year).Concat(org.GetPublicHolidays(year + 1)) - .Concat(org.GetPublicHolidays(year + 2)).ToList(); - } - public void RemoveVacationDay(int consultantId, DateOnly date, string orgUrlKey) { var vacation = _dbContext.Vacation.Single(v => v.ConsultantId == consultantId && v.Date.Equals(date)); diff --git a/backend/Api/Organisation/OrganisationHolidayExtensions.cs b/backend/Api/Organisation/OrganisationHolidayExtensions.cs deleted file mode 100644 index a0f2ee11..00000000 --- a/backend/Api/Organisation/OrganisationHolidayExtensions.cs +++ /dev/null @@ -1,71 +0,0 @@ -using Core.DomainModels; -using Core.Organizations; -using PublicHoliday; - -namespace Api.Organisation; - -public static class OrganisationHolidayExtensions -{ - public static int GetTotalHolidaysOfWeek(this Organization organization, Week week) - { - var datesOfThisWeek = week.GetDatesInWorkWeek(); - return datesOfThisWeek.Count(organization.IsHoliday); - } - - public static double GetTotalHolidayHoursOfWeek(this Organization organization, Week week) - { - var holidayDays = organization.GetTotalHolidaysOfWeek(week); - return holidayDays * organization.HoursPerWorkday; - } - - private static bool IsHoliday(this Organization organization, DateOnly day) - { - return organization.IsPublicHoliday(day) || organization.IsChristmasHoliday(day); - } - - private static bool IsPublicHoliday(this Organization organization, DateOnly day) - { - var publicHoliday = organization.GetPublicHoliday(); - var isPublicHoliday = publicHoliday.IsPublicHoliday(day.ToDateTime(TimeOnly.MinValue)); - return isPublicHoliday; - } - - private static PublicHolidayBase GetPublicHoliday(this Organization organization) - { - var country = organization.Country; - - return country switch - { - "norway" => new NorwayPublicHoliday(), - "sweden" => new SwedenPublicHoliday(), - _ => new NorwayPublicHoliday() - }; - } - - private static bool IsChristmasHoliday(this Organization organization, DateOnly date) - { - if (!organization.HasVacationInChristmas) return false; - - var startDate = new DateOnly(date.Year, 12, 24); - var endDate = new DateOnly(date.Year, 12, 31); - - return date >= startDate && date <= endDate; - } - - public static List GetPublicHolidays(this Organization organization, int year) - { - var publicHoliday = organization.GetPublicHoliday(); - var publicHolidays = publicHoliday.PublicHolidays(year).Select(DateOnly.FromDateTime).ToList(); - if (organization.HasVacationInChristmas) - { - var startDate = new DateTime(year, 12, 24); - var endDate = new DateTime(year, 12, 31); - var list = Enumerable.Range(0, 1 + endDate.Subtract(startDate).Days) - .Select(offset => DateOnly.FromDateTime(startDate.AddDays(offset))) - .ToList(); - publicHolidays = publicHolidays.Concat(list).Distinct().ToList(); - } - - return publicHolidays; - } -} \ No newline at end of file diff --git a/backend/Api/VacationsController/VacationsController.cs b/backend/Api/VacationsController/VacationsController.cs index 1586c534..1fb4aef0 100644 --- a/backend/Api/VacationsController/VacationsController.cs +++ b/backend/Api/VacationsController/VacationsController.cs @@ -23,12 +23,15 @@ public class VacationsController( public async Task>> GetPublicHolidays([FromRoute] string orgUrlKey, CancellationToken ct) { - var selectedOrg = await organisationRepository.GetOrganizationByUrlKey(orgUrlKey, ct); - if (selectedOrg is null) return BadRequest(); + var organization = await organisationRepository.GetOrganizationByUrlKey(orgUrlKey, ct); + if (organization is null) return BadRequest(); - var service = new StorageService(cache, context); - var publicHolidays = service.LoadPublicHolidays(orgUrlKey); - if (publicHolidays is null) return BadRequest("Something went wrong fetching public holidays"); + var year = DateOnly.FromDateTime(DateTime.Now).Year; + + var publicHolidays = + organization.GetPublicHolidays(year) + .Concat(organization.GetPublicHolidays(year + 1)) + .Concat(organization.GetPublicHolidays(year + 2)).ToList(); return publicHolidays; } diff --git a/backend/Core/Organizations/Organization.cs b/backend/Core/Organizations/Organization.cs index b4623003..96a0cd5f 100644 --- a/backend/Core/Organizations/Organization.cs +++ b/backend/Core/Organizations/Organization.cs @@ -1,6 +1,8 @@ using System.Text.Json.Serialization; using Core.Absences; using Core.Customers; +using Core.DomainModels; +using PublicHoliday; namespace Core.Organizations; @@ -19,4 +21,67 @@ public class Organization public required List Customers { get; set; } public List AbsenceTypes { get; set; } + + public int GetTotalHolidaysOfWeek(Week week) + { + var datesOfThisWeek = week.GetDatesInWorkWeek(); + return datesOfThisWeek.Count(IsHoliday); + } + + public double GetTotalHolidayHoursOfWeek(Week week) + { + var holidayDays = GetTotalHolidaysOfWeek(week); + return holidayDays * HoursPerWorkday; + } + + private bool IsHoliday(DateOnly day) + { + return IsPublicHoliday(day) || IsChristmasHoliday(day); + } + + private bool IsPublicHoliday(DateOnly day) + { + var publicHoliday = GetPublicHoliday(); + var isPublicHoliday = publicHoliday.IsPublicHoliday(day.ToDateTime(TimeOnly.MinValue)); + return isPublicHoliday; + } + + private PublicHolidayBase GetPublicHoliday() + { + var country = Country; + + return country switch + { + "norway" => new NorwayPublicHoliday(), + "sweden" => new SwedenPublicHoliday(), + _ => new NorwayPublicHoliday() + }; + } + + private bool IsChristmasHoliday(DateOnly date) + { + if (!HasVacationInChristmas) return false; + + var startDate = new DateOnly(date.Year, 12, 24); + var endDate = new DateOnly(date.Year, 12, 31); + + return date >= startDate && date <= endDate; + } + + public List GetPublicHolidays(int year) + { + var publicHoliday = GetPublicHoliday(); + var publicHolidays = publicHoliday.PublicHolidays(year).Select(DateOnly.FromDateTime).ToList(); + if (HasVacationInChristmas) + { + var startDate = new DateTime(year, 12, 24); + var endDate = new DateTime(year, 12, 31); + var list = Enumerable.Range(0, 1 + endDate.Subtract(startDate).Days) + .Select(offset => DateOnly.FromDateTime(startDate.AddDays(offset))) + .ToList(); + publicHolidays = publicHolidays.Concat(list).Distinct().ToList(); + } + + return publicHolidays; + } } \ No newline at end of file