From deee6b3a59c58d0362aa998b165c1e32a74a31e7 Mon Sep 17 00:00:00 2001 From: Leeza Baidar Date: Fri, 10 Nov 2023 12:09:08 +0545 Subject: [PATCH] #password reset --- .../AccountBusiness/AccountBusiness.cs | 287 ++++++++++-------- 1 file changed, 167 insertions(+), 120 deletions(-) diff --git a/CustomerOnlineV2/CustomerOnlineV2.Business/Business/AccountBusiness/AccountBusiness.cs b/CustomerOnlineV2/CustomerOnlineV2.Business/Business/AccountBusiness/AccountBusiness.cs index 783b109..dbb6a0e 100644 --- a/CustomerOnlineV2/CustomerOnlineV2.Business/Business/AccountBusiness/AccountBusiness.cs +++ b/CustomerOnlineV2/CustomerOnlineV2.Business/Business/AccountBusiness/AccountBusiness.cs @@ -5,8 +5,8 @@ using CustomerOnlineV2.Common.Models.Notification; using CustomerOnlineV2.Repository.Repository.AccountRepository; using Microsoft.Extensions.Logging; using Newtonsoft.Json; +using NPoco.fastJSON; using System.Text; -using System.Web.Helpers; using static CustomerOnlineV2.Common.Models.Notification.PasswordGenerator; namespace CustomerOnlineV2.Business.Business.AccountBusiness @@ -47,24 +47,29 @@ namespace CustomerOnlineV2.Business.Business.AccountBusiness string random_num = result.ToString(); model.RandomPassword = random_num; - var res =await _accountRepo.PasswordReset(model, logindetails); + var res = await _accountRepo.PasswordReset(model, logindetails); if (res.ResponseCode == ResponseHelper.SUCCESS) { + bool emailSent = false; + bool smsSent = false; try { - List bodyMappings = new List(); + List bodyMappings = new List(); bodyMappings.Add(new Mapping() { SValue = "CustomerName", SText = res.Extra2 }); - bodyMappings.Add(new Mapping() { SValue = "PASS_WORD", SText = random_num }); + bodyMappings.Add(new Mapping() { SValue = "PASS_WORD", SText = random_num }); bodyMappings.Add(new Mapping() { SValue = "EMAIL_ID", SText = model.Username }); - SendNotificationRequest request = new SendNotificationRequest() - { - IsBulkNotification = false, - UserName = model.Username, - //ProcessId = processId, - ProviderId = "ResetPassword", - NotificationTypeId = NOTIFICATION_TYPE.EMAIL.ToString(), - Template = NotifyTemplate.RESET_PASSWORD_EMAIL, - Recipients = new List() + + try + { + SendNotificationRequest request = new SendNotificationRequest() + { + IsBulkNotification = false, + UserName = model.Username, + //ProcessId = processId, + ProviderId = "ResetPassword", + NotificationTypeId = NOTIFICATION_TYPE.EMAIL.ToString(), + Template = NotifyTemplate.RESET_PASSWORD_EMAIL, + Recipients = new List() { new RecipientViewModel() { @@ -75,22 +80,64 @@ namespace CustomerOnlineV2.Business.Business.AccountBusiness Address= model.Username } } - }; + }; + _logger.LogDebug("PasswordReset.EMAIL | REQUEST : " + JsonConvert.SerializeObject(request)); + var jsonRx1 = NotifierV2.SendNotification(request, NOTIFICATION_TYPE.EMAIL); + _logger.LogDebug("PasswordReset.EMAIL | RESPONSE : " + JsonConvert.SerializeObject(jsonRx1)); + emailSent = true; + } + catch (Exception emailException) + { + _logger.LogError("Error sending email", emailException); + } + try + { + string ProcessId = Guid.NewGuid().ToString().Replace("-", "") + ":sendSms"; + var mobileNo = getSenderFormattedNumber(res.Extra3); + + SendSMSApiService _sendAPI = new SendSMSApiService(); + StringBuilder s = new StringBuilder(); + s.AppendLine($"Dear {res.Extra2}"); + s.AppendLine($"We received your password reset request and your new credentials are"); + s.AppendLine($"Login ID: {model.Username}"); + s.AppendLine($"Password: {model.RandomPassword}"); + s.AppendLine("Regards, IME London"); - _logger.LogDebug("PasswordReset.EMAIL | REQUEST : " + JsonConvert.SerializeObject(request)); - var jsonRx1 = NotifierV2.SendNotification(request, NOTIFICATION_TYPE.EMAIL); - _logger.LogDebug("PasswordReset.EMAIL | RESPONSE : " + JsonConvert.SerializeObject(jsonRx1)); + SMSRequestModel _req = new SMSRequestModel + { + ProviderId = "onewaysms", + MobileNumber = mobileNo, + SMSBody = s.ToString(), + ProcessId = ProcessId.Substring(ProcessId.Length - 40, 40), + RequestedBy = res.Id, + UserName = res.Id, + method = "send", + ControlNo = "", + }; + _logger.LogDebug("SendNotification.SMS | REQUEST : " + JsonConvert.SerializeObject(_req)); + APIJsonResponse _resp = _sendAPI.SMSTPApi(_req); + + _logger.LogDebug("SendNotification.SMS | RESPONSE : " + JsonConvert.SerializeObject(_resp)); + + smsSent = true; + } + catch (Exception smsException) + { + _logger.LogError("Error sending SMS", smsException); + } } - catch (Exception emailException) + catch (Exception ex) { - _logger.LogError("Error sending email", emailException); + _logger.LogError("Something Went Wrong, Please Try Again!!", ex); + res.SetResponse('1', "Error occurred while calling RequestOTP."); + return res; } } return res; } - + public static string GenerateRandomPassword(PasswordOptions opts = null) { if (opts == null) opts = new PasswordOptions() @@ -146,42 +193,42 @@ namespace CustomerOnlineV2.Business.Business.AccountBusiness return new string(chars.ToArray()); } - //bodyMappings.Add(new Mapping() { SValue = "CustomerName", SText = loginDetails.FullName }); - //bodyMappings.Add(new Mapping() { SValue = "CustomerId", SText = loginDetails.MembershipId.ToString() }); - //bodyMappings.Add(new Mapping() { SValue = "UserId", SText = loginDetails.MembershipId }); - //bodyMappings.Add(new Mapping() { SValue = "FirstName", SText = loginDetails.FullName }); - //bodyMappings.Add(new Mapping() { SValue = "MiddleName", SText = loginDetails.FullName }); - //bodyMappings.Add(new Mapping() { SValue = "LastName", SText = loginDetails.FullName }); - //bodyMappings.Add(new Mapping() { SValue = "MobileNo", SText = loginDetails.MobileNumber }); - //bodyMappings.Add(new Mapping() { SValue = "Address", SText = loginDetails.UserName }); - //bodyMappings.Add(new Mapping() { SValue = "EMAIL_ID", SText = loginDetails.Email }); - //bodyMappings.Add(new Mapping() { SValue = "RegisteredDate", SText = "" }); - - //SendNotificationRequest request = new SendNotificationRequest() - //{ - // IsBulkNotification = false, - // UserName = loginDetails.Email, - // ProviderId = "BasicRegistration", - // NotificationTypeId = NOTIFICATION_TYPE.EMAIL.ToString(), - // Template = NotifyTemplate.BASIC_REGISTRATION_EMAIL, - // Recipients = new List() - // { - // new RecipientViewModel() - // { - // NotificationContent = new NotificationDTO() { - // Body = JsonConvert.SerializeObject(bodyMappings), - // //Title will be set by mapping json - // }, - // Address= loginDetails.Email - // } - //return jsonRx; + //bodyMappings.Add(new Mapping() { SValue = "CustomerName", SText = loginDetails.FullName }); + //bodyMappings.Add(new Mapping() { SValue = "CustomerId", SText = loginDetails.MembershipId.ToString() }); + //bodyMappings.Add(new Mapping() { SValue = "UserId", SText = loginDetails.MembershipId }); + //bodyMappings.Add(new Mapping() { SValue = "FirstName", SText = loginDetails.FullName }); + //bodyMappings.Add(new Mapping() { SValue = "MiddleName", SText = loginDetails.FullName }); + //bodyMappings.Add(new Mapping() { SValue = "LastName", SText = loginDetails.FullName }); + //bodyMappings.Add(new Mapping() { SValue = "MobileNo", SText = loginDetails.MobileNumber }); + //bodyMappings.Add(new Mapping() { SValue = "Address", SText = loginDetails.UserName }); + //bodyMappings.Add(new Mapping() { SValue = "EMAIL_ID", SText = loginDetails.Email }); + //bodyMappings.Add(new Mapping() { SValue = "RegisteredDate", SText = "" }); + + //SendNotificationRequest request = new SendNotificationRequest() + //{ + // IsBulkNotification = false, + // UserName = loginDetails.Email, + // ProviderId = "BasicRegistration", + // NotificationTypeId = NOTIFICATION_TYPE.EMAIL.ToString(), + // Template = NotifyTemplate.BASIC_REGISTRATION_EMAIL, + // Recipients = new List() + // { + // new RecipientViewModel() + // { + // NotificationContent = new NotificationDTO() { + // Body = JsonConvert.SerializeObject(bodyMappings), + // //Title will be set by mapping json + // }, + // Address= loginDetails.Email + // } + //return jsonRx; public JsonRxResponse RequestOTP(RequestOTPModel requestOTPModel) { JsonRxResponse jsonRx = new JsonRxResponse(); try { - requestOTPModel.OTP = Utilities.GenerateOTP(); + requestOTPModel.OTP = Utilities.GenerateOTP(); jsonRx = _accountRepo.RequestOTP(requestOTPModel); _logger.LogDebug("GenerateOTP | RESPONSE : " + JsonConvert.SerializeObject(jsonRx)); @@ -190,24 +237,24 @@ namespace CustomerOnlineV2.Business.Business.AccountBusiness bool emailSent = false; bool smsSent = false; - if (!string.IsNullOrEmpty(jsonRx.ErrorCode) && (jsonRx.ErrorCode.Equals("0") && jsonRx.Extra3.Equals("100"))) - { - List bodyMappings = new List(); + if (!string.IsNullOrEmpty(jsonRx.ErrorCode) && (jsonRx.ErrorCode.Equals("0") && jsonRx.Extra3.Equals("100"))) + { + List bodyMappings = new List(); - bodyMappings.Add(new Mapping() { SValue = "CustomerName", SText = requestOTPModel.CreatedBy }); - //bodyMappings.Add(new Mapping() { SValue = "OTP_CODE", SText = requestOTPModel.OTP }); - bodyMappings.Add(new Mapping() { SValue = "OTP_CODE", SText = jsonRx.Id }); - bodyMappings.Add(new Mapping() { SValue = "TYPE", SText = requestOTPModel.requestFor }); - try + bodyMappings.Add(new Mapping() { SValue = "CustomerName", SText = requestOTPModel.CreatedBy }); + //bodyMappings.Add(new Mapping() { SValue = "OTP_CODE", SText = requestOTPModel.OTP }); + bodyMappings.Add(new Mapping() { SValue = "OTP_CODE", SText = jsonRx.Id }); + bodyMappings.Add(new Mapping() { SValue = "TYPE", SText = requestOTPModel.requestFor }); + try + { + SendNotificationRequest request = new SendNotificationRequest() { - SendNotificationRequest request = new SendNotificationRequest() - { - IsBulkNotification = false, - UserName = requestOTPModel.CreatedBy, - ControlNo = requestOTPModel.receiverId, - ProviderId = "REQUESTOTP_EMAIL", - Template = NotifyTemplate.OTP_EMAIL, - Recipients = new List() + IsBulkNotification = false, + UserName = requestOTPModel.CreatedBy, + ControlNo = requestOTPModel.receiverId, + ProviderId = "REQUESTOTP_EMAIL", + Template = NotifyTemplate.OTP_EMAIL, + Recipients = new List() { new RecipientViewModel() { @@ -218,71 +265,71 @@ namespace CustomerOnlineV2.Business.Business.AccountBusiness Address= requestOTPModel.CreatedBy, DeviceType = requestOTPModel.DeviceType, } } - }; + }; - _logger.LogDebug("SendNotification.EMAIL | REQUEST : " + JsonConvert.SerializeObject(request)); + _logger.LogDebug("SendNotification.EMAIL | REQUEST : " + JsonConvert.SerializeObject(request)); - var jsonRx1 = NotifierV2.SendNotification(request, NOTIFICATION_TYPE.EMAIL); + var jsonRx1 = NotifierV2.SendNotification(request, NOTIFICATION_TYPE.EMAIL); - _logger.LogDebug("SendNotification.EMAIL | RESPONSE : " + JsonConvert.SerializeObject(jsonRx1)); - emailSent = true; - } - catch (Exception emailException) - { - _logger.LogError("Error sending email", emailException); - } + _logger.LogDebug("SendNotification.EMAIL | RESPONSE : " + JsonConvert.SerializeObject(jsonRx1)); + emailSent = true; + } + catch (Exception emailException) + { + _logger.LogError("Error sending email", emailException); + } - try - { - string ProcessId = Guid.NewGuid().ToString().Replace("-", "") + ":sendSms"; - - string mobileNum = jsonRx.Extra2; - var mobNum = getSenderFormattedNumber(mobileNum); - SendSMSApiService _sendAPI = new SendSMSApiService(); - - StringBuilder s = new StringBuilder(); - s.AppendLine($"Dear {jsonRx.Extra}"); - s.AppendLine($"Your OTP code for Customer Registration is {jsonRx.Id}."); - s.AppendLine("Regards, IME London"); - - SMSRequestModel _req = new SMSRequestModel - { - ProviderId = "onewaysms", - MobileNumber = mobileNum, - SMSBody = s.ToString(), - ProcessId = ProcessId.Substring(ProcessId.Length - 40, 40), - RequestedBy = requestOTPModel.userId, - UserName = requestOTPModel.userId, - method = "send", - ControlNo = "", // GetControlNo() - }; - - _logger.LogDebug("SendNotification.SMS | REQUEST : " + JsonConvert.SerializeObject(_req)); - APIJsonResponse _resp = _sendAPI.SMSTPApi(_req); - - _logger.LogDebug("SendNotification.SMS | RESPONSE : " + JsonConvert.SerializeObject(_resp)); - - smsSent = true; - } - catch (Exception smsException) - { - _logger.LogError("Error sending SMS", smsException); - } - if (emailSent || smsSent) + try + { + string ProcessId = Guid.NewGuid().ToString().Replace("-", "") + ":sendSms"; + + string mobileNum = jsonRx.Extra2; + var mobNum = getSenderFormattedNumber(mobileNum); + SendSMSApiService _sendAPI = new SendSMSApiService(); + + StringBuilder s = new StringBuilder(); + s.AppendLine($"Dear {jsonRx.Extra}"); + s.AppendLine($"Your OTP code for Customer Registration is {jsonRx.Id}."); + s.AppendLine("Regards, IME London"); + + SMSRequestModel _req = new SMSRequestModel { - jsonRx.ErrorCode = "0"; - jsonRx.SetResponse("0", "OTP has been Sent."); - return jsonRx; - } + ProviderId = "onewaysms", + MobileNumber = mobileNum, + SMSBody = s.ToString(), + ProcessId = ProcessId.Substring(ProcessId.Length - 40, 40), + RequestedBy = requestOTPModel.userId, + UserName = requestOTPModel.userId, + method = "send", + ControlNo = "", // GetControlNo() + }; + + _logger.LogDebug("SendNotification.SMS | REQUEST : " + JsonConvert.SerializeObject(_req)); + APIJsonResponse _resp = _sendAPI.SMSTPApi(_req); + + _logger.LogDebug("SendNotification.SMS | RESPONSE : " + JsonConvert.SerializeObject(_resp)); + + smsSent = true; + } + catch (Exception smsException) + { + _logger.LogError("Error sending SMS", smsException); } - - + if (emailSent || smsSent) + { + jsonRx.ErrorCode = "0"; + jsonRx.SetResponse("0", "OTP has been Sent."); + return jsonRx; + } + } + + } else { jsonRx.ErrorCode = "1"; - return new JsonRxResponse { ErrorCode = jsonRx.ErrorCode, Msg = jsonRx.Msg , Id= jsonRx.Id, Extra3= jsonRx.Extra3 }; + return new JsonRxResponse { ErrorCode = jsonRx.ErrorCode, Msg = jsonRx.Msg, Id = jsonRx.Id, Extra3 = jsonRx.Extra3 }; }