This commit is contained in:
mmrbnjd
2025-07-03 16:05:44 +03:30
parent bc65878608
commit 87d2360b32
8 changed files with 338 additions and 51 deletions

View File

@@ -13,7 +13,7 @@ namespace Common.Dtos.Company
public string? FullNameManager { get; set; } public string? FullNameManager { get; set; }
public string? WebSite { get; set; } public string? WebSite { get; set; }
public string? Email { get; set; } public string? Email { get; set; }
public byte[]? img { get; set; } public byte[]? logo { get; set; }
public bool Available { get; set; } public bool Available { get; set; }
public bool allowBot { get; set; } public bool allowBot { get; set; }
} }

View File

@@ -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<ReadANDUpdate_CompanyDto, Company>().ReverseMap(); ;
}
}

View File

@@ -9,6 +9,7 @@ namespace Hushian.Application.Models
public T? Value { get; set; } public T? Value { get; set; }
public bool Success { get; set; }=false; public bool Success { get; set; }=false;
public List<string> Errors { get; set; }=new List<string>(); public List<string> Errors { get; set; }=new List<string>();
public List<string> Warning { get; set; }=new List<string>();
} }
} }

View File

@@ -4,6 +4,7 @@ using Common.Models.Auth.UserSide;
using Hushian.Application.Constants; using Hushian.Application.Constants;
using Hushian.Application.Contracts.Persistence; using Hushian.Application.Contracts.Persistence;
using Hushian.Application.Models; using Hushian.Application.Models;
using Hushian.Application.Validation;
using Hushian.Domain.Entites; using Hushian.Domain.Entites;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -38,11 +39,17 @@ namespace Hushian.Application.Services
(AuthRequestFromCompanySide auth) (AuthRequestFromCompanySide auth)
{ {
ResponseBase<AuthResponse> Response = new(); ResponseBase<AuthResponse> Response = new();
if (!FixedValidation.CheckUsername(auth.Username))
{
Response.Errors.Add("نام کاربری اشتباه است");
}
else
{
if (auth.Username.StartsWith("09")) if (auth.Username.StartsWith("09"))
{ {
// in Company Search // in Company Search
var Company= await _CompanyRepository.Get().FirstOrDefaultAsync(f=>f.Mobile== auth.Username && f.Password==auth.Password.GetHash()); var Company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.Mobile == auth.Username && f.Password == auth.Password.GetHash());
if (Company==null) if (Company == null)
{ {
Response.Errors.Add("کاربری یافت نشد"); Response.Errors.Add("کاربری یافت نشد");
} }
@@ -77,12 +84,20 @@ namespace Hushian.Application.Services
}; };
} }
} }
}
return Response; return Response;
} }
public async Task<ResponseBase<int>> AuthenticationFromUserSide public async Task<ResponseBase<int>> AuthenticationFromUserSide
(AuthRequestFromUserSide auth) (AuthRequestFromUserSide auth)
{ {
ResponseBase<int> Response = new(); ResponseBase<int> Response = new();
if (!FixedValidation.CheckUsername(auth.Mobile))
{
Response.Errors.Add("نام کاربری اشتباه است");
}
else
{
if (!await _UserRepository.Get().AnyAsync(a => a.Mobile == auth.Mobile)) if (!await _UserRepository.Get().AnyAsync(a => a.Mobile == auth.Mobile))
{ {
if (!await _UserRepository.ADDBool(new User() { Mobile = auth.Mobile, FullName = auth.FullName })) if (!await _UserRepository.ADDBool(new User() { Mobile = auth.Mobile, FullName = auth.FullName }))
@@ -91,12 +106,14 @@ namespace Hushian.Application.Services
} }
} }
if (Response.Errors.Count==0) if (Response.Errors.Count == 0)
{ {
Response.Value = await _verificationService.GenerateCodeForLoginUser(auth.Mobile); Response.Value = await _verificationService.GenerateCodeForLoginUser(auth.Mobile);
Response.Success = true; Response.Success = true;
} }
}
return Response; return Response;
} }

View File

@@ -1,6 +1,14 @@
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.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http.Headers;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -8,5 +16,149 @@ namespace Hushian.Application.Services
{ {
public class CompanyService public class CompanyService
{ {
private readonly IGenericRepository<Company> _CompanyRepository;
private readonly VerificationService _VerificationService;
private readonly IMapper _mapper;
public async Task<ResponseBase<int>> RegisterCompany(RegisterCompanyDto dto)
{
ResponseBase<int> 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<ReadANDUpdate_CompanyDto> GETCompanyinformation(int CompanyID)
{
var company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.ID == CompanyID);
return _mapper.Map<ReadANDUpdate_CompanyDto>(company);
}
public async Task<ResponseBase<bool>> EditCompany(ReadANDUpdate_CompanyDto model, int CompanyID)
{
ResponseBase<bool> 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<bool> 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<bool> 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<ResponseBase<bool>> NewCompany(RegisterCompanyDto dto)
{
ResponseBase<bool> Response = new();
List<string> Errors = new List<string>();
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<int> Verifi(string Mobile) => await _VerificationService.GenerateCodeByPhoneNumberConfirmed(Mobile);
} }
} }

View File

@@ -11,6 +11,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -19,16 +20,18 @@ namespace Hushian.Application.Services
public class VerificationService public class VerificationService
{ {
private readonly IGenericRepository<VerificationCode> _VerificationCodeRepository; private readonly IGenericRepository<VerificationCode> _VerificationCodeRepository;
private readonly IGenericRepository<Company> _CompanyRepository;
private readonly IMessageSender _messageSender; private readonly IMessageSender _messageSender;
private readonly IGenericRepository<User> _UserRepository; private readonly IGenericRepository<User> _UserRepository;
private readonly AuthService _authService; private readonly AuthService _authService;
public VerificationService(IGenericRepository<VerificationCode> verificationCodeRepository, IMessageSender messageSender, IGenericRepository<User> userRepository, AuthService authService) public VerificationService(IGenericRepository<VerificationCode> verificationCodeRepository, IMessageSender messageSender, IGenericRepository<User> userRepository, AuthService authService, IGenericRepository<Company> companyRepository)
{ {
_VerificationCodeRepository = verificationCodeRepository; _VerificationCodeRepository = verificationCodeRepository;
_messageSender = messageSender; _messageSender = messageSender;
_UserRepository = userRepository; _UserRepository = userRepository;
_authService = authService; _authService = authService;
_CompanyRepository = companyRepository;
} }
public async Task<int> GenerateCodeForLoginUser(string Mobile) public async Task<int> GenerateCodeForLoginUser(string Mobile)
@@ -43,6 +46,18 @@ namespace Hushian.Application.Services
}); });
return response.ID; return response.ID;
} }
public async Task<int> 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<ResponseBase<AuthResponse>> VerificationCode(ConfirmedCodeDto model) public async Task<ResponseBase<AuthResponse>> VerificationCode(ConfirmedCodeDto model)
{ {
var response = new ResponseBase<AuthResponse>(); var response = new ResponseBase<AuthResponse>();
@@ -80,7 +95,20 @@ namespace Hushian.Application.Services
} }
else if (resultConf.Type == VerificationCodeType.PhoneNumberConfirmed) 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 else
{ {
@@ -98,6 +126,22 @@ namespace Hushian.Application.Services
}
public async Task<bool> 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<string> GenerateCode() private async Task<string> GenerateCode()
{ {

View File

@@ -1,16 +1,70 @@
using System; using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hushian.Application.Validation namespace Hushian.Application.Validation
{ {
public static class FixedValidation public static class FixedValidation
{ {
public static bool CheckLawPassword(this string newPassword,ref List<string> errors) public static bool CheckLawPassword(this string password, ref List<string> 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;
} }
} }
} }

View File

@@ -25,6 +25,7 @@ namespace Hushian.Domain.Entites
public byte[]? logo { get; set; } public byte[]? logo { get; set; }
public bool Available { get; set; } = true; public bool Available { get; set; } = true;
public bool allowBot { get; set; } = true; public bool allowBot { get; set; } = true;
public bool Verified { get; set; } = false;
#endregion #endregion
#region Navigation #region Navigation