Пример #1
0
        /// <summary>
        /// OperationId is defined optional just to extend the message pack object - the parameter is still mandatory.
        /// </summary>
        public AccountChangedEvent(DateTime changeTimestamp,
                                   [NotNull] string source,
                                   [NotNull] AccountContract account,
                                   AccountChangedEventTypeContract eventType,
                                   AccountBalanceChangeContract balanceChange = null,
                                   string operationId        = null,
                                   string activitiesMetadata = null)
        {
            if (!Enum.IsDefined(typeof(AccountChangedEventTypeContract), eventType))
            {
                throw new InvalidEnumArgumentException(
                          nameof(eventType),
                          (int)eventType,
                          typeof(AccountChangedEventTypeContract));
            }

            if (changeTimestamp == default(DateTime))
            {
                throw new ArgumentOutOfRangeException(nameof(changeTimestamp));
            }

            Source             = source;
            ChangeTimestamp    = changeTimestamp;
            Account            = account ?? throw new ArgumentNullException(nameof(account));
            EventType          = eventType;
            ActivitiesMetadata = activitiesMetadata;
            BalanceChange      = balanceChange;
            OperationId        = operationId;
        }
        public void SendAccountChangedEvent(string source, IAccount account, AccountChangedEventTypeContract eventType,
                                            string operationId, AccountBalanceChangeContract balanceChangeContract = null,
                                            IAccount previousSnapshot = null,
                                            string orderId            = null)
        {
            var metadata = new AccountChangeMetadata {
                OrderId = orderId
            };

            if (previousSnapshot != null)
            {
                metadata.PreviousAccountSnapshot =
                    _convertService.Convert <IAccount, AccountContract>(previousSnapshot);
            }

            CqrsEngine.PublishEvent(
                new AccountChangedEvent(
                    account.ModificationTimestamp,
                    source,
                    _convertService.Convert <IAccount, AccountContract>(account),
                    eventType,
                    balanceChangeContract,
                    operationId,
                    metadata.ToJson()),
                _contextNames.AccountsManagement);
        }
 private static AccountHistory Map(AccountBalanceChangeContract accountBalanceChangeContract, string correlationId)
 {
     return(new AccountHistory(
                accountBalanceChangeContract.Id,
                changeAmount: accountBalanceChangeContract.ChangeAmount,
                accountId: accountBalanceChangeContract.AccountId,
                changeTimestamp: accountBalanceChangeContract.ChangeTimestamp,
                clientId: accountBalanceChangeContract.ClientId,
                balance: accountBalanceChangeContract.Balance,
                withdrawTransferLimit: accountBalanceChangeContract.WithdrawTransferLimit,
                comment: accountBalanceChangeContract.Comment,
                reasonType: accountBalanceChangeContract.ReasonType.ToType <AccountBalanceChangeReasonType>(),
                eventSourceId: accountBalanceChangeContract.EventSourceId,
                legalEntity: accountBalanceChangeContract.LegalEntity,
                auditLog: accountBalanceChangeContract.AuditLog,
                instrument: accountBalanceChangeContract.Instrument,
                tradingDate: accountBalanceChangeContract.TradingDate,
                correlationId: correlationId));
 }
Пример #4
0
        private async Task Handle(UpdateBalanceInternalCommand command,
            IEventPublisher publisher)
        {
            var executionInfo = await _executionInfoRepository.GetOrAddAsync(
                OperationName,
                command.OperationId,
                () => new OperationExecutionInfo<OperationData>(
                    OperationName,
                    command.OperationId,
                    new  OperationData { State = OperationState.Created },
                    _systemClock.UtcNow.UtcDateTime));

            if (SwitchState(executionInfo.Data, OperationState.Created, OperationState.Started))
            {
                IAccount account = null;
                try
                {
                    account = await _accountsRepository.UpdateBalanceAsync(
                        command.OperationId,
                        command.AccountId,
                        command.AmountDelta,
                        false);
                }
                catch (ValidationException ex)
                {
                    await _log.WriteWarningAsync(nameof(UpdateBalanceCommandsHandler),
                        nameof(UpdateBalanceInternalCommand), ex.Message);
                    
                    publisher.PublishEvent(new AccountBalanceChangeFailedEvent(command.OperationId,
                        _systemClock.UtcNow.UtcDateTime, ex.Message, command.Source));
                
                    await _executionInfoRepository.SaveAsync(executionInfo);
                    
                    return;
                }

                _chaosKitty.Meow(command.OperationId);

                var change = new AccountBalanceChangeContract(
                    command.OperationId,
                    account.ModificationTimestamp,
                    account.Id,
                    account.ClientId,
                    command.AmountDelta,
                    account.Balance,
                    account.WithdrawTransferLimit,
                    command.Comment,
                    Convert(command.ChangeReasonType),
                    command.EventSourceId,
                    account.LegalEntity,
                    command.AuditLog,
                    command.AssetPairId,
                    command.TradingDay);

                var convertedAccount = Convert(account);

                publisher.PublishEvent(
                    new AccountChangedEvent(
                        change.ChangeTimestamp,
                        command.Source,
                        convertedAccount,
                        AccountChangedEventTypeContract.BalanceUpdated,
                        change,
                        command.OperationId)
                );
                
                await _executionInfoRepository.SaveAsync(executionInfo);
            }
        }