Skip to content

Commit

Permalink
moved backend weekconverter to weekExtenstions
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaBonde committed Jan 8, 2025
1 parent 459382c commit f71cfdd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 54 deletions.
44 changes: 0 additions & 44 deletions backend/Core/Weeks/Week.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ public sealed class Week(int year, int weekNumber) : IComparable<Week>, IEquatab
{
public readonly int Year = year;
public readonly int WeekNumber = weekNumber;
public struct MonthsOfWeek
{
public int Month { get; init; }
public int? SecondMonth { get; init; }
public int Distribution { get; init; }
}


public static Week FromInt(int weekAsInt)
{
Expand Down Expand Up @@ -176,43 +169,6 @@ private bool DateIsInWeek(DateOnly day)
return FromDateOnly(day).Equals(this);
}

public MonthsOfWeek GetMonthOfWeek()
{
int daysFromStartOfYear = 1 + (WeekNumber - 1) * 7;
var dayOfWeek = new DateOnly(Year, 1, 1).AddDays(daysFromStartOfYear - 1); // Adjust start of year to the calculated day
var monday = GetPreviousOrCurrentMonday(dayOfWeek);
int month = monday.Month;

double distribution = 100;
int? secondMonth = null;

for (int i = 1; i < 7; i++)
{
var addedDayDate = monday.AddDays(i);
if (addedDayDate.Month != month)
{
distribution = (distribution / 7) * i;
secondMonth = addedDayDate.Month;
break;
}
}

return new MonthsOfWeek
{
Month = month,
SecondMonth = secondMonth,
Distribution = (int)distribution
};
}
static DateOnly GetPreviousOrCurrentMonday(DateOnly date)
{
if (date.DayOfWeek == DayOfWeek.Monday)
{
return date;
}
int daysToSubtract = (date.DayOfWeek - DayOfWeek.Monday + 7) % 7;
return date.AddDays(-daysToSubtract);
}

public static bool operator ==(Week left, Week right)
{
Expand Down
49 changes: 49 additions & 0 deletions backend/Core/Weeks/WeekExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Core.Weeks;
public static class WeekExtensions
{

public struct MonthsOfWeek
{
public int Month { get; init; }
public int? SecondMonth { get; init; }
public int Distribution { get; init; }
}

public static MonthsOfWeek GetMonthOfWeek(this Week week)
{
int daysFromStartOfYear = 1 + (week.WeekNumber - 1) * 7;
var dayOfWeek = new DateOnly(week.Year, 1, 1).AddDays(daysFromStartOfYear - 1); // Adjust start of year to the calculated day
var monday = dayOfWeek.GetPreviousOrCurrentMonday();
int month = monday.Month;

double distribution = 100;
int? secondMonth = null;

for (int i = 1; i < 7; i++)
{
var addedDayDate = monday.AddDays(i);
if (addedDayDate.Month != month)
{
distribution = distribution / 7 * i;
secondMonth = addedDayDate.Month;
break;
}
}

return new MonthsOfWeek
{
Month = month,
SecondMonth = secondMonth,
Distribution = (int)distribution
};
}
public static DateOnly GetPreviousOrCurrentMonday(this DateOnly date)
{
if (date.DayOfWeek == DayOfWeek.Monday)
{
return date;
}
int daysToSubtract = (date.DayOfWeek - DayOfWeek.Monday + 7) % 7;
return date.AddDays(-daysToSubtract);
}
}
2 changes: 0 additions & 2 deletions frontend/src/app/[organisation]/kunder/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { fetchWithToken } from "@/data/apiCallsWithToken";
import { CustomerFilterProvider } from "@/hooks/CustomerFilter/CustomerFilterProvider";
import { CustomerContent } from "@/pagecontent/CustomerContent";
import { Metadata } from "next";
import WeekToMonth from "./weekToMonthConverter";

export const metadata: Metadata = {
title: "Kunder | VIBES",
Expand All @@ -26,7 +25,6 @@ export default async function Kunder({
const absence = customersWAbsence.filter(
(c) => c.engagements.at(0)?.bookingType == EngagementState.Absence,
);
WeekToMonth();

return (
<CustomerFilterProvider customers={customers}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,4 @@ export default async function WeekToMonth() {

return weekToMonthInstance;
}

const test1 = 202522;
const test2 = 202505;
const test3 = 202509;
const test4 = 202514;
const tests = [test1, test2, test3, test4];
const res = tests.map((test) => getMonthOfWeek(weekToWeekType(test)));
console.log("results are: ", res);
}

0 comments on commit f71cfdd

Please sign in to comment.