355 lines
16 KiB
C#
355 lines
16 KiB
C#
using _0_Framework.Application;
|
|
using _0_Framework.Application.Sms;
|
|
using AccountManagement.Application.Contracts.SubAccount;
|
|
using AccountManagement.Domain.AccountAgg;
|
|
using AccountManagement.Domain.CameraAccountAgg;
|
|
using AccountManagement.Domain.SubAccountAgg;
|
|
using AccountManagement.Domain.SubAccountRoleAgg;
|
|
using Company.Domain.WorkshopAccountAgg;
|
|
using Company.Domain.WorkshopSubAccountAgg;
|
|
using CompanyManagment.App.Contracts.Workshop;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
|
|
|
|
|
|
namespace AccountManagement.Application
|
|
{
|
|
public class SubAccountApplication : ISubAccountApplication
|
|
{
|
|
private readonly ISubAccountRepository _subAccountRepository;
|
|
private readonly IPasswordHasher _passwordHasher;
|
|
private readonly ISubAccountRoleRepository _subAccountRoleRepository;
|
|
private readonly IWorkshopSubAccountRepository _workshopSubAccountRepository;
|
|
private readonly ICameraAccountRepository _cameraAccountRepository;
|
|
private readonly IAccountRepository _accountRepository;
|
|
private readonly ISmsService _smsService;
|
|
public SubAccountApplication(ISubAccountRepository subAccountRepository, IPasswordHasher passwordHasher, ISubAccountRoleRepository subAccountRoleRepository,
|
|
IWorkshopSubAccountRepository workshopSubAccountRepository, IAccountRepository accountRepository, ICameraAccountRepository cameraAccountRepository, ISmsService smsService)
|
|
{
|
|
_subAccountRepository = subAccountRepository;
|
|
_passwordHasher = passwordHasher;
|
|
_subAccountRoleRepository = subAccountRoleRepository;
|
|
_workshopSubAccountRepository = workshopSubAccountRepository;
|
|
_accountRepository = accountRepository;
|
|
_cameraAccountRepository = cameraAccountRepository;
|
|
_smsService = smsService;
|
|
}
|
|
|
|
public OperationResult ChangePassword(SubAccountChangePassword cmd)
|
|
{
|
|
OperationResult op = new();
|
|
var entity = _subAccountRepository.Get(cmd.SubAccountId);
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
//(bool verified, bool needsUpgrade) = _passwordHasher.Check(entity.Password, cmd.OldPassword);
|
|
//if (!verified)
|
|
// return op.Failed(ApplicationMessages.WrongUserPass);
|
|
entity.ChangePassword(_passwordHasher.Hash(cmd.NewPassword));
|
|
_subAccountRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
|
|
}
|
|
|
|
public OperationResult ChangePasswordAndPhoneNumber(SubAccountChangePasswordAndPhoneNumber cmd)
|
|
{
|
|
OperationResult op = new();
|
|
cmd.PhoneNumber = cmd.PhoneNumber.Trim();
|
|
var entity = _subAccountRepository.Get(cmd.SubAccountId);
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
var validationResult = IsPhoneNumberAndPasswordValid(cmd.SubAccountId, cmd.PhoneNumber, cmd.Password, cmd.RePassword);
|
|
if (validationResult.IsSuccedded == false)
|
|
return validationResult;
|
|
if (!string.IsNullOrWhiteSpace(cmd.PhoneNumber))
|
|
{
|
|
|
|
entity.ChangePhoneNumber(cmd.PhoneNumber);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(cmd.RePassword))
|
|
{
|
|
|
|
entity.ChangePassword(_passwordHasher.Hash(cmd.Password));
|
|
}
|
|
_subAccountRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
|
|
}
|
|
|
|
public async Task<OperationResult> SendVerifyCodeForPasswordChange(string phone, long id)
|
|
{
|
|
var operation = new OperationResult();
|
|
var subAccount = _subAccountRepository.Get(id);
|
|
if (subAccount == null)
|
|
return operation.Failed(ApplicationMessages.RecordNotFound);
|
|
//var verifyCodeHash = _passwordHasher.Hash(verifyCode);
|
|
Random generator = new Random();
|
|
String r = generator.Next(1, 1000000).ToString("D6");
|
|
subAccount.SetVerifyCode(r);
|
|
_subAccountRepository.SaveChanges();
|
|
_smsService.VerifySend(phone, r);
|
|
|
|
TimeSpan delay = TimeSpan.FromSeconds(130);
|
|
await Task.Delay(delay);
|
|
|
|
subAccount.SetVerifyCode("");
|
|
_accountRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
public SubAccountViewModel GetByVerifyCodeAndPhoneNumber(string code, string phone)
|
|
{
|
|
return _subAccountRepository.GetByVerifyCodeAndPhoneNumber(code, phone);
|
|
}
|
|
public OperationResult Create(CreateSubAccount cmd, List<long> accountWorkshopsList)
|
|
{
|
|
OperationResult op = new();
|
|
|
|
cmd.Username = cmd.Username.ToLower();
|
|
cmd.PhoneNumber = cmd.PhoneNumber.Trim();
|
|
|
|
if (cmd.PhoneNumber.Length != 11)
|
|
return op.Failed("شماره تلفن همراه نامعتبر است");
|
|
|
|
if (!cmd.WorkshopIds.Any())
|
|
return op.Failed("حداقل یک کارگاه را انتخاب کنید");
|
|
|
|
|
|
if (!cmd.WorkshopIds.All(x => accountWorkshopsList.Contains(x)))
|
|
return op.Failed("خطای سیستمی");
|
|
|
|
|
|
if (cmd.SubAccountRoleId == 0 || !_subAccountRoleRepository.Exists(x => cmd.SubAccountRoleId == x.id))
|
|
return op.Failed("نقش مورد نظر وجود ندارد");
|
|
|
|
if (cmd.NationalCode.NationalCodeValid() != "valid")
|
|
return op.Failed("کد ملی وارد شده صحیح نمی باشد");
|
|
|
|
if (_subAccountRepository.Exists(x => x.Username == cmd.Username) || _accountRepository.Exists(x => x.Username == cmd.Username) ||
|
|
_cameraAccountRepository.Exists(x => x.Username == cmd.Username))
|
|
return op.Failed("نام کاربری نمی تواند تکراری باشد");
|
|
|
|
var entity = new SubAccount(cmd.AccountId, cmd.SubAccountRoleId, cmd.NationalCode, cmd.FName, cmd.LName, cmd.PhoneNumber, cmd.Username, _passwordHasher.Hash(cmd.Password),
|
|
cmd.ProfilePhoto);
|
|
|
|
if (_subAccountRepository.Exists(x => x.PhoneNumber == cmd.PhoneNumber) || _accountRepository.Exists(x => x.Mobile == cmd.PhoneNumber) ||
|
|
_cameraAccountRepository.Exists(x => x.Mobile == cmd.PhoneNumber))
|
|
return op.Failed("قبلا یک حساب با این شماره ثبت شده است");
|
|
|
|
_subAccountRepository.Create(entity);
|
|
_subAccountRepository.SaveChanges();
|
|
|
|
|
|
var workshops = cmd.WorkshopIds.Select(x => new WorkshopSubAccount(x, entity.id));
|
|
|
|
foreach (var w in workshops)
|
|
_workshopSubAccountRepository.Create(w);
|
|
|
|
_workshopSubAccountRepository.SaveChanges();
|
|
return op.Succcedded(entity.id);
|
|
}
|
|
|
|
public OperationResult Delete(long id)
|
|
{
|
|
OperationResult op = new();
|
|
var entity = _subAccountRepository.Get(id);
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
_subAccountRepository.Remove(entity);
|
|
_subAccountRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult EditSubAccount(EditSubAccount cmd, List<long> accountWorkshopsList)
|
|
{
|
|
OperationResult op = new();
|
|
|
|
|
|
|
|
var entity = _subAccountRepository.Get(cmd.SubAccountId);
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
|
|
|
|
|
|
|
|
if (!cmd.WorkshopIds.All(x => accountWorkshopsList.Contains(x)))
|
|
return op.Failed("خطای سیستمی");
|
|
|
|
|
|
if (cmd.SubAccountRoleId == 0 || !_subAccountRoleRepository.Exists(x => cmd.SubAccountRoleId == x.id))
|
|
return op.Failed("نقش مورد نظر وجود ندارد");
|
|
|
|
var workshopSubAccounts = _workshopSubAccountRepository.GetWorkshopsSubAccountEntityBySubAccountId(entity.id);
|
|
foreach (var workshopSubAccount in workshopSubAccounts)
|
|
_workshopSubAccountRepository.Remove(workshopSubAccount);
|
|
|
|
|
|
var workshops = cmd.WorkshopIds.Select(x => new WorkshopSubAccount(x, entity.id));
|
|
|
|
foreach (var w in workshops)
|
|
_workshopSubAccountRepository.Create(w);
|
|
|
|
entity.Edit(cmd.SubAccountRoleId, cmd.NationalCode, cmd.FName, cmd.LName, cmd.ProfilePhoto);
|
|
_workshopSubAccountRepository.SaveChanges();
|
|
_subAccountRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public SubAccountViewModel GetDetails(long subAccountId)
|
|
{
|
|
var entity = _subAccountRepository.GetDetails(subAccountId);
|
|
if (entity == null) return null;
|
|
List<(long Id, string Name)> subAccountWorkshops = _workshopSubAccountRepository.GetWorkshopsBySubAccountId(subAccountId).Select(x => (x.WorkshopId, x.WorkshopName)).ToList();
|
|
return new SubAccountViewModel()
|
|
{
|
|
Id = entity.id,
|
|
IsActive = entity.IsActive,
|
|
PhoneNumber = entity.PhoneNumber,
|
|
ProfilePhoto = entity.ProfilePhoto,
|
|
Username = entity.Username,
|
|
SubAccountFullName = entity.FullName,
|
|
SubAccountRole = entity.SubAccountRole.Title,
|
|
SubAccountWorkshops = subAccountWorkshops,
|
|
FName = entity.FName,
|
|
LName = entity.LName,
|
|
NationalCode = entity.NationalCode,
|
|
SubAccountRoleId = entity.SubAccountRoleId
|
|
};
|
|
}
|
|
public SubAccountRoleViewModel GetRoleDetails(long subAccountRoleId)
|
|
{
|
|
var entity = _subAccountRoleRepository.Get(subAccountRoleId);
|
|
if (entity == null) return null;
|
|
return new SubAccountRoleViewModel()
|
|
{
|
|
Id = entity.id,
|
|
Title = entity.Title,
|
|
Permissions = entity.RolePermissions.Select(x => x.PermissionCode).ToList()
|
|
};
|
|
}
|
|
|
|
public List<SubAccountViewModel> GetAllByAccountId(long accountId, int pageIndex)
|
|
{
|
|
return _subAccountRepository.GetAllByAccountId(accountId, pageIndex);
|
|
}
|
|
|
|
public OperationResult CreateRole(CreateSubAccountRole command)
|
|
{
|
|
OperationResult op = new();
|
|
if (_subAccountRoleRepository.Exists(x => x.AccountId == command.AccountId && x.Title.Trim() == command.Title.Trim()))
|
|
return op.Failed("یک نقش با این عنوان وجود دارد");
|
|
var role = new SubAccountRole(command.Title, command.Permissions, command.AccountId);
|
|
_subAccountRoleRepository.Create(role);
|
|
_subAccountRoleRepository.SaveChanges();
|
|
return op.Succcedded(role.id);
|
|
}
|
|
public OperationResult EditRole(EditSubAccountRole cmd)
|
|
{
|
|
OperationResult op = new();
|
|
if (_subAccountRoleRepository.Exists(x => x.AccountId == cmd.AccountId && x.Title.Trim() == cmd.Title.Trim() && x.id != cmd.Id))
|
|
return op.Failed("یک نقش با این عنوان وجود دارد");
|
|
var entity = _subAccountRoleRepository.Get(cmd.Id);
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
entity.Edit(cmd.Title, cmd.Permissions);
|
|
_subAccountRoleRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
public OperationResult AssignRoleToSubAccount(AssignSubAccountRole command)
|
|
{
|
|
OperationResult op = new();
|
|
var entity = _subAccountRepository.Get(command.SubAccountId);
|
|
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
|
|
if (!_subAccountRoleRepository.Exists(x => x.id == command.SubAccountRoleId))
|
|
return op.Failed("نقش انتخاب شده وجود ندارد");
|
|
|
|
entity.AssignRole(command.SubAccountRoleId);
|
|
_subAccountRoleRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
public OperationResult DeleteRole(long id)
|
|
{
|
|
OperationResult op = new();
|
|
var entity = _subAccountRoleRepository.Get(id);
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
if (_subAccountRepository.Exists(x => x.SubAccountRoleId == id))
|
|
return op.Failed("برای حذف نقش نباید حساب کاربری با این نقش وجود داشته باشد");
|
|
_subAccountRoleRepository.Remove(entity);
|
|
_subAccountRoleRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
public OperationResult Activate(long subAccountId)
|
|
{
|
|
OperationResult op = new();
|
|
var entity = _subAccountRepository.Get(subAccountId);
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
entity.Activate();
|
|
_subAccountRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
public OperationResult Deactivate(long subAccountId)
|
|
{
|
|
OperationResult op = new();
|
|
var entity = _subAccountRepository.Get(subAccountId);
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
entity.Deactivate();
|
|
_subAccountRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
public List<SubAccountRoleViewModel> GetSubAccountRolesByAccountId(long accountId)
|
|
{
|
|
return _subAccountRoleRepository.GetSubAccountRolesByAccountId(accountId);
|
|
}
|
|
public List<SubAccountsGroupedByRoleViewModel> GetSubAccountsByAccountIdGroupedByRole(long accountId)
|
|
{
|
|
return _subAccountRepository.GetSubAccountsByAccountIdGroupedByRole(accountId);
|
|
}
|
|
public OperationResult IsPhoneNumberAndPasswordValid(long subAccountId, string phoneNumber, string password, string rePassword)
|
|
{
|
|
OperationResult op = new();
|
|
|
|
var entity = _subAccountRepository.Get(subAccountId);
|
|
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
|
|
if (!string.IsNullOrWhiteSpace(rePassword) || !string.IsNullOrWhiteSpace(password))
|
|
{
|
|
if (rePassword != password)
|
|
return op.Failed("تکرار رمز عبور با رمز عبور مطابقت ندارد");
|
|
|
|
if (password.Length < 8)
|
|
return op.Failed("رمز عبور نمی تواند کمتر از 8 کاراکتر باشد");
|
|
}
|
|
|
|
|
|
if ((string.IsNullOrWhiteSpace(phoneNumber) || entity.PhoneNumber == phoneNumber) && string.IsNullOrWhiteSpace(rePassword))
|
|
return op.Failed("چیزی برای تغییر وجود ندارد");
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(phoneNumber) && entity.PhoneNumber != phoneNumber)
|
|
{
|
|
phoneNumber = phoneNumber.Trim();
|
|
if (phoneNumber.Length != 11)
|
|
return op.Failed("شماره تلفن همراه به درستی وارد نشده است");
|
|
if (_accountRepository.Exists(x => x.Mobile == phoneNumber) ||
|
|
_subAccountRepository.Exists(x => x.PhoneNumber == phoneNumber && x.id != subAccountId) ||
|
|
_cameraAccountRepository.Exists(x => x.Mobile == phoneNumber))
|
|
return op.Failed("قبلا یک حساب با این شماره ثبت شده است");
|
|
}
|
|
|
|
|
|
return op.Succcedded();
|
|
}
|
|
}
|
|
}
|