public static ReferralTokenRequest getAllEmailsByTokenType(TokenType Invite, string email) { ReferralTokenRequest emailMatch = null; DataProvider.ExecuteCmd(GetConnection, "dbo.Token_GetTokenByEmailAndTokenType" , inputParamMapper : delegate(SqlParameterCollection paramCollection) { paramCollection.AddWithValue("@TokenType", TokenType.Invite); paramCollection.AddWithValue("@Email", email); } , map : delegate(IDataReader reader, short set) { int startingIndex = 0; Token c = new Token(); c.Id = reader.GetSafeInt32(startingIndex++); c.DateCreated = reader.GetSafeDateTime(startingIndex++); c.Used = reader.GetSafeDateTime(startingIndex++); c.TokenHash = reader.GetSafeGuid(startingIndex++); c.UserId = reader.GetSafeString(startingIndex++); c.TokenType = reader.GetSafeInt32(startingIndex++); c.CouponId = reader.GetSafeInt32(startingIndex++); c.Email = reader.GetSafeString(startingIndex++); if (c == null) { emailMatch = null; } } ); return(emailMatch); }
public void SendReferrals(string SenderUserId, ReferralCodeRequest model) //pass in the sender's userId and the request model { //Use GetCouponByUserIdAndType method CouponsDomain referralCoupon = _CouponService.GetCouponByUserIdAndType(SenderUserId, CouponType.referral); //referralCoupon now contains the information about the coupon, based on the UserId and the coupon emun referral //initially couponId is set to zero, but will get over-ridden as it goes in the if statement int couponId = 0; //If there is no referral coupon, generate a new referral coupon code for the sender. //If a new person registers, initially they will not have this code. //Therefore, this null check will check and they will have a code generated for them if (referralCoupon == null) { couponId = _CouponService.GenerateReferralCode(SenderUserId); } else //If the user already has a coupon code: { couponId = referralCoupon.Id; } //for each email, generate a token //token needs: couponId, friend's email address, and the userId of the current user //once generated, token emailed to new user via emailservice: either sendgrid or mandrill...depending on the unity.config //do a loop through model.emails... loop is required because user can send more than one referral email foreach (var email in model.InviteEmail) { //Checks the following: //Does invitation email exist in the token database? meaning have they already been sent an email? //Go through the database and see if there is an email that matches the var email ^^ //if there is NO match (not a current user || invitee), bool = false and therefore send the invitation //if there IS a match (current user || already invited), bool = true, and therefore DO NOT send out the invitation { bool checkUser = UserService.IsUser(email.Email); //this var is null-checking if a invitation email already exists ReferralTokenRequest alreadyInvited = TokenService.getAllEmailsByTokenType(TokenType.Invite, email.Email); //if that email does NOT exist in the database, //AND if that email has NOT been sent an invitation....send the referral email: if (checkUser == false && alreadyInvited == null) { //new instance of the ReferralTokenRequest model to get token: ReferralTokenRequest referralTokenModel = new ReferralTokenRequest(); referralTokenModel.Email = email.Email; referralTokenModel.CouponId = couponId; referralTokenModel.UserId = SenderUserId; //Create a unique token for each valid email: Guid CreateTokenForInvites = TokenService.InsertReferralToken(referralTokenModel); //Finally, send out invitation email _EmailService.ReferralEmail(CreateTokenForInvites, email.Email); } } } }
public HttpResponseMessage getReferralsByUserIdAndCouponType() { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } ReferralTokenRequest referralTokenModel = new ReferralTokenRequest(); ItemsResponse <Token> response = new ItemsResponse <Token>(); string UserId = UserService.GetCurrentUserId(); response.Items = TokenService.getTokenByUserIdAndTokenType(UserId); return(Request.CreateResponse(HttpStatusCode.OK, response)); }
//This is where my contribution starts: public static Guid InsertReferralToken(ReferralTokenRequest model) { Guid tokenHash = Guid.Empty; DataProvider.ExecuteNonQuery(GetConnection, "dbo.Token_Insert_v2" , inputParamMapper : delegate(SqlParameterCollection paramCollection) { SqlParameter c = new SqlParameter("@TokenHash", System.Data.SqlDbType.UniqueIdentifier); c.Direction = System.Data.ParameterDirection.Output; paramCollection.Add(c); paramCollection.AddWithValue("@UserId", model.UserId); paramCollection.AddWithValue("@Email", model.Email); paramCollection.AddWithValue("@CouponId", model.CouponId); paramCollection.AddWithValue("@TokenType", TokenType.Invite); }, returnParameters : delegate(SqlParameterCollection param) { Guid.TryParse(param["@TokenHash"].Value.ToString(), out tokenHash); } ); return(tokenHash); }