Files
Backend-Api/ServiceHost/Areas/Client/Pages/Company/RollCall/CameraAccounts.cshtml.cs

215 lines
6.6 KiB
C#

using System.Security.Claims;
using _0_Framework.Application;
using AccountManagement.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;
namespace ServiceHost.Areas.Client.Pages.Company.RollCall
{
[Authorize]
public class CameraAccountsModel : 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 CameraAccountsModel(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()
{
WorkshopFullName = _workshopApplication.GetDetails(_workshopId).WorkshopFullName;
}
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 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("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
}
}