2929import java .time .format .DateTimeFormatter ;
3030import java .util .ArrayList ;
3131import java .util .Arrays ;
32+ import java .util .Collection ;
3233import java .util .HashMap ;
3334import java .util .HashSet ;
3435import java .util .List ;
@@ -706,16 +707,8 @@ public Map<String, Object> validateAndApplyAddtionalShares(JsonCommand jsonComma
706707 }
707708 }
708709 boolean isTransactionBeforeExistingTransactions = false ;
709- Set <ShareAccountTransaction > transactions = account .getShareAccountTransactions ();
710- for (ShareAccountTransaction transaction : transactions ) {
711- if (!transaction .isChargeTransaction ()) {
712- LocalDate transactionDate = transaction .getPurchasedDate ();
713- if (DateUtils .isBefore (requestedDate , transactionDate )) {
714- isTransactionBeforeExistingTransactions = true ;
715- break ;
716- }
717- }
718- }
710+ isTransactionBeforeExistingTransactions = isTransactionBeforeExistingTransactions (requestedDate ,
711+ isTransactionBeforeExistingTransactions , account );
719712 if (isTransactionBeforeExistingTransactions ) {
720713 baseDataValidator .reset ().parameter (ShareAccountApiConstants .requesteddate_paramname ).value (requestedDate )
721714 .failWithCodeNoParameterAddedToErrorCode ("purchase.transaction.date.cannot.be.before.existing.transactions" );
@@ -732,6 +725,23 @@ public Map<String, Object> validateAndApplyAddtionalShares(JsonCommand jsonComma
732725 return actualChanges ;
733726 }
734727
728+ private boolean isTransactionBeforeExistingTransactions (LocalDate requestedDate , boolean isTransactionBeforeExistingTransactions ,
729+ ShareAccount shareAccount ) {
730+ Collection <ShareAccountTransaction > activeTransactions = shareAccount .getShareAccountTransactions ().stream ()
731+ .filter (tr -> tr .isActive () && !tr .isChargeTransaction () && !tr .isPurchaseRejectedTransaction ()).toList ();
732+
733+ for (ShareAccountTransaction transaction : activeTransactions ) {
734+ if (!transaction .isChargeTransaction () && transaction .isActive () && !transaction .isPurchaseRejectedTransaction ()) {
735+ LocalDate transactionDate = transaction .getPurchasedDate ();
736+ if (DateUtils .isBefore (requestedDate , transactionDate )) {
737+ isTransactionBeforeExistingTransactions = true ;
738+ break ;
739+ }
740+ }
741+ }
742+ return isTransactionBeforeExistingTransactions ;
743+ }
744+
735745 private void handleAdditionalSharesChargeTransactions (final ShareAccount account , final ShareAccountTransaction purchaseTransaction ) {
736746 Set <ShareAccountCharge > charges = account .getCharges ();
737747 BigDecimal totalChargeAmount = BigDecimal .ZERO ;
@@ -863,16 +873,8 @@ public Map<String, Object> validateAndRedeemShares(JsonCommand jsonCommand, Shar
863873 baseDataValidator .reset ().parameter (ShareAccountApiConstants .requestedshares_paramname ).value (sharesRequested ).notNull ()
864874 .longGreaterThanZero ();
865875 boolean isTransactionBeforeExistingTransactions = false ;
866- Set <ShareAccountTransaction > transactions = account .getShareAccountTransactions ();
867- for (ShareAccountTransaction transaction : transactions ) {
868- if (!transaction .isChargeTransaction () && transaction .isActive ()) {
869- LocalDate transactionDate = transaction .getPurchasedDate ();
870- if (DateUtils .isBefore (requestedDate , transactionDate )) {
871- isTransactionBeforeExistingTransactions = true ;
872- break ;
873- }
874- }
875- }
876+ isTransactionBeforeExistingTransactions = isTransactionBeforeExistingTransactions (requestedDate ,
877+ isTransactionBeforeExistingTransactions , account );
876878 if (isTransactionBeforeExistingTransactions ) {
877879 baseDataValidator .reset ().parameter (ShareAccountApiConstants .requesteddate_paramname ).value (requestedDate )
878880 .failWithCodeNoParameterAddedToErrorCode ("redeem.transaction.date.cannot.be.before.existing.transactions" );
@@ -1018,16 +1020,8 @@ public Map<String, Object> validateAndClose(JsonCommand jsonCommand, ShareAccoun
10181020 throw new PlatformApiDataValidationException (dataValidationErrors );
10191021 }
10201022 boolean isTransactionBeforeExistingTransactions = false ;
1021- Set <ShareAccountTransaction > transactions = account .getShareAccountTransactions ();
1022- for (ShareAccountTransaction transaction : transactions ) {
1023- if (!transaction .isChargeTransaction ()) {
1024- LocalDate transactionDate = transaction .getPurchasedDate ();
1025- if (DateUtils .isBefore (closedDate , transactionDate )) {
1026- isTransactionBeforeExistingTransactions = true ;
1027- break ;
1028- }
1029- }
1030- }
1023+ isTransactionBeforeExistingTransactions = isTransactionBeforeExistingTransactions (closedDate ,
1024+ isTransactionBeforeExistingTransactions , account );
10311025 if (isTransactionBeforeExistingTransactions ) {
10321026 baseDataValidator .reset ().parameter (ShareAccountApiConstants .closeddate_paramname ).value (closedDate )
10331027 .failWithCodeNoParameterAddedToErrorCode ("share.account.cannot.be.closed.before.existing.transactions" );
0 commit comments