From 87d2360b322ec2a7a11bbcb68894f28afa65104a Mon Sep 17 00:00:00 2001 From: mmrbnjd Date: Thu, 3 Jul 2025 16:05:44 +0330 Subject: [PATCH] ... --- .../Dtos/Company/ReadANDUpdate_CompanyDto.cs | 2 +- Hushian.Application/MappingProfile.cs | 18 ++ Hushian.Application/Models/ResponseBase.cs | 1 + Hushian.Application/Services/AuthService.cs | 91 +++++----- .../Services/CompanyService.cs | 156 +++++++++++++++++- .../Services/VerificationService.cs | 48 +++++- .../Validation/FixedValidation.cs | 72 +++++++- Hushian.Domain/Entites/Company.cs | 1 + 8 files changed, 338 insertions(+), 51 deletions(-) create mode 100644 Hushian.Application/MappingProfile.cs diff --git a/Common/Dtos/Company/ReadANDUpdate_CompanyDto.cs b/Common/Dtos/Company/ReadANDUpdate_CompanyDto.cs index 8da92a6..1cf6586 100644 --- a/Common/Dtos/Company/ReadANDUpdate_CompanyDto.cs +++ b/Common/Dtos/Company/ReadANDUpdate_CompanyDto.cs @@ -13,7 +13,7 @@ namespace Common.Dtos.Company public string? FullNameManager { get; set; } public string? WebSite { get; set; } public string? Email { get; set; } - public byte[]? img { get; set; } + public byte[]? logo { get; set; } public bool Available { get; set; } public bool allowBot { get; set; } } diff --git a/Hushian.Application/MappingProfile.cs b/Hushian.Application/MappingProfile.cs new file mode 100644 index 0000000..f048d35 --- /dev/null +++ b/Hushian.Application/MappingProfile.cs @@ -0,0 +1,18 @@ +using AutoMapper; +using Common.Dtos.Company; +using Hushian.Domain.Entites; + + + +namespace Hushian.Application +{ + public class MappingProfile : Profile + { + + public MappingProfile(/*IUserService userService*/) + { + CreateMap().ReverseMap(); ; + + + } +} diff --git a/Hushian.Application/Models/ResponseBase.cs b/Hushian.Application/Models/ResponseBase.cs index 6eb2364..a9f2c18 100644 --- a/Hushian.Application/Models/ResponseBase.cs +++ b/Hushian.Application/Models/ResponseBase.cs @@ -9,6 +9,7 @@ namespace Hushian.Application.Models public T? Value { get; set; } public bool Success { get; set; }=false; public List Errors { get; set; }=new List(); + public List Warning { get; set; }=new List(); } } diff --git a/Hushian.Application/Services/AuthService.cs b/Hushian.Application/Services/AuthService.cs index 742c21b..ccf32af 100644 --- a/Hushian.Application/Services/AuthService.cs +++ b/Hushian.Application/Services/AuthService.cs @@ -4,6 +4,7 @@ using Common.Models.Auth.UserSide; using Hushian.Application.Constants; using Hushian.Application.Contracts.Persistence; using Hushian.Application.Models; +using Hushian.Application.Validation; using Hushian.Domain.Entites; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; @@ -38,65 +39,81 @@ namespace Hushian.Application.Services (AuthRequestFromCompanySide auth) { ResponseBase Response = new(); - if (auth.Username.StartsWith("09")) + if (!FixedValidation.CheckUsername(auth.Username)) { - // in Company Search - var Company= await _CompanyRepository.Get().FirstOrDefaultAsync(f=>f.Mobile== auth.Username && f.Password==auth.Password.GetHash()); - if (Company==null) - { - Response.Errors.Add("کاربری یافت نشد"); - } - else - { - Response.Success = true; - Response.Value = new AuthResponse() - { - Fullname = Company.FullName, - Id = Company.ID, - MobileOrUserName = Company.Mobile, - Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(Company.Mobile, Company.ID)) - }; - } + Response.Errors.Add("نام کاربری اشتباه است"); } else { - var exper = await _ExperRepository.Get().FirstOrDefaultAsync(f => f.UserName == auth.Username && f.Password == auth.Password.GetHash()); - if (exper == null) + if (auth.Username.StartsWith("09")) { - Response.Errors.Add("کاربری یافت نشد"); + // in Company Search + var Company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.Mobile == auth.Username && f.Password == auth.Password.GetHash()); + if (Company == null) + { + Response.Errors.Add("کاربری یافت نشد"); + } + else + { + Response.Success = true; + Response.Value = new AuthResponse() + { + Fullname = Company.FullName, + Id = Company.ID, + MobileOrUserName = Company.Mobile, + Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(Company.Mobile, Company.ID)) + }; + } } else { - Response.Success = true; - Response.Value = new AuthResponse() + var exper = await _ExperRepository.Get().FirstOrDefaultAsync(f => f.UserName == auth.Username && f.Password == auth.Password.GetHash()); + if (exper == null) { - Fullname = exper.FullName, - Id = exper.ID, - MobileOrUserName = exper.UserName, - Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(exper.UserName, exper.ID)) - }; + Response.Errors.Add("کاربری یافت نشد"); + } + else + { + Response.Success = true; + Response.Value = new AuthResponse() + { + Fullname = exper.FullName, + Id = exper.ID, + MobileOrUserName = exper.UserName, + Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(exper.UserName, exper.ID)) + }; + } } } + return Response; } public async Task> AuthenticationFromUserSide (AuthRequestFromUserSide auth) { ResponseBase Response = new(); - if (!await _UserRepository.Get().AnyAsync(a => a.Mobile == auth.Mobile)) + if (!FixedValidation.CheckUsername(auth.Mobile)) { - if (!await _UserRepository.ADDBool(new User() { Mobile = auth.Mobile, FullName = auth.FullName })) + Response.Errors.Add("نام کاربری اشتباه است"); + } + else + { + if (!await _UserRepository.Get().AnyAsync(a => a.Mobile == auth.Mobile)) { - Response.Errors.Add("خطا در کاربری"); + if (!await _UserRepository.ADDBool(new User() { Mobile = auth.Mobile, FullName = auth.FullName })) + { + Response.Errors.Add("خطا در کاربری"); + } + } + + if (Response.Errors.Count == 0) + { + + Response.Value = await _verificationService.GenerateCodeForLoginUser(auth.Mobile); + Response.Success = true; } } - if (Response.Errors.Count==0) - { - - Response.Value = await _verificationService.GenerateCodeForLoginUser(auth.Mobile); - Response.Success = true; - } return Response; } diff --git a/Hushian.Application/Services/CompanyService.cs b/Hushian.Application/Services/CompanyService.cs index 71da8f1..98fcf2f 100644 --- a/Hushian.Application/Services/CompanyService.cs +++ b/Hushian.Application/Services/CompanyService.cs @@ -1,12 +1,164 @@ -using System; +using AutoMapper; +using Common.Dtos.Company; +using Hushian.Application.Contracts.Persistence; +using Hushian.Application.Models; +using Hushian.Application.Validation; +using Hushian.Domain.Entites; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; namespace Hushian.Application.Services { - public class CompanyService + public class CompanyService { + private readonly IGenericRepository _CompanyRepository; + private readonly VerificationService _VerificationService; + private readonly IMapper _mapper; + + public async Task> RegisterCompany(RegisterCompanyDto dto) + { + ResponseBase Response = new(); + + var newCompany = await NewCompany(dto); + if (newCompany.Success) + { + var id = await Verifi(dto.Mobile); + Response.Value = id; + Response.Success = id != 0; + + } + else Response.Errors.AddRange(newCompany.Errors); + + return Response; + } + public async Task GETCompanyinformation(int CompanyID) + { + var company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.ID == CompanyID); + return _mapper.Map(company); + } + public async Task> EditCompany(ReadANDUpdate_CompanyDto model, int CompanyID) + { + ResponseBase Response = new(); + + + if (!string.IsNullOrEmpty(model.FullNameManager) && !model.FullNameManager.IsOnlyPersianLetters()) + Response.Errors.Add("نام مدیر باید کاملا به صورت فارسی باشد"); + else if (!string.IsNullOrEmpty(model.FullName) && !model.FullName.IsOnlyPersianLetters()) + Response.Errors.Add("نام شرکت باید کاملا به صورت فارسی باشد"); + else if (!string.IsNullOrEmpty(model.Email) && !model.Email.IsValidEmail()) + Response.Errors.Add("پست الکترونیکی صحیح نمی باشد"); + else if (!string.IsNullOrEmpty(model.WebSite) && !model.WebSite.IsValidWebsite()) + Response.Errors.Add("وب سایت صحیح نمی باشد"); + else if (model.logo != null && !model.logo.IsValidImage()) + Response.Errors.Add("تصویر نمی تواند از 5 مگ بیشتر باشد"); + else + { + try + { + var company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.ID == CompanyID); + + company.FullNameManager = model.FullNameManager; + company.FullName = model.FullName; + company.Email = model.Email; + company.WebSite = model.WebSite; + company.logo = model.logo; + + if (await _CompanyRepository.UPDATEBool(company)) + { + Response.Success =Response.Value= true; + } + else + { + Response.Errors.Add("خطا در ذخیره سازی"); + } + } + catch (Exception) + { + Response.Errors.Add("خطا سیستمی 02"); + } + + } + return Response; + } + public async Task ChangeAvailableCompany(bool @checked,int CompanyID) + { + + var company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.ID == CompanyID); + company.Available = @checked; + return await _CompanyRepository.UPDATEBool(company); + + } + public async Task ChangeallowBotCompany(bool @checked, int CompanyID) + { + var company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.ID == CompanyID); + company.allowBot = @checked; + return await _CompanyRepository.UPDATEBool(company); + } + private async Task> NewCompany(RegisterCompanyDto dto) + { + ResponseBase Response = new(); + + List Errors = new List(); + if (!dto.Mobile.CheckMobile()) + { + Response.Errors.Add("فرمت موبایل صحیح نمی باشد"); + } + else if (!dto.Password.CheckLawPassword(ref Errors)) + { + Response.Errors.AddRange(Errors); + } + else if (!dto.FullName.IsOnlyPersianLetters()) + { + Response.Errors.Add("نام باید کاملا به صورت فارسی باشد"); + } + else + { + var AnyCompany = await _CompanyRepository.Get().FirstOrDefaultAsync(a => a.Mobile == dto.Mobile); + if (AnyCompany != null && AnyCompany.Verified) + { + Response.Errors.Add($"موبایل {dto.Mobile} در سیستم ثبت شده"); + } + else + { + if (AnyCompany != null && !AnyCompany.Verified) + { + AnyCompany.Password = dto.Password.GetHash(); + AnyCompany.FullName = dto.FullName; + AnyCompany.Cdatetime = DateTime.Now; + if (await _CompanyRepository.UPDATEBool(AnyCompany)) + { + Response.Success = true; + } + else + { + Response.Errors.Add("خطا در ذخیره سازی اطلاعات کاربر"); + } + } + else + { + if (await _CompanyRepository.ADDBool(new Company() + { + + })) + { + Response.Success = true; + } + else + { + Response.Errors.Add("خطا در ذخیره سازی اطلاعات کاربر"); + } + } + } + } + + return Response; + } + private async Task Verifi(string Mobile) => await _VerificationService.GenerateCodeByPhoneNumberConfirmed(Mobile); + } } diff --git a/Hushian.Application/Services/VerificationService.cs b/Hushian.Application/Services/VerificationService.cs index 26fe438..98bb99f 100644 --- a/Hushian.Application/Services/VerificationService.cs +++ b/Hushian.Application/Services/VerificationService.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -19,16 +20,18 @@ namespace Hushian.Application.Services public class VerificationService { private readonly IGenericRepository _VerificationCodeRepository; + private readonly IGenericRepository _CompanyRepository; private readonly IMessageSender _messageSender; private readonly IGenericRepository _UserRepository; private readonly AuthService _authService; - public VerificationService(IGenericRepository verificationCodeRepository, IMessageSender messageSender, IGenericRepository userRepository, AuthService authService) + public VerificationService(IGenericRepository verificationCodeRepository, IMessageSender messageSender, IGenericRepository userRepository, AuthService authService, IGenericRepository companyRepository) { _VerificationCodeRepository = verificationCodeRepository; _messageSender = messageSender; _UserRepository = userRepository; _authService = authService; + _CompanyRepository = companyRepository; } public async Task GenerateCodeForLoginUser(string Mobile) @@ -43,6 +46,18 @@ namespace Hushian.Application.Services }); return response.ID; } + public async Task GenerateCodeByPhoneNumberConfirmed(string Mobile) + { + string Code = await GenerateCode(); + var response = await _VerificationCodeRepository.ADD + (new Identity.Models.VerificationCode(VerificationCodeType.PhoneNumberConfirmed, Code, Mobile)); + await _messageSender.SendMassage(new Models.Message.Message() + { + msg = Code, + To = Mobile + }); + return response.ID; + } public async Task> VerificationCode(ConfirmedCodeDto model) { var response = new ResponseBase(); @@ -80,7 +95,20 @@ namespace Hushian.Application.Services } else if (resultConf.Type == VerificationCodeType.PhoneNumberConfirmed) { - + var anyCompany=await _CompanyRepository.Get().FirstOrDefaultAsync(w => w.Mobile == resultConf.Mobile && !w.Verified); + if (anyCompany!=null) + { + anyCompany.Verified = true; + if(await _CompanyRepository.UPDATEBool(anyCompany)) + { + response.Success = true; + response.Value = new AuthResponse(); + } + else + { + response.Errors.Add("خطای سیستمی در احراز"); + } + } } else { @@ -98,6 +126,22 @@ namespace Hushian.Application.Services + } + public async Task ReSendCode(int ID) + { + var model=await _VerificationCodeRepository.Get().FirstOrDefaultAsync(f=>f.ID==ID); + if (model==null) + { + return false; + } + else + { + return await _messageSender.SendMassage(new Models.Message.Message() + { + msg = model.Code, + To = model.Mobile + }); + } } private async Task GenerateCode() { diff --git a/Hushian.Application/Validation/FixedValidation.cs b/Hushian.Application/Validation/FixedValidation.cs index f3d65cb..c6d4311 100644 --- a/Hushian.Application/Validation/FixedValidation.cs +++ b/Hushian.Application/Validation/FixedValidation.cs @@ -1,16 +1,70 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - +using System.Text.RegularExpressions; namespace Hushian.Application.Validation { - public static class FixedValidation + public static class FixedValidation { - public static bool CheckLawPassword(this string newPassword,ref List errors) + public static bool CheckLawPassword(this string password, ref List errors) { - return true; + bool ret = true; + if (password.Length != 6) + { + errors.Add("کلمه عبور باید حداقل 6 گاراکتر باشد"); + ret = false; + } + + // بررسی شامل بودن حداقل یک حرف + if (!Regex.IsMatch(password, "[a-zA-Z]")) + { + errors.Add("کلمه عبور باید حداقل شامل یک حرف انگلیسی بزرگ یا کوچک باشد"); + + ret = false; + } + // بررسی شامل بودن حداقل یک عدد + if (!Regex.IsMatch(password, "[0-9]")) + { + errors.Add("کلمه عبور باید حداقل شامل یک عدد باشد"); + ret = false; + } + // بررسی اینکه فقط حروف و اعداد انگلیسی باشد + if(Regex.IsMatch(password, "^[a-zA-Z0-9]+$")) + { + errors.Add("کلمه عبور فقط عدد و حروف انگلیسی مجاز است"); + ret = false; + + } + + return ret; + } + public static bool CheckUsername(this string Username) + => (Username.Length == 11 && Username.StartsWith("09")) + || (Username.Length == 9 && Username.StartsWith("E/")); + public static bool CheckMobile(this string Mobile) => Mobile.Length == 11 && Mobile.StartsWith("09"); + public static bool IsValidEmail(this string email) + { + // الگوی ساده اما معتبر برای بررسی ایمیل + string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"; + + return Regex.IsMatch(email, pattern); + } + public static bool IsOnlyPersianLetters(this string input) + { + // این الگو فقط اجازه حروف فارسی می‌دهد + string pattern = @"^[\u0600-\u06FF\s]+$"; + + return Regex.IsMatch(input, pattern); + } + public static bool IsValidWebsite(this string url) + { + // پروتکل اختیاری است، فقط باید دامنه درست باشد + string pattern = @"^(https?:\/\/)?(www\.)?[a-zA-Z0-9\-]+\.[a-zA-Z]{2,}(\S*)?$"; + return Regex.IsMatch(url, pattern); + } + public static bool IsValidImage(this byte[] imageData, int maxSizeInBytes = 5 * 1024 * 1024) + { + // بررسی خالی بودن یا حجم بیش از حد + return imageData.Length <= maxSizeInBytes; + + } } } diff --git a/Hushian.Domain/Entites/Company.cs b/Hushian.Domain/Entites/Company.cs index c5b5def..4b0337b 100644 --- a/Hushian.Domain/Entites/Company.cs +++ b/Hushian.Domain/Entites/Company.cs @@ -25,6 +25,7 @@ namespace Hushian.Domain.Entites public byte[]? logo { get; set; } public bool Available { get; set; } = true; public bool allowBot { get; set; } = true; + public bool Verified { get; set; } = false; #endregion #region Navigation