Commit 4ea3732a authored by hasan khaddour's avatar hasan khaddour

update some handlers logic

parent 8000595b
using AutoMapper;
using PSManagement.Application.Contracts.Authentication;
using PSManagement.Application.Customers.Common;
using PSManagement.Application.Employees.Common;
using PSManagement.Application.FinancialSpends.Common;
......@@ -12,6 +13,7 @@ using PSManagement.Domain.Customers.Entities;
using PSManagement.Domain.Customers.ValueObjects;
using PSManagement.Domain.Employees.Entities;
using PSManagement.Domain.FinancialSpends.Entities;
using PSManagement.Domain.Identity.Entities;
using PSManagement.Domain.Projects.Entities;
using PSManagement.Domain.Projects.ValueObjects;
using PSManagement.Domain.Tracking;
......@@ -81,7 +83,7 @@ namespace PSManagement.Application.Mappers
))
;
CreateMap<Role,RoleDTO>().ReverseMap();
}
}
......
......@@ -10,12 +10,20 @@ namespace PSManagement.Application.Contracts.Authentication
{
public class AuthenticationResult
{
public int Id { get; set; }
public int EmployeeId { get; set; }
public String Email { get; set; }
public String LastName { get; set; }
public String FirstName { get; set; }
public ICollection<Role> Roles { get; set; }
public ICollection<RoleDTO> Roles { get; set; }
public String Token { get; set; }
}
public class RoleDTO{
public String Name { get; set; }
public int Id { get; set; }
}
}
using PSManagement.Domain.Tracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Application.Contracts.Occupancy
{
public interface IOccupancySystemNotifier
{
public Task SendEmployeeOccupancyNotification(EmployeeTrack employeeTrack);
}
}
using PSManagement.Application.Contracts.Authentication;
using System.Collections.Generic;
namespace PSManagement.Application.Contracts.Providers
{
public interface ICurrentUserProvider
{
int? EmployeeId { get; }
string Email { get; }
int? HiastId { get; }
IEnumerable<string> Roles { get; }
}
}
using PSManagement.Domain.Employees.DomainEvents;
using PSManagement.SharedKernel.DomainEvents;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PSManagement.Application.Employees.EventsHandlers
{
public class EmployeeWorkHoursChangedEventHandler : IDomainEventHandler<EmployeeWorkHoursChangedEvent>
{
public Task Handle(EmployeeWorkHoursChangedEvent notification, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}
......@@ -3,6 +3,7 @@ using PSManagement.Domain.Employees.DomainErrors;
using PSManagement.Domain.Employees.Entities;
using PSManagement.Domain.Employees.Repositories;
using PSManagement.SharedKernel.CQRS.Command;
using PSManagement.SharedKernel.Interfaces;
using System.Threading;
using System.Threading.Tasks;
......@@ -12,24 +13,34 @@ namespace PSManagement.Application.Employees.UseCases.Commands.UpdateEmployeeWor
{
private readonly IEmployeesRepository _employeesRepository;
private readonly static int _workHourLimit = 70;
private readonly IUnitOfWork _unitOfWork;
public UpdateEmployeeWorkHoursCommandHandler(IEmployeesRepository employeesRepository)
public UpdateEmployeeWorkHoursCommandHandler(
IEmployeesRepository employeesRepository,
IUnitOfWork unitOfWork)
{
_employeesRepository = employeesRepository;
_unitOfWork = unitOfWork;
}
public async Task<Result> Handle(UpdateEmployeeWorkHoursCommand request, CancellationToken cancellationToken)
{
Employee employee =await _employeesRepository.GetByIdAsync(request.EmployeeId);
if (employee is null) {
return Result.Invalid(EmployeesErrors.EmployeeUnExist);
}
// check the work hours limitation boundries
if (request.WorkingHour < _workHourLimit && request.WorkingHour > 0)
{
employee.Availability = new(request.WorkingHour, employee.Availability.IsAvailable);
// this method will publish the events of changing the work hours
employee.UpdateWorkHours(request.WorkingHour);
await _employeesRepository.UpdateAsync(employee);
await _unitOfWork.SaveChangesAsync();
return Result.Success();
}
else {
......
......@@ -7,7 +7,7 @@
<ItemGroup>
<Folder Include="Behaviors\AuthorizationBehavior\" />
<Folder Include="Tracks\EventsHandlers\" />
<Folder Include="Projects\UseCases\Commands\ChangeParticipantPartialTime\" />
</ItemGroup>
<ItemGroup>
......
using PSManagement.Application.Contracts.Email;
using PSManagement.Domain.Employees.Entities;
using PSManagement.Domain.Employees.Repositories;
using PSManagement.Domain.Employees.Specification;
using PSManagement.Domain.Projects.DomainEvents;
using PSManagement.Domain.Projects.Entities;
using PSManagement.Domain.Projects.Repositories;
using PSManagement.SharedKernel.DomainEvents;
using PSManagement.SharedKernel.Specification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PSManagement.Application.Projects.EventsHandlers
{
public class ParticipantAddedEventHandler : IDomainEventHandler<ParticipantAddedEvent>
{
private readonly IEmailService _emailService;
private readonly IEmployeesRepository _employeesRepository;
private readonly IProjectsRepository _projectsRepository;
private readonly BaseSpecification<Employee> _specification;
public ParticipantAddedEventHandler(
IEmployeesRepository employeesRepository,
IEmailService emailService,
IProjectsRepository projectsRepository)
{
_employeesRepository = employeesRepository;
_emailService = emailService;
_specification = new EmployeesSpecification();
_projectsRepository = projectsRepository;
}
public async Task Handle(ParticipantAddedEvent notification, CancellationToken cancellationToken)
{
_specification.AddInclude(e => e.User);
Employee employee = await _employeesRepository.GetByIdAsync(notification.EmployeeId, _specification);
Project project = await _projectsRepository.GetByIdAsync(notification.ProjectId);
await _emailService
.SendAsync(
employee.User.Email,
"Project Participation ",
"Hello Mr."
+ employee.PersonalInfo.FirstName +
" you have a new participation in the project " + project.ProjectInfo.Name
+" with the role "+notification.Role
+" with the partial time ratio "+ notification.PartialTimeRatio
+" \n"
);
}
}
}
using PSManagement.Application.Contracts.Email;
using PSManagement.Domain.Employees.Entities;
using PSManagement.Domain.Employees.Repositories;
using PSManagement.Domain.Employees.Specification;
using PSManagement.Domain.Projects.DomainEvents;
using PSManagement.Domain.Projects.Entities;
using PSManagement.Domain.Projects.Repositories;
using PSManagement.SharedKernel.DomainEvents;
using PSManagement.SharedKernel.Specification;
using System.Threading;
using System.Threading.Tasks;
namespace PSManagement.Application.Projects.EventsHandlers
{
public class ParticipantRemovedEventHandler : IDomainEventHandler<ParticipantRemovedEvent>
{
private readonly IEmailService _emailService;
private readonly IEmployeesRepository _employeesRepository;
private readonly IProjectsRepository _projectsRepository;
private readonly BaseSpecification<Employee> _specification;
public ParticipantRemovedEventHandler(
IEmployeesRepository employeesRepository,
IEmailService emailService,
IProjectsRepository projectsRepository)
{
_employeesRepository = employeesRepository;
_emailService = emailService;
_specification = new EmployeesSpecification();
_projectsRepository = projectsRepository;
}
public async Task Handle(ParticipantRemovedEvent notification, CancellationToken cancellationToken)
{
_specification.AddInclude(e => e.User);
Employee employee = await _employeesRepository.GetByIdAsync(notification.EmployeeId ,_specification);
Project project = await _projectsRepository.GetByIdAsync(notification.ProjectId);
await _emailService
.SendAsync(
employee.User.Email,
"Participation Cancelled",
"we are sorry Mr."+employee.PersonalInfo.FirstName+" but you are removed from the project "+project.ProjectInfo.Name);
}
}
}
using PSManagement.Domain.Projects.DomainEvents;
using PSManagement.SharedKernel.DomainEvents;
using System.Threading;
using System.Threading.Tasks;
namespace PSManagement.Application.Projects.EventsHandlers
{
public class ProjectApprovedEventHandler : IDomainEventHandler<ProjectApprovedEvent>
{
public Task Handle(ProjectApprovedEvent notification, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}
using PSManagement.Application.Contracts.Email;
using PSManagement.Domain.Employees.Entities;
using PSManagement.Domain.Employees.Repositories;
using PSManagement.Domain.Employees.Specification;
using PSManagement.Domain.Projects.DomainEvents;
using PSManagement.SharedKernel.DomainEvents;
using PSManagement.SharedKernel.Specification;
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -13,17 +17,35 @@ namespace PSManagement.Application.Projects.EventsHandlers
public class ProjectCreatedEventHandler:IDomainEventHandler<ProjectCreatedEvent>
{
private readonly IEmailService _emailService;
private readonly IEmployeesRepository _employeesRepository;
private readonly BaseSpecification<Employee> _specification;
public ProjectCreatedEventHandler(IEmailService emailService)
public ProjectCreatedEventHandler(
IEmailService emailService,
IEmployeesRepository employeesRepository)
{
_emailService = emailService;
_employeesRepository = employeesRepository;
_specification = new EmployeesSpecification();
}
public async Task Handle(ProjectCreatedEvent notification, CancellationToken cancellationToken)
{
_specification.AddInclude(e => e.User);
await _emailService.SendAsync(""+notification.ProjectManagerId,"gf h gf ","fg fdg");
await _emailService.SendAsync("" + notification.TeamLeaderId, "gf h gf ", "fg fdg");
Employee projectManager = await _employeesRepository.GetByIdAsync(notification.ProjectManagerId,_specification);
Employee teamLeader = await _employeesRepository.GetByIdAsync(notification.TeamLeaderId,_specification);
await _emailService.SendAsync(
projectManager.User.Email,
"Manage a new Project ",
"Mr"+projectManager.PersonalInfo.FirstName+ " you are chosen to Manage a new Project \n ");
await _emailService.SendAsync(
teamLeader.User.Email,
"Lead a new Project ",
"Mr" + teamLeader.PersonalInfo.FirstName + " you are chosen to lead a new Project \n ");
}
}
......
using Ardalis.Result;
using PSManagement.Application.Contracts.Storage;
using PSManagement.Domain.Projects;
using PSManagement.Domain.Projects.Builders;
using PSManagement.Domain.Projects.DomainErrors;
using PSManagement.Domain.Projects.Entities;
......@@ -7,6 +8,7 @@ using PSManagement.Domain.Projects.Repositories;
using PSManagement.SharedKernel.CQRS.Command;
using PSManagement.SharedKernel.Interfaces;
using PSManagement.SharedKernel.Repositories;
using PSManagement.SharedKernel.Specification;
using System;
using System.Threading;
using System.Threading.Tasks;
......@@ -18,6 +20,7 @@ namespace PSManagement.Application.Projects.UseCases.Commands.AddAttachment
private readonly IRepository<Attachment> _attachmentRepository;
private readonly IUnitOfWork _unitOfWork;
private readonly IProjectsRepository _projectsRepository;
private readonly BaseSpecification<Project> _specification;
public AddAttachmentCommandHandler(
IFileService fileService,
......@@ -29,25 +32,39 @@ namespace PSManagement.Application.Projects.UseCases.Commands.AddAttachment
_attachmentRepository = repository;
_unitOfWork = unitOfWork;
_projectsRepository = projectsRepository;
_specification = new ProjectSpecification();
}
public async Task<Result<int>> Handle(AddAttachmentCommand request, CancellationToken cancellationToken)
{
Result<string> pathResult = await _fileService.StoreFile(request.AttachmentName+Guid.NewGuid(),request.File);
Project project = await _projectsRepository.GetByIdAsync(request.ProjectId);
if (project is null) {
_specification.AddInclude(e => e.Attachments);
return Result.Invalid(ProjectsErrors.InvalidEntryError);
}
// save the file on the uploaded files
Result<string> pathResult = await _fileService.StoreFile(request.AttachmentName+Guid.NewGuid(),request.File);
// check if the file uploaded
if (pathResult.IsSuccess)
{
// get the project
Project project = await _projectsRepository.GetByIdAsync(request.ProjectId, _specification);
// checking if the project exist
if (project is null)
{
return Result.Invalid(ProjectsErrors.InvalidEntryError);
}
Attachment attachment = new(pathResult.Value, request.AttachmentName, request.AttachmentDescription, request.ProjectId);
attachment = await _attachmentRepository.AddAsync(attachment);
project.AddAttachment(pathResult.Value, request.AttachmentName, request.AttachmentDescription, request.ProjectId);
await _unitOfWork.SaveChangesAsync();
return Result.Success(attachment.Id);
}
else {
return Result.Invalid(pathResult.ValidationErrors);
}
}
......
......@@ -35,10 +35,10 @@ namespace PSManagement.Application.Projects.UseCases.Commands.ApproveProject
else
{
project.Approve(request.ProjectAggreement);
Result result =project.Approve(request.ProjectAggreement);
await _unitOfWork.SaveChangesAsync();
return Result.Success();
return result;
......
......@@ -37,10 +37,11 @@ namespace PSManagement.Application.Projects.UseCases.Commands.CancelProject
else
{
project.Cancel(_dateTime.UtcNow);
Result result = project.Cancel(_dateTime.UtcNow);
await _unitOfWork.SaveChangesAsync();
return Result.Success();
return result;
......
......@@ -35,11 +35,11 @@ namespace PSManagement.Application.Projects.UseCases.Commands.CompletePlaningPro
else
{
project.Plan();
Result result = project.Plan();
await _unitOfWork.SaveChangesAsync();
return Result.Success();
return result;
......
......@@ -32,17 +32,11 @@ namespace PSManagement.Application.Projects.UseCases.Commands.CompleteProgressPr
{
return Result.Invalid(ProjectsErrors.InvalidEntryError);
}
else
{
project.Complete();
else{
Result result = project.Complete();
await _unitOfWork.SaveChangesAsync();
return Result.Success();
return result;
}
}
}
......
......@@ -46,9 +46,10 @@ namespace PSManagement.Application.Projects.UseCases.Commands.RemoveParticipant
if (project.EmployeeParticipates is null) {
return Result.Invalid(ProjectsErrors.ParticipantUnExistError);
}
var employeeParticipate =project.EmployeeParticipates.Where(e => e.EmployeeId == request.ParticipantId).FirstOrDefault();
EmployeeParticipate employeeParticipate =project.EmployeeParticipates.Where(e => e.EmployeeId == request.ParticipantId).FirstOrDefault();
if (employeeParticipate is null) {
return Result.Invalid(ProjectsErrors.ParticipantUnExistError);
......
......@@ -6,7 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PSManagement.Application.Projects.UseCases.Queries.GetProject
namespace PSManagement.Application.Projects.UseCases.Queries.GetProjectById
{
public record GetProjectByIdQuery(
int ProjectId
......
using Ardalis.Result;
using AutoMapper;
using PSManagement.Application.Projects.Common;
using PSManagement.Application.Projects.UseCases.Queries.GetProjectById;
using PSManagement.Domain.Projects;
using PSManagement.Domain.Projects.Entities;
using PSManagement.Domain.Projects.Repositories;
......@@ -34,6 +35,7 @@ namespace PSManagement.Application.Projects.UseCases.Queries.GetProject
_specification.Includes.Add(p => p.Attachments);
_specification.Includes.Add(p => p.TeamLeader);
_specification.Includes.Add(p => p.Executer);
_specification.Includes.Add(p => p.Proposer);
......
using PSManagement.Application.Contracts.Occupancy;
using PSManagement.Domain.Steps.Repositories;
using PSManagement.Domain.Tracking;
using PSManagement.Domain.Tracking.DomainEvents;
using PSManagement.Domain.Tracking.Specification;
using PSManagement.SharedKernel.DomainEvents;
using PSManagement.SharedKernel.Specification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PSManagement.Application.Tracks.EventsHandlers
{
class TrackCompletedEventHandler : IDomainEventHandler<TrackCompletedEvent>
{
private readonly IOccupancySystemNotifier _occupancySystemNotifier;
private readonly ITracksRepository _trackRepository;
private readonly BaseSpecification<Track> _specification;
public TrackCompletedEventHandler(
ITracksRepository trackRepository,
IOccupancySystemNotifier occupancySystemNotifier)
{
_trackRepository = trackRepository;
_occupancySystemNotifier = occupancySystemNotifier;
_specification = new TrackSpecification();
}
public async Task Handle(TrackCompletedEvent notification, CancellationToken cancellationToken)
{
_specification.AddInclude(s => s.EmployeeTracks);
Track track = await _trackRepository.GetByIdAsync(notification.TrackId , _specification);
foreach (EmployeeTrack employeeTrack in track.EmployeeTracks) {
await _occupancySystemNotifier.SendEmployeeOccupancyNotification(employeeTrack);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment