Exemple #1
0
        private async Task PublishOTP(ReferralLead createdReferralLead)
        {
            var offerToPurchase = new OfferToPurchase
            {
                CurrencyCode         = "AED",
                DiscountAmount       = 1,
                NetPropertyPrice     = 1,
                ReferId              = createdReferralLead.Id,
                SellingPropertyPrice = 1,
                Timestamp            = DateTime.UtcNow,
                VatAmount            = 1
            };
            await _offerToPurchasePurchaseRepository.InsertAsync(offerToPurchase);

            var lead = await _referralLeadRepository.GetAsync(offerToPurchase.ReferId);

            await _offerToPurchasePublisher.PublishAsync(new OfferToPurchaseByLeadEvent
            {
                AgentId              = lead.AgentId.ToString(),
                TimeStamp            = offerToPurchase.Timestamp,
                CurrencyCode         = offerToPurchase.CurrencyCode,
                VatAmount            = offerToPurchase.VatAmount,
                DiscountAmount       = offerToPurchase.DiscountAmount,
                NetPropertyPrice     = offerToPurchase.NetPropertyPrice,
                SellingPropertyPrice = offerToPurchase.SellingPropertyPrice,
                ReferralId           = lead.Id.ToString()
            });
        }
Exemple #2
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()
            });
        }
Exemple #3
0
        private async Task PublishCommissionEvents(ReferralLead createdReferralLead)
        {
            var propertyPurchase = new PropertyPurchase
            {
                CurrencyCode         = "AED",
                DiscountAmount       = 1,
                NetPropertyPrice     = 1,
                ReferralLeadId       = createdReferralLead.Id,
                SellingPropertyPrice = 1,
                Timestamp            = DateTime.UtcNow,
                VatAmount            = 1
            };

            await _propertyPurchaseRepository.InsertAsync(propertyPurchase);

            var lead = await _referralLeadRepository.GetAsync(propertyPurchase.ReferralLeadId);
        }
Exemple #4
0
        private async Task LoadSensitiveDataAsync(ReferralLead referralLead)
        {
            var response = await _customerProfileClient.ReferralLeadProfiles
                           .GetByIdAsync(referralLead.Id);

            if (response.ErrorCode != ReferralLeadProfileErrorCodes.None)
            {
                _log.Error(message: "An error occurred while getting referral lead profile",
                           context: $"referralLeadId: {referralLead.Id}");
            }
            else
            {
                referralLead.FirstName   = response.Data.FirstName;
                referralLead.LastName    = response.Data.LastName;
                referralLead.Email       = response.Data.Email;
                referralLead.PhoneNumber = response.Data.PhoneNumber;
                referralLead.Note        = response.Data.Note;
            }
        }
        public ReferralLeadServiceTestsFixture()
        {
            StakeServiceMock = new Mock <IStakeService>(MockBehavior.Strict);
            PropertyIntegrationClientMock = new Mock <IMAVNPropertyIntegrationClient>(MockBehavior.Strict);
            EventPublisher                   = new Mock <IRabbitPublisher <PropertyLeadApprovedReferralEvent> >(MockBehavior.Strict);
            ReferralLeadRepositoryMock       = new Mock <IReferralLeadRepository>(MockBehavior.Strict);
            NotificationPublisherServiceMock = new Mock <INotificationPublisherService>(MockBehavior.Strict);
            SettingsServiceMock              = new Mock <ISettingsService>(MockBehavior.Strict);
            HashingManagerMock               = new Mock <IHashingManager>(MockBehavior.Strict);
            CustomerProfileMock              = new Mock <ICustomerProfileClient>(MockBehavior.Strict);
            DictionariesClientMock           = new Mock <IDictionariesClient>(MockBehavior.Strict);
            AgentManagementServiceMock       = new Mock <IAgentManagementClient>(MockBehavior.Strict);
            PropertyPurchaseRepositoryMock   = new Mock <IPropertyPurchaseRepository>(MockBehavior.Strict);
            LeadStateChangePublisherMock     = new Mock <IRabbitPublisher <LeadStateChangedEvent> >(MockBehavior.Strict);

            Service = new ReferralLeadService(
                StakeServiceMock.Object,
                PropertyIntegrationClientMock.Object,
                EventPublisher.Object,
                ReferralLeadRepositoryMock.Object,
                NotificationPublisherServiceMock.Object,
                SettingsServiceMock.Object,
                HashingManagerMock.Object,
                AgentManagementServiceMock.Object,
                CustomerProfileMock.Object,
                DictionariesClientMock.Object,
                PropertyPurchaseRepositoryMock.Object,
                LeadStateChangePublisherMock.Object,
                MapperHelper.CreateAutoMapper(),
                EmptyLogFactory.Instance);

            ReferralLead = new ReferralLead
            {
                Id                 = Guid.NewGuid(),
                AgentId            = AgentId,
                FirstName          = FirstName,
                LastName           = LastName,
                Email              = Email,
                PhoneNumber        = PhoneNumber,
                PhoneCountryCodeId = CountryCode,
                ConfirmationToken  = ConfirmationToken,
                AgentSalesforceId  = AgentSalesforceId,
                Note               = Note
            };

            ReferralLeads = new List <ReferralLeadEncrypted>
            {
                new ReferralLeadEncrypted
                {
                    Id      = Guid.NewGuid(),
                    AgentId = Guid.Parse("78ceb436-29a9-499c-92fc-ec77152e32d8"),
                    State   = ReferralLeadState.Pending
                },
                new ReferralLeadEncrypted
                {
                    Id    = Guid.NewGuid(),
                    State = ReferralLeadState.Pending
                },
                new ReferralLeadEncrypted
                {
                    Id                 = Guid.Parse("57e80137-984c-44f0-ad6f-b555d46cd934"),
                    AgentId            = Guid.Parse("78ceb436-29a9-499c-92fc-ec77152e32d8"),
                    EmailHash          = "*****@*****.**",
                    ConfirmationToken  = "3l2k3h4lk",
                    State              = ReferralLeadState.Pending,
                    PhoneNumberHash    = "2384324092345",
                    PhoneCountryCodeId = 359,
                    AgentSalesforceId  = "agentsfid"
                },
                new ReferralLeadEncrypted
                {
                    Id                 = Guid.NewGuid(),
                    AgentId            = Guid.NewGuid(),
                    State              = ReferralLeadState.Confirmed,
                    EmailHash          = "*****@*****.**",
                    PhoneNumberHash    = "0881212838",
                    PhoneCountryCodeId = 359,
                    ConfirmationToken  = "9l2l7a4lk"
                },
                new ReferralLeadEncrypted
                {
                    Id                 = Guid.NewGuid(),
                    AgentId            = Guid.NewGuid(),
                    State              = ReferralLeadState.Approved,
                    EmailHash          = "*****@*****.**",
                    PhoneNumberHash    = "0884543421",
                    PhoneCountryCodeId = 359,
                    ConfirmationToken  = "7a00a3a8p"
                },
                new ReferralLeadEncrypted
                {
                    Id                 = Guid.NewGuid(),
                    AgentId            = Guid.NewGuid(),
                    State              = ReferralLeadState.Approved,
                    EmailHash          = "*****@*****.**",
                    PhoneNumberHash    = "0884543421",
                    PhoneCountryCodeId = 359,
                    ConfirmationToken  = "1b9yhklj3"
                }
            };

            ReferralLeadsWithDetails = new List <ReferralLeadEncryptedWithDetails>
            {
                new ReferralLeadEncryptedWithDetails
                {
                    Id      = Guid.NewGuid(),
                    AgentId = Guid.Parse("78ceb436-29a9-499c-92fc-ec77152e32d8"),
                    State   = ReferralLeadState.Pending
                },
                new ReferralLeadEncryptedWithDetails
                {
                    Id    = Guid.NewGuid(),
                    State = ReferralLeadState.Pending
                },
                new ReferralLeadEncryptedWithDetails
                {
                    Id                 = Guid.Parse("57e80137-984c-44f0-ad6f-b555d46cd934"),
                    AgentId            = Guid.Parse("78ceb436-29a9-499c-92fc-ec77152e32d8"),
                    EmailHash          = "*****@*****.**",
                    ConfirmationToken  = "3l2k3h4lk",
                    State              = ReferralLeadState.Pending,
                    PhoneNumberHash    = "2384324092345",
                    PhoneCountryCodeId = 359,
                    AgentSalesforceId  = "agentsfid"
                },
                new ReferralLeadEncryptedWithDetails
                {
                    Id                 = Guid.NewGuid(),
                    AgentId            = Guid.NewGuid(),
                    State              = ReferralLeadState.Confirmed,
                    EmailHash          = "*****@*****.**",
                    PhoneNumberHash    = "0881212838",
                    PhoneCountryCodeId = 359,
                    ConfirmationToken  = "9l2l7a4lk"
                },
                new ReferralLeadEncryptedWithDetails
                {
                    Id                 = Guid.NewGuid(),
                    AgentId            = Guid.NewGuid(),
                    State              = ReferralLeadState.Approved,
                    EmailHash          = "*****@*****.**",
                    PhoneNumberHash    = "0884543421",
                    PhoneCountryCodeId = 359,
                    ConfirmationToken  = "7a00a3a8p"
                },
                new ReferralLeadEncryptedWithDetails
                {
                    Id                 = Guid.NewGuid(),
                    AgentId            = Guid.NewGuid(),
                    State              = ReferralLeadState.Approved,
                    EmailHash          = "*****@*****.**",
                    PhoneNumberHash    = "0884543421",
                    PhoneCountryCodeId = 359,
                    ConfirmationToken  = "1b9yhklj3"
                }
            };

            ReferralLeadProfileResponse = new ReferralLeadProfileResponse
            {
                ErrorCode = ReferralLeadProfileErrorCodes.None,
                Data      = new ReferralLeadProfile
                {
                    ReferralLeadId = Guid.NewGuid(),
                    Email          = Email,
                    FirstName      = FirstName,
                    LastName       = LastName,
                    PhoneNumber    = PhoneNumber,
                    Note           = Note
                }
            };

            SetupCalls();
        }
Exemple #6
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);
        }
        public ReferralLeadServiceNewTests()
        {
            _referralLeadProfile = new ReferralLeadProfile
            {
                ReferralLeadId = _referralId,
                FirstName      = "first name",
                LastName       = "last name",
                PhoneNumber    = "+70000000000",
                Email          = "*****@*****.**",
                Note           = "note"
            };

            _referralLeadCreateInfo = new ReferralLead
            {
                FirstName          = _referralLeadProfile.FirstName,
                LastName           = _referralLeadProfile.LastName,
                PhoneCountryCodeId = _countryPhoneCode.Id,
                PhoneNumber        = "000 000 00 00",
                Email   = _referralLeadProfile.Email,
                Note    = _referralLeadProfile.Note,
                AgentId = Guid.Parse(_customerId)
            };

            _agent = new AgentModel
            {
                CustomerId   = Guid.Parse(_customerId),
                Status       = AgentStatus.ApprovedAgent,
                SalesforceId = "SalesforceId"
            };

            _customerProfileClientMock.Setup(o => o.CustomerProfiles.GetByCustomerIdAsync(
                                                 It.Is <string>(customerId => customerId == _customerId), It.IsAny <bool>(), It.IsAny <bool>()))
            .ReturnsAsync((string customerId, bool includeNonVerified, bool includeDeactivated) => new CustomerProfileResponse
            {
                ErrorCode = CustomerProfileErrorCodes.None
            });

            _customerProfileClientMock
            .Setup(o => o.ReferralLeadProfiles.AddAsync(It.IsAny <ReferralLeadProfileRequest>()))
            .ReturnsAsync((ReferralLeadProfileRequest request) => new ReferralLeadProfileResponse());

            _customerProfileClientMock.Setup(o => o.ReferralLeadProfiles.GetByIdAsync(
                                                 It.Is <Guid>(referralId => referralId == _referralId)))
            .ReturnsAsync((Guid referralId) => new ReferralLeadProfileResponse
            {
                ErrorCode = ReferralLeadProfileErrorCodes.None, Data = _referralLeadProfile
            });

            _dictionariesClientMock.Setup(o => o.Salesforce.GetCountryPhoneCodeByIdAsync(
                                              It.Is <int>(id => id == _countryPhoneCode.Id)))
            .ReturnsAsync((int id) => _countryPhoneCode);

            _agentManagementClientMock.Setup(o => o.Agents.GetByCustomerIdAsync(
                                                 It.Is <Guid>(customerId => customerId == Guid.Parse(_customerId))))
            .ReturnsAsync((Guid customerId) => _agent);

            _referralLeadRepositoryMock.Setup(o => o.CreateAsync(It.IsAny <ReferralLeadEncrypted>()))
            .ReturnsAsync((ReferralLeadEncrypted referralLeadEncrypted) =>
            {
                referralLeadEncrypted.Id = _referralId;
                referralLeadEncrypted.CreationDateTime = DateTime.UtcNow;

                return(referralLeadEncrypted);
            });

            _referralLeadRepositoryMock.Setup(o => o.GetByEmailHashAsync(It.IsAny <string>()))
            .ReturnsAsync((string emailHash) =>
                          _referralLeadsEncrypted.Where(o => o.EmailHash == emailHash).ToList());

            _referralLeadRepositoryMock.Setup(o => o.GetByPhoneNumberHashAsync(It.IsAny <int>(), It.IsAny <string>()))
            .ReturnsAsync((int countryPhoneCodeId, string phoneNumberHash) =>
                          _referralLeadsEncrypted.Where(o => o.PhoneNumberHash == phoneNumberHash &&
                                                        o.PhoneCountryCodeId == countryPhoneCodeId).ToList());

            _referralLeadRepositoryMock.Setup(o => o.GetApprovedAsync())
            .ReturnsAsync(() => _referralLeadsEncrypted.Where(o => o.State == ReferralLeadState.Approved).ToList());

            _hashingManagerMock.Setup(o => o.GenerateBase(It.IsAny <string>()))
            .Returns((string value) => value);

            _settingsServiceMock.Setup(o => o.GetLeadConfirmationTokenLength())
            .Returns(() => _confirmationTokenLength);

            _service = new ReferralLeadService(
                _stakeServiceMock.Object,
                _propertyIntegrationClientMock.Object,
                _propertyLeadApprovedReferralPublisherMock.Object,
                _referralLeadRepositoryMock.Object,
                _notificationPublisherServiceMock.Object,
                _settingsServiceMock.Object,
                _hashingManagerMock.Object,
                _agentManagementClientMock.Object,
                _customerProfileClientMock.Object,
                _dictionariesClientMock.Object,
                _propertyPurchaseRepositoryMock.Object,
                _leadStateChangedPublisherMock.Object,
                MapperHelper.CreateAutoMapper(),
                EmptyLogFactory.Instance);
        }
Exemple #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);
        }