Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move holiday into core logic, remove LoadHolidays from StorageService #528

Merged
merged 1 commit into from
Oct 8, 2024
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
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 @@ -385,7 +384,7 @@
consultant.CompetenceConsultant.Clear();

// For each new competence, create a new CompetenceConsultant entity
foreach (var competence in body?.Competences)

Check warning on line 387 in backend/Api/Common/StorageService.cs

View workflow job for this annotation

GitHub Actions / dotnet_core_project_tests (8.x.x)

Dereference of a possibly null reference.
consultant.CompetenceConsultant.Add(new CompetenceConsultant
{
ConsultantId = consultant.Id,
Expand All @@ -398,7 +397,7 @@
_dbContext.SaveChanges();
ClearConsultantCache(org.UrlKey);

return consultant;

Check warning on line 400 in backend/Api/Common/StorageService.cs

View workflow job for this annotation

GitHub Actions / dotnet_core_project_tests (8.x.x)

Possible null reference return.
}

public Customer UpdateOrCreateCustomer(Organization org, string customerName, string orgUrlKey)
Expand All @@ -424,51 +423,11 @@
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 @@
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 @@ -14,9 +16,72 @@
public required bool HasVacationInChristmas { get; set; }
public required double HoursPerWorkday { get; set; }

[JsonIgnore] public List<Department> Departments { get; set; }

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

View workflow job for this annotation

GitHub Actions / dotnet_core_project_tests (8.x.x)

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

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