SHA256
GuestLog - Improvements to GuestDocument creation
- Added DocumentNumber validations. - Added new page to see GuestDocument details.
This commit is contained in:
@@ -15,29 +15,31 @@
|
||||
<MudPaper Class="pa-4" Elevation="3" Height="100%">
|
||||
<MudText Typo="Typo.subtitle1" Align="Align.Center">@LanguageLocale.control_GuestDocForm_sectionDocument</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle" Class="mb-4" />
|
||||
<MudStack Row>
|
||||
<MudStack Row style="@FieldMarginY">
|
||||
<MudSelect @bind-Value="Model.DocumentType" For="@(() => Model.DocumentType)" Label="@LanguageLocale.dto_GuestDocument_DocumentType"
|
||||
Required>
|
||||
Required ReadOnly="true">
|
||||
@foreach (string docType in dataListService.GetDataListByName(GuestConsts.DataList_DocumentTypes))
|
||||
{
|
||||
<MudSelectItem Value="docType">@dataListService.GetLocalizedValue(GuestConsts.DataList_DocumentTypes, docType)</MudSelectItem>
|
||||
}
|
||||
</MudSelect>
|
||||
<MudTextField @bind-Value="Model.DocumentNum" For="@(() => Model.DocumentNum)" Label="@LanguageLocale.dto_GuestDocument_DocumentNum"
|
||||
Immediate="true" Required />
|
||||
Immediate="true" Required ReadOnly="true" />
|
||||
</MudStack>
|
||||
|
||||
<MudStack Row>
|
||||
<MudStack Row style="@FieldMarginY">
|
||||
<MudDatePicker @bind-Date="Model.IssueDate" For="@(() => Model.IssueDate)" Label="@LanguageLocale.dto_GuestDocument_IssueDate"
|
||||
Clearable OpenTo="OpenTo.Year" Editable />
|
||||
|
||||
<MudTextField @bind-Value="Model.SupportNum" For="@(() => Model.SupportNum)" Label="@LanguageLocale.dto_GuestDocument_SupportNum"
|
||||
Immediate="true" Required="@isSuportNumberRequired" />
|
||||
Immediate="true" Required="@isSuportNumberRequired" Mask="@(new PatternMask("aaa000000"))"
|
||||
OnBlur="@(() => Model.SupportNum = Model.SupportNum?.ToUpper())" />
|
||||
</MudStack>
|
||||
|
||||
<MudAutocomplete T="string" @bind-Value="Model.Nationality" For="@(() => Model.Nationality)" Label="@LanguageLocale.dto_GuestDocument_Nationality"
|
||||
SearchFunc="@SearchCountryCodes" ToStringFunc="@(e=> string.IsNullOrEmpty(e) ? "" : @dataListService.GetLocalizedValue(GuestConsts.DataList_CountryCodes, e))"
|
||||
SelectValueOnTab="true" AdornmentIcon="@Icons.Material.Filled.Search" />
|
||||
SelectValueOnTab="true" AdornmentIcon="@Icons.Material.Filled.Search"
|
||||
style="@FieldMarginY" />
|
||||
</MudPaper>
|
||||
</MudItem>
|
||||
<MudItem xs="12" md="6">
|
||||
@@ -45,13 +47,17 @@
|
||||
<MudText Typo="Typo.subtitle1" Align="Align.Center">@LanguageLocale.control_GuestDocForm_sectionGuest</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle" Class="mb-4" />
|
||||
|
||||
<MudTextField @bind-Value="Model.Name" For="@(() => Model.Name)" Immediate="true" Label="@LanguageLocale.dto_GuestDocument_Name" />
|
||||
<MudTextField @bind-Value="Model.Surname" For="@(() => Model.Surname)" Immediate="true"
|
||||
Label="@LanguageLocale.dto_GuestDocument_Surname" />
|
||||
<MudTextField @bind-Value="Model.Surname2" For="@(() => Model.Surname2)" Immediate="true"
|
||||
Label="Surname2" />
|
||||
<MudTextField @bind-Value="Model.Name" For="@(() => Model.Name)"
|
||||
Label="@LanguageLocale.dto_GuestDocument_Name"
|
||||
Immediate="true" OnBlur="@(() => Model.Name = Model.Name?.ToUpper())" />
|
||||
<MudTextField @bind-Value="Model.Surname" For="@(() => Model.Surname)"
|
||||
Label="@LanguageLocale.dto_GuestDocument_Surname"
|
||||
Immediate="true" OnBlur="@(() => Model.Surname = Model.Surname?.ToUpper())" />
|
||||
<MudTextField @bind-Value="Model.Surname2" For="@(() => Model.Surname2)"
|
||||
Label="@LanguageLocale.dto_GuestDocument_Surname2"
|
||||
Immediate="true" OnBlur="@(() => Model.Surname2 = Model.Surname2?.ToUpper())" />
|
||||
|
||||
<MudStack Row>
|
||||
<MudStack Row style="@FieldMarginY">
|
||||
<MudDatePicker @bind-Date="Model.BirthDate" For="@(() => Model.BirthDate)" Label="@LanguageLocale.dto_GuestDocument_BirthDate"
|
||||
Clearable OpenTo="OpenTo.Year" Editable />
|
||||
|
||||
@@ -71,10 +77,12 @@
|
||||
<MudDivider DividerType="DividerType.Middle" Class="mb-4" />
|
||||
|
||||
<MudStack Row>
|
||||
<MudTextField @bind-Value="Model.PhoneNumber" For="@(() => Model.PhoneNumber)" Immediate="true"
|
||||
Label="@LanguageLocale.dto_GuestDocument_PhoneNumber" />
|
||||
<MudTextField @bind-Value="Model.EmailAddress" For="@(() => Model.EmailAddress)" Immediate="true"
|
||||
Label="@LanguageLocale.dto_GuestDocument_EmailAddress" />
|
||||
<MudTextField @bind-Value="Model.PhoneNumber" For="@(() => Model.PhoneNumber)"
|
||||
Label="@LanguageLocale.dto_GuestDocument_PhoneNumber"
|
||||
InputType="InputType.Telephone" Immediate="true" />
|
||||
<MudTextField @bind-Value="Model.EmailAddress" For="@(() => Model.EmailAddress)"
|
||||
Label="@LanguageLocale.dto_GuestDocument_EmailAddress"
|
||||
InputType="InputType.Email" Immediate="true" />
|
||||
</MudStack>
|
||||
</MudPaper>
|
||||
</MudItem>
|
||||
@@ -85,16 +93,17 @@
|
||||
<MudDivider DividerType="DividerType.Middle" Class="mb-4" />
|
||||
|
||||
<MudTextField @bind-Value="Model.Address" For="@(() => Model.Address)" Immediate="true" Label="@LanguageLocale.dto_GuestDocument_Address" />
|
||||
<MudStack Row>
|
||||
<MudTextField @bind-Value="Model.AddressCity" For="@(() => Model.AddressCity)" Immediate="true"
|
||||
Label="@LanguageLocale.dto_GuestDocument_AddressCity" />
|
||||
<MudTextField @bind-Value="Model.AddressProvince" For="@(() => Model.AddressProvince)" Immediate="true"
|
||||
Label="@LanguageLocale.dto_GuestDocument_AddressProvince" />
|
||||
<MudTextField @bind-Value="Model.AddressState" For="@(() => Model.AddressState)" Immediate="true"
|
||||
Label="@LanguageLocale.dto_GuestDocument_AddressState" />
|
||||
<MudStack Row style="@FieldMarginY">
|
||||
<MudTextField @bind-Value="Model.AddressCity" For="@(() => Model.AddressCity)"
|
||||
Label="@LanguageLocale.dto_GuestDocument_AddressCity" Immediate="true" />
|
||||
<MudTextField @bind-Value="Model.AddressProvince" For="@(() => Model.AddressProvince)"
|
||||
Label="@LanguageLocale.dto_GuestDocument_AddressProvince" Immediate="true" />
|
||||
<MudTextField @bind-Value="Model.AddressState" For="@(() => Model.AddressState)"
|
||||
Label="@LanguageLocale.dto_GuestDocument_AddressState" Immediate="true" />
|
||||
</MudStack>
|
||||
<MudStack Row>
|
||||
<MudAutocomplete T="string" @bind-Value="Model.AddressCountry" For="@(() => Model.AddressCountry)" Label="@LanguageLocale.dto_GuestDocument_AddressCountry"
|
||||
<MudStack Row style="@FieldMarginY">
|
||||
<MudAutocomplete T="string" @bind-Value="Model.AddressCountry" For="@(() => Model.AddressCountry)"
|
||||
Label="@LanguageLocale.dto_GuestDocument_AddressCountry"
|
||||
SearchFunc="@SearchCountryCodes" ToStringFunc="@(e=> string.IsNullOrEmpty(e) ? "" : @dataListService.GetLocalizedValue(GuestConsts.DataList_CountryCodes, e))"
|
||||
SelectValueOnTab="true" AdornmentIcon="@Icons.Material.Filled.Search" />
|
||||
|
||||
@@ -117,9 +126,10 @@
|
||||
|
||||
|
||||
private MudForm _form = null!;
|
||||
private const string FieldMarginY = "margin-block: 0.5em";
|
||||
private Dictionary<string, string>? countryDict = null;
|
||||
|
||||
private bool isSuportNumberRequired { get => Model?.Nationality == "ESP"; }
|
||||
private bool isSuportNumberRequired { get => Model?.Nationality == GuestConsts.CountryCode_ESP; }
|
||||
private string CreateTimeString
|
||||
{
|
||||
get
|
||||
@@ -146,11 +156,13 @@
|
||||
private async Task<IEnumerable<string>> SearchCountryCodes(string value, CancellationToken token)
|
||||
{
|
||||
// if text is null or empty, show complete list
|
||||
if (string.IsNullOrEmpty(value)) {
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return new string[0];
|
||||
}
|
||||
|
||||
if(countryDict is null || countryDict.Count == 0) {
|
||||
if (countryDict is null || countryDict.Count == 0)
|
||||
{
|
||||
var countryCodeSet = dataListService.GetDataListByName(GuestConsts.DataList_CountryCodes);
|
||||
countryDict = new(countryCodeSet.Count);
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
@attribute [Route($"{GuestConsts.Page_GuestDocs_Detail}")]
|
||||
|
||||
@using Somnia.GuestLog.Models.DTOs;
|
||||
@using Somnia.GuestLog.Services;
|
||||
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IDialogService DialogService
|
||||
@inject NavigationManager Navigation
|
||||
@inject IGuestDocumentService guestDocService
|
||||
|
||||
|
||||
@inject FluentValidation.IValidator<GuestDocIdDTO> docIdValidator;
|
||||
|
||||
|
||||
<PageTitle>Somnia - Guest Docs - Details</PageTitle>
|
||||
|
||||
<MudStack Row="true" Class="my-4">
|
||||
<MudText Typo="Typo.h4" GutterBottom="true">Guest Document - "@docId"</MudText>
|
||||
<MudSpacer />
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Success" Size="Size.Small" StartIcon="@Icons.Material.Filled.Add"
|
||||
OnClick="() => BtnEdit()">
|
||||
@LanguageLocale.general_btnEdit
|
||||
</MudButton>
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Default" Size="Size.Small"
|
||||
StartIcon="@Icons.Material.Filled.ArrowBack" OnClick="() => BtnReturn()">
|
||||
@LanguageLocale.general_btnReturn
|
||||
</MudButton>
|
||||
</MudStack>
|
||||
|
||||
@if (documentModel is not null)
|
||||
{
|
||||
<GuestDocForm @ref="_form" Model="documentModel" ReadOnly="true" />
|
||||
}
|
||||
|
||||
@code {
|
||||
|
||||
[Parameter]
|
||||
public string? docId { get; set; }
|
||||
|
||||
GuestDocForm _form = null!;
|
||||
|
||||
GuestDocumentDTO? documentModel = null;
|
||||
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (docId is null)
|
||||
{
|
||||
Snackbar.Add("No GuestDocument provided!!", Severity.Error);
|
||||
Navigation.NotFound();
|
||||
return;
|
||||
}
|
||||
|
||||
GuestDocIdDTO? docIdModel = GuestDocIdDTO.DecodeFromString(docId);
|
||||
if (docIdModel is null)
|
||||
{
|
||||
Snackbar.Add($"Provided GuestDocId can't be decoded: '{docId}'", Severity.Error);
|
||||
Navigation.NotFound();
|
||||
return;
|
||||
}
|
||||
|
||||
//Get model from db.
|
||||
documentModel = await guestDocService.GetDtoByIdAsync(docIdModel.DocumentType, docIdModel.DocumentNum);
|
||||
if (documentModel is null)
|
||||
{
|
||||
Snackbar.Add($"GuestDocument not found with ID: '{docId}'", Severity.Error);
|
||||
Navigation.NotFound();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void BtnEdit()
|
||||
{
|
||||
Snackbar.Add("Not implemented yet!", Severity.Info);
|
||||
}
|
||||
|
||||
private void BtnReturn()
|
||||
{
|
||||
Navigation.NavigateTo(GuestConsts.Page_GuestDocs_List);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,16 @@
|
||||
@attribute [Route($"{GuestConsts.Page_GuestDocs_New}")]
|
||||
|
||||
@using Microsoft.EntityFrameworkCore;
|
||||
@using Somnia.GuestLog.Models.DTOs;
|
||||
@using Somnia.GuestLog.Infrastructure;
|
||||
@using Somnia.GuestLog.Services;
|
||||
@using Somnia.GuestLog.Validators;
|
||||
|
||||
@inject IDbContextFactory<GuestLogContext> contextFactory
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IDialogService DialogService
|
||||
@inject NavigationManager Navigation
|
||||
@inject IGuestDocumentService guestDocService
|
||||
@inject IDataListService dataListService;
|
||||
|
||||
@inject FluentValidation.IValidator<GuestDocIdDTO> docIdValidator;
|
||||
|
||||
|
||||
<PageTitle>Somnia - Guest Docs - New</PageTitle>
|
||||
@@ -26,16 +27,98 @@
|
||||
@LanguageLocale.general_btnReturn
|
||||
</MudButton>
|
||||
</MudStack>
|
||||
<GuestDocForm @ref="_form" />
|
||||
|
||||
@if (currentPageMode == ePageMode.selectDocId || currentPageMode == ePageMode.alreadyExistDoc)
|
||||
{
|
||||
<MudPaper Class="pa-4" Elevation="3" Height="100%" Style="">
|
||||
<MudForm @ref="_docIdForm" Model="@currentDocId"
|
||||
Validation="@((docIdValidator as GuestDocIdDTOValidator)!.ValidateValue)">
|
||||
<MudStack Breakpoint="Breakpoint.Sm">
|
||||
<MudSelect @bind-Value="currentDocId.DocumentType" For="@(() => currentDocId.DocumentType)"
|
||||
Label="@LanguageLocale.dto_GuestDocument_DocumentType" Immediate="true" Required>
|
||||
|
||||
@foreach (string docType in dataListService.GetDataListByName(GuestConsts.DataList_DocumentTypes))
|
||||
{
|
||||
<MudSelectItem Value="docType">
|
||||
@dataListService.GetLocalizedValue(GuestConsts.DataList_DocumentTypes, docType)
|
||||
</MudSelectItem>
|
||||
}
|
||||
|
||||
</MudSelect>
|
||||
<MudTextField @bind-Value="currentDocId.DocumentNum" For="@(() => currentDocId.DocumentNum)"
|
||||
Label="@LanguageLocale.dto_GuestDocument_DocumentNum" Required
|
||||
OnBlur="@(() => currentDocId.DocumentNum = currentDocId.DocumentNum.ToUpper())" />
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary" Size="Size.Small"
|
||||
StartIcon="@Icons.Material.Filled.Search" OnClick="() => BtnCheckDocId()" Disabled="@loadingDocId">
|
||||
@LanguageLocale.general_btnSearch
|
||||
</MudButton>
|
||||
</MudStack>
|
||||
</MudForm>
|
||||
|
||||
@if (currentPageMode == ePageMode.alreadyExistDoc)
|
||||
{
|
||||
<MudStack>
|
||||
<MudDivider Class="my-4" />
|
||||
<MudText Typo="Typo.body1" Style="text-align: center;">@LanguageLocale.page_GuestDocNew_msgAlreadyExists
|
||||
</MudText>
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary" Size="Size.Small"
|
||||
StartIcon="@Icons.Material.Filled.Search" OnClick="() => BtnGoToDocument()">
|
||||
@LanguageLocale.page_GuestDocNew_btnGoExistingDoc
|
||||
</MudButton>
|
||||
</MudStack>
|
||||
}
|
||||
</MudPaper>
|
||||
}
|
||||
|
||||
@if (currentPageMode == ePageMode.createNew)
|
||||
{
|
||||
<GuestDocForm @ref="_form" Model="newDocumentModel" />
|
||||
}
|
||||
|
||||
|
||||
@code {
|
||||
|
||||
GuestDocForm _form = null!;
|
||||
MudForm _docIdForm = null!;
|
||||
|
||||
ePageMode currentPageMode = ePageMode.selectDocId;
|
||||
GuestDocIdDTO currentDocId = new() { DocumentNum = "", DocumentType = "" };
|
||||
GuestDocumentDTO newDocumentModel = new();
|
||||
|
||||
bool loadingDocId = false;
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
/// <summary>
|
||||
/// Check if given Document already exists or can be created.
|
||||
/// </summary>
|
||||
private async Task BtnCheckDocId()
|
||||
{
|
||||
await _docIdForm.ValidateAsync();
|
||||
if (!_docIdForm.IsValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
loadingDocId = true;
|
||||
GuestDocumentDTO? dto = await guestDocService.GetDtoByIdAsync(currentDocId.DocumentType, currentDocId.DocumentNum);
|
||||
loadingDocId = false;
|
||||
|
||||
if (dto is null)
|
||||
{
|
||||
//Document don't exist -> Create it.
|
||||
currentPageMode = ePageMode.createNew;
|
||||
|
||||
//Update model with current DocId
|
||||
newDocumentModel.DocumentType = currentDocId.DocumentType;
|
||||
newDocumentModel.DocumentNum = currentDocId.DocumentNum;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//GuestDoc already exists!
|
||||
currentPageMode = ePageMode.alreadyExistDoc;
|
||||
}
|
||||
|
||||
private void BtnReturn()
|
||||
{
|
||||
Navigation.NavigateTo(GuestConsts.Page_GuestDocs_List);
|
||||
@@ -69,6 +152,27 @@
|
||||
Navigation.NavigateTo(GuestConsts.Page_GuestDocs_List);
|
||||
}
|
||||
|
||||
private void BtnGoToDocument()
|
||||
{
|
||||
if (currentPageMode != ePageMode.alreadyExistDoc)
|
||||
{
|
||||
Snackbar.Add("Invalid operation!", Severity.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
string docId = GuestDocIdDTO.EncodeAsString(currentDocId);
|
||||
string uri = GuestConsts.Page_GuestDocs_Detail.Replace("{docId}", docId);
|
||||
Navigation.NavigateTo(uri);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Models
|
||||
|
||||
enum ePageMode
|
||||
{
|
||||
selectDocId, alreadyExistDoc, createNew
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ public static class GuestConsts
|
||||
|
||||
public const string Page_GuestDocs_List = "/guest-documents";
|
||||
public const string Page_GuestDocs_New = "/guest-documents/new";
|
||||
public const string Page_GuestDocs_Detail = "/guest-documents/d/{docId}";
|
||||
|
||||
|
||||
public const string regexPattern_phoneNum = @"\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}";
|
||||
@@ -13,4 +14,11 @@ public static class GuestConsts
|
||||
public const string DataList_DocumentTypes = "DocumentType";
|
||||
public const string DataList_GenderOptions = "GenderOption";
|
||||
public const string DataList_CountryCodes = "CountryCode";
|
||||
|
||||
|
||||
public const string CountryCode_ESP = "ESP";
|
||||
|
||||
public const string DocumentType_DNI = "DNI";
|
||||
public const string DocumentType_NIE = "NIE";
|
||||
public const string DocumentType_PASSPORT = "PASSPORT";
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace Somnia.GuestLog.Models.DTOs;
|
||||
|
||||
public class GuestDocIdDTO
|
||||
{
|
||||
public required string DocumentType { get; set; }
|
||||
public required string DocumentNum { get; set; }
|
||||
|
||||
|
||||
public static string EncodeAsString(GuestDocIdDTO docId)
|
||||
{
|
||||
return $"{docId.DocumentType}-{docId.DocumentNum}";
|
||||
}
|
||||
|
||||
public static GuestDocIdDTO? DecodeFromString(string str)
|
||||
{
|
||||
var parts = str.Split("-");
|
||||
if (parts.Length < 2)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
DocumentType = parts[0],
|
||||
DocumentNum = parts[1]
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ builder.Services.AddRazorComponents()
|
||||
|
||||
// Add Validation Services
|
||||
builder.Services.AddSingleton<IValidator<GuestDocumentDTO>, GuestDocumentDTOValidator>();
|
||||
builder.Services.AddSingleton<IValidator<GuestDocIdDTO>, GuestDocIdDTOValidator>();
|
||||
|
||||
// Add Services
|
||||
builder.Services.AddSingleton<IGuestDocumentService, GuestDocumentService>();
|
||||
|
||||
@@ -149,11 +149,18 @@
|
||||
<data name="general.confirmDeleteMsg" xml:space="preserve"><value>Deleting can not be undone!</value></data>
|
||||
<data name="general.btnDelete" xml:space="preserve"><value>Delete</value></data>
|
||||
<data name="general.btnCreate" xml:space="preserve"><value>Create</value></data>
|
||||
<data name="general.btnEdit" xml:space="preserve"><value>Edit</value></data>
|
||||
<data name="general.btnReturn" xml:space="preserve"><value>Return</value></data>
|
||||
<data name="general.btnClose" xml:space="preserve"><value>Close</value></data>
|
||||
<data name="general.btnSearch" xml:space="preserve"><value>Search</value></data>
|
||||
<data name="general.btnSave" xml:space="preserve"><value>Save</value></data>
|
||||
<data name="general.btnCancel" xml:space="preserve"><value>Cancel</value></data>
|
||||
|
||||
<data name="page.GuestDocList.btnNewGuest" xml:space="preserve"><value>New Guest</value></data>
|
||||
|
||||
<data name="page.GuestDocNew.msgAlreadyExists" xml:space="preserve"><value>This Guest Document Already exists!!</value></data>
|
||||
<data name="page.GuestDocNew.btnGoExistingDoc" xml:space="preserve"><value>Go to existing document</value></data>
|
||||
|
||||
<data name="control.GuestDocForm.sectionDocument" xml:space="preserve"><value>Document Details</value></data>
|
||||
<data name="control.GuestDocForm.sectionGuest" xml:space="preserve"><value>Guest Details</value></data>
|
||||
<data name="control.GuestDocForm.sectionContact" xml:space="preserve"><value>Contact Details</value></data>
|
||||
|
||||
@@ -51,9 +51,9 @@ public class StaticDataListService(
|
||||
#region DataList Dicts
|
||||
|
||||
private static readonly HashSet<string> _documentTypeSet = [
|
||||
"DNI",
|
||||
"NIE",
|
||||
"PASSPORT",
|
||||
GuestConsts.DocumentType_DNI,
|
||||
GuestConsts.DocumentType_NIE,
|
||||
GuestConsts.DocumentType_PASSPORT,
|
||||
"ID",
|
||||
"OTHER",
|
||||
];
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
namespace Somnia.GuestLog.Utils;
|
||||
|
||||
public static class DNIUtils
|
||||
{
|
||||
#region Validators
|
||||
|
||||
public static bool IsValidDNI(string dni)
|
||||
{
|
||||
//Bypass validation if letter if 'Ñ'
|
||||
if (dni.Length > 1 && dni[^1] == 'Ñ')
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (dni.Length != 9 || !RegexUtils.RegexDniFormat().IsMatch(dni))
|
||||
{
|
||||
return false;
|
||||
// throw new InvalidOperationException($"DNI with invalid format!! Value: '{dni}'");
|
||||
}
|
||||
|
||||
char calculatedLetter = CalculateLetterDniFromDigits(dni[..8]);
|
||||
|
||||
return calculatedLetter == dni[8];
|
||||
}
|
||||
|
||||
public static bool IsValidNIE(string nie)
|
||||
{
|
||||
//Bypass validation if letter if 'Ñ'
|
||||
if (nie.Length > 1 && nie[^1] == 'Ñ')
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (nie.Length != 9 || !RegexUtils.RegexNieFormat().IsMatch(nie))
|
||||
{
|
||||
return false;
|
||||
// throw new InvalidOperationException($"NIE with invalid format!! Value: '{nie}'");
|
||||
}
|
||||
|
||||
char calculatedLetter = CalculateLetterNieFromDigits(nie[..8]);
|
||||
|
||||
return calculatedLetter == nie[8];
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Letter Calculators
|
||||
|
||||
public static char CalculateLetterDniFromDigits(string dniDigits)
|
||||
{
|
||||
if (dniDigits.Length != 8)
|
||||
{
|
||||
throw new InvalidOperationException($"Invalid digit length! Received value '{dniDigits}' is not 8 digits long");
|
||||
}
|
||||
|
||||
return GetDniLetter(dniDigits.AsSpan());
|
||||
}
|
||||
|
||||
public static char CalculateLetterNieFromDigits(string dniDigits)
|
||||
{
|
||||
//Convert first letter to number.
|
||||
int firstLetterAsDigit = NiePrefixToDigit(dniDigits[0]);
|
||||
|
||||
string newDigits = firstLetterAsDigit + dniDigits[1..];
|
||||
|
||||
return CalculateLetterDniFromDigits(newDigits);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Internal Calculations
|
||||
|
||||
//Letters for remainder 0..22
|
||||
private const string Letters = "TRWAGMYFPDXBNJZSQVHLCKE";
|
||||
|
||||
|
||||
// NIE prefixes map to digits:
|
||||
// X -> 0, Y -> 1, Z -> 2 (so they become 0/1/2 + 7 digits for mod computation)
|
||||
private static int NiePrefixToDigit(char prefix)
|
||||
=> prefix switch
|
||||
{
|
||||
'X' or 'x' => 0,
|
||||
'Y' or 'y' => 1,
|
||||
'Z' or 'z' => 2,
|
||||
_ => throw new ArgumentException("Invalid NIE prefix. Expected X, Y, or Z.")
|
||||
};
|
||||
|
||||
private static char GetDniLetter(ReadOnlySpan<char> digits)
|
||||
{
|
||||
// Fast path: avoid allocating by parsing digits incrementally as a number mod 23.
|
||||
int mod = 0;
|
||||
for (int i = 0; i < digits.Length; i++)
|
||||
{
|
||||
char c = digits[i];
|
||||
// Optional validation: digits only
|
||||
if ((uint)(c - '0') > 9) throw new ArgumentException("Non-digit character found.");
|
||||
mod = (mod * 10 + (c - '0')) % 23;
|
||||
}
|
||||
|
||||
return Letters[mod];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Somnia.GuestLog.Utils;
|
||||
|
||||
public static partial class RegexUtils
|
||||
{
|
||||
[GeneratedRegex("^[0-9]{8}[A-Z]$")]
|
||||
public static partial Regex RegexDniFormat();
|
||||
|
||||
[GeneratedRegex("^[XYZ][0-9]{7}[A-Z]$")]
|
||||
public static partial Regex RegexNieFormat();
|
||||
|
||||
[GeneratedRegex("^[A-Z]{3}[0-9]{6}$")]
|
||||
public static partial Regex RegexGuestDocSuportNumber();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using FluentValidation;
|
||||
using Somnia.GuestLog.Models.DTOs;
|
||||
using Somnia.GuestLog.Services;
|
||||
|
||||
namespace Somnia.GuestLog.Validators;
|
||||
|
||||
public static class CustomValidators
|
||||
{
|
||||
public static IRuleBuilderOptions<T, IList<TElement>> ListMustContainFewerThan<T, TElement>(this IRuleBuilder<T, IList<TElement>> ruleBuilder, int num)
|
||||
{
|
||||
return ruleBuilder.Must(list => list.Count < num).WithMessage("The list contains too many items");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using FluentValidation;
|
||||
using Somnia.GuestLog.Models.DTOs;
|
||||
using Somnia.GuestLog.Services;
|
||||
using Somnia.GuestLog.Utils;
|
||||
|
||||
namespace Somnia.GuestLog.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// Validator for GuestDocumentDTO which contains multiple rules and can be shared with the back end API
|
||||
/// </summary>
|
||||
public class GuestDocIdDTOValidator : AbstractValidator<GuestDocIdDTO>
|
||||
{
|
||||
public GuestDocIdDTOValidator(IDataListService dataListService)
|
||||
{
|
||||
RuleFor(x => x.DocumentType)
|
||||
.NotEmpty()
|
||||
.Must(val => val != null && dataListService.IsValueInList(GuestConsts.DataList_DocumentTypes, val));
|
||||
|
||||
RuleFor(x => x.DocumentNum)
|
||||
.NotEmpty()
|
||||
.Length(6, 32);
|
||||
|
||||
RuleFor(x => x.DocumentNum)
|
||||
.Cascade(CascadeMode.Stop)
|
||||
.NotEmpty()
|
||||
.Must(num => DNIUtils.IsValidDNI(num))
|
||||
.WithMessage("Invalid DNI")
|
||||
.When(x => x.DocumentType == GuestConsts.DocumentType_DNI);
|
||||
|
||||
RuleFor(x => x.DocumentNum)
|
||||
.Cascade(CascadeMode.Stop)
|
||||
.NotEmpty()
|
||||
.Must(num => DNIUtils.IsValidNIE(num))
|
||||
.WithMessage("Invalid NIE")
|
||||
.When(x => x.DocumentType == GuestConsts.DocumentType_NIE);
|
||||
}
|
||||
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GuestDocIdDTO>.CreateWithOptions((GuestDocIdDTO)model, x =>
|
||||
x.IncludeProperties(propertyName)));
|
||||
return result.IsValid ? [] : result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using FluentValidation;
|
||||
using Somnia.GuestLog.Models.DTOs;
|
||||
using Somnia.GuestLog.Services;
|
||||
using Somnia.GuestLog.Utils;
|
||||
|
||||
namespace Somnia.GuestLog.Validators;
|
||||
|
||||
@@ -12,6 +13,7 @@ public class GuestDocumentDTOValidator : AbstractValidator<GuestDocumentDTO>
|
||||
public GuestDocumentDTOValidator(IDataListService dataListService)
|
||||
{
|
||||
RuleFor(x => x.DocumentType)
|
||||
.Cascade(CascadeMode.Stop)
|
||||
.NotEmpty()
|
||||
.Must(val => val != null && dataListService.IsValueInList(GuestConsts.DataList_DocumentTypes, val));
|
||||
|
||||
@@ -23,6 +25,12 @@ public class GuestDocumentDTOValidator : AbstractValidator<GuestDocumentDTO>
|
||||
.NotEmpty()
|
||||
.LessThanOrEqualTo(DateTime.Today);
|
||||
|
||||
RuleFor(x => x.SupportNum)
|
||||
.Cascade(CascadeMode.Stop)
|
||||
.NotEmpty()
|
||||
.Matches(RegexUtils.RegexGuestDocSuportNumber()).WithMessage("Invalid Format. Expected 'AAA000000'.")
|
||||
.When(x => x.Nationality == GuestConsts.CountryCode_ESP);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty()
|
||||
.Length(1, 100);
|
||||
@@ -36,6 +44,7 @@ public class GuestDocumentDTOValidator : AbstractValidator<GuestDocumentDTO>
|
||||
.LessThanOrEqualTo(DateTime.Today);
|
||||
|
||||
RuleFor(x => x.Nationality)
|
||||
.Cascade(CascadeMode.Stop)
|
||||
.NotEmpty()
|
||||
.Must(val => val != null && dataListService.IsValueInList(GuestConsts.DataList_CountryCodes, val));
|
||||
|
||||
@@ -53,12 +62,6 @@ public class GuestDocumentDTOValidator : AbstractValidator<GuestDocumentDTO>
|
||||
.NotEmpty()
|
||||
.Must(val => val != null && dataListService.IsValueInList(GuestConsts.DataList_CountryCodes, val));
|
||||
|
||||
// RuleFor(x => x.Email)
|
||||
// .Cascade(CascadeMode.Stop)
|
||||
// .NotEmpty()
|
||||
// .EmailAddress()
|
||||
// .MustAsync(async (value, _) => await IsUniqueAsync(value));
|
||||
|
||||
// RuleFor(x => x.CCNumber)
|
||||
// .NotEmpty()
|
||||
// .Length(1, 100)
|
||||
|
||||
Reference in New Issue
Block a user