Skip to content

Commit

Permalink
remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbjoralt committed Nov 6, 2023
1 parent 4262f15 commit 4ebb80d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 112 deletions.
116 changes: 5 additions & 111 deletions backend/Api/Consultants/ConsultantExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,98 +37,6 @@ public static ConsultantReadModel MapToReadModelList(
isOccupied);
}


public static ConsultantReadModel MapConsultantToReadModel(this Consultant consultant, Week firstWeek, int weeks)
{
const double tolerance = 0.1;
var currentYear = DateTime.Now.Year;
var yearsOfExperience =
currentYear - (consultant.GraduationYear is null or 0 ? currentYear : consultant.GraduationYear) ?? 0;

var bookedHours = GetBookedHoursForWeeks(consultant, firstWeek, weeks);
var detailedBooking = GetDetailedBooking(consultant, firstWeek, weeks);

var isOccupied = bookedHours.All(b => b.BookingModel.TotalSellableTime <= 0 + tolerance);

return new ConsultantReadModel(
consultant.Id,
consultant.Name,
consultant.Email,
consultant.Competences.Select(comp => comp.Name).ToList(),
consultant.Department.Name,
yearsOfExperience,
consultant.Degree ?? Degree.None,
bookedHours,
detailedBooking,
isOccupied
);
}

private static List<DetailedBooking> GetDetailedBooking(Consultant consultant, Week firstWeek, int weeks)
{
var org = consultant.Department.Organization;

var hoursPrWorkDay = org.HoursPerWorkday;

var weeksInSet = DateService.GetNextWeeks(firstWeek, weeks);

var projects = consultant.Staffings
.Where(s => weeksInSet.Contains(new Week(s.Year, s.Week)))
.Where(s => s.Project.State == ProjectState.Active || s.Project.State == ProjectState.Offer)
.GroupBy(s => s.Project.Customer.Name)
.Select(grouping => new DetailedBooking(
new BookingDetails(grouping.Key,
grouping.First().Project.State == ProjectState.Active ? BookingType.Booking : BookingType.Offer),
weeksInSet.Select(w => new WeeklyHours(
int.Parse($"{w.Year}{w.WeekNumber}"),
grouping
.Where(staffing => w.Equals(new Week(staffing.Year, staffing.Week)))
.Select(staffing => staffing.Hours)
.Sum()
)).ToList()
))
.ToList();

var plannedAbsences = consultant.PlannedAbsences
.Where(pa => weeksInSet.Contains(new Week(pa.Year, pa.WeekNumber)))
.GroupBy(pa => pa.Absence.Name)
.Select(grouping => new DetailedBooking(
new BookingDetails(grouping.Key, BookingType.PlannedAbsence),
weeksInSet.Select(w => new WeeklyHours(
int.Parse($"{w.Year}{w.WeekNumber}"),
grouping
.Where(plannedAbsence => w.Equals(new Week(plannedAbsence.Year, plannedAbsence.WeekNumber)))
.Select(plannedAbsence => plannedAbsence.Hours)
.Sum()
)).ToList()
))
.ToList();

var detailedBookings = projects.Concat(plannedAbsences);

var firstDay = DateService.GetDatesInWorkWeek(firstWeek.Year, firstWeek.WeekNumber).First();
var lastDay = DateService.GetDatesInWorkWeek(weeksInSet.Last().Year, weeksInSet.Last().WeekNumber).Last();

if (consultant.Vacations.Exists(v => v.Date >= firstDay && v.Date <= lastDay))
{
var vacationList = weeksInSet
.Aggregate(new List<WeeklyHours>(), (list, week) =>
list.Append(new WeeklyHours(
int.Parse($"{week.Year}{week.WeekNumber}"),
consultant.Vacations.Count(v =>
DateService.DateIsInWeek(v.Date, week))
* hoursPrWorkDay)).ToList());

var vacations = new DetailedBooking(
new BookingDetails("Ferie", BookingType.Vacation),
new List<WeeklyHours>(vacationList));

detailedBookings = detailedBookings.Append(vacations).ToList();
}

return detailedBookings.ToList();
}

public static WeeklyBookingReadModel GetBookingModelForWeek(this Consultant consultant, int year, int week)
{
var org = consultant.Department.Organization;
Expand Down Expand Up @@ -168,21 +76,6 @@ public static WeeklyBookingReadModel GetBookingModelForWeek(this Consultant cons
totalOverbooked);
}

private static List<BookedHoursPerWeek> GetBookedHoursForWeeks(this Consultant consultant, Week firstWeek,
int weeksAhead)
{
var nextWeeks = DateService.GetNextWeeks(firstWeek, weeksAhead);

return nextWeeks
.Select(week => new BookedHoursPerWeek(
week.Year,
week.WeekNumber,
int.Parse($"{week.Year}{week.WeekNumber}"),
GetDatesForWeek(week),
GetBookingModelForWeek(consultant, week.Year, week.WeekNumber)))
.ToList();
}

private static string GetDatesForWeek(Week week)
{
return DateService.GetDatesInWorkWeek(week.Year, week.WeekNumber)[0].ToString("dd.MM") +
Expand All @@ -195,19 +88,20 @@ private static BookedHoursPerWeek GetBookedHours(Week week, IEnumerable<Detailed
{
var totalHolidayHours = consultant.Department.Organization.GetTotalHolidayHoursOfWeek(week);

var detailedBookingsArray = detailedBookings as DetailedBooking[] ?? detailedBookings.ToArray();
var totalBillable =
DetailedBooking.GetTotalHoursPrBookingTypeAndWeek(detailedBookings, BookingType.Booking,
DetailedBooking.GetTotalHoursPrBookingTypeAndWeek(detailedBookingsArray, BookingType.Booking,
week);

var totalOffered = DetailedBooking.GetTotalHoursPrBookingTypeAndWeek(detailedBookings,
var totalOffered = DetailedBooking.GetTotalHoursPrBookingTypeAndWeek(detailedBookingsArray,
BookingType.Offer,
week);

var totalAbsence = DetailedBooking.GetTotalHoursPrBookingTypeAndWeek(detailedBookings,
var totalAbsence = DetailedBooking.GetTotalHoursPrBookingTypeAndWeek(detailedBookingsArray,
BookingType.PlannedAbsence,
week);

var totalVacations = DetailedBooking.GetTotalHoursPrBookingTypeAndWeek(detailedBookings,
var totalVacations = DetailedBooking.GetTotalHoursPrBookingTypeAndWeek(detailedBookingsArray,
BookingType.Vacation,
week);

Expand Down
2 changes: 1 addition & 1 deletion backend/Api/Consultants/ConsultantReadModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record BookedHoursPerWeek(int Year, int WeekNumber, int SortableWeek, str

public record DetailedBooking(BookingDetails BookingDetails, List<WeeklyHours> Hours)
{
public double TotalHoursForWeek(Week week)
private double TotalHoursForWeek(Week week)
{
return Hours.Where(weeklySum => weeklySum.Week == week.ToSortableInt()).Sum(weeklyHours => weeklyHours.Hours);
}
Expand Down

0 comments on commit 4ebb80d

Please sign in to comment.