Skip to content

Commit

Permalink
Replace static list on start page by organization data
Browse files Browse the repository at this point in the history
  • Loading branch information
markusrt committed Jun 23, 2024
1 parent efa619c commit c5c7b46
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 147 deletions.
3 changes: 2 additions & 1 deletion NRZMyk.Components/NRZMyk.Components.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand Down Expand Up @@ -32,6 +32,7 @@

<ItemGroup>
<Watch Remove="Helpers\LoadState.cs" />
<Watch Remove="Pages\IndexBase.cs" />
<Watch Remove="Pages\SentinelEntryPage\CryoView.razor" />
<Watch Remove="Pages\SentinelEntryPage\CryoViewBase.cs" />
<Watch Remove="Pages\SentinelEntryPage\Details.razor" />
Expand Down
81 changes: 31 additions & 50 deletions NRZMyk.Components/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@page "/"
@using NRZMyk.Services.Models
@using NRZMyk.Services.Utils

@inherits IndexBase

<p>
Das vom Robert Koch-Institut und dem Bundesministerium für Gesundheit berufene
Expand Down Expand Up @@ -38,57 +41,35 @@

<AuthorizeView Roles="@nameof(Role.User)">
<Authorized>
<h2>Feste Monate der Einsendung</h2>
<div class="row">
<div class="col-9">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Monat</th>
<th scope="col">Mikrobiologie</th>
</tr>
</thead>
<tbody>
<tr>
<td>Januar</td><td>München (TU)</td>
</tr>
<tr>
<td>Februar</td><td>Düsseldorf</td>
</tr>
<tr>
<td>März</td><td>München (LMU)</td>
</tr>
<tr>
<td>April</td><td>Essen</td>
</tr>
<tr>
<td>Mai</td><td>Freiburg</td>
</tr>
<tr>
<td>Juni</td><td>Berlin</td>
</tr>
<tr>
<td>Juli</td><td>Oldenburg</td>
</tr>
<tr>
<td>August</td><td>Frankfurt</td>
</tr>
<tr>
<td>September</td><td>Erlangen</td>
</tr>
<tr>
<td>Oktober</td><td>Aachen</td>
</tr>
<tr>
<td>November</td><td>Nürnberg</td>
</tr>
<tr>
<td>Dezember</td><td>Würzburg</td>
</tr>
</tbody>
</table>
@if (Organizations == null)
{
<p><em>Lädt...</em></p>
}
else
{
<h2>Feste Monate der Einsendung</h2>
<div class="row">
<div class="col-9">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Monat</th>
<th scope="col">Mikrobiologie</th>
</tr>
</thead>
<tbody>
@foreach (var organization in Organizations)
{
<tr>
<td>@EnumUtils.GetEnumDescription(organization.DispatchMonth)</td>
<td>@organization.Name</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
}
</Authorized>
</AuthorizeView>

Expand Down
28 changes: 28 additions & 0 deletions NRZMyk.Components/Pages/IndexBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
using NRZMyk.Components.Helpers;
using NRZMyk.Services.Data.Entities;
using NRZMyk.Services.Services;

namespace NRZMyk.Components.Pages
{
public class IndexBase : BlazorComponent
{
[Inject]
private IAccountService AccountService { get; set; } = default!;

[Inject]
private ILogger<IndexBase> Logger { get; set; } = default!;

internal ICollection<Organization> Organizations { get; set; } = default!;

internal SaveState SaveState { get; set; }

protected override async Task OnInitializedAsync()
{
Logger.LogInformation("Now loading... /Index");
Organizations = await AccountService.ListOrganizations().ConfigureAwait(true);
await base.OnInitializedAsync().ConfigureAwait(true);
}
}
}
44 changes: 34 additions & 10 deletions NRZMyk.Mocks/MockServices/MockAccountService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NRZMyk.Services.Data.Entities;
Expand All @@ -11,6 +12,24 @@ public class MockAccountService : IAccountService
{
public static int Delay = 2000;

public static Random Random = new Random();

private static List<Tuple<string, MonthToDispatch>> Organizations = new List<Tuple<string, MonthToDispatch>>
{
Tuple.Create("München (TU)", MonthToDispatch.January),
Tuple.Create("Düsseldorf", MonthToDispatch.February),
Tuple.Create("München (LMU)", MonthToDispatch.March),
Tuple.Create("Essen", MonthToDispatch.April),
Tuple.Create("Freiburg", MonthToDispatch.May),
Tuple.Create("Berlin", MonthToDispatch.June),
Tuple.Create("Oldenburg", MonthToDispatch.July),
Tuple.Create("Frankfurt", MonthToDispatch.August),
Tuple.Create("Erlangen", MonthToDispatch.September),
Tuple.Create("Aachen", MonthToDispatch.October),
Tuple.Create("Nürnberg", MonthToDispatch.November),
Tuple.Create("Würzburg", MonthToDispatch.December)
};

private List<RemoteAccount> _accounts = new List<RemoteAccount>();

private List<Organization> _organization = new List<Organization>();
Expand All @@ -20,16 +39,21 @@ public MockAccountService()
var filler = new Filler<RemoteAccount>();
_accounts.AddRange(filler.Create(10));

_organization.Add(new Organization
{
Id = 1,
Name = "Organization 1"
});
_organization.Add(new Organization
int id = 1;
foreach (var organization in Organizations)
{
Id = 2,
Name = "Organization 2"
});
var randomDayOffset = Random.Next(365 * 2);
var latestStrainArrivalDate = DateTime.Today.AddDays(-1 * randomDayOffset);
var latestDataEntryDate = latestStrainArrivalDate.AddDays((int)(randomDayOffset/2));
_organization.Add(new Organization
{
Id = id++,
Name = organization.Item1,
DispatchMonth = organization.Item2,
LatestStrainArrivalDate = latestStrainArrivalDate,
LatestDataEntryDate = latestDataEntryDate
});
}
}

public async Task<ICollection<RemoteAccount>> ListAccounts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,35 @@
using FluentAssertions;
using NRZMyk.Services.Services;

namespace NRZMyk.Services.Tests.Data;
namespace NRZMyk.Services.Tests.Services;

public class OrganizationTests
public class ReminderServiceTests
{
[Test]
public void CheckDataAndSendReminders_NoDataEntered_Last12Months_SendEmail()
{
var org = new Organization
{
Id = 0,
Name = null,
Members = null,
Email = null,
DispatchMonth = MonthToDispatch.None,
LatestDataEntryDate = default,
LatestStrainArrivalDate = default
};
org.Email = "[email protected]";
org.DispatchMonth = MonthToDispatch.January;
org.LatestDataEntryDate = new DateTime(2022, 1, 1);
org.LatestStrainArrivalDate = new DateTime(2022, 1, 15);

var emailService = Substitute.For<IEmailNotificationService>();

org.CheckDataAndSendReminders(emailService);

emailService.Received(1).SendEmail("[email protected]", "No data was entered during the last 12 months.");
}

[Test]
public void CheckDataAndSendReminders_DataEntered_NoStrainArrived_SendEmail()
{
var emailNotificationServiceMock = Substitute.For<IEmailNotificationService>();
var sut = CreateSut(emailNotificationServiceMock);
var org = CreateOrganization();
org.DispatchMonth = MonthToDispatch.January;
org.LatestDataEntryDate = new DateTime(2023, 1, 5);
org.LatestStrainArrivalDate = new DateTime(2022, 12, 20);

var emailService = Substitute.For<IEmailNotificationService>();

org.CheckDataAndSendReminders(emailService);
sut.CheckDataAndSendReminders(org);

emailService.Received(1).SendEmail("[email protected]", "Data was entered, but no strain has arrived yet.");
emailNotificationServiceMock.Received(1).SendEmail("[email protected]", "Data was entered, but no strain has arrived yet.");
}

[Test]
public void WhenExpectedNextSendingIsThisMonth_ShowsHumanReadableInformation()
{
var sut = CreateSut();
var org = CreateOrganization();
var today = DateTime.Today;
org.DispatchMonth = (MonthToDispatch)today.Month;
org.LatestStrainArrivalDate = today.Subtract(TimeSpan.FromDays(200));

org.ExpectedNextSending.Should().Be("diesen Monat");
sut.HumanReadableExpectedNextSending(org).Should().Be("diesen Monat");
}

[TestCase(1, 6, "in 5 Monaten")]
Expand All @@ -74,12 +50,13 @@ public void WhenExpectedNextSendingIsThisMonth_ShowsHumanReadableInformation()
[TestCase(10, 2, "in einem Monat")]
public void WhenExpectedNextSendingIsChecked_ShowsHumanReadableInformation(int monthSinceLatestStrainArrival, int monthUntilNextArrival, string expectedNextSending)
{
var sut = CreateSut();
var org = CreateOrganization();
var todayInSixMonths = DateTime.Today.AddMonths(monthUntilNextArrival);
org.DispatchMonth = (MonthToDispatch)todayInSixMonths.Month;
org.LatestStrainArrivalDate = DateTime.Today.AddMonths(-1*monthSinceLatestStrainArrival);
org.LatestStrainArrivalDate = DateTime.Today.AddMonths(-1 * monthSinceLatestStrainArrival);

org.ExpectedNextSending.Should().Be(expectedNextSending);
sut.HumanReadableExpectedNextSending(org).Should().Be(expectedNextSending);
}

private static Organization CreateOrganization()
Expand All @@ -93,4 +70,9 @@ private static Organization CreateOrganization()
DispatchMonth = MonthToDispatch.None,
};
}

private ReminderService CreateSut(IEmailNotificationService emailNotificationServiceMock = null)
{
return new ReminderService(emailNotificationServiceMock ?? Substitute.For<IEmailNotificationService>());
}
}
52 changes: 0 additions & 52 deletions NRZMyk.Services/Data/Entities/Organization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,5 @@ public class Organization : BaseEntity, IAggregateRoot
public MonthToDispatch DispatchMonth { get; set; }
public DateTime LatestDataEntryDate { get; set; }
public DateTime LatestStrainArrivalDate { get; set; }

[Display(Name = "Nächste Einsendung erwarted")]
public string ExpectedNextSending => CalculateExpectedNextSending();

private string CalculateExpectedNextSending()
{
var today = DateTime.Today;
var expectedArrival = new DateTime(today.Year, (int)DispatchMonth, 15);
var timeSinceLastArrival = expectedArrival.Subtract(LatestStrainArrivalDate);

if (timeSinceLastArrival.TotalDays < 365)
{
if (DispatchMonth == (MonthToDispatch)today.Month)
{
return "diesen Monat";
}
}
else
{
expectedArrival = new DateTime(Math.Min(today.Year - 1, LatestStrainArrivalDate.Year), (int)DispatchMonth, 21);
}

return expectedArrival.Humanize(culture: CultureInfo.GetCultureInfo("de-de"));
}

public void CheckDataAndSendReminders(IEmailNotificationService emailService)
{
var currentDate = DateTime.Now;
var lastDispatchMonth = new DateTime(currentDate.Year, (int)DispatchMonth, 1);
var nextDispatchMonth = lastDispatchMonth.AddMonths(12);

// Check if the current date is the first day of the dispatch month
if (currentDate.Date == lastDispatchMonth.Date)
{
// Check if no data was entered during the last 12 months
if (LatestDataEntryDate < lastDispatchMonth)
{
string message = "No data was entered during the last 12 months.";
emailService.SendEmail(Email, message);
}
}
// Check if the current date is the first day after the dispatch month ends
else if (currentDate.Date == nextDispatchMonth.Date)
{
// Check if data was entered but no strain has arrived yet
if (LatestDataEntryDate >= lastDispatchMonth && LatestStrainArrivalDate < lastDispatchMonth)
{
string message = "Data was entered, but no strain has arrived yet.";
emailService.SendEmail(Email, message);
}
}
}
}
}
5 changes: 5 additions & 0 deletions NRZMyk.Services/Services/IReminderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace NRZMyk.Services.Services;

public interface IReminderService
{
}
Loading

0 comments on commit c5c7b46

Please sign in to comment.