Skip to content

Commit

Permalink
feat(api): log transactions received from Nordigen
Browse files Browse the repository at this point in the history
  • Loading branch information
VMelnalksnis committed Oct 14, 2024
1 parent d0d072b commit a0b98d8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
35 changes: 35 additions & 0 deletions source/Gnomeshade.WebApi/Logging/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021 Valters Melnalksnis
// Licensed under the GNU Affero General Public License v3.0 or later.
// See LICENSE.txt file in the project root for full license information.

using System.Text.Json;

using Microsoft.Extensions.Logging;

using VMelnalksnis.NordigenDotNet.Accounts;

using static Microsoft.Extensions.Logging.LogLevel;

namespace Gnomeshade.WebApi.Logging;

/// <summary>Helper methods for logging.</summary>
internal static partial class LoggerExtensions
{
[LoggerMessage(1, Debug, "Getting requisition for {InstitutionId}")]
internal static partial void GettingRequisition(this ILogger logger, string institutionId);

[LoggerMessage(2, Debug, "Creating new requisition for {InstitutionId}")]
internal static partial void CreatingRequisition(this ILogger logger, string institutionId);

[LoggerMessage(3, Debug, "Matched report account to {AccountName}")]
internal static partial void MatchedReportAccount(this ILogger logger, string accountName);

[LoggerMessage(4, Trace, "Parsing transaction {ServicerReference}; {BookedTransaction}")]
internal static partial void ParsingTransaction(this ILogger logger, string servicerReference, string bookedTransaction);

internal static void ParsingTransaction(this ILogger logger, BookedTransaction transaction)
{
var json = JsonSerializer.Serialize(transaction, LoggingSerializerContext.Default.BookedTransaction);
logger.ParsingTransaction(transaction.TransactionId, json);
}
}
14 changes: 14 additions & 0 deletions source/Gnomeshade.WebApi/Logging/LoggingSerializerContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2021 Valters Melnalksnis
// Licensed under the GNU Affero General Public License v3.0 or later.
// See LICENSE.txt file in the project root for full license information.

using System.Text.Json.Serialization;

using VMelnalksnis.NordigenDotNet.Accounts;

namespace Gnomeshade.WebApi.Logging;

/// <inheritdoc cref="JsonSerializerContext" />
[JsonSourceGenerationOptions(IgnoreReadOnlyProperties = true)]
[JsonSerializable(typeof(BookedTransaction))]
internal sealed partial class LoggingSerializerContext : JsonSerializerContext;
9 changes: 5 additions & 4 deletions source/Gnomeshade.WebApi/V1/Controllers/NordigenController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Gnomeshade.Data.Entities;
using Gnomeshade.Data.Repositories;
using Gnomeshade.WebApi.Client;
using Gnomeshade.WebApi.Logging;
using Gnomeshade.WebApi.Models.Importing;
using Gnomeshade.WebApi.V1.Authentication;
using Gnomeshade.WebApi.V1.Importing;
Expand Down Expand Up @@ -109,7 +110,7 @@ public async Task<ActionResult> Import(string id, [Required] string timeZone)
return BadRequest(ModelState);
}

_logger.LogDebug("Getting requisition for {InstitutionId}", id);
_logger.GettingRequisition(id);
var existing = await _nordigenClient
.Requisitions
.Get()
Expand All @@ -120,7 +121,7 @@ public async Task<ActionResult> Import(string id, [Required] string timeZone)

if (existing is null)
{
_logger.LogDebug("Creating new requisition for {InstitutionId}", id);
_logger.CreatingRequisition(id);

Check warning on line 124 in source/Gnomeshade.WebApi/V1/Controllers/NordigenController.cs

View check run for this annotation

Codecov / codecov/patch

source/Gnomeshade.WebApi/V1/Controllers/NordigenController.cs#L124

Added line #L124 was not covered by tests
var requisition = await _nordigenClient.Requisitions.Post(new(new("https://gnomeshade.org/"), id));
return Redirect(requisition.Link.AbsoluteUri);
}
Expand All @@ -143,7 +144,7 @@ public async Task<ActionResult> Import(string id, [Required] string timeZone)
var (reportAccount, currency, createdAccount) = await _importService
.FindUserAccountAsync(new(account.Iban, details.Currency), user, dbTransaction);

_logger.LogDebug("Matched report account to {AccountName}", reportAccount.Name);
_logger.MatchedReportAccount(reportAccount.Name);

var resultBuilder = new AccountReportResultBuilder(_mapper, reportAccount, createdAccount);

Expand Down Expand Up @@ -254,7 +255,7 @@ private static (string? Domain, string? Family, string? SubFamily) GetCode(strin
UserEntity user,
DateTimeZone dateTimeZone)
{
_logger.LogTrace("Parsing transaction {ServicerReference}", bookedTransaction.TransactionId);
_logger.ParsingTransaction(bookedTransaction);

var amount = Math.Abs(bookedTransaction.TransactionAmount.Amount);
var (domainCode, familyCode, subFamilyCode) = GetCode(bookedTransaction.BankTransactionCode);
Expand Down

0 comments on commit a0b98d8

Please sign in to comment.