201 lines
9.3 KiB
C#
201 lines
9.3 KiB
C#
using FluentValidation;
|
|
using FluentValidation.Results;
|
|
|
|
namespace NTP.Application.Grains.Registry;
|
|
|
|
/// <summary>
|
|
/// Server-side port of the checks the legacy pages did in JavaScript before calling register.cfc
|
|
/// (validateFields Required / Date / SpecialChars, manufacture-year range, masks). Pure: returns the failures,
|
|
/// <see cref="ThrowIfAny"/> raises a FluentValidation <see cref="ValidationException"/> (HTTP 400 with per-field errors).
|
|
/// </summary>
|
|
public static class RegistryValidation
|
|
{
|
|
public static void ThrowIfAny(IReadOnlyList<ValidationFailure> failures)
|
|
{
|
|
if (failures.Count > 0) throw new ValidationException(failures);
|
|
}
|
|
|
|
private static void Required(List<ValidationFailure> f, string field, object? value)
|
|
{
|
|
var empty = value switch
|
|
{
|
|
null => true,
|
|
string s => string.IsNullOrWhiteSpace(s),
|
|
_ => false
|
|
};
|
|
if (empty) f.Add(new ValidationFailure(field, RegistryMessages.RequiredFields));
|
|
}
|
|
|
|
private static void MaxLen(List<ValidationFailure> f, string field, string? value, int max)
|
|
{
|
|
if (value is not null && value.Trim().Length > max)
|
|
f.Add(new ValidationFailure(field, $"الحد الأقصى {max} خانة"));
|
|
}
|
|
|
|
private static void NoSpecial(List<ValidationFailure> f, string field, string? value)
|
|
{
|
|
if (RegistryRules.HasSpecialChars(value)) f.Add(new ValidationFailure(field, RegistryMessages.SpecialChars));
|
|
}
|
|
|
|
private static void Digits(List<ValidationFailure> f, string field, string? value, int max)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(value) && !RegistryRules.IsDigits(value.Trim(), max))
|
|
f.Add(new ValidationFailure(field, RegistryMessages.InvalidNumber));
|
|
}
|
|
|
|
// ------------------------------------------------------------------ truck (registerGrainsTruck.cfm saveTruck / editTruck)
|
|
|
|
/// <summary>
|
|
/// <paramref name="tcscac"/> = company of the truck (the form's company select; for an update the stored company).
|
|
/// Company 327 requires the full data set; every other company only the number (and axials on edit).
|
|
/// </summary>
|
|
public static IReadOnlyList<ValidationFailure> ValidateTruck(SaveTruckRequest r, long? tcscac, bool isUpdate, long badiyaScac, int currentYear)
|
|
{
|
|
var f = new List<ValidationFailure>();
|
|
if (!isUpdate)
|
|
{
|
|
Required(f, nameof(r.Tn), r.Tn);
|
|
Digits(f, nameof(r.Tn), r.Tn, 12);
|
|
}
|
|
Required(f, nameof(r.Tcscac), tcscac);
|
|
|
|
if (tcscac == badiyaScac)
|
|
{
|
|
Required(f, nameof(r.ChasesNumber), r.ChasesNumber);
|
|
Required(f, nameof(r.Axials), r.Axials);
|
|
Required(f, nameof(r.RegDate), r.RegDate);
|
|
Required(f, nameof(r.OwnerName), r.OwnerName);
|
|
Required(f, nameof(r.OwnerNnpn), r.OwnerNnpn);
|
|
Required(f, nameof(r.Color), r.Color);
|
|
Required(f, nameof(r.Model), r.Model);
|
|
Required(f, nameof(r.ManifactureYear), r.ManifactureYear);
|
|
Required(f, nameof(r.HeadType), r.HeadType);
|
|
NoSpecial(f, nameof(r.ChasesNumber), r.ChasesNumber);
|
|
NoSpecial(f, nameof(r.OwnerName), r.OwnerName);
|
|
NoSpecial(f, nameof(r.OwnerNnpn), r.OwnerNnpn);
|
|
if (!string.IsNullOrWhiteSpace(r.ManifactureYear) && !RegistryRules.IsYearWithinRange(r.ManifactureYear, currentYear))
|
|
f.Add(new ValidationFailure(nameof(r.ManifactureYear), RegistryMessages.YearRange(currentYear)));
|
|
}
|
|
else
|
|
{
|
|
if (isUpdate)
|
|
{
|
|
// edit for the other companies: required '00,12,09' and the year must be in range (an empty year fails the legacy check)
|
|
Required(f, nameof(r.Axials), r.Axials);
|
|
if (!RegistryRules.IsYearWithinRange(r.ManifactureYear, currentYear))
|
|
f.Add(new ValidationFailure(nameof(r.ManifactureYear), RegistryMessages.YearRange(currentYear)));
|
|
}
|
|
else if (!string.IsNullOrWhiteSpace(r.ManifactureYear) && !RegistryRules.IsYearWithinRange(r.ManifactureYear, currentYear))
|
|
f.Add(new ValidationFailure(nameof(r.ManifactureYear), RegistryMessages.YearRange(currentYear)));
|
|
}
|
|
|
|
MaxLen(f, nameof(r.ChasesNumber), r.ChasesNumber, 20);
|
|
MaxLen(f, nameof(r.OwnerName), r.OwnerName, 50);
|
|
MaxLen(f, nameof(r.OwnerNnpn), r.OwnerNnpn, 20);
|
|
MaxLen(f, nameof(r.Color), r.Color, 30);
|
|
MaxLen(f, nameof(r.Model), r.Model, 30);
|
|
MaxLen(f, nameof(r.HeadType), r.HeadType, 30);
|
|
MaxLen(f, nameof(r.PhoneNumber), r.PhoneNumber, 10);
|
|
Digits(f, nameof(r.PhoneNumber), r.PhoneNumber, 10);
|
|
return f;
|
|
}
|
|
|
|
// ------------------------------------------------------------------ trailer (registerGrainsTrailer.cfm saveTrailer / editTrailer)
|
|
|
|
public static IReadOnlyList<ValidationFailure> ValidateTrailer(SaveTrailerRequest r, long? tcscac, bool isUpdate, long badiyaScac)
|
|
{
|
|
var f = new List<ValidationFailure>();
|
|
if (!isUpdate)
|
|
{
|
|
Required(f, nameof(r.Tn), r.Tn);
|
|
Digits(f, nameof(r.Tn), r.Tn, 12);
|
|
}
|
|
Required(f, nameof(r.Tcscac), tcscac);
|
|
|
|
// company 327 on create, every company on edit (editTrailer requires the full set)
|
|
if (isUpdate || tcscac == badiyaScac)
|
|
{
|
|
Required(f, nameof(r.ChasesNumber), r.ChasesNumber);
|
|
Required(f, nameof(r.RegDate), r.RegDate);
|
|
Required(f, nameof(r.Axials), r.Axials);
|
|
Required(f, nameof(r.OwnerName), r.OwnerName);
|
|
Required(f, nameof(r.OwnerNnpn), r.OwnerNnpn);
|
|
NoSpecial(f, nameof(r.ChasesNumber), r.ChasesNumber);
|
|
NoSpecial(f, nameof(r.OwnerName), r.OwnerName);
|
|
NoSpecial(f, nameof(r.OwnerNnpn), r.OwnerNnpn);
|
|
}
|
|
|
|
MaxLen(f, nameof(r.ChasesNumber), r.ChasesNumber, 20);
|
|
MaxLen(f, nameof(r.OwnerName), r.OwnerName, 50);
|
|
MaxLen(f, nameof(r.OwnerNnpn), r.OwnerNnpn, 20);
|
|
MaxLen(f, nameof(r.PhoneNumber), r.PhoneNumber, 10);
|
|
Digits(f, nameof(r.PhoneNumber), r.PhoneNumber, 10);
|
|
return f;
|
|
}
|
|
|
|
// ------------------------------------------------------------------ driver (regesterDriver.cfm saveDriverDetails)
|
|
|
|
public static IReadOnlyList<ValidationFailure> ValidateDriver(SaveDriverRequest r)
|
|
{
|
|
var f = new List<ValidationFailure>();
|
|
Required(f, nameof(r.Nnpn), r.Nnpn);
|
|
Required(f, nameof(r.Name), r.Name);
|
|
Required(f, nameof(r.Dln), r.Dln);
|
|
Required(f, nameof(r.Dled), r.Dled);
|
|
Required(f, nameof(r.Nat), r.Nat);
|
|
Required(f, nameof(r.Cat), r.Cat);
|
|
Required(f, nameof(r.Mobile1), r.Mobile1);
|
|
NoSpecial(f, nameof(r.Name), r.Name);
|
|
NoSpecial(f, nameof(r.Nnpn), r.Nnpn);
|
|
MaxLen(f, nameof(r.Nnpn), r.Nnpn, 20);
|
|
MaxLen(f, nameof(r.Name), r.Name, 50);
|
|
Digits(f, nameof(r.Dln), r.Dln, 9);
|
|
Digits(f, nameof(r.Mobile1), r.Mobile1, 10);
|
|
Digits(f, nameof(r.Mobile2), r.Mobile2, 10);
|
|
if (!string.IsNullOrWhiteSpace(r.Cat) && !(r.Cat.Trim().Length == 1 && r.Cat.Trim()[0] is >= '1' and <= '6'))
|
|
f.Add(new ValidationFailure(nameof(r.Cat), RegistryMessages.InvalidNumber));
|
|
return f;
|
|
}
|
|
|
|
// ------------------------------------------------------------------ blacklist (createBlacklist.cfm, confirmDeleteBL.cfm)
|
|
|
|
public static IReadOnlyList<ValidationFailure> ValidateBlacklist(AddBlacklistRequest r)
|
|
{
|
|
var f = new List<ValidationFailure>();
|
|
var type = string.IsNullOrWhiteSpace(r.EntityType) ? NTP.Domain.Grains.Registry.BlacklistValues.EntityPermit : r.EntityType.Trim().ToUpperInvariant();
|
|
Required(f, nameof(r.EntityNum), r.EntityNum);
|
|
if (!string.IsNullOrWhiteSpace(r.EntityNum) && !RegistryRules.IsDigits(r.EntityNum.Trim(), 18))
|
|
f.Add(new ValidationFailure(nameof(r.EntityNum), RegistryMessages.InvalidNumber));
|
|
if (!RegistryRules.IsBlacklistEntityType(type))
|
|
f.Add(new ValidationFailure(nameof(r.EntityType), RegistryMessages.RequiredFields));
|
|
if (!RegistryRules.IsBlacklistCategory(r.Category))
|
|
f.Add(new ValidationFailure(nameof(r.Category), RegistryMessages.RequiredFields));
|
|
if (!RegistryRules.BlacklistDays.Contains(r.ExpiryDays))
|
|
f.Add(new ValidationFailure(nameof(r.ExpiryDays), RegistryMessages.RequiredFields));
|
|
MaxLen(f, nameof(r.Remarks), r.Remarks, 400);
|
|
return f;
|
|
}
|
|
|
|
public static IReadOnlyList<ValidationFailure> ValidateCancelBlacklist(CancelBlacklistRequest r)
|
|
{
|
|
var f = new List<ValidationFailure>();
|
|
Required(f, nameof(r.Remarks), r.Remarks);
|
|
MaxLen(f, nameof(r.Remarks), r.Remarks, 400);
|
|
return f;
|
|
}
|
|
|
|
// ------------------------------------------------------------------ weighbridge control (WeighbridgesControl.cfm)
|
|
|
|
public static IReadOnlyList<ValidationFailure> ValidateWeighbridge(SaveWeighbridgeControlRequest r)
|
|
{
|
|
var f = new List<ValidationFailure>();
|
|
Required(f, nameof(r.Tn), r.Tn);
|
|
Required(f, nameof(r.LocationId), r.LocationId);
|
|
Required(f, nameof(r.Notes), r.Notes);
|
|
Required(f, nameof(r.ExpiryDate), r.ExpiryDate);
|
|
MaxLen(f, nameof(r.Tn), r.Tn, 20);
|
|
MaxLen(f, nameof(r.Notes), r.Notes, 400);
|
|
return f;
|
|
}
|
|
}
|