public async Task <ReferralHotel> ConfirmReferralHotelAsync(string confirmationToken) { ReferralHotel confirmReferralHotelAsync = null; try { var referralHotelEncrypted = await _referralHotelsRepository.GetByConfirmationTokenAsync(confirmationToken); if (referralHotelEncrypted == null) { throw new ReferralDoesNotExistException(); } if (referralHotelEncrypted.State == ReferralHotelState.Used) { confirmReferralHotelAsync = await DecryptAsync(referralHotelEncrypted); return(confirmReferralHotelAsync); } referralHotelEncrypted.State = ReferralHotelState.Used; referralHotelEncrypted = await _referralHotelsRepository.UpdateAsync(referralHotelEncrypted); await PublishHotelUsedEvent(referralHotelEncrypted); confirmReferralHotelAsync = await DecryptAsync(referralHotelEncrypted); _log.Info($"PublishHotelUsedEvent '{confirmationToken}'"); return(confirmReferralHotelAsync); } catch (Exception e) { _log.Error("Demo service failed to process the request.", e); } return(confirmReferralHotelAsync); }
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); }