示例#1
0
        public async Task <IActionResult> ApproveVendor(VendorApprovedModel model)
        {
            var response = await _vendor.ApprovedAsync(model, User.Identity.Name);

            return(Json(response));
        }
示例#2
0
        public async Task <DbResponse> ApprovedAsync(VendorApprovedModel model, string userName)
        {
            try
            {
                var registrationId = _db.Registration.GetRegID_ByUserName(userName);
                if (registrationId == 0)
                {
                    return(new DbResponse(false, "Invalid User"));
                }

                model.ApprovedByRegistrationId = registrationId;

                if (_db.Vendor.IsNull(model.VendorId))
                {
                    return(new DbResponse(false, "Vendor not Found"));
                }

                var vendor = _db.Vendor.Get(model.VendorId);

                //Identity Create
                var user = new IdentityUser {
                    UserName = vendor.Email, Email = vendor.Email
                };
                var password = Password.Generate(8, 3);
                var result   = await _userManager.CreateAsync(user, password).ConfigureAwait(false);

                if (!result.Succeeded)
                {
                    return(new DbResponse(false, result.Errors.FirstOrDefault()?.Description));
                }

                await _userManager.AddToRoleAsync(user, UserType.Seller.ToString()).ConfigureAwait(false);


                //Update vendor table
                _db.Vendor.Approved(model);
                _db.SaveChanges();


                #region SMS Code

                var textSms = $"Your Account Successfully activated, Your Id: {vendor.Email} & password: {password} . Please change your password for better security";

                var massageLength = SmsValidator.MassageLength(textSms);
                var smsCount      = SmsValidator.TotalSmsCount(textSms);

                var smsProvider = new SmsProviderBuilder();

                var smsBalance = smsProvider.SmsBalance();
                if (smsBalance < smsCount)
                {
                    return(new DbResponse(false, "No SMS Balance"));
                }

                var providerSendId = smsProvider.SendSms(textSms, vendor.VerifiedPhone);

                if (!smsProvider.IsSuccess)
                {
                    return(new DbResponse(false, smsProvider.Error));
                }

                #endregion


                return(new DbResponse(true, "Success"));
            }
            catch (Exception e)
            {
                return(new DbResponse(false, e.Message));
            }
        }