Esempio n. 1
0
        public async Task <IActionResult> Create([FromBody] AddTransactionCommand command)
        {
            var result = await _mediator.Send(command);

            return(Ok(result));
        }
 public async Task HandleAsync(AddTransactionCommand addTransactionCommand)
 {
     await transactionRepository.Add(addTransactionCommand);
 }
Esempio n. 3
0
        /// <summary>
        /// Delivery to store
        /// </summary>
        /// <remarks>
        /// -Amount Source Organization (Warehouse)
        ///  +Amount Target Organization (Store)
        /// </remarks>
        /// <param name="command"></param>
        /// <param name="cargo"></param>
        public void DeliveryToStore(AddTransactionCommand command, CargoTypeDetailedDTO cargo)
        {
            if (!command.SourceOrganizationId.HasValue | !command.TargetOrganizationId.HasValue)
            {
                throw new ArgumentException("NO_SOURCE_OR_TARGET_ORGANIZATION_IN_REQUEST");
            }

            var sourceOrganizationResponse =
                _organizationService.GetOrganizationById(new GetOrganizationByIdRequest
            {
                Id = command.SourceOrganizationId.Value
            });

            if (sourceOrganizationResponse.ServiceStatus != ServiceStatus.Success)
            {
                throw new ServiceErrorException(sourceOrganizationResponse.ErrorMessage, "OrganizationService");
            }
            if (!sourceOrganizationResponse.OrganizationDetailed.OrganizationType.Equals("warehouse", StringComparison.CurrentCultureIgnoreCase))
            {
                throw new ArgumentException("TYPE_OF_SOURCE_ORGANIZATION_IS_NOT_WAREOUSE");
            }

            var targetOrganizationResponse =
                _organizationService.GetOrganizationById(new GetOrganizationByIdRequest
            {
                Id = command.TargetOrganizationId.Value
            });

            if (targetOrganizationResponse.ServiceStatus != ServiceStatus.Success)
            {
                throw new ServiceErrorException(targetOrganizationResponse.ErrorMessage, "OrganizationService");
            }
            if (!targetOrganizationResponse.OrganizationDetailed.OrganizationType.Equals("store", StringComparison.CurrentCultureIgnoreCase))
            {
                throw new ArgumentException("TYPE_OF_TARGET_ORGANIZATION_IS_NOT_STORE");
            }

            var sourceLedger = _unitOfWork.LedgerTransactionRepository.Add(new LedgerTransaction()
            {
                Amount         = -command.Amount,
                OrganizationId = sourceOrganizationResponse.OrganizationDetailed.Id,
                TotalPrice     = command.Amount * cargo.Price
            });
            var targetLedger = _unitOfWork.LedgerTransactionRepository.Add(new LedgerTransaction()
            {
                Amount         = command.Amount,
                OrganizationId = targetOrganizationResponse.OrganizationDetailed.Id,
                TotalPrice     = command.Amount * cargo.Price
            });

            _unitOfWork.Commit();
            _eventBus.Publish(GetLedgerTransactionEvent(command, sourceLedger, cargo, sourceOrganizationResponse.OrganizationDetailed));
            _eventBus.Publish(GetLedgerTransactionEvent(command, targetLedger, cargo, targetOrganizationResponse.OrganizationDetailed));

            // Minus balance for source
            var balanceSourse = AddOrUpdateMinusBalance(command.Amount,
                                                        sourceOrganizationResponse.OrganizationDetailed.Id, cargo.Id, sourceLedger.Id);
            // Plus balance for source
            var balanceTarget = AddOrUpdatePlusBalance(command.Amount,
                                                       targetOrganizationResponse.OrganizationDetailed.Id, cargo.Id, targetLedger.Id);

            // Add transaction
            _unitOfWork.TransactionRepository.Add(new Transaction()
            {
                Amount             = command.Amount,
                CargoId            = cargo.Id,
                Comments           = command.Comments,
                LedgerTransactions = new List <LedgerTransaction>()
                {
                    sourceLedger, targetLedger
                },
                SourceOrganizationId = sourceOrganizationResponse.OrganizationDetailed.Id,
                TargetOrganizationId = targetOrganizationResponse.OrganizationDetailed.Id,
                Timestamp            = DateTime.Now,
                TransactionType      = command.TransactionType,
                UserId = "test"
            });

            _unitOfWork.Commit();
            _eventBus.Publish(GetBalanceUpdatedEvent(command, balanceSourse, cargo, sourceOrganizationResponse.OrganizationDetailed));
            _eventBus.Publish(GetBalanceUpdatedEvent(command, balanceTarget, cargo, targetOrganizationResponse.OrganizationDetailed));
        }
Esempio n. 4
0
        /// <summary>
        ///  Manual transfer of cargo
        /// </summary>
        /// <remarks>
        ///  +Amount Source Organization (Any type - Negative amount allowed)
        ///  -Amount Target Organization (Any type - Negative amount allowed)
        /// </remarks>
        /// <param name="command"></param>
        /// <param name="cargo"></param>
        public void ManualTransferOfCargo(AddTransactionCommand command, CargoTypeDetailedDTO cargo)
        {
            if (!command.SourceOrganizationId.HasValue | !command.TargetOrganizationId.HasValue)
            {
                throw new ArgumentException("NO_SOURCE_OR_TARGET_ORGANIZATION_IN_REQUEST");
            }

            var sourceOrganizationResponse =
                _organizationService.GetOrganizationById(new GetOrganizationByIdRequest
            {
                Id = command.SourceOrganizationId.Value
            });

            if (sourceOrganizationResponse.ServiceStatus != ServiceStatus.Success)
            {
                throw new ServiceErrorException(sourceOrganizationResponse.ErrorMessage, "OrganizationService");
            }

            var targetOrganizationResponse =
                _organizationService.GetOrganizationById(new GetOrganizationByIdRequest
            {
                Id = command.TargetOrganizationId.Value
            });

            if (targetOrganizationResponse.ServiceStatus != ServiceStatus.Success)
            {
                throw new ServiceErrorException(targetOrganizationResponse.ErrorMessage, "OrganizationService");
            }

            var sourceLedger = _unitOfWork.LedgerTransactionRepository.Add(new LedgerTransaction()
            {
                Amount         = command.Amount,
                OrganizationId = sourceOrganizationResponse.OrganizationDetailed.Id,
                TotalPrice     = command.Amount * cargo.Price
            });

            var targetLedger = _unitOfWork.LedgerTransactionRepository.Add(new LedgerTransaction()
            {
                Amount         = -command.Amount,
                OrganizationId = targetOrganizationResponse.OrganizationDetailed.Id,
                TotalPrice     = command.Amount * cargo.Price
            });

            _unitOfWork.Commit();
            _eventBus.Publish(GetLedgerTransactionEvent(command, sourceLedger, cargo, sourceOrganizationResponse.OrganizationDetailed));
            _eventBus.Publish(GetLedgerTransactionEvent(command, targetLedger, cargo, targetOrganizationResponse.OrganizationDetailed));

            var balanceSourse = AddOrUpdatePlusBalance(command.Amount, sourceOrganizationResponse.OrganizationDetailed.Id, cargo.Id, sourceLedger.Id);
            var balanceTarget = AddOrUpdateMinusBalance(command.Amount,
                                                        targetOrganizationResponse.OrganizationDetailed.Id, cargo.Id, targetLedger.Id);

            // Add transaction
            _unitOfWork.TransactionRepository.Add(new Transaction()
            {
                Amount             = command.Amount,
                CargoId            = cargo.Id,
                Comments           = command.Comments,
                LedgerTransactions = new List <LedgerTransaction>()
                {
                    sourceLedger, targetLedger
                },
                SourceOrganizationId = sourceOrganizationResponse.OrganizationDetailed.Id,
                TargetOrganizationId = targetOrganizationResponse.OrganizationDetailed.Id,
                Timestamp            = DateTime.Now,
                TransactionType      = command.TransactionType,
                UserId = "test"
            });

            _unitOfWork.Commit();
            _eventBus.Publish(GetBalanceUpdatedEvent(command, balanceSourse, cargo, sourceOrganizationResponse.OrganizationDetailed));
            _eventBus.Publish(GetBalanceUpdatedEvent(command, balanceTarget, cargo, targetOrganizationResponse.OrganizationDetailed));
        }
Esempio n. 5
0
 public IActionResult AddTransaction(AddTransactionCommand command)
 {
     _mediator.Send(command);
     return(new OkResult());
 }