From 4ebeb12f50bb2f19532943a726228aee35871cba Mon Sep 17 00:00:00 2001 From: StuartFerguson Date: Sat, 11 Oct 2025 07:07:25 +0100 Subject: [PATCH] refactor AP Model Factory --- .../Factories/ModelFactoryTests.cs | 8 +- .../Factories/ModelFactory.cs | 105 +++++++++++------- 2 files changed, 69 insertions(+), 44 deletions(-) diff --git a/TransactionProcessor.Tests/Factories/ModelFactoryTests.cs b/TransactionProcessor.Tests/Factories/ModelFactoryTests.cs index c3fb6adc..3f9fa80b 100644 --- a/TransactionProcessor.Tests/Factories/ModelFactoryTests.cs +++ b/TransactionProcessor.Tests/Factories/ModelFactoryTests.cs @@ -565,7 +565,7 @@ public void ModelFactory_Merchant_NullAddresses_IsConverted() merchantResponse.MerchantId.ShouldBe(merchantModel.MerchantId); merchantResponse.MerchantName.ShouldBe(merchantModel.MerchantName); - merchantResponse.Addresses.ShouldBeNull(); + merchantResponse.Addresses.ShouldBeEmpty(); merchantResponse.Contacts.ShouldHaveSingleItem(); ContactResponse contactResponse = merchantResponse.Contacts.Single(); @@ -612,7 +612,7 @@ public void ModelFactory_Merchant_NullContacts_IsConverted() addressResponse.Country.ShouldBe(merchantModel.Addresses.Single().Country); addressResponse.PostalCode.ShouldBe(merchantModel.Addresses.Single().PostalCode); - merchantResponse.Contacts.ShouldBeNull(); + merchantResponse.Contacts.ShouldBeEmpty(); merchantResponse.Devices.ShouldHaveSingleItem(); KeyValuePair device = merchantResponse.Devices.Single(); @@ -659,7 +659,7 @@ public void ModelFactory_Merchant_NullDevices_IsConverted() contactResponse.ContactName.ShouldBe(merchantModel.Contacts.Single().ContactName); contactResponse.ContactPhoneNumber.ShouldBe(merchantModel.Contacts.Single().ContactPhoneNumber); - merchantResponse.Devices.ShouldBeNull(); + merchantResponse.Devices.ShouldBeEmpty(); merchantResponse.Operators.ShouldHaveSingleItem(); MerchantOperatorResponse operatorDetails = merchantResponse.Operators.Single(); @@ -706,7 +706,7 @@ public void ModelFactory_Merchant_NullOperators_IsConverted() device.Key.ShouldBe(merchantModel.Devices.Single().DeviceId); device.Value.ShouldBe(merchantModel.Devices.Single().DeviceIdentifier); - merchantResponse.Operators.ShouldBeNull(); + merchantResponse.Operators.ShouldBeEmpty(); } [Fact] diff --git a/TransactionProcessor/Factories/ModelFactory.cs b/TransactionProcessor/Factories/ModelFactory.cs index 2844a62f..b7d97538 100644 --- a/TransactionProcessor/Factories/ModelFactory.cs +++ b/TransactionProcessor/Factories/ModelFactory.cs @@ -377,30 +377,12 @@ private static TransactionProcessor.DataTransferObjects.Responses.Merchant.Settl #endregion - public static Result ConvertFrom(Models.Merchant.Merchant merchant) - { - if (merchant == null) - { - return Result.Invalid("merchant cannot be null"); - } - - MerchantResponse merchantResponse = new MerchantResponse - { - EstateId = merchant.EstateId, - EstateReportingId = merchant.EstateReportingId, - MerchantId = merchant.MerchantId, - MerchantReportingId = merchant.MerchantReportingId, - MerchantName = merchant.MerchantName, - SettlementSchedule = ModelFactory.ConvertFrom(merchant.SettlementSchedule), - MerchantReference = merchant.Reference, - NextStatementDate = merchant.NextStatementDate - }; + private static List ConvertFrom(List
addresses) { - if (merchant.Addresses != null && merchant.Addresses.Any()) + List result = new List(); + if (addresses != null && addresses.Any()) { - merchantResponse.Addresses = new List(); - - merchant.Addresses.ForEach(a => merchantResponse.Addresses.Add(new AddressResponse + addresses.ForEach(a => result.Add(new AddressResponse { AddressId = a.AddressId, Town = a.Town, @@ -414,11 +396,15 @@ public static Result ConvertFrom(Models.Merchant.Merchant merc })); } - if (merchant.Contacts != null && merchant.Contacts.Any()) - { - merchantResponse.Contacts = new List(); + return result; + } - merchant.Contacts.ForEach(c => merchantResponse.Contacts.Add(new ContactResponse + private static List ConvertFrom(List contacts) { + List result = new List(); + + if (contacts != null && contacts.Any()) + { + contacts.ForEach(c => result.Add(new ContactResponse { ContactId = c.ContactId, ContactPhoneNumber = c.ContactPhoneNumber, @@ -427,21 +413,27 @@ public static Result ConvertFrom(Models.Merchant.Merchant merc })); } - if (merchant.Devices != null && merchant.Devices.Any()) - { - merchantResponse.Devices = new Dictionary(); + return result; + } - foreach (Device device in merchant.Devices) - { - merchantResponse.Devices.Add(device.DeviceId, device.DeviceIdentifier); + public static Dictionary ConvertFrom(List devices) { + Dictionary result = new(); + + if (devices != null && devices.Any()) { + foreach (Device device in devices) { + result.Add(device.DeviceId, device.DeviceIdentifier); } } - if (merchant.Operators != null && merchant.Operators.Any()) - { - merchantResponse.Operators = new List(); + return result; + } + + private static List ConvertFrom(List operators) { + List result = new List(); - merchant.Operators.ForEach(a => merchantResponse.Operators.Add(new MerchantOperatorResponse + if (operators != null && operators.Any()) + { + operators.ForEach(a => result.Add(new MerchantOperatorResponse { Name = a.Name, MerchantNumber = a.MerchantNumber, @@ -451,11 +443,16 @@ public static Result ConvertFrom(Models.Merchant.Merchant merc })); } - if (merchant.Contracts != null && merchant.Contracts.Any()) + return result; + } + + private static List ConvertFrom(List contracts) { + List result = new(); + + if (contracts != null && contracts.Any()) { - merchantResponse.Contracts = new List(); - merchant.Contracts.ForEach(mc => { - merchantResponse.Contracts.Add(new MerchantContractResponse() + contracts.ForEach(mc => { + result.Add(new MerchantContractResponse() { ContractId = mc.ContractId, ContractProducts = mc.ContractProducts, @@ -464,6 +461,34 @@ public static Result ConvertFrom(Models.Merchant.Merchant merc }); } + return result; + } + + public static Result ConvertFrom(Models.Merchant.Merchant merchant) + { + if (merchant == null) + { + return Result.Invalid("merchant cannot be null"); + } + + MerchantResponse merchantResponse = new MerchantResponse + { + EstateId = merchant.EstateId, + EstateReportingId = merchant.EstateReportingId, + MerchantId = merchant.MerchantId, + MerchantReportingId = merchant.MerchantReportingId, + MerchantName = merchant.MerchantName, + SettlementSchedule = ModelFactory.ConvertFrom(merchant.SettlementSchedule), + MerchantReference = merchant.Reference, + NextStatementDate = merchant.NextStatementDate + }; + + merchantResponse.Addresses = ConvertFrom(merchant.Addresses); + merchantResponse.Contacts = ConvertFrom(merchant.Contacts); + merchantResponse.Devices = ConvertFrom(merchant.Devices); + merchantResponse.Operators = ConvertFrom(merchant.Operators); + merchantResponse.Contracts = ConvertFrom(merchant.Contracts); + return merchantResponse; } }