using Brizco.Domain.Entities.Tasks; using Task = Brizco.Domain.Entities.Tasks.Task; namespace Brizco.Core.EntityServices.Handlers.Activities; public class CreateActivitiesByShiftPlanCommandHandler(IRepositoryWrapper repositoryWrapper, IMediator mediator) : IRequestHandler { public async Task Handle(CreateActivitiesByShiftPlanCommand 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().Entities join taskShift in repositoryWrapper.SetRepository().Entities on task.Id equals taskShift.TaskId join taskRoutine in repositoryWrapper.SetRepository().Entities on task.Id equals taskRoutine .TaskId where task.IsActivity == false && 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().Entities join taskShift in repositoryWrapper.SetRepository().Entities on task.Id equals taskShift.TaskId join taskDay in repositoryWrapper.SetRepository().Entities on task.Id equals taskDay.TaskId join taskRoutine in repositoryWrapper.SetRepository().Entities on task.Id equals taskRoutine .TaskId where task.IsActivity == false && 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().Entities join taskShift in repositoryWrapper.SetRepository().Entities on task.Id equals taskShift.TaskId join taskRoutine in repositoryWrapper.SetRepository().Entities on task.Id equals taskRoutine .TaskId where task.IsActivity == false && 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(); tasks.AddRange(weeklyTasks); tasks.AddRange(dailyTasks); tasks.AddRange(customTasks); var shiftPlanPositions = await repositoryWrapper.SetRepository() .TableNoTracking .Where(spu => spu.ShiftPlanId == shiftPlan.Id) .ToListAsync(cancellationToken); foreach (var task in tasks) { var taskPositions = await repositoryWrapper.SetRepository() .TableNoTracking .Where(tp => tp.TaskId == task.Id) .ToListAsync(cancellationToken); foreach (var taskPosition in taskPositions) { var fundedUser = shiftPlanPositions.FirstOrDefault(spu => spu.PositionId == taskPosition.PositionId); if (fundedUser != null) { await mediator.Send(new CreateActivityCommand(task.Type, task.Title, task.Description, task.IsDisposable, shiftPlan.PlanFor, task.HasDisposed, task.Amount, task.AmountType, task.ScheduleType, shiftPlan.Id, fundedUser.UserId), cancellationToken); } } } return true; } }