You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
343 lines
16 KiB
343 lines
16 KiB
using CustomerOnlineV2.Common.Helper;
|
|
using CustomerOnlineV2.Common.Models;
|
|
using CustomerOnlineV2.Common.Models.AccountModel;
|
|
using CustomerOnlineV2.Common.Models.Notification;
|
|
using CustomerOnlineV2.Repository.Repository.AccountRepository;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using System.Text;
|
|
using static CustomerOnlineV2.Common.Models.Notification.PasswordGenerator;
|
|
|
|
namespace CustomerOnlineV2.Business.Business.AccountBusiness
|
|
{
|
|
public class AccountBusiness : IAccountBusiness
|
|
{
|
|
private readonly ILogger<AccountBusiness> _logger;
|
|
private readonly IAccountRepository _accountRepo;
|
|
|
|
public AccountBusiness(ILogger<AccountBusiness> logger, IAccountRepository accountRepo)
|
|
{
|
|
_logger = logger;
|
|
_accountRepo = accountRepo;
|
|
}
|
|
|
|
public async Task<LoginResponse> Login(LoginModel model)
|
|
{
|
|
return await _accountRepo.Login(model);
|
|
}
|
|
|
|
public async Task<CommonResponse> ChangePassword(ForceChangePasswordModel model, LoginResponse logindetails)
|
|
{
|
|
if (model.NewPassword != model.ConfirmNewPassword)
|
|
{
|
|
return new CommonResponse(ResponseHelper.FAILED, "New Password and confirm new password does not match!");
|
|
}
|
|
|
|
return await _accountRepo.ChangePassword(model, logindetails);
|
|
}
|
|
public async Task<CommonResponse> PasswordReset(PasswordResetModel model, LoginResponse logindetails)
|
|
{
|
|
//var randomPassword = PasswordGenerator.GenerateRandomPassword();
|
|
//model.RandomPassword = randomPassword;
|
|
|
|
|
|
SendNotificationRequest request = new SendNotificationRequest()
|
|
{
|
|
IsBulkNotification = false,
|
|
UserName = model.Username,
|
|
//ProcessId = processId,
|
|
ProviderId = "ResetPassword",
|
|
NotificationTypeId = NOTIFICATION_TYPE.EMAIL.ToString(),
|
|
//Template = Common.Model.Enum.NotifyTemplate.RESET_PASSWORD_EMAIL,
|
|
Recipients = new List<RecipientViewModel>()
|
|
{
|
|
new RecipientViewModel()
|
|
{
|
|
NotificationContent = new NotificationDTO() {
|
|
// Body = JsonConvert.SerializeObject(bodyMappings),
|
|
//Title will be set by mapping json
|
|
},
|
|
Address= model.Username
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
return await _accountRepo.PasswordReset(model, logindetails);
|
|
}
|
|
|
|
|
|
public static string GenerateRandomPassword(PasswordOptions opts = null)
|
|
{
|
|
if (opts == null) opts = new PasswordOptions()
|
|
{
|
|
RequiredLength = 8,
|
|
RequiredUniqueChars = 4,
|
|
RequireDigit = true,
|
|
RequireLowercase = true,
|
|
RequireNonAlphanumeric = true,
|
|
RequireUppercase = true
|
|
};
|
|
|
|
string[] randomChars = new[] {
|
|
"ABCDEFGHJKMNPQRSTUVWXYZ", // uppercase
|
|
"abcdefghjkmnpqrstuvwxyz", // lowercase
|
|
"23456789", // digits
|
|
"@#" // non-alphanumeric
|
|
};
|
|
string[] randomChars1 = new[] {
|
|
// uppercase
|
|
"abcdefghjkmnpqrstuvwxyz", // lowercase
|
|
"23456789", // digits
|
|
// non-alphanumeric
|
|
};
|
|
Random rand = new Random();
|
|
List<char> chars = new List<char>();
|
|
|
|
if (opts.RequireUppercase)
|
|
chars.Insert(rand.Next(0, chars.Count),
|
|
randomChars[0][rand.Next(0, randomChars[0].Length)]);
|
|
|
|
if (opts.RequireLowercase)
|
|
chars.Insert(rand.Next(0, chars.Count),
|
|
randomChars[1][rand.Next(0, randomChars[1].Length)]);
|
|
|
|
if (opts.RequireDigit)
|
|
chars.Insert(rand.Next(0, chars.Count),
|
|
randomChars[2][rand.Next(0, randomChars[2].Length)]);
|
|
|
|
if (opts.RequireNonAlphanumeric)
|
|
chars.Insert(rand.Next(0, chars.Count),
|
|
randomChars[3][rand.Next(0, randomChars[3].Length)]);
|
|
|
|
for (int i = chars.Count; i < opts.RequiredLength
|
|
|| chars.Distinct().Count() < opts.RequiredUniqueChars; i++)
|
|
{
|
|
string rcs = randomChars1[rand.Next(0, randomChars1.Length)];
|
|
|
|
chars.Insert(rand.Next(0, chars.Count),
|
|
rcs[rand.Next(0, rcs.Length)]);
|
|
}
|
|
|
|
return new string(chars.ToArray());
|
|
}
|
|
|
|
public async Task<JsonRxResponse> VerifyOtp(RequestOTPModel requestOTPModel, LoginResponse loginDetails)
|
|
{
|
|
JsonRxResponse jsonRx = new JsonRxResponse();
|
|
try
|
|
{
|
|
jsonRx = await _accountRepo.VerifyOtp(requestOTPModel, loginDetails);
|
|
_logger.LogDebug("SubmitOTP | DB RESPONSE : " + JsonConvert.SerializeObject(jsonRx));
|
|
|
|
|
|
//if (jsonRx.ErrorCode.Equals("0"))
|
|
//{
|
|
|
|
// List<Mapping> bodyMappings = new List<Mapping>();
|
|
|
|
// 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<RecipientViewModel>()
|
|
// {
|
|
// new RecipientViewModel()
|
|
// {
|
|
// NotificationContent = new NotificationDTO() {
|
|
// Body = JsonConvert.SerializeObject(bodyMappings),
|
|
// //Title will be set by mapping json
|
|
// },
|
|
// Address= loginDetails.Email
|
|
// }
|
|
// }
|
|
// };
|
|
// jsonRx = NotifierV2.SendNotification(request, NOTIFICATION_TYPE.EMAIL);
|
|
//}
|
|
|
|
//return jsonRx;
|
|
return await Task.FromResult(jsonRx);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError("Something Went Wrong, Please Try Again!!", ex);
|
|
jsonRx.SetResponse("1", "Error occurred while calling RequestOTP.");
|
|
return await Task.FromResult(jsonRx);
|
|
}
|
|
}
|
|
|
|
public JsonRxResponse RequestOTP(RequestOTPModel requestOTPModel)
|
|
{
|
|
JsonRxResponse jsonRx = new JsonRxResponse();
|
|
try
|
|
{
|
|
requestOTPModel.OTP = Utilities.GenerateOTP();
|
|
jsonRx = _accountRepo.RequestOTP(requestOTPModel);
|
|
_logger.LogDebug("GenerateOTP | RESPONSE : " + JsonConvert.SerializeObject(jsonRx));
|
|
|
|
if (jsonRx.ErrorCode == "0")
|
|
{
|
|
bool emailSent = false;
|
|
bool smsSent = false;
|
|
if (requestOTPModel.userId.IsValidEmail())
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(jsonRx.ErrorCode) && jsonRx.ErrorCode.Equals("0"))
|
|
{
|
|
List<Mapping> bodyMappings = new List<Mapping>();
|
|
|
|
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()
|
|
{
|
|
IsBulkNotification = false,
|
|
UserName = requestOTPModel.CreatedBy,
|
|
ControlNo = requestOTPModel.receiverId,
|
|
ProviderId = "REQUESTOTP_EMAIL",
|
|
Template = NotifyTemplate.OTP_EMAIL,
|
|
Recipients = new List<RecipientViewModel>()
|
|
{
|
|
new RecipientViewModel()
|
|
{
|
|
NotificationContent = new NotificationDTO() {
|
|
Body = JsonConvert.SerializeObject(bodyMappings),
|
|
//Title will be set by mapping json
|
|
},
|
|
Address= requestOTPModel.CreatedBy,
|
|
DeviceType = requestOTPModel.DeviceType,
|
|
} }
|
|
};
|
|
|
|
|
|
_logger.LogDebug("SendNotification.EMAIL | REQUEST : " + JsonConvert.SerializeObject(request));
|
|
|
|
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);
|
|
}
|
|
|
|
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 {requestOTPModel.OTP}.");
|
|
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)
|
|
{
|
|
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 };
|
|
|
|
}
|
|
|
|
jsonRx.ErrorCode = jsonRx.ErrorCode == "103" ? "0" : jsonRx.ErrorCode;//103 = previous OTP was not expired so same was used
|
|
return jsonRx;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError("Something Went Wrong, Please Try Again!!", ex);
|
|
jsonRx.SetResponse("1", "Error occurred while calling RequestOTP.");
|
|
return jsonRx;
|
|
}
|
|
}
|
|
|
|
private string getSenderFormattedNumber(string number)
|
|
{
|
|
string finalNo = number;
|
|
if (!number.Contains("+44"))
|
|
{
|
|
string mobileFirst = number.Substring(0, 1);
|
|
if (mobileFirst == "0")
|
|
{
|
|
if (number.Length == 11)
|
|
{
|
|
finalNo = "+44" + number.Substring(1, number.Length - 1);
|
|
return finalNo;
|
|
}
|
|
else if (number.Length < 11)
|
|
{
|
|
finalNo = $"+44{number}";
|
|
}
|
|
}
|
|
else if (number.Substring(0, 1) != "0" && number.Length == 10)
|
|
{
|
|
finalNo = $"+44{number}";
|
|
}
|
|
}
|
|
else if (number.Contains("+44"))
|
|
{
|
|
string MobN = number.Substring(4, 1);
|
|
if (MobN == "0" && number.Length > 14)
|
|
{
|
|
finalNo = number.Remove(4, 1);
|
|
}
|
|
}
|
|
if (!finalNo.Substring(0, 1).Contains("+"))
|
|
finalNo = $"+{finalNo}";
|
|
return finalNo;
|
|
}
|
|
}
|
|
}
|