Skip to content

Commit

Permalink
updated after review
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsDue committed Dec 13, 2024
1 parent b1c5407 commit ee48811
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,8 @@ private async Task<IReadOnlyCollection<ValidationError>> CheckTransactionIdsAsyn
}
}

var duplicatedTransactionIds = await TransactionIdIsDuplicatedAsync(
message.SenderNumber,
transactionIdsToBeStored,
cancellationToken)
var duplicatedTransactionIds = await transactionIdRepository
.GetDuplicatedTransactionIdsAsync(message.SenderNumber, transactionIdsToBeStored, cancellationToken)
.ConfigureAwait(false);
foreach (var duplicatedTransactionId in duplicatedTransactionIds)
{
Expand All @@ -211,14 +209,4 @@ _ when transactionIdsToBeStored.Contains(transactionId) =>
_ => null,
};
}

private async Task<IReadOnlyList<string>> TransactionIdIsDuplicatedAsync(
string senderNumber,
IReadOnlyList<string> transactionIds,
CancellationToken cancellationToken)
{
return await transactionIdRepository
.TransactionIdExistsAsync(senderNumber, transactionIds, cancellationToken)
.ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface ITransactionIdRepository
/// <param name="senderId"></param>
/// <param name="transactionIds"></param>
/// <param name="cancellationToken"></param>
Task<IReadOnlyList<string>> TransactionIdExistsAsync(string senderId, IReadOnlyCollection<string> transactionIds, CancellationToken cancellationToken);
Task<IReadOnlyList<string>> GetDuplicatedTransactionIdsAsync(string senderId, IReadOnlyCollection<string> transactionIds, CancellationToken cancellationToken);

/// <summary>
/// Store transaction ids for the specified sender
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public TransactionIdRepository(IncomingMessagesContext incomingMessagesContext)
_incomingMessagesContext = incomingMessagesContext;
}

public async Task<IReadOnlyList<string>> TransactionIdExistsAsync(
public async Task<IReadOnlyList<string>> GetDuplicatedTransactionIdsAsync(
string senderId,
IReadOnlyCollection<string> transactionIds,
CancellationToken cancellationToken)
{
var transactionIdsForSender = await GetTransactionFromDbAsync(senderId, transactionIds, cancellationToken)
var duplicatedTransactionIdsForSender = await GetDuplicatedTransactionsFromDbAsync(senderId, transactionIds, cancellationToken)
.ConfigureAwait(false);
if (!transactionIds.Any())
transactionIdsForSender = GetTransactionFromInMemoryCollection(senderId, transactionIds);
duplicatedTransactionIdsForSender = GetDuplicatedTransactionsFromInMemoryCollection(senderId, transactionIds);

return transactionIdsForSender.Select(x => x.TransactionId).ToList();
return duplicatedTransactionIdsForSender.Select(x => x.TransactionId).ToList();
}

public async Task AddAsync(
Expand All @@ -52,7 +52,7 @@ public async Task AddAsync(
}
}

private IReadOnlyList<TransactionIdForSender> GetTransactionFromInMemoryCollection(
private IReadOnlyList<TransactionIdForSender> GetDuplicatedTransactionsFromInMemoryCollection(
string senderId,
IReadOnlyCollection<string> transactionIds)
{
Expand All @@ -63,7 +63,7 @@ private IReadOnlyList<TransactionIdForSender> GetTransactionFromInMemoryCollecti
.ToList();
}

private async Task<IReadOnlyList<TransactionIdForSender>> GetTransactionFromDbAsync(
private async Task<IReadOnlyList<TransactionIdForSender>> GetDuplicatedTransactionsFromDbAsync(
string senderId,
IReadOnlyCollection<string> transactionIds,
CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,37 @@ public async Task When_MultipleTransactionsWithSameId_Then_ResultContainExcepted
result.Errors.Should().Contain(error => error is DuplicateTransactionIdDetected);
}

[Fact]
public async Task When_MultipleTransactionsWithSameIdAsExisting_Then_ResultContainExceptedValidationError()
{
var documentFormat = DocumentFormat.Ebix;
var existingTransactionIdForSender = "123456";
var newTransactionIdForSender = "654321";
await StoreTransactionIdForActorAsync(existingTransactionIdForSender, _actorIdentity.ActorNumber.Value);
var message = MeteredDataForMeasurementPointBuilder.CreateIncomingMessage(
documentFormat,
_actorIdentity.ActorNumber,
[
(existingTransactionIdForSender,
Instant.FromUtc(2024, 1, 1, 0, 0),
Instant.FromUtc(2024, 1, 2, 0, 0),
Resolution.QuarterHourly),
(newTransactionIdForSender,
Instant.FromUtc(2024, 1, 1, 0, 0),
Instant.FromUtc(2024, 1, 2, 0, 0),
Resolution.QuarterHourly),
]);

var (incomingMessage, _) = await ParseMessageAsync(message.Stream, documentFormat);
var result = await _validateIncomingMessage.ValidateAsync(
incomingMessage!,
documentFormat,
CancellationToken.None);

result.Success.Should().BeFalse();
result.Errors.Should().Contain(error => error is DuplicateTransactionIdDetected);
}

[Fact]
public async Task When_TransactionIdIsEmpty_Then_ResultContainExceptedValidationError()
{
Expand Down Expand Up @@ -618,6 +649,17 @@ public async Task When_BusinessTypeIsNotAllowed_Then_ExpectedValidationError()
throw new NotSupportedException($"No message parser found for message format '{documentFormat}' and document type '{IncomingDocumentType.NotifyValidatedMeasureData}'");
}

private async Task StoreTransactionIdForActorAsync(string existingTransactionIdForSender, string senderActorNumber)
{
var databaseConnectionFactory = GetService<IDatabaseConnectionFactory>();
using var dbConnection = await databaseConnectionFactory.GetConnectionAndOpenAsync(CancellationToken.None).ConfigureAwait(false);

await dbConnection.ExecuteAsync(
"INSERT INTO [dbo].[TransactionRegistry] ([TransactionId], [SenderId]) VALUES (@TransactionId, @SenderId)",
new { TransactionId = existingTransactionIdForSender, SenderId = senderActorNumber })
.ConfigureAwait(false);
}

private async Task StoreMessageIdForActorAsync(string messageId, string senderActorNumber)
{
var databaseConnectionFactory = GetService<IDatabaseConnectionFactory>();
Expand Down

0 comments on commit ee48811

Please sign in to comment.