예제 #1
0
        async Task <Guid> ITenantService.Create(
            string name,
            string email,
            string address,
            string phoneNumber,
            string taxCode,
            string registrationCode,
            string certificates,
            TenantTypes type)
        {
            var function = ethereumService.GetFunction(EthereumFunctions.AddChainPoint);

            try
            {
                var tenant = new Tenant()
                {
                    Name             = name,
                    Email            = email,
                    PrimaryAddress   = address,
                    PhoneNumber      = phoneNumber,
                    TaxCode          = taxCode,
                    RegistrationCode = registrationCode,
                    Certificates     = certificates,
                    DateCreated      = DateTime.UtcNow,
                    Type             = type
                };
                Guid newTenantId = tenantRepository.CreateAndReturnId(tenant);

                var transactionHash = await function.SendTransactionAsync(
                    ethereumService.GetEthereumAccount(),
                    new HexBigInteger(4000000),
                    new HexBigInteger(Nethereum.Web3.Web3.Convert.ToWei(50, UnitConversion.EthUnit.Gwei)),
                    new HexBigInteger(0),
                    functionInput : new object[] {
                    newTenantId.ToString(),
                    name,
                    address,
                    phoneNumber,
                    taxCode,
                    registrationCode
                });

                tenant.TransactionHash = transactionHash;
                tenantRepository.Update(tenant);

                // Create auth0 user.
                var userRole  = (type == TenantTypes.Manufacturer) ? "manufacturer" : (type == TenantTypes.Distributor ? "distributor" : (type == TenantTypes.Retailer ? "retailer" : "unknown"));
                var userAuth0 = auth0Service.CreateUser(newTenantId.ToString(), email, "123456789?a", userRole);

                BackgroundJob.Schedule <ITenantBackgroundJob>(
                    backgroundJob => backgroundJob.WaitForTransactionToSuccessThenFinishCreatingTenant(tenant),
                    TimeSpan.FromSeconds(3)
                    );

                return(newTenantId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }