@@ -1025,47 +1025,8 @@ join e in context.Estates on eo.EstateId equals e.EstateId
10251025
10261026 foreach ( var contractData in query )
10271027 {
1028- // attempt to find the contract
1029- ContractModel contract = contracts . SingleOrDefault ( c => c . ContractId == contractData . Contract . ContractId ) ;
1030-
1031- if ( contract == null )
1032- {
1033- // create the contract
1034- contract = new ContractModel
1035- {
1036- EstateReportingId = contractData . Estate . EstateReportingId ,
1037- EstateId = contractData . Estate . EstateId ,
1038- OperatorId = contractData . Contract . OperatorId ,
1039- OperatorName = contractData . Operator . Name ,
1040- Products = new List < Product > ( ) ,
1041- Description = contractData . Contract . Description ,
1042- IsCreated = true ,
1043- ContractId = contractData . Contract . ContractId ,
1044- ContractReportingId = contractData . Contract . ContractReportingId
1045- } ;
1046-
1047- contracts . Add ( contract ) ;
1048- }
1049-
1050- // Now add the product if not already added
1051- Boolean productFound = contract . Products . Any ( p => p . ContractProductId == contractData . Product . ContractProductId ) ;
1052-
1053- if ( productFound == false )
1054- {
1055- if ( contractData . Product != null )
1056- {
1057- // Not already there so need to add it
1058- contract . Products . Add ( new Product
1059- {
1060- ContractProductId = contractData . Product . ContractProductId ,
1061- TransactionFees = null ,
1062- Value = contractData . Product . Value ,
1063- Name = contractData . Product . ProductName ,
1064- DisplayText = contractData . Product . DisplayText ,
1065- ContractProductReportingId = contractData . Product . ContractProductReportingId
1066- } ) ;
1067- }
1068- }
1028+ ContractModel contract = this . GetOrCreateContract ( contracts , contractData . Estate , contractData . Contract , contractData . Operator ) ;
1029+ this . AddProductIfRequired ( contract , contractData . Product ) ;
10691030 }
10701031
10711032 return Result . Success ( contracts ) ;
@@ -1095,6 +1056,60 @@ private async Task<EstateManagementContext> GetContext(Guid estateId)
10951056 return resolvedContext . Context ;
10961057 }
10971058
1059+ private ContractModel GetOrCreateContract ( List < ContractModel > contracts ,
1060+ Estate estate ,
1061+ Contract contractData ,
1062+ Operator operatorData )
1063+ {
1064+ ContractModel contract = contracts . SingleOrDefault ( c => c . ContractId == contractData . ContractId ) ;
1065+
1066+ if ( contract != null )
1067+ {
1068+ return contract ;
1069+ }
1070+
1071+ contract = new ContractModel
1072+ {
1073+ EstateReportingId = estate . EstateReportingId ,
1074+ EstateId = estate . EstateId ,
1075+ OperatorId = contractData . OperatorId ,
1076+ OperatorName = operatorData . Name ,
1077+ Products = new List < Product > ( ) ,
1078+ Description = contractData . Description ,
1079+ IsCreated = true ,
1080+ ContractId = contractData . ContractId ,
1081+ ContractReportingId = contractData . ContractReportingId
1082+ } ;
1083+
1084+ contracts . Add ( contract ) ;
1085+
1086+ return contract ;
1087+ }
1088+
1089+ private void AddProductIfRequired ( ContractModel contract ,
1090+ ContractProduct product )
1091+ {
1092+ if ( product == null )
1093+ {
1094+ return ;
1095+ }
1096+
1097+ Boolean productFound = contract . Products . Any ( p => p . ContractProductId == product . ContractProductId ) ;
1098+
1099+ if ( ! productFound )
1100+ {
1101+ contract . Products . Add ( new Product
1102+ {
1103+ ContractProductId = product . ContractProductId ,
1104+ TransactionFees = null ,
1105+ Value = product . Value ,
1106+ Name = product . ProductName ,
1107+ DisplayText = product . DisplayText ,
1108+ ContractProductReportingId = product . ContractProductReportingId
1109+ } ) ;
1110+ }
1111+ }
1112+
10981113 public async Task < Result > AddContractProductTransactionFee ( ContractDomainEvents . TransactionFeeForProductAddedToContractEvent domainEvent ,
10991114 CancellationToken cancellationToken ) {
11001115 EstateManagementContext context = await this . GetContext ( domainEvent . EstateId ) ;
0 commit comments