Skip to content

Commit

Permalink
fix: old readdress event should be handled in a single transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
jvandaal committed May 22, 2024
1 parent 5942efe commit 37f0af8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ await DetachBecauseRemoved(
}
}

await backOfficeContext.Database.BeginTransactionAsync();
await backOfficeContext.Database.BeginTransactionAsync(ct);

foreach (var parcelId in commandByParcels.Select(x => x.ParcelId))
{
var parcel = await parcels.GetAsync(new ParcelStreamId(parcelId), ct);
Expand All @@ -237,7 +237,7 @@ await DetachBecauseRemoved(
}
}

await backOfficeContext.Database.CommitTransactionAsync();
await backOfficeContext.Database.CommitTransactionAsync(ct);
});

When<AddressWasRejectedBecauseOfReaddress>(async (commandHandler, message, ct) =>
Expand Down
18 changes: 6 additions & 12 deletions src/ParcelRegistry.Projections.BackOffice/BackOfficeProjections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ await backOfficeContext.RemoveIdempotentParcelAddressRelation(

if (previousAddress is not null && previousAddress.Count == 1)
{
await backOfficeContext.RemoveIdempotentParcelAddressRelation(
new ParcelId(message.Message.ParcelId),
new AddressPersistentLocalId(message.Message.PreviousAddressPersistentLocalId),
cancellationToken);
backOfficeContext.ParcelAddressRelations.Remove(previousAddress);
}
else if (previousAddress is not null)
{
Expand All @@ -97,22 +94,19 @@ await backOfficeContext.RemoveIdempotentParcelAddressRelation(

if (newAddress is null)
{
await backOfficeContext.AddIdempotentParcelAddressRelation(
new ParcelId(message.Message.ParcelId),
new AddressPersistentLocalId(message.Message.NewAddressPersistentLocalId),
await backOfficeContext.ParcelAddressRelations.AddAsync(
new ParcelAddressRelation(message.Message.ParcelId, message.Message.NewAddressPersistentLocalId),
cancellationToken);
}
else
{
newAddress.Count += 1;
}
});

When<Envelope<ParcelAddressesWereReaddressed>>((_, message, cancellationToken) =>
{
// Do nothing
return Task.CompletedTask;
await backOfficeContext.SaveChangesAsync(cancellationToken);
});

When<Envelope<ParcelAddressesWereReaddressed>>((_, _, _) => Task.CompletedTask);
}
}
}

0 comments on commit 37f0af8

Please sign in to comment.