Files
Backend-Api/ServiceHost/Areas/Client/Pages/Company/SubAccounts/Index.cshtml.cs
2025-05-30 23:23:53 +03:30

390 lines
12 KiB
C#

using _0_Framework.Application;
using AccountManagement.Application.Contracts.SubAccount;
using CompanyManagment.App.Contracts.Workshop;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.CodeAnalysis.Elfie.Diagnostics;
using Microsoft.Identity.Client;
using System.Security.Claims;
using _0_Framework.Infrastructure;
using AccountManagement.Application.Contracts.SubAccountPermissionSubtitle;
using CompanyManagment.App.Contracts.Employee;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using AccountManagement.Application.Contracts.Account;
namespace ServiceHost.Areas.Client.Pages.Company.SubAccounts
{
[Authorize]
[NeedsPermission(SubAccountPermissionHelper.UserManagementOperationsPermissionCode)]
public class IndexModel : PageModel
{
private readonly IPasswordHasher _passwordHasher;
private readonly IWorkshopApplication _workshopApplication;
private readonly IEmployeeApplication _employeeApplication;
private readonly ISubAccountApplication _subAccountApplication;
private readonly ISubAccountPermissionSubtitleApplication _permissionSubtitleApplication;
private readonly IAccountApplication _accountApplication;
private readonly IAuthHelper _authHelper;
public string WorkshopFullName;
public bool HasRole;
public List<SubAccountRoleViewModel> Roles=new();
public IndexModel(IPasswordHasher passwordHasher, IWorkshopApplication workshopApplication, IAuthHelper authHelper, ISubAccountApplication subAccountApplication, ISubAccountPermissionSubtitleApplication permissionSubtitleApplication, IEmployeeApplication employeeApplication, IAccountApplication accountApplication)
{
_passwordHasher = passwordHasher;
_workshopApplication = workshopApplication;
_authHelper = authHelper;
_subAccountApplication = subAccountApplication;
_permissionSubtitleApplication = permissionSubtitleApplication;
_employeeApplication = employeeApplication;
_accountApplication = accountApplication;
}
public IActionResult OnGet()
{
long accountId = _authHelper.CurrentAccountId();
var workshopHash = User.FindFirstValue("WorkshopSlug");
long workshopId = _passwordHasher.SlugDecrypt(workshopHash);
if (workshopId <= 0)
return BadRequest();
WorkshopFullName = _authHelper.GetWorkshopName();
Roles = _subAccountApplication.GetSubAccountRolesByAccountId(accountId);
HasRole = Roles.Any();
return Page();
}
public IActionResult OnGetSubAccountsGroupedByRole()
{
long accountId = _authHelper.CurrentAccountId();
var resultData = _subAccountApplication.GetSubAccountsByAccountIdGroupedByRole(accountId);
return new JsonResult(new
{
success = true,
data = resultData
});
}
public IActionResult OnGetCreateRole()
{
var command = new CreateSubAccountRole();
return Partial("ModalCreateRole", command);
}
public IActionResult OnGetPermissions()
{
var result = _permissionSubtitleApplication.GetAllSubtitlesNested();
return new JsonResult(new
{
success = true,
data = result
});
}
public IActionResult OnPostCreateRole(CreateSubAccountRole command)
{
long accountId = _authHelper.CurrentAccountId();
command.AccountId = accountId;
OperationResult result = _subAccountApplication.CreateRole(command);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message
});
}
public IActionResult OnGetEditRole(long id)
{
var getDetails = _subAccountApplication.GetRoleDetails(id);
var command = new EditSubAccountRole()
{
Id = getDetails.Id,
Title = getDetails.Title,
Permissions = getDetails.Permissions,
WorkshopIds = getDetails.WorkshopIds
};
return Partial("ModalEditRole", command);
}
public IActionResult OnPostEditRole(EditSubAccountRole command)
{
command.AccountId = _authHelper.CurrentAccountId();
OperationResult result = _subAccountApplication.EditRole(command);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message
});
}
public IActionResult OnPostRemoveRole(long id)
{
OperationResult result = _subAccountApplication.DeleteRole(id);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message
});
}
public IActionResult OnGetWorkshopList()
{
var workshopHash = User.FindFirstValue("WorkshopSlug");
var workshopId = _passwordHasher.SlugDecrypt(workshopHash);
if (workshopId <= 0)
return new JsonResult(new
{
success = false,
message = "کارگاه ای یافت نشد",
});
var result = _authHelper.CurrentAccountInfo().WorkshopList;
return new JsonResult(new
{
success = true,
data = result
});
}
public IActionResult OnGetRoleList()
{
long accountId = _authHelper.CurrentAccountId();
var workshopHash = User.FindFirstValue("WorkshopSlug");
var workshopId = _passwordHasher.SlugDecrypt(workshopHash);
if (workshopId <= 0)
return new JsonResult(new
{
success = false,
message = "کارگاه ای یافت نشد",
});
var result = _subAccountApplication.GetSubAccountRolesByAccountId(accountId);
return new JsonResult(new
{
success = true,
data = result
});
}
public IActionResult OnGetInfoAccount(string nationalCode)
{
List<long> workshopIds = _authHelper.CurrentAccountInfo().WorkshopList.Select(x => x.Id).ToList();
var resultData = _employeeApplication.GetEmployeeByNationalCodeIfHasActiveLeftWork(nationalCode, workshopIds);
return new JsonResult(new
{
success = true,
data = resultData
});
}
public IActionResult OnGetCreateAccount()
{
var command = new CreateSubAccount();
return Partial("ModalCreateSubAccount", command);
}
public IActionResult OnPostCreateAccount(CreateSubAccount command)
{
var authModel = _authHelper.CurrentAccountInfo();
command.AccountId = authModel.Id;
var workshopIds = authModel.WorkshopList.Select(x => x.Id).ToList();
OperationResult result = _subAccountApplication.Create(command, workshopIds);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message
});
}
public IActionResult OnGetEditAccount(long id)
{
var subAccount = _subAccountApplication.GetDetails(id);
var command = new SubAccountViewModel()
{
Id = id,
SubAccountFullName = subAccount.SubAccountFullName,
PhoneNumber = subAccount.PhoneNumber,
Username = subAccount.Username,
FName = subAccount.FName,
LName = subAccount.LName,
NationalCode = subAccount.NationalCode,
ProfilePhoto = subAccount.ProfilePhoto,
SubAccountRoleId = subAccount.SubAccountRoleId,
SubAccountWorkshops = subAccount.SubAccountWorkshops
};
return Partial("ModalEditSubAccount", command);
}
public IActionResult OnPostEditAccount(EditSubAccount command)
{
//command.AccountId = _authHelper.CurrentAccountId();
var workshopIds = _authHelper.CurrentAccountInfo().WorkshopList.Select(x => x.Id).ToList();
OperationResult result = _subAccountApplication.EditSubAccount(command, workshopIds);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message
});
}
public IActionResult OnPostRemoveAccount(long id)
{
OperationResult result = _subAccountApplication.Delete(id);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message
});
}
#region Sub Account
public IActionResult OnPostSendSms()
{
var accountInfo = _authHelper.CurrentAccountInfo();
if (accountInfo.SubAccountId == 0)
{
var accountId = accountInfo.Id;
var result = _accountApplication.Search(new AccountSearchModel() { Id = accountId }).FirstOrDefault();
if (result != null)
{
_accountApplication.SendVerifyCodeToChangingPass(result.Mobile, accountId);
return new JsonResult(new
{
isSuccess = true
});
}
}
else
{
var result = _subAccountApplication.GetDetails(accountInfo.SubAccountId);
if (result != null && result != default)
{
_subAccountApplication.SendVerifyCodeForPasswordChange(result.PhoneNumber, accountInfo.SubAccountId);
return new JsonResult(new
{
isSuccess = true
});
}
}
return new JsonResult(new
{
isSuccess = false
});
}
public IActionResult OnPostCheckCode(string code)
{
var accountInfo = _authHelper.CurrentAccountInfo();
if (accountInfo.SubAccountId == 0)
{
var accountId = accountInfo.Id;
var result = _accountApplication.Search(new AccountSearchModel() { Id = accountId }).FirstOrDefault();
var verfiyResult = _accountApplication.GetByVerifyCode(code, result.Mobile);
if (verfiyResult != null)
{
return new JsonResult(new
{
exist = true,
});
}
}
else
{
var subAccountId = accountInfo.SubAccountId;
var result = _subAccountApplication.GetDetails(accountInfo.SubAccountId);
if (result != null && result != default)
{
var verfiyResult = _subAccountApplication.GetByVerifyCodeAndPhoneNumber(code, result.PhoneNumber);
if (verfiyResult != null)
{
return new JsonResult(new
{
exist = true,
});
}
}
}
return new JsonResult(new
{
exist = false,
});
}
public IActionResult OnGetEmployeeSubAccounts(int pageIndex)
{
var accountId = _authHelper.CurrentAccountId();
var result = _subAccountApplication.GetAllByAccountId(accountId, pageIndex);
return new JsonResult(new
{
success = true,
data = result,
pageIndex = result.Count
});
}
public IActionResult OnGetEditEmployeeSubAccounts(long subAccountId)
{
var subAccount = _subAccountApplication.GetDetails(subAccountId);
var command = new SubAccountViewModel()
{
Id = subAccountId,
SubAccountFullName = subAccount.SubAccountFullName,
PhoneNumber = subAccount.PhoneNumber,
Username = subAccount.Username
};
return Partial("ModalEditSubAccountPasswordAndPhone", command);
}
public IActionResult OnPostEditEmployeeSubAccounts(SubAccountChangePasswordAndPhoneNumber command)
{
var result = _subAccountApplication.ChangePasswordAndPhoneNumber(command);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message
});
}
public IActionResult OnPostSubAccountChangeStatus(long id, string type)
{
OperationResult result = type == "active" ? _subAccountApplication.Activate(id) : _subAccountApplication.Deactivate(id);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message
});
}
public IActionResult OnPostCheckSubAccountValidToChanging(long accountId, string phoneNumber, string password, string rePassword)
{
var result = _subAccountApplication.IsPhoneNumberAndPasswordValid(accountId, phoneNumber, password, rePassword);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message,
});
}
#endregion
}
}