From a0b98d8524fd9db8b29ceae708c92f8d933e6644 Mon Sep 17 00:00:00 2001 From: Valters Melnalksnis Date: Mon, 14 Oct 2024 07:55:47 +0300 Subject: [PATCH] feat(api): log transactions received from Nordigen --- .../Logging/LoggerExtensions.cs | 35 +++++++++++++++++++ .../Logging/LoggingSerializerContext.cs | 14 ++++++++ .../V1/Controllers/NordigenController.cs | 9 ++--- 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 source/Gnomeshade.WebApi/Logging/LoggerExtensions.cs create mode 100644 source/Gnomeshade.WebApi/Logging/LoggingSerializerContext.cs diff --git a/source/Gnomeshade.WebApi/Logging/LoggerExtensions.cs b/source/Gnomeshade.WebApi/Logging/LoggerExtensions.cs new file mode 100644 index 00000000..a6e06e65 --- /dev/null +++ b/source/Gnomeshade.WebApi/Logging/LoggerExtensions.cs @@ -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; + +/// Helper methods for logging. +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); + } +} diff --git a/source/Gnomeshade.WebApi/Logging/LoggingSerializerContext.cs b/source/Gnomeshade.WebApi/Logging/LoggingSerializerContext.cs new file mode 100644 index 00000000..85f3b5f2 --- /dev/null +++ b/source/Gnomeshade.WebApi/Logging/LoggingSerializerContext.cs @@ -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; + +/// +[JsonSourceGenerationOptions(IgnoreReadOnlyProperties = true)] +[JsonSerializable(typeof(BookedTransaction))] +internal sealed partial class LoggingSerializerContext : JsonSerializerContext; diff --git a/source/Gnomeshade.WebApi/V1/Controllers/NordigenController.cs b/source/Gnomeshade.WebApi/V1/Controllers/NordigenController.cs index 1d8960ef..06db7efa 100644 --- a/source/Gnomeshade.WebApi/V1/Controllers/NordigenController.cs +++ b/source/Gnomeshade.WebApi/V1/Controllers/NordigenController.cs @@ -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; @@ -109,7 +110,7 @@ public async Task 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() @@ -120,7 +121,7 @@ public async Task Import(string id, [Required] string timeZone) if (existing is null) { - _logger.LogDebug("Creating new requisition for {InstitutionId}", id); + _logger.CreatingRequisition(id); var requisition = await _nordigenClient.Requisitions.Post(new(new("https://gnomeshade.org/"), id)); return Redirect(requisition.Link.AbsoluteUri); } @@ -143,7 +144,7 @@ public async Task 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); @@ -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);