fix BaseTakeSelfiePageModel

This commit is contained in:
jojo aquino 2025-02-25 21:25:22 +00:00
parent b313d21a11
commit 7da749777e

View File

@ -11,9 +11,11 @@ namespace EnotaryoPH.Web.Pages
public class BaseTakeSelfiePageModel : PageModel public class BaseTakeSelfiePageModel : PageModel
{ {
protected readonly NotaryoDBContext _notaryoDBContext; protected readonly NotaryoDBContext _notaryoDBContext;
private readonly ICurrentUserService _currentUserService;
private readonly ICompreFaceClient _compreFaceClient; private readonly ICompreFaceClient _compreFaceClient;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly ICurrentUserService _currentUserService;
private Transaction _transactionEntity;
private User _user;
public BaseTakeSelfiePageModel(NotaryoDBContext notaryoDBContext, ICurrentUserService currentUserService, ICompreFaceClient compreFaceClient, IConfiguration configuration) public BaseTakeSelfiePageModel(NotaryoDBContext notaryoDBContext, ICurrentUserService currentUserService, ICompreFaceClient compreFaceClient, IConfiguration configuration)
{ {
@ -26,15 +28,22 @@ namespace EnotaryoPH.Web.Pages
protected async Task<bool> PostAsync() protected async Task<bool> PostAsync()
{ {
var selfieImage = Convert.FromBase64String(SelfieBase64Image.Split(",")[1]) ?? []; var selfieImage = Convert.FromBase64String(SelfieBase64Image.Split(",")[1]) ?? [];
var identificationDocument = _notaryoDBContext.IdentificationDocuments.First(e => e.UserID == UserID); var identificationDocument = _notaryoDBContext.IdentificationDocuments.First(e => e.UserID == CurrentUser.UserID);
var faceMatches = await VerifySelfieAsync(selfieImage, identificationDocument); var faceMatches = await VerifySelfieAsync(selfieImage, identificationDocument);
var isMatchSuccess = faceMatches.Any(); var isMatchSuccess = faceMatches.Any();
if (isMatchSuccess) if (isMatchSuccess)
{ {
var signatory = _notaryoDBContext.TransactionSignatories.First(x => x.UserID == UserID); var signatory = _notaryoDBContext.TransactionSignatories.FirstOrDefault(x => x.UserID == CurrentUser.UserID);
signatory.Status = nameof(SignatoryStatus.FaceMatch); if (signatory != null)
_notaryoDBContext.Update(signatory); {
signatory.Status = nameof(SignatoryStatus.FaceMatch);
_notaryoDBContext.Update(signatory);
}
else if (!_user.Role.IsInList(nameof(UserType.Principal), nameof(UserType.SuperUser), nameof(UserType.Administrator)))
{
return false;
}
var selfieEntity = CreateOrUpdateSelfie(selfieImage); var selfieEntity = CreateOrUpdateSelfie(selfieImage);
if (_notaryoDBContext.Entry(selfieEntity).State == EntityState.Detached) if (_notaryoDBContext.Entry(selfieEntity).State == EntityState.Detached)
@ -50,32 +59,26 @@ namespace EnotaryoPH.Web.Pages
return isMatchSuccess; return isMatchSuccess;
} }
private Transaction _transactionEntity; private TransactionSelfie CreateOrUpdateSelfie(byte[] selfieImage)
private int TransactionID
{ {
get TransactionSelfie selfieEntity;
if (TransactionSelfie_UID == Guid.Empty)
{ {
if (_transactionEntity == null) selfieEntity = new TransactionSelfie
{ {
_transactionEntity = _notaryoDBContext.Transactions.AsNoTracking().First(x => x.Transaction_UID == Transaction_UID); CreatedOn = DateTime.UtcNow,
} TransactionSelfie_UID = Guid.CreateVersion7(DateTime.UtcNow),
return _transactionEntity?.TransactionID ?? 0; UserID = CurrentUser.UserID,
TransactionID = TransactionID
};
TransactionSelfie_UID = selfieEntity.TransactionSelfie_UID.Value;
} }
} else
private User _user;
private int UserID
{
get
{ {
if (_user == null) selfieEntity = _notaryoDBContext.TransactionSelfies.FirstOrDefault(e => e.TransactionSelfie_UID == TransactionSelfie_UID);
{
_user = _notaryoDBContext.Users.AsNoTracking().First(x => x.User_UID == _currentUserService.GetUser_UID());
}
return _user?.UserID ?? 0;
} }
selfieEntity.File = Convert.FromBase64String(SelfieBase64Image.Split(",")[1]);
return selfieEntity;
} }
private async Task<IEnumerable<FaceMatches>> VerifySelfieAsync(byte[] selfieImage, IdentificationDocument identificationDocument) private async Task<IEnumerable<FaceMatches>> VerifySelfieAsync(byte[] selfieImage, IdentificationDocument identificationDocument)
@ -103,35 +106,37 @@ namespace EnotaryoPH.Web.Pages
return faceMatches; return faceMatches;
} }
private TransactionSelfie CreateOrUpdateSelfie(byte[] selfieImage)
{
TransactionSelfie selfieEntity;
if (TransactionSelfie_UID == Guid.Empty)
{
selfieEntity = new TransactionSelfie
{
CreatedOn = DateTime.UtcNow,
TransactionSelfie_UID = Guid.CreateVersion7(DateTime.UtcNow),
UserID = UserID,
TransactionID = TransactionID
};
TransactionSelfie_UID = selfieEntity.TransactionSelfie_UID.Value;
}
else
{
selfieEntity = _notaryoDBContext.TransactionSelfies.FirstOrDefault(e => e.TransactionSelfie_UID == TransactionSelfie_UID);
}
selfieEntity.File = Convert.FromBase64String(SelfieBase64Image.Split(",")[1]);
return selfieEntity;
}
[BindProperty(SupportsGet = true)]
public Guid Transaction_UID { get; set; }
[BindProperty] [BindProperty]
public string SelfieBase64Image { get; set; } public string SelfieBase64Image { get; set; }
[BindProperty(SupportsGet = true)]
public Guid Transaction_UID { get; set; }
[BindProperty(SupportsGet = true)] [BindProperty(SupportsGet = true)]
public Guid TransactionSelfie_UID { get; set; } public Guid TransactionSelfie_UID { get; set; }
private User CurrentUser
{
get
{
if (_user == null)
{
_user = _notaryoDBContext.Users.AsNoTracking().First(x => x.User_UID == _currentUserService.GetUser_UID());
}
return _user ?? new();
}
}
private int TransactionID
{
get
{
if (_transactionEntity == null)
{
_transactionEntity = _notaryoDBContext.Transactions.AsNoTracking().First(x => x.Transaction_UID == Transaction_UID);
}
return _transactionEntity?.TransactionID ?? 0;
}
}
} }
} }