This commit is contained in:
mmrbnjd
2024-05-07 17:49:02 +03:30
parent 91fcef70d7
commit b5546810b1
6 changed files with 217 additions and 52 deletions

View File

@@ -1,4 +1,5 @@
using Back.Services;
using Back.Validations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shared.DTOs;
@@ -13,9 +14,11 @@ namespace Back.Controllers
private readonly CheckPermission _checkPermission;
private readonly servUser _servUser;
private readonly servCustomer _servCus;
public CustomerController(CheckPermission checkPermission, servUser servUser, servCustomer servCus)
private readonly AddCustomerValidation _addCustomerValidation;
public CustomerController(CheckPermission checkPermission, servUser servUser
, servCustomer servCus, AddCustomerValidation addCustomerValidation)
{
_addCustomerValidation = addCustomerValidation;
_checkPermission = checkPermission;
_servUser = servUser;
_servCus = servCus;
@@ -33,5 +36,33 @@ namespace Back.Controllers
}
[HttpPost("Add")]
public async Task<ActionResult<bool>> Add(RCustomer item)
{
var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
var UserID = claim.Value;
var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
var resultValidationmodel = await _addCustomerValidation.ValidateAsync(Tuple.Create(user.RolUsers.First().CompanyID,item));
if (!resultValidationmodel.IsValid)
return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());
return Ok(_servCus.AddCustomer(new Data.Models.Customer
{
Address = item.Address,
BranchID = item.BranchID,
CompanyID = user.RolUsers.First().CompanyID,
CustomerType = item.CustomerType,
EconomicCode = item.EconomicCode,
Email = item.Email,
FullName = item.FullName,
Info = item.Info,
MeliCode = item.MeliCode,
PassportNumber = item.PassportNumber,
Phone = item.Phone,
ZipCode = item.ZipCode,
IsDeleted = false
})) ;
}
}
}

View File

@@ -39,6 +39,7 @@ builder.Services.AddScoped<servUser>();
builder.Services.AddScoped<servTaxPayer>();
builder.Services.AddScoped<servCustomer>();
builder.Services.AddScoped<CompanyRegistrationValidation>();
builder.Services.AddScoped<AddCustomerValidation>();
builder.Services.AddScoped(c => new mpNuget.RestClient("09119660045", "C54S2"));
string origins = "OriginTaxPayer";

View File

@@ -98,48 +98,48 @@ namespace Back.Services
// })
// .FirstOrDefaultAsync();
//}
//public async Task<Customer?> AddORUpdateCustomer(Customer item)
//{
// try
// {
// SysLog log = new SysLog()
// {
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCustomer",
// Value ="*"+ JsonConvert.SerializeObject(item),
// Route = _httpContextAccessor.HttpContext.Request.Path,
// Type = "User"
// };
// _contextMongodb.InsertItem(log);
public async Task<bool> AddCustomer(Customer item)
{
try
{
//SysLog log = new SysLog()
//{
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCustomer",
// Value = "*" + JsonConvert.SerializeObject(item),
// Route = _httpContextAccessor.HttpContext.Request.Path,
// Type = "User"
//};
//_contextMongodb.InsertItem(log);
// if (item.ID == null || item.ID == 0)
// {
// var ret = await _repositores.ADD(item);
// _checkPermission.ExtensionofAccess(item.CompanyID, 5, "-1");
// return item;
// }
// else
// {
// return await _repositores.UPDATE(item);
// }
// }
// catch (Exception ex)
// {
// SysLog log = new SysLog()
// {
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCustomer",
// Value = ex.Message,
// Route = _httpContextAccessor.HttpContext.Request.Path,
// Type = "catch"
// };
// _contextMongodb.InsertItem(log);
// return null;
// }
//}
if (item.ID == null || item.ID == 0)
{
var ret = await _repoCus.AddBoolResultAsync(item);
await _checkPermission.ExtensionofAccess(item.CompanyID, 5, "-1");
return ret;
}
else
{
return await _repoCus.UpdateAsync(item);
}
}
catch (Exception ex)
{
//SysLog log = new SysLog()
//{
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCustomer",
// Value = ex.Message,
// Route = _httpContextAccessor.HttpContext.Request.Path,
// Type = "catch"
//};
//_contextMongodb.InsertItem(log);
return false;
}
}
//public async Task<bool> DeleteCustomer(Customer item)
//{
@@ -172,10 +172,10 @@ namespace Back.Services
// };
// _contextMongodb.InsertItem(log);
// return false;
// }
//}
}
}

View File

@@ -0,0 +1,61 @@
using Back.Services;
using FluentValidation;
using Shared.DTOs;
namespace Back.Validations
{
public class AddCustomerValidation : AbstractValidator<Tuple<int, RCustomer>>
{
public AddCustomerValidation(CheckPermission _checkPermission)
{
CascadeMode = CascadeMode.Stop;
RuleFor(r => r.Item1)
.Custom((CompanyID, context) => {
if (! _checkPermission.AllowAddCustomerInCompany(CompanyID).Result)
context.AddFailure("اضافه کردن مشتری محدود شده است");
});
RuleFor(r => r.Item2.FullName)
.NotNull().WithMessage("نام نمی تواند خالی باشذ")
.NotEmpty().WithMessage("نام نمی تواند خالی باشذ")
.MinimumLength(5).WithMessage("حداقل کارکتر مجار نام 5 می باشد");
RuleFor(r => r.Item2.Phone)
.NotNull().WithMessage("تلفن نمی تواند خالی باشذ")
.NotEmpty().WithMessage("تلفن نمی تواند خالی باشذ")
.MinimumLength(8).WithMessage("حداقل کارکتر مجار تلفن 8 می باشد");
RuleFor(r => r.Item2.Email)
.Custom((model, context) => {
if (!string.IsNullOrEmpty(model) && (!model.Contains("@") || !model.Contains(".")))
context.AddFailure("فرمت پست الکترونیک صحبح نمی باشد");
});
RuleFor(r => r.Item2.EconomicCode)
.Custom((model, context) => {
if (!string.IsNullOrEmpty(model) && (model.Length<11 || model.Length > 14))
context.AddFailure("تعداد شماره اقتصادی صحبح نمی باشد");
});
RuleFor(r => r.Item2.BranchID)
.Custom((model, context) => {
if (!string.IsNullOrEmpty(model) && (model.Length > 10 ))
context.AddFailure("تعداد کد شعبه صحبح نمی باشد");
});
RuleFor(r => r.Item2.ZipCode)
.Custom((model, context) => {
if (!string.IsNullOrEmpty(model) && (model.Length !=10))
context.AddFailure("تعداد کد پستی صحبح نمی باشد");
});
RuleFor(r => r.Item2.MeliCode)
.Custom((model, context) => {
if (!string.IsNullOrEmpty(model) && (model.Length < 10 || model.Length > 12))
context.AddFailure("تعداد شناسه ملی صحبح نمی باشد");
});
RuleFor(r => r.Item2.PassportNumber)
.Custom((model, context) => {
if (!string.IsNullOrEmpty(model) && (model.Length!=9))
context.AddFailure("تعداد شماره گذرنامه صحبح نمی باشد");
});
}
}
}