Ejemplo n.º 1
0
        public static MedicineBatchTransferQueryData ToMedicineBatchTransferQueryData(this MedicineBatchTransfer transfer)
        {
            if (transfer == null)
            {
                return(null);
            }
            var result = new MedicineBatchTransferQueryData()
            {
                From              = transfer.From.ToTenantQueryData(),
                To                = transfer.To.ToTenantQueryData(),
                Id                = transfer.Id,
                MedicineBatch     = transfer.MedicineBatch.ToMedicineBatchQueryData(),
                Quantity          = transfer.Quantity,
                IsConfirmed       = transfer.IsConfirmed,
                ContractAddress   = transfer.ContractAddress,
                DateCreated       = transfer.DateCreated,
                TransactionHash   = transfer.TransactionHash,
                TransactionStatus = transfer.TransactionStatus.ToString()
            };

            return(result);
        }
        void IMedicineBatchTransferBackgroundJob.WaitForTransactionToSuccessThenFinishCreating(MedicineBatchTransfer transfer)
        {
            bool isTransactionSuccess = false;

            do
            {
                var receipt = ethereumService.GetTransactionReceipt(transfer.TransactionHash).Result;
                if (receipt == null)
                {
                    continue;
                }
                if (receipt.Status.Value == (new HexBigInteger(1)).Value)
                {
                    isTransactionSuccess = true;
                    var contractAddress = ethereumService.GetObjectContractAddress(transfer.Id).Result;
                    transfer.ContractAddress   = contractAddress;
                    transfer.TransactionStatus = TransactionStatuses.Success;
                    medicineBatchTransferRepository.Update(transfer);
                }
            }while (isTransactionSuccess != true);
        }
Ejemplo n.º 3
0
 Guid IMedicineBatchTransferRepository.CreateAndReturnId(MedicineBatchTransfer medicineBatchTransfer)
 {
     (this as IMedicineBatchTransferRepository).Create(medicineBatchTransfer);
     return(medicineBatchTransfer.Id);
 }
Ejemplo n.º 4
0
 void IMedicineBatchTransferRepository.Update(MedicineBatchTransfer medicineBatchTransfer)
 {
     dbContext.MedicineBatchTransfers.Update(medicineBatchTransfer);
     dbContext.SaveChanges();
 }
Ejemplo n.º 5
0
        async Task <Guid> IMedicineBatchTransferService.Create(Guid medicineBatchId, Guid fromTenantId, Guid toTenantId, uint quantity)
        {
            var allTransfer = medicineBatchTransferRepository.GetAll();
            var batch       = medicineBatchRepository.Get(medicineBatchId);
            var transfer    = new MedicineBatchTransfer()
            {
                MedicineBatchId = batch.Id,
                FromId          = fromTenantId,
                ToId            = toTenantId,
                Quantity        = quantity,
                IsConfirmed     = false,
                DateCreated     = DateTime.UtcNow
            };

            // TODO: Check inventory of fromTenant

            // Set the tier.
            uint tier = 999;

            if (fromTenantId == batch.ManufacturerId)
            {
                tier = 0;
            }
            else
            {
                var firstTransaction = allTransfer
                                       .Where(t => t.MedicineBatchId == batch.Id && t.FromId == batch.ManufacturerId)
                                       .SingleOrDefault();
                if (fromTenantId == firstTransaction.ToId)
                {
                    tier = 1;
                }
                else
                {
                    // Loop through all parent transactions of FromTenantId
                    var transferPool = allTransfer.Where(t => t.MedicineBatchId == batch.Id).ToList();
                    bool parentMatchCondition(MedicineBatchTransfer t) => t.ToId == transfer.FromId;

                    if (transfer.HasParent(transferPool, parentMatchCondition))
                    {
                        tier = transfer.Parent(transferPool, parentMatchCondition).First().Tier + 1;
                    }
                    // TODO: Some errors occured here.
                }
            }

            transfer.Tier = tier;
            var newTransferId = medicineBatchTransferRepository.CreateAndReturnId(transfer);

            var function        = ethereumService.GetFunction(EthereumFunctions.AddMedicineBatchTransfer);
            var transactionHash = await function.SendTransactionAsync(
                ethereumService.GetEthereumAccount(),
                new HexBigInteger(6000000),
                new HexBigInteger(Nethereum.Web3.Web3.Convert.ToWei(10, UnitConversion.EthUnit.Gwei)),
                new HexBigInteger(0),
                functionInput : new object[] {
                newTransferId.ToString(),
                transfer.MedicineBatchId.ToString(),
                transfer.FromId.ToString(),
                transfer.ToId.ToString(),
                transfer.Quantity,
                transfer.DateCreated.ToUnixTimestamp(),
                tier
            });

            transfer.TransactionHash = transactionHash;
            medicineBatchTransferRepository.Update(transfer);


            BackgroundJob.Schedule <IMedicineBatchTransferBackgroundJob>(
                job => job.WaitForTransactionToSuccessThenFinishCreating(transfer),
                TimeSpan.FromSeconds(3)
                );

            return(newTransferId);
        }