Files
Backend-Api/AccountManagement.Application/AccountApplication.cs
2024-07-10 20:25:54 +03:30

449 lines
18 KiB
C#

using System;
using _0_Framework.Application;
using AccountManagement.Application.Contracts.Account;
using AccountManagement.Domain.AccountAgg;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using _0_Framework.Application.Sms;
using AccountManagement.Domain.CameraAccountAgg;
using AccountManagement.Domain.RoleAgg;
using Microsoft.AspNetCore.Http;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using TaskManager.Domain.PositionAgg;
//using AccountManagement.Domain.RoleAgg;
namespace AccountManagement.Application;
public class AccountApplication : IAccountApplication
{
private readonly IFileUploader _fileUploader;
private readonly IPasswordHasher _passwordHasher;
private readonly IAccountRepository _accountRepository;
private readonly IAuthHelper _authHelper;
private readonly IRoleRepository _roleRepository;
private readonly ISmsService _smsService;
private readonly ICameraAccountRepository _cameraAccountRepository;
private readonly IPositionRepository _positionRepository;
public AccountApplication(IAccountRepository accountRepository, IPasswordHasher passwordHasher,
IFileUploader fileUploader, IAuthHelper authHelper, IRoleRepository roleRepository, IWorker worker, ISmsService smsService, ICameraAccountRepository cameraAccountRepository, IPositionRepository positionRepository)
{
_authHelper = authHelper;
_roleRepository = roleRepository;
_smsService = smsService;
_cameraAccountRepository = cameraAccountRepository;
_positionRepository = positionRepository;
_fileUploader = fileUploader;
_passwordHasher = passwordHasher;
_accountRepository = accountRepository;
}
public OperationResult EditClient(EditClientAccount command)
{
var opreation = new OperationResult();
var editAccount = _accountRepository.Get(command.Id);
if (editAccount == null)
return opreation.Failed(ApplicationMessages.RecordNotFound);
if (string.IsNullOrWhiteSpace(command.Fullname) || string.IsNullOrWhiteSpace(command.Username)
|| string.IsNullOrWhiteSpace(command.Mobile) ||
string.IsNullOrWhiteSpace(command.NationalCode))
return opreation.Failed("پر کردنموارد ستاره دار الزامی است");
if (_accountRepository.Exists(x =>
(x.Username == command.Username && x.id != command.Id)))
return opreation.Failed("نام کاربری تکراری است");
if (_accountRepository.Exists(x =>
(x.Mobile == command.Mobile && x.id != command.Id)))
return opreation.Failed("شماره موبایل تکراری است");
if (_accountRepository.Exists(x =>
(x.NationalCode == command.NationalCode && !string.IsNullOrWhiteSpace(x.NationalCode) && x.id != command.Id)))
return opreation.Failed("کد ملی تکراری است");
if (_accountRepository.Exists(x =>
(x.Email == command.Email && !string.IsNullOrWhiteSpace(x.Email) && x.id != command.Id)))
return opreation.Failed("ایمیل تکراری است");
var path = $"profilePhotos";
var picturePath = _fileUploader.Upload(command.ProfilePhoto, path);
editAccount.EditClient(command.Fullname,command.Username,command.Mobile,picturePath,command.Email,command.NationalCode);
_accountRepository.SaveChanges();
return opreation.Succcedded();
}
public OperationResult ChangePassword(ChangePassword command)
{
var operation = new OperationResult();
var account = _accountRepository.Get(command.Id);
if (account == null)
return operation.Failed(ApplicationMessages.RecordNotFound);
if (command.Password != command.RePassword)
return operation.Failed(ApplicationMessages.PasswordsNotMatch);
var password = _passwordHasher.Hash(command.Password);
account.ChangePassword(password);
_accountRepository.SaveChanges();
return operation.Succcedded();
}
public AccountViewModel GetAccountBy(long id)
{
var account = _accountRepository.Get(id);
return new AccountViewModel()
{
Fullname = account.Fullname,
Mobile = account.Mobile
};
}
public OperationResult Create(CreateAccount command)
{
var operation = new OperationResult();
if (_accountRepository.Exists(x => x.Username == command.Username || x.Mobile == command.Mobile))
return operation.Failed(ApplicationMessages.DuplicatedRecord);
var password = _passwordHasher.Hash(command.Password);
var roleName = _roleRepository.GetDetails(command.RoleId);
var path = $"profilePhotos";
if (_fileUploader != null)
{
var picturePath = _fileUploader.Upload(command.ProfilePhoto, path);
var account = new Account(command.Fullname, command.Username, password, command.Mobile, command.RoleId,
picturePath, roleName.Name,"true","false");
_accountRepository.Create(account);
}
_accountRepository.SaveChanges();
return operation.Succcedded();
}
public OperationResult RegisterClient(RegisterAccount command)
{
var opreation = new OperationResult();
if (string.IsNullOrWhiteSpace(command.Fullname) || string.IsNullOrWhiteSpace(command.Username)
|| string.IsNullOrWhiteSpace(command.Mobile) ||
string.IsNullOrWhiteSpace(command.NationalCode)
|| string.IsNullOrWhiteSpace(command.Password))
return opreation.Failed("پر کردن تمامی فیلدها الزامی است");
if (_accountRepository.Exists(x => x.Username == command.Username))
return opreation.Failed("نام کاربری تکراری است");
if (_accountRepository.Exists(x => x.Mobile == command.Mobile ||
(x.NationalCode == command.NationalCode && !string.IsNullOrWhiteSpace(x.NationalCode))))
return opreation.Failed("مقادیر وارد شده تکراری است");
//var nationalCodeValidation = command.NationalCode.NationalCodeValid();
//switch (nationalCodeValidation)
//{
// case "incorrect":
// return opreation.Failed("اعداد وارد شده برای کد ملی صحیح نیست");
// break;
// case "invalid":
// return opreation.Failed("کد ملی وارد شده معتبر نیست");
// break;
// case "lessThan10":
// return opreation.Failed("کد ملی وارد شده کمتر از 10 رقم است");
// break;
//}
var password = _passwordHasher.Hash(command.Password);
var register =new Account(command.Fullname,command.Username, password, command.Mobile, command.NationalCode);
_accountRepository.Create(register);
_accountRepository.SaveChanges();
return opreation.Succcedded(register.id,message: "ثبت نام شما با موفقیت انجام شد");
}
public OperationResult Edit(EditAccount command)
{
var operation = new OperationResult();
var account = _accountRepository.Get(command.Id);
if (account == null)
return operation.Failed(ApplicationMessages.RecordNotFound);
if (_accountRepository.Exists(x =>
(x.Username == command.Username || x.Mobile == command.Mobile) && x.id != command.Id))
return operation.Failed(ApplicationMessages.DuplicatedRecord);
var roleName = _roleRepository.GetDetails(command.RoleId);
var path = $"profilePhotos";
var picturePath = _fileUploader.Upload(command.ProfilePhoto, path);
account.Edit(command.Fullname, command.Username, command.Mobile, command.RoleId, picturePath, roleName.Name);
_accountRepository.SaveChanges();
return operation.Succcedded();
}
public EditAccount GetDetails(long id)
{
return _accountRepository.GetDetails(id);
}
public OperationResult Login(Login command)
{
long idAutoriz = 0;
var operation = new OperationResult();
var account = _accountRepository.GetBy(command.Username);
var cameraAccount = _cameraAccountRepository.GetBy(command.Username);
if (account == null && cameraAccount == null)
return operation.Failed(ApplicationMessages.WrongUserPass);
if (account != null)
{
(bool Verified, bool NeedUpgrade) result = _passwordHasher.Check(account.Password, command.Password);
if (!result.Verified)
return operation.Failed(ApplicationMessages.WrongUserPass);
var permissions = _roleRepository.Get(account.RoleId)
.Permissions
.Select(x => x.Code)
.ToList();
int? positionValue;
if (account.PositionId != null)
{
positionValue = _positionRepository.Get((long)account.PositionId).PositionValue;
}
else
{
positionValue = null;
}
var authViewModel = new AuthViewModel(account.id, account.RoleId, account.Fullname
, account.Username, account.Mobile, account.ProfilePhoto, permissions, account.RoleName, account.AdminAreaPermission, account.ClientAriaPermission, positionValue);
_authHelper.Signin(authViewModel);
if ((account.AdminAreaPermission == "true" && account.ClientAriaPermission == "true" && account.IsActiveString == "true") || (account.AdminAreaPermission == "true" && account.ClientAriaPermission == "false" && account.IsActiveString == "true"))
idAutoriz = 1;
if (account.ClientAriaPermission == "true" && account.AdminAreaPermission == "false" && account.IsActiveString == "true")
idAutoriz = 2;
}
if (cameraAccount != null)
{
(bool Verified, bool NeedUpgrade) result = _passwordHasher.Check(cameraAccount.Password, command.Password);
if (!result.Verified)
return operation.Failed(ApplicationMessages.WrongUserPass);
var mobile = string.IsNullOrWhiteSpace(cameraAccount.Mobile) ? " " : cameraAccount.Mobile;
var authViewModel = new CameraAuthViewModel(cameraAccount.id, cameraAccount.WorkshopId,
cameraAccount.Username, mobile, cameraAccount.WorkshopName, cameraAccount.AccountId,cameraAccount.IsActiveSting);
if (cameraAccount.IsActiveSting == "true")
{
_authHelper.CameraSignIn(authViewModel);
idAutoriz = 3;
}
else
{
idAutoriz = 0;
}
}
return operation.Succcedded(idAutoriz);
}
public OperationResult LoginWithMobile(long id)
{
var operation = new OperationResult();
var account = _accountRepository.GetById(id);
if (account == null)
return operation.Failed(ApplicationMessages.WrongUserPass);
var permissions = _roleRepository.Get(account.RoleId)
.Permissions
.Select(x => x.Code)
.ToList();
int? positionValue;
if (account.PositionId != null)
{
positionValue = _positionRepository.Get((long)account.PositionId).PositionValue;
}
else
{
positionValue = null;
}
var authViewModel = new AuthViewModel(account.id, account.RoleId, account.Fullname
, account.Username, account.Mobile, account.ProfilePhoto, permissions, account.RoleName, account.AdminAreaPermission, account.ClientAriaPermission, positionValue);
_authHelper.Signin(authViewModel);
long idAutoriz = 0;
if (account.AdminAreaPermission == "true" && account.ClientAriaPermission == "true" || account.AdminAreaPermission == "true" && account.ClientAriaPermission == "false")
idAutoriz = 1;
if (account.ClientAriaPermission == "true" && account.AdminAreaPermission == "false")
idAutoriz = 2;
return operation.Succcedded(idAutoriz);
}
public void Logout()
{
_authHelper.SignOut();
}
public List<AccountViewModel> GetAccounts()
{
return _accountRepository.GetAccounts();
}
public List<AccountViewModel> GetClientsAccount()
{
return _accountRepository.GetClientsAccount();
}
public async Task<OperationResult> SendVerifyCodeToChangingPass(string phone, long id)
{
var operation = new OperationResult();
var account = _accountRepository.Get(id);
if (account == null)
return operation.Failed(ApplicationMessages.RecordNotFound);
//var verifyCodeHash = _passwordHasher.Hash(verifyCode);
Random generator = new Random();
String r = generator.Next(1, 1000000).ToString("D6");
account.SetVerifyCode(r);
_accountRepository.SaveChanges();
_smsService.VerifySend(phone, r);
TimeSpan delay = TimeSpan.FromSeconds(130);
await Task.Delay(delay);
account.SetVerifyCode("");
_accountRepository.SaveChanges();
return operation.Succcedded();
}
public EditAccount GetByVerifyCode(string code, string phone)
{
return _accountRepository.GetByVerifyCode(code, phone);
}
public EditAccount GetByUserNameAndId(long id, string username)
{
return _accountRepository.GetByUserNameAndId(id, username);
}
public async Task <OperationResult> SetVerifyCode(string phone, long id)
{
var operation = new OperationResult();
var account = _accountRepository.Get(id);
if (account == null || account.IsActiveString == "false")
return operation.Failed(ApplicationMessages.RecordNotFound);
//var verifyCodeHash = _passwordHasher.Hash(verifyCode);
Random generator = new Random();
String r = generator.Next(1, 1000000).ToString("D6");
account.SetVerifyCode(r);
_accountRepository.SaveChanges();
_smsService.LoginSend(phone, r);
TimeSpan delay = TimeSpan.FromSeconds(130);
await Task.Delay(delay);
account.SetVerifyCode("");
_accountRepository.SaveChanges();
return operation.Succcedded();
}
public List<AccountViewModel> Search(AccountSearchModel searchModel)
{
return _accountRepository.Search(searchModel);
}
public OperationResult Active(long id)
{
var opration = new OperationResult();
var acc = _accountRepository.Get(id);
if (acc == null)
return opration.Failed("رکورد مورد نظر یافت نشد");
acc.Active();
_accountRepository.SaveChanges();
return opration.Succcedded();
}
public OperationResult DeActive(long id)
{
var opration = new OperationResult();
var acc = _accountRepository.Get(id);
if (acc == null)
return opration.Failed("رکورد مورد نظر یافت نشد");
acc.DeActive();
_accountRepository.SaveChanges();
return opration.Succcedded();
}
public OperationResult DirectLogin(long id)
{
var prAcc = _authHelper.CurrentAccountInfo();
var operation = new OperationResult();
var account = _accountRepository.GetById(id);
if (account == null)
return operation.Failed("این اکان وجود ندارد");
var permissions = _roleRepository.Get(account.RoleId)
.Permissions
.Select(x => x.Code)
.ToList();
_authHelper.SignOut();
var authViewModel = new AuthViewModel(account.id, account.RoleId, account.Fullname
, account.Username, account.Mobile, account.ProfilePhoto, permissions, account.RoleName, "false", "true",null);
_authHelper.Signin(authViewModel);
return operation.Succcedded(2);
}
public List<AccountViewModel> AccountsForAssign(long accountId)
{
return _accountRepository.AccountsForAssign(accountId);
}
public List<AccountViewModel> GetAccountsByPositionId(long positionId)
{
if (!_positionRepository.Exists(x => x.id == positionId))
{
return new List<AccountViewModel>();
}
return _accountRepository.GetAccountsByPositionId(positionId);
}
public List<AccountViewModel> GetAccountLowerPositionvalue()
{
return _accountRepository.GetAccountLowerPositionvalue();
}
public OperationResult ReLogin()
{
var prAcc = _authHelper.CurrentAccountInfo();
var operation = new OperationResult();
var account = _accountRepository.GetIncludePositions(prAcc.Id);
if (account == null)
return operation.Failed("این اکانت وجود ندارد");
var permissions = _roleRepository.Get(account.RoleId)
.Permissions
.Select(x => x.Code)
.ToList();
_authHelper.SignOut();
var authViewModel = new AuthViewModel(account.id, account.RoleId, account.Fullname
, account.Username, account.Mobile, account.ProfilePhoto, permissions, account.RoleName, account.AdminAreaPermission, account.ClientAriaPermission, account.Position.PositionValue);
_authHelper.Signin(authViewModel);
return operation.Succcedded(2);
}
}