Files
Backend-Api/ServiceHost/Areas/Client/Pages/Company/RollCall/CameraAccounts/Index.cshtml.cs
2025-05-05 14:03:33 +03:30

264 lines
7.5 KiB
C#

using _0_Framework.Application;
using AccountManagement.Application.Contracts.Account;
using AccountManagement.Application.Contracts.CameraAccount;
using AccountManagement.Application.Contracts.SubAccount;
using CompanyManagment.App.Contracts.Workshop;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Security.Claims;
using _0_Framework.Infrastructure;
using CompanyManagment.App.Contracts.CustomizeWorkshopSettings;
using CompanyManagment.App.Contracts.RollCall;
using CompanyManagment.Application;
using System.Transactions;
namespace ServiceHost.Areas.Client.Pages.Company.RollCall.CameraAccounts
{
[Authorize]
[NeedsPermission(SubAccountPermissionHelper.CameraAccountSettingsPermissionCode)]
public class IndexModel : PageModel
{
public string Mess { get; set; }
private readonly IAuthHelper _authHelper;
private readonly IAccountApplication _accountApplication;
private readonly IWorkshopApplication _workshopApplication;
private readonly IPasswordHasher _passwordHasher;
private readonly ICameraAccountApplication _cameraAccountApplication;
private readonly IHttpContextAccessor _contextAccessor;
private readonly ISubAccountApplication _subAccountApplication;
private readonly long _workshopId;
public string WorkshopFullName;
public bool HasCamera;
public IndexModel(IAuthHelper authHelper, IPasswordHasher passwordHasher, ICameraAccountApplication cameraAccountApplication, IAccountApplication accountApplication, IWorkshopApplication workshopApplication, IHttpContextAccessor contextAccessor, ISubAccountApplication subAccountApplication)
{
_authHelper = authHelper;
_passwordHasher = passwordHasher;
_cameraAccountApplication = cameraAccountApplication;
_accountApplication = accountApplication;
_workshopApplication = workshopApplication;
_contextAccessor = contextAccessor;
_subAccountApplication = subAccountApplication;
var workshopHash = _contextAccessor.HttpContext?.User.FindFirstValue("WorkshopSlug");
_workshopId = _passwordHasher.SlugDecrypt(workshopHash);
if (_workshopId < 1)
throw new InvalidDataException("اختلال در کارگاه");
}
public void OnGet()
{
var accountId = _authHelper.CurrentAccountId();
WorkshopFullName = _workshopApplication.GetDetails(_workshopId).WorkshopFullName;
HasCamera = _cameraAccountApplication.HasCameraAccount(_workshopId, accountId);
}
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,
});
}
#region Create Camera
public IActionResult OnGetCreateCameraAccount()
{
var command = new CreateCameraAccount();
command.WorkshopName = _workshopApplication.GetWorkshopFullname(_workshopId);
return Partial("_Partials/ModalCreateAccountSetting", command);
}
public IActionResult OnPostCreateCameraAccount(CreateCameraAccount command)
{
var createAccountCommand = new CreateCameraAccount()
{
WorkshopId = _workshopId,
AccountId = _authHelper.CurrentAccountId(),
Username = command.Username,
Password = command.Password,
RePassword = command.RePassword,
WorkshopName = _workshopApplication.GetDetails(_workshopId).WorkshopFullName,
IsActiveString = "true",
};
var result = _cameraAccountApplication.Create(createAccountCommand);
return new JsonResult(new
{
Success = result.IsSuccedded,
message = result.Message,
});
}
#endregion
#region Camera Account
public IActionResult OnGetCameraAccounts()
{
var authModel = _authHelper.CurrentAccountInfo();
List<(long Id, string Name)> workshops = authModel.WorkshopList.Select(x => (x.Id, x.Name)).ToList();
var result = _cameraAccountApplication.GetAllByWorkshopIdAndAccountId(authModel.Id, workshops);
return new JsonResult(new
{
success = true,
data = result
});
}
public IActionResult OnGetChangeCameraPassword(long id)
{
var camera = _cameraAccountApplication.GetDetails(id);
var command = new CameraAccountViewModel()
{
Id = id,
Username = camera.Username,
WorkshopName = camera.WorkshopName,
};
return Partial("_Partials/ModalCameraAccountChangePassword", command);
}
public IActionResult OnPostChangeCameraPassword(ChangePassword command)
{
var result = _cameraAccountApplication.ChangePass(command);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message
});
}
public IActionResult OnGetCameraValidation(string password, string rePassword)
{
if (string.IsNullOrWhiteSpace(password) || string.IsNullOrWhiteSpace(rePassword))
{
return new JsonResult(new
{
success = false,
message = "رمز عبور و تکرار آن نباید خالی باشد"
});
}
if (password != rePassword)
{
return new JsonResult(new
{
success = false,
message = "رمز عبور یکسان نیست",
});
}
if (password.Length < 8)
{
return new JsonResult(new
{
success = false,
message = "رمز عبور نمی تواند کمتر از 8 کاراکتر باشد",
});
}
return new JsonResult(new
{
success = true,
});
}
public IActionResult OnPostCameraAccountChangeStatus(long id, string type)
{
OperationResult result = type == "active" ? _cameraAccountApplication.Active(id) : _cameraAccountApplication.DeActive(id);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message
});
}
#endregion
public IActionResult OnGetCheckAccount(string username)
{
var result = _cameraAccountApplication.CheckUsername(username);
return new JsonResult(new
{
Success = result.IsSuccedded,
message = result.IsSuccedded ? "نام کاربری با مورد تاییداست" : result.Message,
});
}
}
}