private async Task PublishHotelUsedEvent(ReferralHotelEncrypted referralHotelEncrypted)
 {
     await _hotelReferralUsedPublisher.PublishAsync(new HotelReferralUsedEvent
     {
         CustomerId   = referralHotelEncrypted.ReferrerId,
         Amount       = 1,
         CurrencyCode = _baseCurrencyCode,
         PartnerId    = referralHotelEncrypted.PartnerId,
         ReferralId   = referralHotelEncrypted.Id,
     });
 }
Example #2
0
        public async Task <ReferralHotelEncrypted> UpdateAsync(ReferralHotelEncrypted referralHotelEncrypted)
        {
            using (var context = _msSqlContextFactory.CreateDataContext())
            {
                var entity = _mapper.Map <ReferralHotelEntity>(referralHotelEncrypted);

                context.Update(entity);

                await context.SaveChangesAsync();

                return(_mapper.Map <ReferralHotelEncrypted>(entity));
            }
        }
        private async Task <ReferralHotel> DecryptAsync(ReferralHotelEncrypted referralHotelEncrypted)
        {
            var referralHotel = _mapper.Map <ReferralHotel>(referralHotelEncrypted);

            var response = await _customerProfileClient.ReferralHotelProfiles
                           .GetByIdAsync(Guid.Parse(referralHotelEncrypted.Id));

            if (response.ErrorCode != ReferralHotelProfileErrorCodes.None)
            {
                _log.Error(message: "An error occurred while getting referral hotel profile",
                           context: $"referralHotelId: {referralHotelEncrypted.Id}");
            }
            else
            {
                referralHotel.Email = response.Data.Email;
            }

            return(referralHotel);
        }
Example #4
0
        public async Task <ReferralHotel> CreateAsync(
            string email,
            string referrerId,
            Guid?campaignId,
            int phoneCountryCodeId,
            string phoneNumber,
            string fullName)
        {
            if (!await CustomerExistsAsync(referrerId))
            {
                throw new CustomerDoesNotExistException();
            }

            if (await CustomerReferencesHimselfAsync(referrerId, email))
            {
                throw new ReferYourselfException();
            }

            if (await LimitExceededAsync(referrerId))
            {
                throw new ReferralHotelLimitExceededException();
            }

            var emailHash = email.ToSha256Hash();

            if (await ConfirmedReferralExistsAsync(emailHash))
            {
                throw new ReferralAlreadyConfirmedException();
            }

            if (await ExpiredReferralExistsAsync(emailHash))
            {
                throw new ReferralExpiredException(emailHash);
            }

            if (await ReferralByThisReferrerExistsAsync(referrerId, emailHash))
            {
                throw new ReferralAlreadyExistException();
            }

            var referralStake = await _stakeService.GetReferralStake(campaignId, ConditionType);

            Guid?partnerId = null;

            if (campaignId.HasValue)
            {
                var campaign = await _campaignClient.Campaigns.GetByIdAsync(campaignId.Value.ToString("D"));

                if (campaign.ErrorCode == CampaignServiceErrorCodes.EntityNotFound)
                {
                    throw new CampaignNotFoundException($"Campaign with id '{campaignId.Value}' was not found.");
                }

                var condition = campaign.Conditions.FirstOrDefault(c => c.Type == ConditionType);

                if (condition != null)
                {
                    if (condition.PartnerIds.Length > 1)
                    {
                        // Currently we only support one partner
                        _log.Warning($"Campaign condition with multiple partners found.", context: campaign);
                    }

                    if (condition.PartnerIds.Any())
                    {
                        partnerId = condition.PartnerIds.First();
                    }
                }
            }

            var creationDateTime = DateTime.UtcNow;

            var referralHotelEncrypted = new ReferralHotelEncrypted
            {
                Id                 = Guid.NewGuid().ToString("D"),
                EmailHash          = emailHash,
                FullNameHash       = fullName.ToSha256Hash(),
                PhoneCountryCodeId = phoneCountryCodeId,
                PhoneNumberHash    = phoneNumber.ToSha256Hash(),
                ReferrerId         = referrerId,
                ConfirmationToken  = GenerateConfirmationToken(),
                CreationDateTime   = creationDateTime,
                ExpirationDateTime = creationDateTime + _referralExpirationPeriod,
                CampaignId         = campaignId,
                PartnerId          = partnerId?.ToString("D"),
                StakeEnabled       = referralStake != null
            };

            if (referralStake != null)
            {
                await _stakeService.SetStake(
                    referralStake,
                    referralHotelEncrypted.ReferrerId,
                    referralHotelEncrypted.Id);
            }

            referralHotelEncrypted = await _referralHotelsRepository.CreateAsync(referralHotelEncrypted);

            var createdReferralHotel = _mapper.Map <ReferralHotel>(referralHotelEncrypted);

            createdReferralHotel.Email       = email;
            createdReferralHotel.PhoneNumber = phoneNumber;
            createdReferralHotel.FullName    = fullName;

            var response = await _customerProfileClient.ReferralHotelProfiles.AddAsync(new ReferralHotelProfileRequest
            {
                ReferralHotelId = Guid.Parse(createdReferralHotel.Id),
                Email           = createdReferralHotel.Email,
                PhoneNumber     = createdReferralHotel.PhoneNumber,
                Name            = createdReferralHotel.FullName
            });

            if (response.ErrorCode != ReferralHotelProfileErrorCodes.None)
            {
                _log.Error(message: "An error occurred while creating referral hotel profile",
                           context: $"referralHotelId: {createdReferralHotel.Id}");
            }

            await _notificationPublisherService.HotelReferralConfirmRequestAsync(createdReferralHotel.ReferrerId,
                                                                                 createdReferralHotel.Email, _referralExpirationPeriod, createdReferralHotel.ConfirmationToken);

            _log.Info("Referral hotel created", context: $"referralHotelId: {createdReferralHotel.Id}");

            return(createdReferralHotel);
        }
Example #5
0
 private static bool ReferralExpired(ReferralHotelEncrypted referral)
 {
     return(referral.ExpirationDateTime < DateTime.UtcNow);
 }
        public async Task <ReferralHotel> CreateHotelReferralAsync(
            Guid?campaignId,
            string email,
            string referrerId,
            string fullName,
            int phoneCountryCodeId,
            string phoneNumber)
        {
            ReferralHotel createdReferralHotel = null;

            try
            {
                if (!await CustomerExistsAsync(referrerId))
                {
                    throw new CustomerDoesNotExistException();
                }

                var emailHash = email.ToSha256Hash();

                var creationDateTime = DateTime.UtcNow;

                var referralHotelEncrypted = new ReferralHotelEncrypted
                {
                    EmailHash          = emailHash,
                    ReferrerId         = referrerId,
                    FullNameHash       = fullName.ToSha256Hash(),
                    PhoneCountryCodeId = phoneCountryCodeId,
                    PhoneNumberHash    = phoneNumber.ToSha256Hash(),
                    ConfirmationToken  = GenerateConfirmationToken(),
                    CreationDateTime   = creationDateTime,
                    ExpirationDateTime = creationDateTime + _referralExpirationPeriod,
                    State      = ReferralHotelState.Pending,
                    CampaignId = campaignId,
                };
                if (campaignId != null)
                {
                    var campaign = await _campaignClient.Campaigns.GetByIdAsync(campaignId.Value.ToString());

                    var condition = campaign?.Conditions.FirstOrDefault(c => c.Type == ConditionType);
                    if (condition?.PartnerIds != null && condition.PartnerIds.Length > 0)
                    {
                        referralHotelEncrypted.PartnerId = condition.PartnerIds.First().ToString();
                    }
                }

                referralHotelEncrypted = await _referralHotelsRepository.CreateAsync(referralHotelEncrypted);

                createdReferralHotel = _mapper.Map <ReferralHotel>(referralHotelEncrypted);

                createdReferralHotel.Email = email;

                var response = await _customerProfileClient.ReferralHotelProfiles.AddAsync(new ReferralHotelProfileRequest
                {
                    ReferralHotelId = Guid.Parse(createdReferralHotel.Id),
                    Email           = createdReferralHotel.Email,
                    Name            = fullName,
                    PhoneNumber     = phoneNumber,
                });

                if (response.ErrorCode != ReferralHotelProfileErrorCodes.None)
                {
                    _log.Error(message: "An error occurred while creating referral hotel profile",
                               context: $"referralHotelId: {createdReferralHotel.Id}");
                }

                await _notificationPublisherService.HotelReferralConfirmRequestAsync(createdReferralHotel.ReferrerId,
                                                                                     createdReferralHotel.Email, _referralExpirationPeriod, createdReferralHotel.ConfirmationToken);

                _log.Info("Referral hotel created", context: $"referralHotelId: {createdReferralHotel.Id}");
            }
            catch (Exception e)
            {
                _log.Error("Demo service failed to process the request.", e);
            }

            return(createdReferralHotel);
        }