Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions TransactionProcessor.Aggregates/MerchantAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Shared.General;
using SimpleResults;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using TransactionProcessor.DomainEvents;
using TransactionProcessor.Models.Contract;
using TransactionProcessor.Models.Merchant;
Expand Down Expand Up @@ -294,10 +295,13 @@ public static Result SwapDevice(this MerchantAggregate aggregate,
if (result.IsFailed)
return result;

KeyValuePair<Guid, Device> originalDevice = aggregate.Devices.Single(d => d.Value.DeviceIdentifier == originalDeviceIdentifier);

Guid deviceId = Guid.NewGuid();

MerchantDomainEvents.DeviceSwappedForMerchantEvent deviceSwappedForMerchantEvent = new MerchantDomainEvents.DeviceSwappedForMerchantEvent(aggregate.AggregateId, aggregate.EstateId,
deviceId, originalDeviceIdentifier, newDeviceIdentifier);
MerchantDomainEvents.DeviceSwappedForMerchantEvent deviceSwappedForMerchantEvent = new(aggregate.AggregateId, aggregate.EstateId,
deviceId,
originalDevice.Value.DeviceId, newDeviceIdentifier);

aggregate.ApplyAndAppend(deviceSwappedForMerchantEvent);

Expand Down Expand Up @@ -852,7 +856,7 @@ public static void PlayEvent(this MerchantAggregate aggregate, MerchantDomainEve

public static void PlayEvent(this MerchantAggregate aggregate, MerchantDomainEvents.DeviceSwappedForMerchantEvent domainEvent)
{
KeyValuePair<Guid, Device> device = aggregate.Devices.Single(d => d.Value.DeviceIdentifier == domainEvent.OriginalDeviceIdentifier);
KeyValuePair<Guid, Device> device = aggregate.Devices.Single(d => d.Key == domainEvent.OriginalDeviceId);
aggregate.Devices[device.Key] = device.Value with{
IsEnabled = false
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,26 @@ public static async Task<Result<Operator>> LoadOperator(this EstateManagementCon
return loadOperatorResult;
}

public static async Task<Result<MerchantDevice>> LoadOriginalMerchantDevice(this EstateManagementContext context, IDomainEvent domainEvent, CancellationToken cancellationToken)
{
Guid merchantId = DomainEventHelper.GetMerchantId(domainEvent);
Guid deviceId = DomainEventHelper.GetOriginalDeviceId(domainEvent);
MerchantDevice device = await context.MerchantDevices.SingleOrDefaultAsync(d => d.DeviceId == deviceId &&
d.MerchantId == merchantId, cancellationToken);

return device switch
{
null => Result.NotFound($"Original Device Id {deviceId} not found for Merchant {merchantId}"),
_ => Result.Success(device)
};
}

public static async Task<Result<MerchantDevice>> LoadMerchantDevice(this EstateManagementContext context, IDomainEvent domainEvent, CancellationToken cancellationToken)
{
Guid merchantId = DomainEventHelper.GetMerchantId(domainEvent);
Guid deviceId = DomainEventHelper.GetDeviceId(domainEvent);
MerchantDevice device = await context.MerchantDevices.SingleOrDefaultAsync(d => d.DeviceId == deviceId &&
d.MerchantId == merchantId, cancellationToken);
d.MerchantId == merchantId, cancellationToken);

return device switch
{
Expand Down Expand Up @@ -596,6 +610,7 @@ public static class DomainEventHelper
public static Guid GetFileId(IDomainEvent domainEvent) => DomainEventHelper.GetProperty<Guid>(domainEvent, "FileId");

public static Guid GetFileImportLogId(IDomainEvent domainEvent) => DomainEventHelper.GetProperty<Guid>(domainEvent, "FileImportLogId");
public static Guid GetOriginalDeviceId(IDomainEvent domainEvent) => DomainEventHelper.GetProperty<Guid>(domainEvent, "OriginalDeviceId");
public static Guid GetDeviceId(IDomainEvent domainEvent) => DomainEventHelper.GetProperty<Guid>(domainEvent, "DeviceId");
public static Guid GetMerchantId(IDomainEvent domainEvent) => DomainEventHelper.GetProperty<Guid>(domainEvent, "MerchantId");
public static Guid GetAddressId(IDomainEvent domainEvent) => DomainEventHelper.GetProperty<Guid>(domainEvent, "AddressId");
Expand Down
1 change: 1 addition & 0 deletions TransactionProcessor.Database/Entities/MerchantDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class MerchantDevice
public String DeviceIdentifier { get; set; }

public Guid MerchantId { get; set; }
public Boolean IsEnabled { get; set; }

#endregion
}
Expand Down
Loading
Loading