Skip to content

Commit

Permalink
Merge pull request #488 from ucdavis/swe/NotifyAPI
Browse files Browse the repository at this point in the history
Add Notify API
  • Loading branch information
sprucely authored Oct 4, 2024
2 parents 36382ed + dd225dc commit cd15fce
Show file tree
Hide file tree
Showing 37 changed files with 207 additions and 98 deletions.
7 changes: 3 additions & 4 deletions Hippo.Core/Hippo.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Hippo.Email\Hippo.Email.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AggieEnterpriseApi" Version="0.2.219" />
Expand All @@ -22,6 +20,7 @@
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="5.2.9" />
<PackageReference Include="Mjml.Net" Version="3.3.0" />
<PackageReference Include="Octokit" Version="7.0.0" />
<PackageReference Include="Razor.Templating.Core" Version="1.7.0" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="SSH.NET" Version="2023.0.0" />
<PackageReference Include="YamlDotNet" Version="13.1.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Hippo.Email.Models
namespace Hippo.Core.Models.Email
{
public class DecisionModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Hippo.Email.Models
namespace Hippo.Core.Models.Email
{
public class EmailButtonModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Hippo.Email.Models
namespace Hippo.Core.Models.Email
{
public class EmailInnerDetailsModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Hippo.Email.Models
namespace Hippo.Core.Models.Email
{
public class EmailOrderPaymentModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Hippo.Email.Models
namespace Hippo.Core.Models.Email
{
public class NewRequestModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Hippo.Email.Models
namespace Hippo.Core.Models.Email
{
public class OrderNotificationModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Hippo.Email.Models
namespace Hippo.Core.Models.Email
{
public class SampleModel
{
Expand Down
28 changes: 28 additions & 0 deletions Hippo.Core/Models/Email/SimpleNotificationModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Hippo.Core.Validation;

namespace Hippo.Core.Models.Email
{
public class SimpleNotificationModel
{
[Required]
[ListOfEmailAddress]
public string[] Emails { get; set; } = Array.Empty<string>();
[Required]
[ListOfEmailAddress]
public string[] CcEmails { get; set; } = Array.Empty<string>();
[Required]
public string Subject { get; set; } = "";
public string Header { get; set; } = "";
[Required]
public List<string> Paragraphs { get; set; } = new();
[JsonIgnore]
public string UcdLogoUrl { get; set; } = "";
}
}
10 changes: 9 additions & 1 deletion Hippo.Core/Services/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@
using System.Net.Mail;
using System.Net.Mime;
using Microsoft.Extensions.Options;
using System.ComponentModel.DataAnnotations;
using Hippo.Core.Validation;

namespace Hippo.Core.Services
{
public class EmailModel
{
[Required]
[ListOfEmailAddress]
public string[] Emails { get; set; } = Array.Empty<string>();
[Required]
[ListOfEmailAddress]
public string[] CcEmails { get; set; } = Array.Empty<string>();
[Required]
public string Subject { get; set; } = "";
[Required]
public string TextBody { get; set; } = "";
public string HtmlBody { get; set; } = "";
public string Subject { get; set; } = "";
}

public interface IEmailService
Expand Down
14 changes: 7 additions & 7 deletions Hippo.Core/Services/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Text;
using System.Threading.Tasks;
using Serilog;
using Hippo.Email.Models;
using Hippo.Core.Models.Email;
using Hippo.Core.Extensions;
using Razor.Templating.Core;
using Mjml.Net;
Expand All @@ -22,7 +22,7 @@ public interface INotificationService
Task<bool> AccountRequest(Request request);
Task<bool> AccountDecision(Request request, bool isApproved, string decidedBy , string reason = null);
Task<bool> AdminOverrideDecision(Request request, bool isApproved, User adminUser, string reason = null);
Task<bool> SimpleNotification(SimpleNotificationModel simpleNotificationModel, string[] emails, string[] ccEmails = null);
Task<bool> SimpleNotification(SimpleNotificationModel simpleNotificationModel);

Task<bool> AdminPaymentFailureNotification(string[] emails, string clusterName, int[] orderIds);
Task<bool> SponsorPaymentFailureNotification(string[] emails, Order order); //Could possibly just pass the order Id, but there might be more order info we want to include
Expand Down Expand Up @@ -144,7 +144,7 @@ public async Task<bool> AccountRequest(Request request)
}
}

public async Task<bool> SimpleNotification(SimpleNotificationModel simpleNotificationModel, string[] emails, string[] ccEmails = null)
public async Task<bool> SimpleNotification(SimpleNotificationModel simpleNotificationModel)
{
if (string.IsNullOrWhiteSpace(simpleNotificationModel.UcdLogoUrl))
{
Expand All @@ -154,9 +154,9 @@ public async Task<bool> SimpleNotification(SimpleNotificationModel simpleNotific
{
var emailModel = new EmailModel
{
Emails = emails,
CcEmails = ccEmails ?? Array.Empty<string>(),
Subject = "HPC Software Install Request",
Emails = simpleNotificationModel.Emails,
CcEmails = simpleNotificationModel.CcEmails ?? Array.Empty<string>(),
Subject = simpleNotificationModel.Subject,
TextBody = string.Join($"{Environment.NewLine}{Environment.NewLine}", simpleNotificationModel.Paragraphs),
HtmlBody = await _mjmlRenderer.RenderView("/Views/Emails/SimpleNotification_mjml.cshtml", simpleNotificationModel)
};
Expand All @@ -166,7 +166,7 @@ public async Task<bool> SimpleNotification(SimpleNotificationModel simpleNotific
}
catch (Exception ex)
{
Log.Error("Error acknowledging software request", ex);
Log.Error("Error sending email", ex);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion Hippo.Core/Services/SlothService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using Hippo.Core.Domain;
using Serilog;
using System.Net;
using Hippo.Email.Models;
using Hippo.Core.Models.Email;


namespace Hippo.Core.Services
Expand Down
39 changes: 39 additions & 0 deletions Hippo.Core/Validation/ListOfEmailAddressAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace Hippo.Core.Validation;

/// <summary>
/// Validates that all elements of a list are email addresses
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class ListOfEmailAddressAttribute : ValidationAttribute
{
private readonly bool _nonEmpty;
private readonly EmailAddressAttribute _emailAddressAttribute;

public ListOfEmailAddressAttribute(bool nonEmpty = false)
: base()
{
_nonEmpty = nonEmpty;
_emailAddressAttribute = new EmailAddressAttribute();
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var list = value as IList<string>;
if (_nonEmpty && (list == null || !list.Any()))
return new ValidationResult($"'{validationContext.MemberName}' requires at least 1 email address");

if (list == null)
return ValidationResult.Success;

foreach (var email in list)
{
if (!_emailAddressAttribute.IsValid(email))
return new ValidationResult($"'{validationContext.MemberName}' contains an invalid email address.");
}

return ValidationResult.Success;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using Hippo.Email.Models
@model Hippo.Email.Models.DecisionModel
@using Hippo.Core.Models.Email
@model Hippo.Core.Models.Email.DecisionModel

@{
ViewData["EmailTitle"] = "Testing";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using Hippo.Email.Models
@model Hippo.Email.Models.NewRequestModel
@using Hippo.Core.Models.Email
@model Hippo.Core.Models.Email.NewRequestModel

@{
ViewData["EmailTitle"] = "Testing";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using Hippo.Email.Models
@model Hippo.Email.Models.DecisionModel
@using Hippo.Core.Models.Email
@model Hippo.Core.Models.Email.DecisionModel

@{
ViewData["EmailTitle"] = "Testing";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using Hippo.Email.Models
@model Hippo.Email.Models.OrderNotificationModel
@using Hippo.Core.Models.Email
@model Hippo.Core.Models.Email.OrderNotificationModel

@{
ViewData["EmailTitle"] = "Order Notification";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using Hippo.Email.Models
@model Hippo.Email.Models.OrderNotificationModel
@using Hippo.Core.Models.Email
@model Hippo.Core.Models.Email.OrderNotificationModel

@{
ViewData["EmailTitle"] = "Order Notification";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using Hippo.Email.Models
@model Hippo.Email.Models.EmailOrderPaymentModel
@using Hippo.Core.Models.Email
@model Hippo.Core.Models.Email.EmailOrderPaymentModel

@{
ViewData["EmailTitle"] = "Order Notification";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using Hippo.Email.Models
@model Hippo.Email.Models.SampleModel
@using Hippo.Core.Models.Email
@model Hippo.Core.Models.Email.SampleModel

@{
ViewData["EmailTitle"] = "Testing";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using Hippo.Email.Models
@model Hippo.Email.Models.SimpleNotificationModel
@using Hippo.Core.Models.Email
@model Hippo.Core.Models.Email.SimpleNotificationModel

@{
ViewData["EmailTitle"] = "Testing";
Expand All @@ -13,7 +13,7 @@
@if (!string.IsNullOrWhiteSpace(@Model.Header))
{
<mj-text padding-top="0px" padding-bottom="0px" font-size="24px" color="#1F1F1F">
<p>Software request received</p>
<p>@Model.Header</p>
</mj-text>
}
<mj-text padding-top="0px" padding-bottom="0px" font-size="20px" color="rgba(31,31,31,0.8)">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using Hippo.Email.Models
@using Hippo.Core.Models.Email
@model EmailButtonModel

<mj-section border-left="1px solid #c7c8cc" border-right="1px solid #c7c8cc" background-color="#E8E1EC"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using Hippo.Email.Models
@using Hippo.Core.Models.Email
@model EmailInnerDetailsModel

<mj-text padding-top="0px" padding-left="0px" padding-bottom="5px">
Expand Down
16 changes: 0 additions & 16 deletions Hippo.Email/Hippo.Email.csproj

This file was deleted.

16 changes: 0 additions & 16 deletions Hippo.Email/Models/SimpleNotificationModel.cs

This file was deleted.

10 changes: 0 additions & 10 deletions Hippo.Email/README.md

This file was deleted.

Loading

0 comments on commit cd15fce

Please sign in to comment.