-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
notifications funcitonality added #GCPActive (#62)
* copied all code * Added repository logic * all notifications code in single folder * Reverted changes to other components * added readme * copied with namespace * added nullable enable * My suggested changes to feature/notifications (#65) --------- Co-authored-by: Ivar Nesje <[email protected]>
- Loading branch information
Showing
64 changed files
with
2,497 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Notifications/API/Controllers/EmailNotificationOrdersController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#nullable enable | ||
#if !LOCALTEST | ||
using Altinn.Notifications.Configuration; | ||
#endif | ||
using Altinn.Notifications.Core.Models; | ||
using Altinn.Notifications.Core.Models.Orders; | ||
using Altinn.Notifications.Core.Services.Interfaces; | ||
using Altinn.Notifications.Extensions; | ||
using Altinn.Notifications.Mappers; | ||
using Altinn.Notifications.Models; | ||
using Altinn.Notifications.Validators; | ||
|
||
using FluentValidation; | ||
|
||
#if !LOCALTEST | ||
using Microsoft.AspNetCore.Authorization; | ||
#endif | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
#if !LOCALTEST | ||
using Swashbuckle.AspNetCore.Annotations; | ||
using Swashbuckle.AspNetCore.Filters; | ||
#endif | ||
|
||
namespace Altinn.Notifications.Controllers; | ||
|
||
/// <summary> | ||
/// Controller for all operations related to email notification orders | ||
/// </summary> | ||
[Route("notifications/api/v1/orders/email")] | ||
[ApiController] | ||
#if !LOCALTEST | ||
[Authorize(Policy = AuthorizationConstants.POLICY_CREATE_SCOPE_OR_PLATFORM_ACCESS)] | ||
[SwaggerResponse(401, "Caller is unauthorized")] | ||
[SwaggerResponse(403, "Caller is not authorized to access the requested resource")] | ||
# endif | ||
public class EmailNotificationOrdersController : ControllerBase | ||
{ | ||
private readonly IValidator<EmailNotificationOrderRequestExt> _validator; | ||
private readonly IEmailNotificationOrderService _orderService; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EmailNotificationOrdersController"/> class. | ||
/// </summary> | ||
public EmailNotificationOrdersController(IValidator<EmailNotificationOrderRequestExt> validator, IEmailNotificationOrderService orderService) | ||
{ | ||
_validator = validator; | ||
_orderService = orderService; | ||
} | ||
|
||
/// <summary> | ||
/// Add an email notification order. | ||
/// </summary> | ||
/// <remarks> | ||
/// The API will accept the request after som basic validation of the request. | ||
/// The system will also attempt to verify that it will be possible to fulfill the order. | ||
/// </remarks> | ||
/// <returns>The id of the registered notification order</returns> | ||
[HttpPost] | ||
[Consumes("application/json")] | ||
[Produces("application/json")] | ||
#if !LOCALTEST | ||
[SwaggerResponse(202, "The notification order was accepted", typeof(OrderIdExt))] | ||
[SwaggerResponse(400, "The notification order is invalid", typeof(ValidationProblemDetails))] | ||
[SwaggerResponseHeader(202, "Location", "string", "Link to access the newly created notification order.")] | ||
#endif | ||
public async Task<ActionResult<OrderIdExt>> Post(EmailNotificationOrderRequestExt emailNotificationOrderRequest) | ||
{ | ||
var validationResult = _validator.Validate(emailNotificationOrderRequest); | ||
if (!validationResult.IsValid) | ||
{ | ||
validationResult.AddToModelState(ModelState); | ||
return ValidationProblem(ModelState); | ||
} | ||
|
||
#if LOCALTEST | ||
string creator = "localtest"; | ||
#else | ||
string? creator = HttpContext.GetOrg(); | ||
|
||
if (creator == null) | ||
{ | ||
return Forbid(); | ||
} | ||
#endif | ||
|
||
var orderRequest = emailNotificationOrderRequest.MapToOrderRequest(creator); | ||
(NotificationOrder? registeredOrder, ServiceError? error) = await _orderService.RegisterEmailNotificationOrder(orderRequest); | ||
|
||
if (error != null) | ||
{ | ||
return StatusCode(error.ErrorCode, error.ErrorMessage); | ||
} | ||
|
||
string selfLink = registeredOrder!.GetSelfLink(); | ||
return Accepted(selfLink, new OrderIdExt(registeredOrder!.Id)); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/Notifications/API/Extensions/ResourceLinkExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#nullable enable | ||
using Altinn.Notifications.Core.Models.Orders; | ||
using Altinn.Notifications.Models; | ||
|
||
namespace Altinn.Notifications.Extensions; | ||
|
||
/// <summary> | ||
/// Extension class for ResourceLinks | ||
/// </summary> | ||
public static class ResourceLinkExtensions | ||
{ | ||
private static string? _baseUri; | ||
|
||
/// <summary> | ||
/// Initializes the ResourceLinkExtensions with the base URI from settings. | ||
/// </summary> | ||
/// <remarks> | ||
/// Should be called during startup to ensure base url is set | ||
/// </remarks> | ||
public static void Initialize(string baseUri) | ||
{ | ||
_baseUri = baseUri; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the resource links on an external notification order | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException">Exception if class has not been initialized in Program.cs</exception> | ||
public static void SetResourceLinks(this NotificationOrderExt order) | ||
{ | ||
if (_baseUri == null) | ||
{ | ||
throw new InvalidOperationException("ResourceLinkExtensions has not been initialized with the base URI."); | ||
} | ||
|
||
string self = _baseUri + "/notifications/api/v1/orders/" + order.Id; | ||
|
||
order.Links = new() | ||
{ | ||
Self = self, | ||
Status = self + "/status", | ||
Notifications = self + "/notifications" | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the self link for the provided notification order | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException">Exception if class has not been initialized in Program.cs</exception> | ||
public static void NotificationSummaryResourceLinks(this NotificationOrderWithStatusExt order) | ||
{ | ||
if (_baseUri == null) | ||
{ | ||
throw new InvalidOperationException("ResourceLinkExtensions has not been initialized with the base URI."); | ||
} | ||
|
||
string baseUri = $"{_baseUri}/notifications/api/v1/orders/{order!.Id}/notifications/"; | ||
|
||
if (order.NotificationsStatusSummary?.Email != null) | ||
{ | ||
order.NotificationsStatusSummary.Email.Links = new() | ||
{ | ||
Self = baseUri + "email" | ||
}; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the self link for the provided notification order | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException">Exception if class has not been initialized in Program.cs</exception> | ||
public static string GetSelfLink(this NotificationOrder order) | ||
{ | ||
if (_baseUri == null) | ||
{ | ||
throw new InvalidOperationException("ResourceLinkExtensions has not been initialized with the base URI."); | ||
} | ||
|
||
return _baseUri + "/notifications/api/v1/orders/" + order!.Id; | ||
} | ||
} |
Oops, something went wrong.