Skip to content

Commit

Permalink
First version of using ContactRegistration telemetry service
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-M-Krystyan committed Mar 25, 2024
1 parent 0286d7f commit e5f2e66
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace EventsHandler.Behaviors.Mapping.Models.POCOs.NotifyNL
/// </summary>
internal struct DeliveryReceipt : IJsonSerializable
{
internal static DeliveryReceipt Default { get; } = new();

/// <summary>
/// Notify’s id for the status receipts.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public sealed class EventsController : OmcController
/// <param name="validator">The input validating service.</param>
/// <param name="processor">The input processing service (business logic).</param>
/// <param name="responder">The output standardization service (UX/UI).</param>
/// <param name="logger">The logging service.</param>
/// <param name="logger">The logging service registering API events.</param>
public EventsController(
ISerializationService serializer,
IValidationService<NotificationEvent> validator,
Expand Down
32 changes: 26 additions & 6 deletions EventsHandler/Api/EventsHandler/Controllers/NotifyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using EventsHandler.Constants;
using EventsHandler.Controllers.Base;
using EventsHandler.Services.Serialization.Interfaces;
using EventsHandler.Services.Telemetry.Interfaces;
using EventsHandler.Services.UserCommunication.Interfaces;
using EventsHandler.Utilities.Swagger.Examples;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -30,21 +31,25 @@ public sealed class NotifyController : OmcController
{
private readonly ISerializationService _serializer;
private readonly IRespondingService<ProcessingResult, string> _responder;

private readonly ITelemetryService _telemetry;

/// <summary>
/// Initializes a new instance of the <see cref="EventsController"/> class.
/// </summary>
/// <param name="serializer">The input de(serializing) service.</param>
/// <param name="responder">The output standardization service (UX/UI).</param>
/// <param name="logger">The logging service.</param>
/// <param name="telemetry">The telemetry service registering API events.</param>
/// <param name="logger">The logging service registering API events.</param>
public NotifyController(
ISerializationService serializer,
IRespondingService<ProcessingResult, string> responder,
ITelemetryService telemetry,
ILogger<NotifyController> logger)
: base(logger)
{
this._serializer = serializer;
this._responder = responder;
this._telemetry = telemetry;
}

/// <summary>
Expand All @@ -60,34 +65,49 @@ public NotifyController(
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ProcessingFailed.Detailed))] // REASON: The delivery receipt with failure status
[ProducesResponseType(StatusCodes.Status401Unauthorized, Type = typeof(string))] // REASON: JWT Token is invalid or expired
[ProducesResponseType(StatusCodes.Status500InternalServerError)] // REASON: Internal server error (if-else / try-catch-finally handle)
public IActionResult Confirm([Required, FromBody] object json)
public async Task<IActionResult> ConfirmAsync([Required, FromBody] object json)
{
DeliveryReceipt callback = DeliveryReceipt.Default;
string callbackDetails = string.Empty;

try
{
// Deserialize received JSON payload
DeliveryReceipt callback = this._serializer.Deserialize<DeliveryReceipt>(json);
callback = this._serializer.Deserialize<DeliveryReceipt>(json);

if (callback.Status is not (DeliveryStatus.PermanentFailure or
DeliveryStatus.TemporaryFailure or
DeliveryStatus.TechnicalFailure))
{
return LogAndReturnApiResponse(LogLevel.Information,
this._responder.GetStandardized_Processing_ActionResult(ProcessingResult.Success, GetCallbackDetails(callback)));
this._responder.GetStandardized_Processing_ActionResult(ProcessingResult.Success, callbackDetails = GetCallbackDetails(callback)));
}

return LogAndReturnApiResponse(LogLevel.Error,
this._responder.GetStandardized_Processing_ActionResult(ProcessingResult.Failure, GetCallbackDetails(callback)));
this._responder.GetStandardized_Processing_ActionResult(ProcessingResult.Failure, callbackDetails = GetCallbackDetails(callback)));
}
catch (Exception exception)
{
// NOTE: If callback.Id == Guid.Empty then to be suspected is exception during DeliveryReceipt deserialization
callbackDetails = GetErrorDetails(callback, exception);

return LogAndReturnApiResponse(LogLevel.Critical,
this._responder.GetStandardized_Exception_ActionResult(exception));
}
finally
{
_ = await this._telemetry.ReportCompletionAsync(default, default, callbackDetails);
}
}

private static string GetCallbackDetails(DeliveryReceipt callback)
{
return $"The status of notification with ID {callback.Id} is: {callback.Status}.";
}

private static string GetErrorDetails(DeliveryReceipt callback, Exception exception)
{
return $"An unexpected error occurred during processing the notification with ID {callback.Id}: {exception.Message}.";
}
}
}

0 comments on commit e5f2e66

Please sign in to comment.