Skip to content

Commit

Permalink
refactor: move holiday into core logic, remove LoadHolidays from Stor…
Browse files Browse the repository at this point in the history
…ageService (#528)
  • Loading branch information
jonasbjoralt authored Oct 8, 2024
1 parent d64ebfc commit 5d4ba00
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 127 deletions.
51 changes: 0 additions & 51 deletions backend/Api/Common/StorageService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Api.Consultants;
using Api.Organisation;
using Core.Consultants;
using Core.Customers;
using Core.DomainModels;
Expand Down Expand Up @@ -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
Expand All @@ -492,16 +451,6 @@ public List<Vacation> LoadConsultantVacation(int consultantId)
return _dbContext.Vacation.Where(v => v.ConsultantId == consultantId).ToList();
}

public List<DateOnly>? 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));
Expand Down
71 changes: 0 additions & 71 deletions backend/Api/Organisation/OrganisationHolidayExtensions.cs

This file was deleted.

13 changes: 8 additions & 5 deletions backend/Api/VacationsController/VacationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ public class VacationsController(
public async Task<ActionResult<List<DateOnly>>> 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;
}
Expand Down
65 changes: 65 additions & 0 deletions backend/Core/Organizations/Organization.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Text.Json.Serialization;
using Core.Absences;
using Core.Customers;
using Core.DomainModels;
using PublicHoliday;

namespace Core.Organizations;

Expand All @@ -19,4 +21,67 @@ public class Organization
public required List<Customer> Customers { get; set; }

public List<Absence> AbsenceTypes { get; set; }

Check warning on line 23 in backend/Core/Organizations/Organization.cs

View workflow job for this annotation

GitHub Actions / dotnet_core_project_tests (8.x.x)

Non-nullable property 'AbsenceTypes' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

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<DateOnly> 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;
}
}

0 comments on commit 5d4ba00

Please sign in to comment.