From 87332fb06e2995087fe9dbd2f128ce2794987213 Mon Sep 17 00:00:00 2001 From: Mohammad Shabeer Date: Sun, 6 Oct 2024 23:56:44 +0530 Subject: [PATCH] feat:addNewUserDone --- Models/Dto/UserDto/CreateUserDto.cs | 7 ++ Models/Dto/UserDto/CreateUserResDto.cs | 6 ++ .../UserValidation/CreateUserValidator.cs | 64 +++++++++++++++---- Services/Impl/AdminService.cs | 1 + 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/Models/Dto/UserDto/CreateUserDto.cs b/Models/Dto/UserDto/CreateUserDto.cs index 034d77c..f95c976 100644 --- a/Models/Dto/UserDto/CreateUserDto.cs +++ b/Models/Dto/UserDto/CreateUserDto.cs @@ -10,5 +10,12 @@ public class CreateUserDto public string Email { get; set; } public string Password { get; set; } public string Role { get; set; } + public string Telephone { get; set; } + public int Age { get; set; } + public string District { get; set; } + public string Province { get; set; } + public string City { get; set; } + public string ZipCode { get; set; } + public string Company { get; set; } } diff --git a/Models/Dto/UserDto/CreateUserResDto.cs b/Models/Dto/UserDto/CreateUserResDto.cs index 0f403eb..d702d4a 100644 --- a/Models/Dto/UserDto/CreateUserResDto.cs +++ b/Models/Dto/UserDto/CreateUserResDto.cs @@ -15,4 +15,10 @@ public class CreateUserResDto // Can be 'PENDING', 'ACTIVE', or 'DEACTIVATED' public string Status { get; set; } public string DateCreated { get; set; } + public string Telephone { get; set; } + public int Age { get; set; } + public string District { get; set; } + public string Province { get; set; } + public string City { get; set; } + } diff --git a/Models/Validation/UserValidation/CreateUserValidator.cs b/Models/Validation/UserValidation/CreateUserValidator.cs index 5bf217c..4f46a11 100644 --- a/Models/Validation/UserValidation/CreateUserValidator.cs +++ b/Models/Validation/UserValidation/CreateUserValidator.cs @@ -7,29 +7,69 @@ namespace apekade.Models.Validation; public class CreateUserValidator : AbstractValidator { - public CreateUserValidator(){ + public CreateUserValidator() + { RuleFor(x => x.FirstName) + .NotEmpty() + .WithMessage("First name is required."); + + RuleFor(x => x.LastName) .NotEmpty() - .WithMessage("First name is required."); + .WithMessage("Last name is required."); - RuleFor(x => x.Email) + RuleFor(x => x.Role) .NotEmpty() - .EmailAddress() - .WithMessage("A valid email is required."); + .WithMessage("Role should not be empty.") + .Must(IsValidRole) + .WithMessage("Role is not valid."); - RuleFor(x => x.Password) + RuleFor(x => x.Telephone) .NotEmpty() - .MinimumLength(6) - .WithMessage("Password must be at least 6 characters."); + .WithMessage("Telephone is required.") + .Matches(@"^\d{9}$") + .WithMessage("Telephone must be exactly 9 digits."); - RuleFor(x => x.Role) + RuleFor(x => x.Age) .NotEmpty() - .WithMessage("role should not be empty") - .Must(IsValidRole) - .WithMessage("role not valid ."); + .WithMessage("Age is required.") + .InclusiveBetween(14, 100) + .WithMessage("Age must be between 14 and 100."); + + RuleFor(x => x.Province) + .NotEmpty() + .WithMessage("Province is required.") + .Must(IsValidProvince) + .WithMessage("Province is not valid."); + + RuleFor(x => x.District) + .NotEmpty() + .WithMessage("District is required.") + .Must(IsValidDistrict) + .WithMessage("District is not valid."); + + RuleFor(x => x.City) + .NotEmpty() + .WithMessage("City is required."); + + RuleFor(x => x.ZipCode) + .NotEmpty() + .WithMessage("ZipCode is required."); + + RuleFor(x => x.Company) + .MaximumLength(100) // Limit length to a reasonable size + .WithMessage("Company name cannot exceed 100 characters.") + .When(x => !string.IsNullOrEmpty(x.Company)); } private bool IsValidRole(string role) { return Enum.TryParse(typeof(Role), role, true, out _); } + private bool IsValidProvince(string province) + { + return Enum.TryParse(typeof(Province), province, true, out _); + } + private bool IsValidDistrict(string district) + { + return Enum.TryParse(typeof(District), district, true, out _); + } } diff --git a/Services/Impl/AdminService.cs b/Services/Impl/AdminService.cs index 39fd3a5..a867a6b 100644 --- a/Services/Impl/AdminService.cs +++ b/Services/Impl/AdminService.cs @@ -82,6 +82,7 @@ public async Task CreateUser(CreateUserDto createUserDto) var newUser = _mapper.Map(createUserDto); // auto activate account when admin create users newUser.IsApproved = true; + newUser.Status= Models.Enums.Status.ACTIVE; await _adminRepository.CreateNewUser(newUser); return new ApiRes(201, true, "User created successfully", new { });