84 lines
4.5 KiB
C#
84 lines
4.5 KiB
C#
using Brizco.Domain.Entities.Tasks;
|
|
using Task = Brizco.Domain.Entities.Tasks.Task;
|
|
|
|
namespace Brizco.Core.EntityServices.Handlers.Activities;
|
|
|
|
public class UpdateActivitiesByShiftPlanCommandHandler(IRepositoryWrapper repositoryWrapper,IMediator mediator) : IRequestHandler<UpdateActivitiesByShiftPlanCommand, bool>
|
|
{
|
|
public async Task<bool> Handle(UpdateActivitiesByShiftPlanCommand request, CancellationToken cancellationToken)
|
|
{
|
|
|
|
var shiftPlan = await mediator.Send(new GetShiftPlanQuery(request.ShiftPlanId), cancellationToken);
|
|
if (shiftPlan.Id == Guid.Empty)
|
|
return false;
|
|
|
|
var dailyTasks = await(from task in repositoryWrapper.SetRepository<Task>().Entities
|
|
join taskShift in repositoryWrapper.SetRepository<TaskShift>().Entities on task.Id equals taskShift.TaskId
|
|
join taskRoutine in repositoryWrapper.SetRepository<TaskRoutine>().Entities on task.Id equals taskRoutine
|
|
.TaskId
|
|
where taskShift.ShiftId == shiftPlan.ShiftId && taskRoutine.RoutineId == shiftPlan.RoutineId && task.ScheduleType == TaskScheduleType.Daily
|
|
select task).AsNoTracking().ToListAsync(cancellationToken);
|
|
|
|
var weeklyTasks = await(from task in repositoryWrapper.SetRepository<Task>().Entities
|
|
join taskShift in repositoryWrapper.SetRepository<TaskShift>().Entities on task.Id equals taskShift.TaskId
|
|
join taskDay in repositoryWrapper.SetRepository<TaskDay>().Entities on task.Id equals taskDay.TaskId
|
|
join taskRoutine in repositoryWrapper.SetRepository<TaskRoutine>().Entities on task.Id equals taskRoutine
|
|
.TaskId
|
|
where taskShift.ShiftId == shiftPlan.ShiftId && taskRoutine.RoutineId == shiftPlan.RoutineId &&
|
|
taskDay.DayOfWeek == shiftPlan.PlanFor.DayOfWeek && task.ScheduleType == TaskScheduleType.Weekly
|
|
group task by task.Id into groupedTask
|
|
select groupedTask.FirstOrDefault()).AsNoTracking().ToListAsync(cancellationToken);
|
|
|
|
var customTasks = await(from task in repositoryWrapper.SetRepository<Task>().Entities
|
|
join taskShift in repositoryWrapper.SetRepository<TaskShift>().Entities on task.Id equals taskShift.TaskId
|
|
join taskRoutine in repositoryWrapper.SetRepository<TaskRoutine>().Entities on task.Id equals taskRoutine
|
|
.TaskId
|
|
where taskShift.ShiftId == shiftPlan.ShiftId && taskRoutine.RoutineId == shiftPlan.RoutineId &&
|
|
task.ScheduleType == TaskScheduleType.Custom && task.SetFor.Date == shiftPlan.PlanFor.Date
|
|
select task).AsNoTracking().ToListAsync(cancellationToken);
|
|
|
|
var tasks = new List<Task>();
|
|
tasks.AddRange(weeklyTasks);
|
|
|
|
tasks.AddRange(dailyTasks);
|
|
tasks.AddRange(customTasks);
|
|
|
|
var shiftPlanPositions = await repositoryWrapper.SetRepository<ShiftPlanUser>()
|
|
.TableNoTracking
|
|
.Where(spu => spu.ShiftPlanId == shiftPlan.Id)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
|
|
var activities = await repositoryWrapper.SetRepository<Activity>()
|
|
.TableNoTracking
|
|
.Where(a => a.ShiftPlanId == shiftPlan.Id && a.SetFor.Date == shiftPlan.PlanFor.Date)
|
|
.Select(ActivityMapper.ProjectToLDto)
|
|
.ToListAsync(cancellationToken);
|
|
foreach (var activity in activities)
|
|
{
|
|
var foundedTask = tasks.FirstOrDefault(t => t.Title == activity.Title);
|
|
if (foundedTask == null)
|
|
continue;
|
|
|
|
var taskPositions = await repositoryWrapper.SetRepository<TaskPosition>()
|
|
.TableNoTracking
|
|
.Where(tp => tp.TaskId == foundedTask.Id)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
foreach (var taskPosition in taskPositions)
|
|
{
|
|
var foundedUser = shiftPlanPositions.FirstOrDefault(spu => spu.PositionId == taskPosition.PositionId);
|
|
if (foundedUser == null)
|
|
continue;
|
|
if (activity.UserId != foundedUser.UserId)
|
|
{
|
|
|
|
await mediator.Send(new UpdateActivityCommand(activity.Id, activity.Status, activity.DoneAt, activity.PerformanceDescription,
|
|
activity.Type, activity.Title, activity.Description, activity.IsDisposable, activity.SetFor, activity.HasDisposed, activity.Amount
|
|
, activity.AmountType, activity.ScheduleType, shiftPlan.Id, foundedUser.UserId), cancellationToken);
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
} |