Exemplo n.º 1
0
        private BlockChainTransaction ConfirmDonation(User currentUser)
        {
            var currentLevel = _query
                               .CurrentBitLevel(currentUser)
                               .ThrowIfNull("User has no BitLevel yet");

            //for a donation without receiver and sender
            if (currentLevel.Donation.SenderId <= 0 ||
                currentLevel.Donation.ReceiverId <= 0)
            {
                throw new Exception("Invalid Donation transaction Receiver/Sender");
            }

            var blockChainTransaction = _blockChain
                                        .AcquireTransactionDetails(currentLevel.Donation.TransactionHash)
                                        .Resolve()
                                        .ThrowIf(_bct => _bct.LedgerCount < 3, "Transaction ledger count is less than threshold");

            if (IsSameTransaction(currentLevel.Donation, blockChainTransaction) &&
                blockChainTransaction.Status == BlockChainTransactionStatus.Verified)
            {
                currentLevel.Donation.Status      = BlockChainTransactionStatus.Verified;
                currentLevel.Donation.LedgerCount = blockChainTransaction.LedgerCount;
                _pcommand.Update(currentLevel.Donation);

                //increment receiver's donnation count
                var receiverLevel = _query.CurrentBitLevel(blockChainTransaction.Receiver.Owner);
                receiverLevel.DonationCount++;
                _pcommand.Update(receiverLevel);

                //notify the receiver
                var message = @"
<h3>Congratulations!</h3>
<p>You just received a donation of " + blockChainTransaction.Amount + @" from " + AddressOwnerName(blockChainTransaction.Receiver) + @"</p>
Cheers.
";
                _backgroundProcessor.EnqueueOperation <IEmailPush>(_mp => _mp.SendMail(new GenericMessage
                {
                    From        = Constants.MailOrigin_DoNotReply,
                    Recipients  = new[] { blockChainTransaction.Receiver.OwnerId },
                    Subject     = "Donation Received",
                    LogoUrl     = _urlProvider.LogoUri().Resolve(),
                    LogoTextUrl = _urlProvider.LogoTextUri().Resolve(),
                    Message     = message
                }))
                .Resolve();

                _notifier.NotifyUser(new Notification
                {
                    Message  = message,
                    TargetId = blockChainTransaction.Receiver.OwnerId,
                    Title    = "Donation Received",
                    Type     = NotificationType.Info
                });

                return(currentLevel.Donation);
            }
            else
            {
                throw new Exception("Invalid transaction hash");
            }
        }