Пример #1
0
        private async Task <ReferralLead> DecryptAsync(ReferralLeadEncrypted referralLeadEncrypted)
        {
            var referralLead = _mapper.Map <ReferralLead>(referralLeadEncrypted);

            await LoadSensitiveDataAsync(referralLead);

            return(referralLead);
        }
Пример #2
0
        public PropertyPurchaseServiceTestsFixture()
        {
            ReferralLeadRepositoryMock    = new Mock <IReferralLeadRepository>(MockBehavior.Strict);
            PropertyPurchaseRepositorMock = new Mock <IPropertyPurchaseRepository>(MockBehavior.Strict);
            StakeServiceMock = new Mock <IStakeService>(MockBehavior.Strict);

            Service = new PropertyPurchaseService(
                ReferralLeadRepositoryMock.Object,
                PropertyPurchaseRepositorMock.Object,
                StakeServiceMock.Object,
                new CommissionManager(MapperHelper.CreateAutoMapper()));

            ReferralLead = new ReferralLeadEncrypted
            {
                Id                = Guid.NewGuid(),
                AgentId           = AgentId,
                PhoneNumberHash   = PhoneNumber,
                EmailHash         = Email,
                ConfirmationToken = ConfirmationToken,
                CampaignId        = Guid.NewGuid()
            };

            ReferralLeadWithDetails = new ReferralLeadEncryptedWithDetails
            {
                Id                = Guid.NewGuid(),
                AgentId           = AgentId,
                PhoneNumberHash   = PhoneNumber,
                EmailHash         = Email,
                ConfirmationToken = ConfirmationToken
            };

            PropertyPurchase = new PropertyPurchase
            {
                Id             = Guid.NewGuid(),
                ReferralLeadId = Guid.NewGuid(),
                Timestamp      = DateTime.UtcNow
            };

            ReferralLeads = new List <ReferralLeadEncrypted> {
                ReferralLead
            };

            ReferralLeadsWithDetails = new List <ReferralLeadEncryptedWithDetails> {
                ReferralLeadWithDetails
            };

            PropertyPurchases = new List <PropertyPurchase>
            {
                PropertyPurchase,
                PropertyPurchase,
                PropertyPurchase
            };

            SetupCalls();
        }
Пример #3
0
        private async Task PublishLeadApproved(ReferralLead referralLead, ReferralLeadEncrypted referralLeadEncrypted)
        {
            await PublishLeadChangeStateEvent(referralLead.Id.ToString(),
                                              Contract.Enums.ReferralLeadState.Approved);

            await _propertyLeadApprovedReferralPublisher.PublishAsync(new PropertyLeadApprovedReferralEvent
            {
                ReferrerId = referralLeadEncrypted.AgentId.ToString(),
                TimeStamp  = DateTime.UtcNow,
                ReferralId = referralLeadEncrypted.Id.ToString()
            });
        }
        public async Task <ReferralLeadEncrypted> UpdateAsync(ReferralLeadEncrypted referralLeadEncrypted)
        {
            using (var context = _msSqlContextFactory.CreateDataContext())
            {
                var entity = _mapper.Map <ReferralLeadEntity>(referralLeadEncrypted);

                context.Update(entity);

                await context.SaveChangesAsync();

                return(_mapper.Map <ReferralLeadEncrypted>(entity));
            }
        }
        public async Task <ReferralLeadEncrypted> CreateAsync(ReferralLeadEncrypted referralLeadEncrypted)
        {
            using (var context = _msSqlContextFactory.CreateDataContext())
            {
                var entity = _mapper.Map <ReferralLeadEntity>(referralLeadEncrypted);

                entity.CreationDateTime = DateTime.UtcNow;

                await context.AddAsync(entity);

                await context.SaveChangesAsync();

                return(_mapper.Map <ReferralLeadEncrypted>(entity));
            }
        }
        public T ToCommissionEvent <T>(PropertyPurchase propertyPurchase, ReferralLeadEncrypted lead)
            where T : PropertyPurchaseReferralEvent
        {
            var propertyPurchaseEvent = new PropertyPurchaseReferralEvent
            {
                ReferrerId                 = lead.AgentId.ToString(),
                TimeStamp                  = propertyPurchase.Timestamp,
                CurrencyCode               = propertyPurchase.CurrencyCode,
                VatAmount                  = propertyPurchase.VatAmount,
                DiscountAmount             = propertyPurchase.DiscountAmount,
                NetPropertyPrice           = propertyPurchase.NetPropertyPrice,
                SellingPropertyPrice       = propertyPurchase.SellingPropertyPrice,
                CalculatedCommissionAmount = propertyPurchase.CalculatedCommissionAmount,
                ReferralId                 = lead.Id.ToString()
            };

            return(_mapper.Map <T>(propertyPurchaseEvent));
        }
Пример #7
0
        public async Task <ReferralLead> CreateReferralLeadAsync(ReferralLead referralLead)
        {
            ReferralLead createdReferralLead = null;

            try
            {
                var agentCustomer = await _customerProfileClient.CustomerProfiles
                                    .GetByCustomerIdAsync(referralLead.AgentId.ToString());

                if (agentCustomer.ErrorCode == CustomerProfileErrorCodes.CustomerProfileDoesNotExist)
                {
                    throw new CustomerDoesNotExistException($"Customer '{referralLead.AgentId}' does not exist.");
                }

                var agent = await _agentManagementClient.Agents.GetByCustomerIdAsync(referralLead.AgentId);

                if (agent == null || agent.Status != AgentStatus.ApprovedAgent)
                {
                    throw new CustomerNotApprovedAgentException("Customer isn't an approved agent.");
                }

                var countryPhoneCode = await _dictionariesClient.Salesforce
                                       .GetCountryPhoneCodeByIdAsync(referralLead.PhoneCountryCodeId);

                if (countryPhoneCode == null)
                {
                    throw new CountryCodeDoesNotExistException(
                              $"Country information for Country code id '{referralLead.PhoneCountryCodeId}' does not exist.");
                }

                var emailHash = referralLead.Email.ToSha256Hash();

                var phoneNumberE164 = PhoneUtils.GetE164FormattedNumber(referralLead.PhoneNumber, countryPhoneCode.IsoCode);

                if (string.IsNullOrEmpty(phoneNumberE164))
                {
                    _log.Error(message: "Referral lead has invalid phone number.",
                               context: $"agentId: {referralLead.AgentId}");

                    throw new InvalidPhoneNumberException();
                }

                var phoneNumberHash = phoneNumberE164.ToSha256Hash();

                var referralLeadEncrypted = new ReferralLeadEncrypted
                {
                    PhoneCountryCodeId = referralLead.PhoneCountryCodeId,
                    PhoneNumberHash    = phoneNumberHash,
                    EmailHash          = emailHash,
                    AgentId            = referralLead.AgentId,
                    AgentSalesforceId  = agent.SalesforceId,
                    ConfirmationToken  = GenerateConfirmationToken(),
                    // State is automatically approved
                    State = ReferralLeadState.Pending
                };

                referralLeadEncrypted = await _referralLeadRepository.CreateAsync(referralLeadEncrypted);

                createdReferralLead = _mapper.Map <ReferralLead>(referralLeadEncrypted);

                createdReferralLead.FirstName   = referralLead.FirstName;
                createdReferralLead.LastName    = referralLead.LastName;
                createdReferralLead.Email       = referralLead.Email;
                createdReferralLead.PhoneNumber = phoneNumberE164;
                createdReferralLead.Note        = referralLead.Note;

                var response = await _customerProfileClient.ReferralLeadProfiles.AddAsync(new ReferralLeadProfileRequest
                {
                    ReferralLeadId = referralLeadEncrypted.Id,
                    FirstName      = createdReferralLead.FirstName,
                    LastName       = createdReferralLead.LastName,
                    PhoneNumber    = createdReferralLead.PhoneNumber,
                    Email          = createdReferralLead.Email,
                    Note           = createdReferralLead.Note
                });

                if (response.ErrorCode != ReferralLeadProfileErrorCodes.None)
                {
                    _log.Error(message: "An error occurred while creating referral lead profile",
                               context: $"referralLeadId: {createdReferralLead.Id}");
                }

                await _notificationPublisherService.LeadConfirmRequestAsync(
                    referralLead.AgentId.ToString(),
                    phoneNumberE164,
                    referralLeadEncrypted.ConfirmationToken);
            }
            catch (Exception e)
            {
                _log.Error("Demo service failed to process the request.", e);
            }

            return(createdReferralLead);
        }
Пример #8
0
        public async Task <ReferralLead> CreateReferralLeadAsync(ReferralLead referralLead)
        {
            var agentCustomer = await _customerProfileClient.CustomerProfiles
                                .GetByCustomerIdAsync(referralLead.AgentId.ToString(), false, false);

            if (agentCustomer.ErrorCode == CustomerProfileErrorCodes.CustomerProfileDoesNotExist)
            {
                throw new CustomerDoesNotExistException($"Customer '{referralLead.AgentId}' does not exist.");
            }

            var agent = await _agentManagementClient.Agents.GetByCustomerIdAsync(referralLead.AgentId);

            if (agent == null || agent.Status != AgentStatus.ApprovedAgent)
            {
                throw new CustomerNotApprovedAgentException("Customer isn't an approved agent.");
            }

            var countryPhoneCode = await _dictionariesClient.Salesforce
                                   .GetCountryPhoneCodeByIdAsync(referralLead.PhoneCountryCodeId);

            if (countryPhoneCode == null)
            {
                throw new CountryCodeDoesNotExistException(
                          $"Country information for Country code id '{referralLead.PhoneCountryCodeId}' does not exist.");
            }

            if (agentCustomer.Profile.Email == referralLead.Email ||
                agentCustomer.Profile.CountryPhoneCodeId == referralLead.PhoneCountryCodeId &&
                (agentCustomer.Profile.PhoneNumber == referralLead.PhoneNumber ||
                 agentCustomer.Profile.ShortPhoneNumber == referralLead.PhoneNumber))
            {
                throw new ReferYourselfException("You can not refer yourself as lead.");
            }

            var emailHash = referralLead.Email.ToSha256Hash();

            var phoneNumberE164 = PhoneUtils.GetE164FormattedNumber(referralLead.PhoneNumber, countryPhoneCode.IsoCode);

            if (string.IsNullOrEmpty(phoneNumberE164))
            {
                _log.Error(message: "Referral lead has invalid phone number.",
                           context: $"agentId: {referralLead.AgentId}");

                throw new InvalidPhoneNumberException();
            }

            var phoneNumberHash = phoneNumberE164.ToSha256Hash();

            if (await ReferralLeadAlreadyExistsAsync(referralLead.AgentId, emailHash, referralLead.PhoneCountryCodeId, phoneNumberHash))
            {
                throw new ReferralAlreadyExistException("Lead with the same Phone or Email is already referred from you.");
            }

            if (await ConfirmedLeadExistsAsync(emailHash, referralLead.PhoneCountryCodeId, phoneNumberHash))
            {
                await _notificationPublisherService.LeadAlreadyConfirmedAsync(
                    referralLead.AgentId.ToString(), referralLead.FirstName, referralLead.LastName, phoneNumberE164);

                throw new ReferralLeadAlreadyConfirmedException("Lead with the same Phone or Email already confirmed.");
            }

            if (agent == null || agent.Status != AgentStatus.ApprovedAgent)
            {
                throw new CustomerNotApprovedAgentException("Customer isn't an approved agent.");
            }

            var referralStake = await _stakeService.GetReferralStake(referralLead.CampaignId, EstatePurchaseConditionName);

            var referralLeadEncrypted = new ReferralLeadEncrypted
            {
                Id = Guid.NewGuid(),
                PhoneCountryCodeId = referralLead.PhoneCountryCodeId,
                PhoneNumberHash    = phoneNumberHash,
                EmailHash          = emailHash,
                AgentId            = referralLead.AgentId,
                AgentSalesforceId  = agent.SalesforceId,
                ConfirmationToken  = GenerateConfirmationToken(),
                State        = ReferralLeadState.Pending,
                CampaignId   = referralLead.CampaignId,
                StakeEnabled = referralStake != null
            };

            if (referralStake != null)
            {
                await _stakeService.SetStake(
                    referralStake,
                    referralLead.AgentId.ToString("D"),
                    referralLeadEncrypted.Id.ToString("D"));
            }

            referralLeadEncrypted = await _referralLeadRepository.CreateAsync(referralLeadEncrypted);

            var createdReferralLead = _mapper.Map <ReferralLead>(referralLeadEncrypted);

            createdReferralLead.FirstName   = referralLead.FirstName;
            createdReferralLead.LastName    = referralLead.LastName;
            createdReferralLead.Email       = referralLead.Email;
            createdReferralLead.PhoneNumber = phoneNumberE164;
            createdReferralLead.Note        = referralLead.Note;

            var response = await _customerProfileClient.ReferralLeadProfiles.AddAsync(new ReferralLeadProfileRequest
            {
                ReferralLeadId = referralLeadEncrypted.Id,
                FirstName      = createdReferralLead.FirstName,
                LastName       = createdReferralLead.LastName,
                PhoneNumber    = createdReferralLead.PhoneNumber,
                Email          = createdReferralLead.Email,
                Note           = createdReferralLead.Note
            });

            if (response.ErrorCode != ReferralLeadProfileErrorCodes.None)
            {
                _log.Error(message: "An error occurred while creating referral lead profile",
                           context: $"referralLeadId: {createdReferralLead.Id}");
            }

            await _notificationPublisherService.LeadConfirmRequestAsync(referralLead.AgentId.ToString(),
                                                                        phoneNumberE164, referralLeadEncrypted.ConfirmationToken);

            await PublishLeadChangeStateEvent(referralLeadEncrypted.Id.ToString(),
                                              _mapper.Map <Contract.Enums.ReferralLeadState>(referralLead.State));

            return(createdReferralLead);
        }