2025-06-29 15:29:51 +03:30
|
|
|
|
using Common.Contracts.Infrastructure;
|
|
|
|
|
using Common.Dtos.Verification;
|
|
|
|
|
using Common.Enums;
|
|
|
|
|
using Common.Models.Auth;
|
|
|
|
|
using Hushian.Application.Contracts.Persistence;
|
|
|
|
|
using Hushian.Application.Models;
|
2025-07-03 16:38:14 +03:30
|
|
|
|
using Hushian.Application.Validation;
|
2025-06-29 15:29:51 +03:30
|
|
|
|
using Hushian.Domain.Entites;
|
|
|
|
|
using Identity.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
using System.Linq;
|
2025-07-03 16:05:44 +03:30
|
|
|
|
using System.Reflection;
|
2025-06-29 15:29:51 +03:30
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Hushian.Application.Services
|
|
|
|
|
{
|
|
|
|
|
public class VerificationService
|
|
|
|
|
{
|
|
|
|
|
private readonly IGenericRepository<VerificationCode> _VerificationCodeRepository;
|
2025-07-03 16:05:44 +03:30
|
|
|
|
private readonly IGenericRepository<Company> _CompanyRepository;
|
2025-07-03 16:38:14 +03:30
|
|
|
|
private readonly IGenericRepository<Exper> _ExperRepository;
|
2025-06-29 15:29:51 +03:30
|
|
|
|
private readonly IMessageSender _messageSender;
|
|
|
|
|
private readonly IGenericRepository<User> _UserRepository;
|
|
|
|
|
private readonly AuthService _authService;
|
2025-06-29 16:14:42 +03:30
|
|
|
|
|
2025-07-07 22:04:07 +03:30
|
|
|
|
public VerificationService(IGenericRepository<VerificationCode> verificationCodeRepository
|
|
|
|
|
, IMessageSender messageSender
|
|
|
|
|
, IGenericRepository<User> userRepository
|
|
|
|
|
, AuthService authService
|
|
|
|
|
, IGenericRepository<Company> companyRepository
|
|
|
|
|
, IGenericRepository<Exper> experRepository)
|
2025-06-29 16:14:42 +03:30
|
|
|
|
{
|
|
|
|
|
_VerificationCodeRepository = verificationCodeRepository;
|
|
|
|
|
_messageSender = messageSender;
|
|
|
|
|
_UserRepository = userRepository;
|
|
|
|
|
_authService = authService;
|
2025-07-03 16:05:44 +03:30
|
|
|
|
_CompanyRepository = companyRepository;
|
2025-07-03 16:38:14 +03:30
|
|
|
|
_ExperRepository = experRepository;
|
2025-06-29 16:14:42 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> GenerateCodeForLoginUser(string Mobile)
|
2025-06-29 15:29:51 +03:30
|
|
|
|
{
|
|
|
|
|
string Code = await GenerateCode();
|
|
|
|
|
var response= await _VerificationCodeRepository.ADD
|
|
|
|
|
(new Identity.Models.VerificationCode(VerificationCodeType.Login, Code, Mobile));
|
|
|
|
|
await _messageSender.SendMassage(new Models.Message.Message()
|
|
|
|
|
{
|
|
|
|
|
msg = Code,
|
|
|
|
|
To = Mobile
|
|
|
|
|
});
|
|
|
|
|
return response.ID;
|
|
|
|
|
}
|
2025-07-03 16:05:44 +03:30
|
|
|
|
public async Task<int> GenerateCodeByPhoneNumberConfirmed(string Mobile)
|
|
|
|
|
{
|
|
|
|
|
string Code = await GenerateCode();
|
|
|
|
|
var response = await _VerificationCodeRepository.ADD
|
|
|
|
|
(new Identity.Models.VerificationCode(VerificationCodeType.PhoneNumberConfirmed, Code, Mobile));
|
|
|
|
|
await _messageSender.SendMassage(new Models.Message.Message()
|
|
|
|
|
{
|
|
|
|
|
msg = Code,
|
|
|
|
|
To = Mobile
|
|
|
|
|
});
|
|
|
|
|
return response.ID;
|
|
|
|
|
}
|
2025-07-03 16:38:14 +03:30
|
|
|
|
public async Task<int> GenerateCodeByForgetPassword(string Mobile)
|
|
|
|
|
{
|
|
|
|
|
string Code = await GenerateCode();
|
|
|
|
|
var response = await _VerificationCodeRepository.ADD
|
|
|
|
|
(new Identity.Models.VerificationCode(VerificationCodeType.ForgetPassword, Code, Mobile));
|
|
|
|
|
await _messageSender.SendMassage(new Models.Message.Message()
|
|
|
|
|
{
|
|
|
|
|
msg = Code,
|
|
|
|
|
To = Mobile
|
|
|
|
|
});
|
|
|
|
|
return response.ID;
|
|
|
|
|
}
|
2025-06-29 15:29:51 +03:30
|
|
|
|
public async Task<ResponseBase<AuthResponse>> VerificationCode(ConfirmedCodeDto model)
|
|
|
|
|
{
|
|
|
|
|
var response = new ResponseBase<AuthResponse>();
|
|
|
|
|
var resultConf=await _VerificationCodeRepository.Get()
|
|
|
|
|
.FirstOrDefaultAsync(w => w.ID == model.Id && w.Code == model.code && w.Type == model.codeType);
|
|
|
|
|
if (resultConf!=null)
|
|
|
|
|
{
|
|
|
|
|
if (resultConf.Type == VerificationCodeType.Login)
|
|
|
|
|
{
|
|
|
|
|
var User= await _UserRepository.Get().FirstOrDefaultAsync(w => w.Mobile == resultConf.Mobile);
|
|
|
|
|
if (User!=null)
|
|
|
|
|
{
|
|
|
|
|
response.Success = true;
|
|
|
|
|
response.Value = new AuthResponse()
|
|
|
|
|
{
|
|
|
|
|
Fullname = User.FullName,
|
|
|
|
|
Id = User.ID,
|
|
|
|
|
MobileOrUserName = User.Mobile,
|
2025-07-11 20:37:28 +03:30
|
|
|
|
Role="User",
|
|
|
|
|
Token = new JwtSecurityTokenHandler().WriteToken(await _authService.GenerateToken(User.Mobile, User.ID, "User"))
|
2025-06-29 15:29:51 +03:30
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("کاربری یافت نشد");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (resultConf.Type == VerificationCodeType.ForgetPassword)
|
|
|
|
|
{
|
2025-07-03 16:38:14 +03:30
|
|
|
|
List<string> errors = new();
|
|
|
|
|
if (model.value.CheckLawPassword(ref errors))
|
|
|
|
|
response.Errors.AddRange(errors);
|
2025-06-29 15:29:51 +03:30
|
|
|
|
else
|
|
|
|
|
{
|
2025-07-03 16:38:14 +03:30
|
|
|
|
if (resultConf.Mobile.StartsWith("09"))
|
|
|
|
|
{
|
|
|
|
|
var anyCompany = await _CompanyRepository.Get().FirstOrDefaultAsync(w => w.Mobile == resultConf.Mobile);
|
|
|
|
|
anyCompany.Password = model.value.GetHash();
|
|
|
|
|
anyCompany.Verified = true;
|
|
|
|
|
if (await _CompanyRepository.UPDATEBool(anyCompany))
|
|
|
|
|
{
|
|
|
|
|
response.Success = true;
|
|
|
|
|
response.Value = new AuthResponse();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("خطای سیستمی در احراز");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var anyexper=await _ExperRepository.Get().FirstOrDefaultAsync(w => w.UserName == resultConf.Mobile);
|
|
|
|
|
anyexper.Password = model.value.GetHash();
|
|
|
|
|
if (await _ExperRepository.UPDATEBool(anyexper))
|
|
|
|
|
{
|
|
|
|
|
response.Success = true;
|
|
|
|
|
response.Value = new AuthResponse();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("خطای سیستمی در احراز");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-29 15:29:51 +03:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (resultConf.Type == VerificationCodeType.PhoneNumberConfirmed)
|
|
|
|
|
{
|
2025-07-03 16:05:44 +03:30
|
|
|
|
var anyCompany=await _CompanyRepository.Get().FirstOrDefaultAsync(w => w.Mobile == resultConf.Mobile && !w.Verified);
|
|
|
|
|
if (anyCompany!=null)
|
|
|
|
|
{
|
|
|
|
|
anyCompany.Verified = true;
|
|
|
|
|
if(await _CompanyRepository.UPDATEBool(anyCompany))
|
|
|
|
|
{
|
|
|
|
|
response.Success = true;
|
|
|
|
|
response.Value = new AuthResponse();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("خطای سیستمی در احراز");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-29 15:29:51 +03:30
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("احراز صحیح نمی باشد");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _VerificationCodeRepository.DELETE(resultConf);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("احراز یافت نشد");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-03 16:05:44 +03:30
|
|
|
|
}
|
|
|
|
|
public async Task<bool> ReSendCode(int ID)
|
|
|
|
|
{
|
|
|
|
|
var model=await _VerificationCodeRepository.Get().FirstOrDefaultAsync(f=>f.ID==ID);
|
|
|
|
|
if (model==null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return await _messageSender.SendMassage(new Models.Message.Message()
|
|
|
|
|
{
|
|
|
|
|
msg = model.Code,
|
|
|
|
|
To = model.Mobile
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-06-29 15:29:51 +03:30
|
|
|
|
}
|
|
|
|
|
private async Task<string> GenerateCode()
|
|
|
|
|
{
|
|
|
|
|
int Code = Random.Shared.Next(1000, 9000);
|
|
|
|
|
while (await _VerificationCodeRepository.Get().AnyAsync(w => w.Code == Code.ToString()))
|
|
|
|
|
Code = Random.Shared.Next(1000, 9000);
|
|
|
|
|
|
|
|
|
|
return Code.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|