Skip to content

Changes old #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Core/Application/Common/Repositories/IEntityDbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ public interface IEntityDbSet
public DbSet<LeadActivity> LeadActivity { get; set; }
public DbSet<SalesTeam> SalesTeam { get; set; }
public DbSet<SalesRepresentative> SalesRepresentative { get; set; }
public DbSet<Config> Config { get; set; }
}

18 changes: 18 additions & 0 deletions Core/Application/Common/Services/CleanerData/CleanupReport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.Common.Services.CleanerData
{
public class CleanupReport
{
public bool Success { get; set; }
public string Message { get; set; }
public string ErrorMessage { get; set; }
public Exception Exception { get; set; }
public int TotalEntitiesRemoved { get; set; }
}

}
16 changes: 16 additions & 0 deletions Core/Application/Common/Services/CleanerData/CleanupResponseDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.Common.Services.CleanerData
{
public class CleanupResponseDto
{
public bool Success { get; set; }
public string Message { get; set; }
public string ErrorMessage { get; set; }
public int TotalEntitiesRemoved { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

namespace Application.Common.Services.CleanerData;

public interface IDatabaseCleanerService
{
Task<CleanupReport> CleanAllDataAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public UpdateBudgetValidator()
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Title).NotEmpty();
RuleFor(x => x.BudgetDate).NotNull();
RuleFor(x => x.Amount).NotNull();
RuleFor(x => x.Amount).NotNull().GreaterThan(0);
RuleFor(x => x.CampaignId).NotEmpty();
RuleFor(x => x.Status).NotEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public UpdateCampaignValidator()
RuleFor(x => x.CampaignDateStart).NotEmpty();
RuleFor(x => x.CampaignDateFinish).NotEmpty();
RuleFor(x => x.Status).NotEmpty();
RuleFor(x => x.Status).NotEmpty();
RuleFor(x => x.TargetRevenueAmount).NotEmpty().GreaterThan(0);
}
}

Expand Down
81 changes: 81 additions & 0 deletions Core/Application/Features/ConfigManager/GetConfigByName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Application.Common.Repositories;
using AutoMapper;
using Domain.Entities;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.ConfigManager;

public class GetConfigByNameRequest : IRequest<GetConfigByNameResult>
{
public string Name { get; set; }

public GetConfigByNameRequest(string name)
{
Name = name;
}
public GetConfigByNameRequest()
{
}

}

public class GetConfigByNameResult
{
public string? Id { get; set; }
public string? Name { get; set; }
public string? Value { get; set; }
public DateTime? CreatedAtUtc { get; set; }
}

public class GetConfigByNameHandler : IRequestHandler<GetConfigByNameRequest, GetConfigByNameResult>
{
private readonly ICommandRepository<Config> _configRepository;
private readonly IMapper _mapper;

public GetConfigByNameHandler(ICommandRepository<Config> configRepository, IMapper mapper)
{
_configRepository = configRepository;
_mapper = mapper;
}

public async Task<GetConfigByNameResult> Handle(GetConfigByNameRequest request, CancellationToken cancellationToken)
{
var config = await _configRepository.GetQuery().FirstOrDefaultAsync(c => c.Name == request.Name && !c.IsDeleted, cancellationToken);

if (config == null)
{
return null;
}

return _mapper.Map<GetConfigByNameResult>(config);
}

}

public class ConfigProfile : Profile
{
public ConfigProfile()
{
CreateMap<Config, GetConfigByNameResult>();
}
}

public class ConfigMethode
{
private readonly IMediator _mediator;

public ConfigMethode(IMediator mediator)
{
_mediator = mediator;
}

public async Task<string?> GetConfigByNameAsync(string name)
{
var request = new GetConfigByNameRequest(name);
var resp= await _mediator.Send(request);
return resp?.Value;
}
}


79 changes: 79 additions & 0 deletions Core/Application/Features/ConfigManager/UpdateConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Application.Common.Repositories;
using AutoMapper;
using Domain.Entities;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.Features.ConfigManager;

public class UpdateConfigRequest : IRequest<UpdateConfigResult>
{
public string? Id { get; init; }
public string? Name { get; init; }
public string? Value { get; init; }
public string? UpdatedById { get; init; }
}

public class UpdateConfigResult
{
public Config? Data { get; set; }
}

public class UpdateConfigValidator : AbstractValidator<UpdateConfigRequest>
{
public UpdateConfigValidator()
{
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Name).NotEmpty();
RuleFor(x => x.Value).NotEmpty();
}
}

public class UpdateConfigHandler : IRequestHandler<UpdateConfigRequest, UpdateConfigResult>
{
private readonly ICommandRepository<Config> _repository;
private readonly IUnitOfWork _unitOfWork;

public UpdateConfigHandler(
ICommandRepository<Config> repository,
IUnitOfWork unitOfWork
)
{
_repository = repository;
_unitOfWork = unitOfWork;
}

public async Task<UpdateConfigResult> Handle(UpdateConfigRequest request, CancellationToken cancellationToken)
{
Console.WriteLine(request.Id);
Console.WriteLine(request.Name);
Console.WriteLine(request.Value);


var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);

if (entity == null)
{
throw new Exception($"Entity not found: {request.Id}");
}

entity.Name = request.Name;
entity.Value = request.Value;
entity.UpdatedById = request.UpdatedById;
entity.UpdatedAtUtc = DateTime.UtcNow;

_repository.Update(entity);
await _unitOfWork.SaveAsync(cancellationToken);

return new UpdateConfigResult
{
Data = entity
};
}
}
101 changes: 100 additions & 1 deletion Core/Application/Features/ExpenseManager/Commands/CreateExpense.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Application.Common.Repositories;
using Application.Features.BudgetManager.Queries;
using Application.Features.ConfigManager;
using Application.Features.ExpenseManager.Queries;
using Application.Features.NumberSequenceManager;
using Domain.Entities;
using Domain.Enums;
Expand Down Expand Up @@ -40,16 +43,19 @@ public class CreateExpenseHandler : IRequestHandler<CreateExpenseRequest, Create
private readonly ICommandRepository<Expense> _repository;
private readonly IUnitOfWork _unitOfWork;
private readonly NumberSequenceService _numberSequenceService;
private readonly IMediator _mediator;

public CreateExpenseHandler(
ICommandRepository<Expense> repository,
IUnitOfWork unitOfWork,
NumberSequenceService numberSequenceService
NumberSequenceService numberSequenceService,
IMediator mediator
)
{
_repository = repository;
_unitOfWork = unitOfWork;
_numberSequenceService = numberSequenceService;
_mediator = mediator;
}

public async Task<CreateExpenseResult> Handle(CreateExpenseRequest request, CancellationToken cancellationToken = default)
Expand All @@ -66,6 +72,18 @@ public async Task<CreateExpenseResult> Handle(CreateExpenseRequest request, Canc
CampaignId = request.CampaignId
};

//AnalyseExpense analyse= new AnalyseExpense(_mediator);
//ConfigMethode configService = new ConfigMethode(_mediator);
//var config = await configService.GetConfigByNameAsync("AlertBudget");

//Console.WriteLine("Valeur de la config");
//Console.WriteLine(config);

//if (await analyse.IsExpenseExceedingBudget(entity.Amount ?? 0, entity.CampaignId, int.Parse(config) ))
//{
// throw new Exception("Expense exceeds the budget.");
//}

await _repository.CreateAsync(entity, cancellationToken);
await _unitOfWork.SaveAsync(cancellationToken);

Expand All @@ -74,4 +92,85 @@ public async Task<CreateExpenseResult> Handle(CreateExpenseRequest request, Canc
Data = entity
};
}
}

public class AnalyseExpense
{

private readonly IMediator _mediator;


public AnalyseExpense(IMediator mediator)
{
_mediator = mediator;
}

public async Task<bool> IsExpenseExceedingBudgetAlert(double amount, string idCampaign, int? alert, DateTime date)
{
// Get the list of budgets for the campaign using GetBudgetByCampaignIdListRequest
var budgetRequest = new GetBudgetByCampaignIdListRequest { CampaignId = idCampaign };
var budgetResult = await _mediator.Send(budgetRequest);
var budgets = budgetResult.Data;
if (budgets == null || !budgets.Any())
{
throw new Exception("No budgets found for the campaign.");
}


var expensesRequest = new GetExpenseByCampaignIdListRequest { CampaignId = idCampaign };
var expensesResult = await _mediator.Send(expensesRequest);

// Get the list of confirmed expenses for the campaign
var expenses = expensesResult.Data;

// Filter expenses to only include confirmed ones
var confirmedExpenses = expenses.Where(e => e.Status == ExpenseStatus.Confirmed);
var confirmedBudget = budgets.Where(e => e.Status == BudgetStatus.Confirmed);

var budgetBeforDate = confirmedBudget.Where(b => b.BudgetDate <= date);

// Calculate the total budget
double totalBudget = budgetBeforDate.Sum(b => b.Amount ?? 0);

// Calculate the total confirmed expenses
double totalExpenses = confirmedExpenses.Sum(e => e.Amount ?? 0) + amount;

Console.WriteLine("bugetAvecLimte");
Console.WriteLine(alert);
Console.WriteLine(totalBudget);

Console.WriteLine(totalBudget - ((totalBudget * alert) / 100));

return totalExpenses > totalBudget - ((totalBudget * alert) / 100);
}

public async Task<bool> IsExpenseExceedingBudget(double amount, string idCampaign)
{
// Get the list of budgets for the campaign using GetBudgetByCampaignIdListRequest
var budgetRequest = new GetBudgetByCampaignIdListRequest { CampaignId = idCampaign };
var budgetResult = await _mediator.Send(budgetRequest);
var budgets = budgetResult.Data;
if (budgets == null || !budgets.Any())
{
throw new Exception("No budgets found for the campaign.");
}


var expensesRequest = new GetExpenseByCampaignIdListRequest { CampaignId = idCampaign };
var expensesResult = await _mediator.Send(expensesRequest);

// Get the list of confirmed expenses for the campaign
var expenses = expensesResult.Data;

// Filter expenses to only include confirmed ones
var confirmedExpenses = expenses.Where(e => e.Status == ExpenseStatus.Confirmed);

// Calculate the total budget
double totalBudget = budgets.Sum(b => b.Amount ?? 0);

// Calculate the total confirmed expenses
double totalExpenses = confirmedExpenses.Sum(e => e.Amount ?? 0) + amount;

return totalExpenses > totalBudget;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public UpdateExpenseValidator()
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Title).NotEmpty();
RuleFor(x => x.ExpenseDate).NotNull();
RuleFor(x => x.Amount).NotNull();
RuleFor(x => x.Amount).NotNull().GreaterThan(0);
RuleFor(x => x.CampaignId).NotEmpty();
RuleFor(x => x.Status).NotEmpty();
}
Expand Down
Loading