[Route("add"), HttpPost] // inserting the create user request model
        public HttpResponseMessage RegisterNewUser(CreateUserRequest model)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
            // if email/phone number taken, return bad request.
            // checks for a null websiteId via slug
            bool   checkUser    = _UserProfileService.GetUserByEmailAndPhoneNumber(model);
            string thisSlug     = model.Slug;
            var    checkWebsite = WebsiteService.GetWebsiteIdBySlug(thisSlug);

            if (checkUser == true && checkWebsite == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
            // if user has not already been registered run user service to create new user
            else
            {
                ApplicationUser newUser = null;
                try
                {   // submit users email, password, and phone number for registration
                    newUser = (ApplicationUser)UserService.CreateUser(model.Email, model.Password, model.Phone);
                }
                catch (IdentityResultException ex)
                {   // catches exceptions returns bad request
                    string validatedData = UtilityService.ValidateData(ex);
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, validatedData, ex));
                }
                //send User to database
                //only run if there are no duplicate phone/email
                //NOTHING SHOULD GET INTO THE DATABASE IF THERE IS A 400 ERROR
                if (newUser != null)
                {    // passing user model to service
                    _UserProfileService.CreateUserProfile(newUser.Id, model);
                }
                ItemResponse <UserProfile> response = new ItemResponse <UserProfile>();
                // return the newly created user profile
                response.Item = (UserProfile)_UserProfileService.GetUserById(newUser.Id);

                return(Request.CreateResponse(response));
            }
        }
        //Service ran for creating a new user - pass in users id and request model
        public void CreateUserProfile(string userId, CreateUserRequest model)
        {
            //Updated the create user so that it can take in the tokenHash param if there is one provided
            DataProvider.ExecuteNonQuery(GetConnection, "UserProfiles_Insert" // stored procedure
                                         , inputParamMapper : delegate(SqlParameterCollection paramCollection)
            {                                                                 //input fields
                paramCollection.AddWithValue("@UserId", userId);
                paramCollection.AddWithValue("@FirstName", model.FirstName);
                paramCollection.AddWithValue("@LastName", model.LastName);
                paramCollection.AddWithValue("@TokenHash", model.TokenHash);     //this is provided if they were referred by a current customer
            }
                                         );
            //referral coupon
            //if TokenHash referral is null, they're making a new account.
            //if TokenHash referral is NOT null, we want to give them (new user) a Coupon value and add it to their Credit.
            // we also want to give the original user (one who referred) the same Coupon value and add it to their Credit.
            if (model.TokenHash != null)
            {
                //retrieve coupon information + token information based on the token hash.
                CouponsDomain userCoupon = TokenService.GetReferralTokenByGuid(model.TokenHash);
                if (userCoupon.Token.Used == null)
                {
                    //send token to Credit service
                    UserCreditsRequest insertRefferalCredits = new UserCreditsRequest();
                    UserCreditsRequest insertFriendCredits   = new UserCreditsRequest();

                    //send credits to new User who was referred
                    insertRefferalCredits.Amount          = userCoupon.CouponValue;
                    insertRefferalCredits.TransactionType = "Add";
                    insertRefferalCredits.UserId          = userId;
                    _CreditsService.InsertUserCredits(insertRefferalCredits);
                }

                else
                {
                    //send error message that token has already been used.
                }
            }

            //Activity Services
            ActivityLogRequest Activity = new ActivityLogRequest();

            Activity.ActivityType = ActivityTypeId.NewAccount;
            _ActivityLogService.InsertActivityToLog(userId, Activity);

            //----------//create a new Website Id ---------updated the AddUserToWebsite using the updated domain with int[]
            //getting the websiteId via the website Slug
            Website w    = null;
            string  Slug = model.Slug;

            w = WebsiteService.GetWebsiteIdBySlug(Slug);

            int[] WebsiteIds = new int[1];
            WebsiteIds[0] = w.Id;
            //populating userwebsite object
            UserWebsite userWebsite = new UserWebsite();

            userWebsite.UserId     = userId;
            userWebsite.WebsiteIds = WebsiteIds;
            WebsiteService.AddUserToWebsite(userWebsite);

            //creae a new Customer role
            //set role as customer by default - change role by admin panel
            UserProfile aspUser = new UserProfile();

            aspUser.FirstName = model.FirstName;
            aspUser.LastName  = model.LastName;
            aspUser.RoleId    = ConfigService.CustomerRole;
            _AdminService.CreateUserRole(userId, aspUser);

            //create a new Braintree account using UserID
            //braintree used for handling credit card transactions
            CustomerPaymentRequest Payment = new CustomerPaymentRequest();

            Payment.FirstName = model.FirstName;
            Payment.LastName  = model.LastName;
            Payment.UserId    = userId;
            Payment.Phone     = model.Phone;

            //Send a confirmation Text Msg
            string UserSMSToken = TokenSMSService.TokenSMSInsert(userId);
            //send a text msg
            NotifySMSRequest NotifyCustomer = new NotifySMSRequest();

            NotifyCustomer.Phone    = model.Phone;
            NotifyCustomer.TokenSMS = UserSMSToken;

            try
            {
                NotifySMSService.SendConfirmText(NotifyCustomer);
            }
            catch (ArgumentException ex)
            {
                //if phone number is already registered will not send a registration check
                //should never get this far
                throw new System.ArgumentException(ex.Message);
            }

            //bringg create account
            RegisterBringgRequest bringgRequest = new RegisterBringgRequest();

            bringgRequest.Name   = model.FirstName + " " + model.LastName;
            bringgRequest.Phone  = model.Phone;
            bringgRequest.Email  = model.Email;
            bringgRequest.UserId = userId;
            this._CreateCustomerTask.Execute(bringgRequest);

            _CreateCustomerTask.Execute(bringgRequest);

            BrainTreeService brainTree = new BrainTreeService();

            brainTree.newCustomerInsert(Payment);

            ////generate a new token
            Guid userTokenGuid = TokenService.tokenInsert(userId);

            //send a confirmation email
            _EmailService.SendProfileEmail(userTokenGuid, model.Email);
        }
Пример #3
0
        public void CreateUserProfile(string userId, CreateUserRequest model)
        {
            //Updated the create user so that it can take in the tokenHash param if there is one provided --Anna

            DataProvider.ExecuteNonQuery(GetConnection, "UserProfiles_Insert"
                                         , inputParamMapper : delegate(SqlParameterCollection paramCollection)
            {
                paramCollection.AddWithValue("@UserId", userId);
                paramCollection.AddWithValue("@FirstName", model.FirstName);
                paramCollection.AddWithValue("@LastName", model.LastName);
                paramCollection.AddWithValue("@TokenHash", model.TokenHash);     //this is provided if they were referred by a current customer
            }
                                         );

            //referral coupon
            //if TokenHash referral is null, they're making a new account.
            //if TokenHash referral is NOT null, give new user a Coupon value and add it to their Credit.
            // we also want to give the original user (one who referred) the same Coupon value and add it to their Credit.
            if (model.TokenHash != null)
            {
                //retrieve coupon information + token information based on the token hash.
                CouponsDomain userCoupon = TokenService.GetReferralTokenByGuid(model.TokenHash);
                if (userCoupon.Token.Used == null)
                {
                    //send token to Credit service
                    UserCreditsRequest insertRefferalCredits = new UserCreditsRequest();

                    //send credits to new User who was referred
                    insertRefferalCredits.Amount          = userCoupon.CouponValue;
                    insertRefferalCredits.TransactionType = "Add";
                    insertRefferalCredits.UserId          = userId;
                    _CreditsService.InsertUserCredits(insertRefferalCredits);

                    //send credits to Friend who referred new user BUT ONLY AFTER THE REFERRED FRIEND HAS COMPLETED THEIR FIRST ORDER----
                    //see BrainTreeService, Line 130
                }
            }

            //Activity Services - update the activity log
            ActivityLogRequest Activity = new ActivityLogRequest();

            Activity.ActivityType = ActivityTypeId.NewAccount;
            _ActivityLogService.InsertActivityToLog(userId, Activity);

            //Associating a User to website(s)
            Website w    = null;
            string  Slug = model.Slug;

            w = WebsiteService.GetWebsiteIdBySlug(Slug);

            int[] WebsiteIds = new int[1];
            WebsiteIds[0] = w.Id;

            UserWebsite userWebsite = new UserWebsite();

            userWebsite.UserId     = userId;
            userWebsite.WebsiteIds = WebsiteIds;
            WebsiteService.AddUserToWebsite(userWebsite);

            //creae a new Customer role
            UserProfile aspUser = new UserProfile();

            aspUser.FirstName = model.FirstName;
            aspUser.LastName  = model.LastName;
            aspUser.RoleId    = ConfigService.CustomerRole;

            _AdminService.CreateUserRole(userId, aspUser);

            //create a new Braintree account using UserID
            CustomerPaymentRequest Payment = new CustomerPaymentRequest();

            Payment.FirstName = model.FirstName;
            Payment.LastName  = model.LastName;
            Payment.UserId    = userId;
            Payment.Phone     = model.Phone;

            //Send a confirmation Text Msg
            string UserSMSToken = TokenSMSService.TokenSMSInsert(userId);
            //send a text msg
            NotifySMSRequest NotifyCustomer = new NotifySMSRequest();

            NotifyCustomer.Phone    = model.Phone;
            NotifyCustomer.TokenSMS = UserSMSToken;

            try
            {
                NotifySMSService.SendConfirmText(NotifyCustomer);
            }
            catch (ArgumentException /*ex*/)
            {
                DeleteUserProfileByUserId(userId);
                DeleteUserWebsiteByUserId(userId);
                DeleteAspNetUserByUserId(userId);

                throw new System.ArgumentException(ex.Message);
            }

            //send a confirmation email
            _EmailService.SendProfileEmail(userTokenGuid, model.Email);

            //bringg create account
            RegisterBringgRequest bringgRequest = new RegisterBringgRequest();

            bringgRequest.Name   = model.FirstName + " " + model.LastName;
            bringgRequest.Phone  = model.Phone;
            bringgRequest.Email  = model.Email;
            bringgRequest.UserId = userId;
            this._CreateCustomerTask.Execute(bringgRequest);



            _CreateCustomerTask.Execute(bringgRequest);

            BrainTreeService brainTree = new BrainTreeService();

            brainTree.newCustomerInsert(Payment);

            ////generate a new token
            Guid userTokenGuid = TokenService.tokenInsert(userId);

            //send a confirmation email

            _EmailService.SendProfileEmail(userTokenGuid, model.Email, model.Slug);
        }