using CPRNIMS.Domain.Contracts.Account; using CPRNIMS.Infrastructure.Database; using CPRNIMS.Infrastructure.Entities.Common; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CPRNIMS.Domain.Services.Account { public class ForgotPassword : IForgotPassword { private readonly NonInventoryDbContext _purchasingDbContext; public ForgotPassword(NonInventoryDbContext purchasingDbContext) { _purchasingDbContext = purchasingDbContext; } public async Task ValidateTokenURLForgotPassword(Infrastructure.Entities.Account.ForgotPassword forgotPassword) { var isValid = await _purchasingDbContext.ForgotPasswords .FirstOrDefaultAsync(fp => fp.ServerURIToken == forgotPassword.ServerURIToken); if (isValid != null) { return true; } return false; } async Task IForgotPassword.SaveToken(Infrastructure.Entities.Account.ForgotPassword forgotPassword) { try { var isEmailExist = await _purchasingDbContext.ForgotPasswords .Where(tv => tv.IsValid == true) .FirstOrDefaultAsync(fp => fp.Email == forgotPassword.Email) ; if (isEmailExist != null) { forgotPassword.UpdatedDate = DateTime.Now; forgotPassword.UpdatedBy = forgotPassword.Email; forgotPassword.ServerURIToken = forgotPassword.ServerURIToken; await _purchasingDbContext.SaveChangesAsync(); } else { forgotPassword.IsValid = true; await _purchasingDbContext.ForgotPasswords.AddAsync(forgotPassword); await _purchasingDbContext.SaveChangesAsync(); } } catch (Exception ex) { ex.ToString(); throw; } } async Task IForgotPassword.ForgotPassword(Infrastructure.Entities.Account.ForgotPassword forgotPassword) { await _purchasingDbContext.ForgotPasswords.AddAsync(forgotPassword); await _purchasingDbContext.SaveChangesAsync(); } public Task OptimizeMessageBody(Infrastructure.Entities.Account.ForgotPassword forgotPassword) { throw new NotImplementedException(); } public async Task ValidateOTP(Otps forgotPassword) { DateTime currentDateTime = DateTime.Now; var expiredOTP = await _purchasingDbContext.Otps .FirstOrDefaultAsync(otp => otp.Email == forgotPassword.Email && otp.IsValid == true && otp.CreatedDate < currentDateTime.AddMinutes(-30)); if (expiredOTP != null) { await SetOTPInvalidAsync(expiredOTP.OTP); return expiredOTP != null; } var isValid = await _purchasingDbContext.Otps .FirstOrDefaultAsync(otp => otp.OTP == forgotPassword.OTP && otp.Email == forgotPassword.Email && otp.CreatedDate > DateTime.Now.AddMinutes(-30)); // Use '-' to subtract 30 minutes return isValid != null; } public async Task SetOTPInvalidAsync(string otp) { try { var isOTPExist = await _purchasingDbContext.Otps .Where(tv => tv.IsValid == true) .FirstOrDefaultAsync(fp => fp.OTP == otp); if (isOTPExist != null) { isOTPExist.IsValid = false; isOTPExist.UpdatedDate = DateTime.Now; isOTPExist.UpdatedBy = "SysAdmin"; await _purchasingDbContext.SaveChangesAsync(); } else { } } catch (Exception ex) { ex.ToString(); throw; } } public async Task SaveUpdateOTPAsync(Otps forgotPassword, bool isPassChanged) { try { var isEmailExist = await _purchasingDbContext.Otps .Where(tv => tv.IsValid == true) .FirstOrDefaultAsync(fp => fp.Email == forgotPassword.Email); if (isPassChanged == true) { if (isEmailExist != null) { isEmailExist.IsValid = false; isEmailExist.UpdatedDate = DateTime.Now; isEmailExist.UpdatedBy = forgotPassword.Email; await _purchasingDbContext.SaveChangesAsync(); } } else { if (isEmailExist != null) { isEmailExist.UpdatedDate = DateTime.Now; isEmailExist.UpdatedBy = forgotPassword.Email; isEmailExist.OTP = forgotPassword.OTP; await _purchasingDbContext.SaveChangesAsync(); } else { forgotPassword.IsValid = true; await _purchasingDbContext.Otps.AddAsync(forgotPassword); await _purchasingDbContext.SaveChangesAsync(); } } } catch (Exception ex) { ex.ToString(); throw; } } } }