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); }
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); }