public async Task <ReferralCreateResponse> PostAsync([FromBody] ReferralCreateRequest referralCreate) { if (!TryParseGuid(referralCreate.CustomerId, nameof(PostAsync), out var customerIdGuid)) { return(new ReferralCreateResponse { ErrorCode = ReferralErrorCodes.GuidCanNotBeParsed, ErrorMessage = InvalidIdentifierMessage }); } var result = await _referralService.GetOrCreateReferralForCustomerIdAsync(customerIdGuid); return(new ReferralCreateResponse() { ReferralCode = result }); }
public async Task <ReferralFriend> CreateAsync(Guid referrerId, Guid campaignId, string email, string fullName) { var profile = await _customerProfileClient.CustomerProfiles.GetByCustomerIdAsync(referrerId.ToString("D")); if (profile.ErrorCode == CustomerProfileErrorCodes.CustomerProfileDoesNotExist) { throw new CustomerDoesNotExistException($"Connector with id '{referrerId}' does not exist."); } if (string.Compare(profile.Profile.Email, email, StringComparison.InvariantCultureIgnoreCase) == 0) { throw new ReferYourselfException($"Cannot refer to yourself."); } var campaign = await _campaignClient.Campaigns.GetByIdAsync(campaignId.ToString("D")); if (campaign.ErrorCode == CampaignServiceErrorCodes.EntityNotFound) { throw new CampaignNotFoundException($"Campaign with id '{campaignId}' not found."); } var existingCustomer = await _customerProfileClient.CustomerProfiles.GetByEmailAsync(new GetByEmailRequestModel { Email = email, IncludeNotVerified = true }); if (existingCustomer.ErrorCode == CustomerProfileErrorCodes.None) { throw new ReferralAlreadyConfirmedException($"Customer with such email already exists."); } var existingFriendProfile = await _customerProfileClient.ReferralFriendProfiles.GetByEmailAndReferrerAsync( new ReferralFriendByEmailAndReferrerProfileRequest { Email = email, ReferrerId = referrerId }); if (existingFriendProfile.ErrorCode == ReferralFriendProfileErrorCodes.None) { throw new ReferralAlreadyExistException($"Referral with such email for this referrer '{referrerId}' already exists."); } var referralCode = await _referralService.GetOrCreateReferralForCustomerIdAsync(referrerId); var referralFriend = new ReferralFriend { Email = email, FullName = fullName, ReferrerId = referrerId, CampaignId = campaignId, CreationDateTime = DateTime.UtcNow, State = ReferralFriendState.Pending }; referralFriend = await _friendReferralHistoryRepository.CreateAsync(referralFriend); var response = await _customerProfileClient.ReferralFriendProfiles.AddAsync(new ReferralFriendProfileRequest { Email = email, FullName = fullName, ReferralFriendId = referralFriend.Id, ReferrerId = referrerId }); if (response.ErrorCode != ReferralFriendProfileErrorCodes.None) { _log.Error(message: "An error occurred while creating referral friend profile", context: $"referralFriendId: {referralFriend.Id}"); } await _notificationPublisherService.FriendReferralConfirmRequestAsync( referrerId.ToString("D"), referralCode, profile.Profile.FirstName, profile.Profile.LastName, email); _log.Info("Referral friend created", context: $"referralFriendId: {referralFriend.Id}"); return(referralFriend); }