2024-07-08 17:45:17 +03:30
|
|
|
|
namespace Brizco.Repository.Handlers.ShiftPlans;
|
2023-09-18 15:41:41 +03:30
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
public class UpdateShiftPlanCommandHandler(IRepositoryWrapper repositoryWrapper)
|
|
|
|
|
|
: IRequestHandler<UpdateShiftPlanCommand, bool>
|
2023-09-18 15:41:41 +03:30
|
|
|
|
{
|
|
|
|
|
|
public async Task<bool> Handle(UpdateShiftPlanCommand request, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var shiftPlan = await repositoryWrapper.SetRepository<ShiftPlan>()
|
2023-09-18 15:41:41 +03:30
|
|
|
|
.TableNoTracking.FirstOrDefaultAsync(s => s.Id == request.Id,cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
if (shiftPlan == null)
|
|
|
|
|
|
throw new AppException("ShiftPlan not found", ApiResultStatusCode.NotFound);
|
|
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var shift = await repositoryWrapper.SetRepository<Shift>()
|
2023-09-18 15:41:41 +03:30
|
|
|
|
.TableNoTracking.FirstOrDefaultAsync(s => s.Id == request.ShiftId, cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
if (shift == null)
|
|
|
|
|
|
throw new AppException("Shift not found", ApiResultStatusCode.NotFound);
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-02-21 02:07:41 +03:30
|
|
|
|
var newPlan = shift.AddPlan(DateTimeExtensions.UnixTimeStampToDateTime(request.PlanDate), request.RoutineId,request.SupervisionUserId);
|
2023-09-18 15:41:41 +03:30
|
|
|
|
newPlan.Id = request.Id;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var shiftPlanUsers = await repositoryWrapper.SetRepository<ShiftPlanUser>()
|
2023-09-18 15:41:41 +03:30
|
|
|
|
.TableNoTracking
|
|
|
|
|
|
.Where(s => s.ShiftPlanId == newPlan.Id)
|
|
|
|
|
|
.ToListAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var shiftPlanUser in shiftPlanUsers)
|
|
|
|
|
|
{
|
2023-11-19 22:06:49 +03:30
|
|
|
|
if (request.UserAndPositionIds.Contains(new KeyValuePair<Guid, Guid>(shiftPlanUser.PositionId,shiftPlanUser.UserId)))
|
|
|
|
|
|
request.UserAndPositionIds.Remove(new KeyValuePair<Guid, Guid>(shiftPlanUser.PositionId, shiftPlanUser.UserId));
|
2023-09-18 15:41:41 +03:30
|
|
|
|
else
|
2023-12-13 12:05:04 +03:30
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
repositoryWrapper.SetRepository<ShiftPlanUser>()
|
2023-09-18 15:41:41 +03:30
|
|
|
|
.Delete(shiftPlanUser);
|
2024-08-09 21:42:04 +03:30
|
|
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
2023-12-13 12:05:04 +03:30
|
|
|
|
}
|
2023-09-18 15:41:41 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-19 22:06:49 +03:30
|
|
|
|
foreach (var userId in request.UserAndPositionIds)
|
|
|
|
|
|
newPlan.AddUser(userId.Key,userId.Value);
|
2023-09-18 15:41:41 +03:30
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
repositoryWrapper.SetRepository<ShiftPlan>()
|
2023-09-18 15:41:41 +03:30
|
|
|
|
.Update(newPlan);
|
|
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
2023-09-18 15:41:41 +03:30
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|