fix(GetShiftPlanSortByShift)

master
Amir Hossein Khademi 2024-08-09 20:03:12 +03:30
parent 5328e74f79
commit bca1447ad4
3 changed files with 35 additions and 27 deletions

View File

@ -1,24 +1,20 @@
namespace Brizco.Repository.Handlers.ShiftPlans;
public class CreateShiftPlanCommandHandler : IRequestHandler<CreateShiftPlanCommand, ShiftPlanLDto>
public class CreateShiftPlanCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
: IRequestHandler<CreateShiftPlanCommand, ShiftPlanLDto>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ICurrentUserService _currentUserService;
private readonly ICurrentUserService _currentUserService = currentUserService;
public CreateShiftPlanCommandHandler(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService)
{
_repositoryWrapper = repositoryWrapper;
_currentUserService = currentUserService;
}
public async Task<ShiftPlanLDto> Handle(CreateShiftPlanCommand request, CancellationToken cancellationToken)
{
var shift = await _repositoryWrapper.SetRepository<Shift>()
var shift = await repositoryWrapper.SetRepository<Shift>()
.TableNoTracking
.FirstOrDefaultAsync(s => s.Id == request.ShiftId, cancellationToken);
if (shift == null)
throw new AppException("Shift not found", ApiResultStatusCode.NotFound);
var planFor = DateTimeExtensions.UnixTimeStampToDateTime(request.PlanDate);
var existedShiftPlan = await _repositoryWrapper.SetRepository<ShiftPlan>()
var planDate = DateTimeExtensions.UnixTimeStampToDateTime(request.PlanDate);
var planFor = new DateTime(planDate.Year, planDate.Month, planDate.Day, shift.StartAt.Hours, shift.StartAt.Minutes, shift.StartAt.Seconds);
var existedShiftPlan = await repositoryWrapper.SetRepository<ShiftPlan>()
.TableNoTracking
.FirstOrDefaultAsync(s => s.ShiftId == request.ShiftId && s.PlanFor.Date == planFor.Date, cancellationToken);
@ -27,7 +23,7 @@ public class CreateShiftPlanCommandHandler : IRequestHandler<CreateShiftPlanComm
try
{
await _repositoryWrapper.BeginTransaction(cancellationToken);
await repositoryWrapper.BeginTransaction(cancellationToken);
var shiftPlan = shift.AddPlan(planFor, request.RoutineId, request.SupervisionUserId);
if (request.UserAndPositionIds.Count == 0)
@ -39,14 +35,14 @@ public class CreateShiftPlanCommandHandler : IRequestHandler<CreateShiftPlanComm
shiftPlan.AddUser(userAndPositionId.Key, userAndPositionId.Value);
}
_repositoryWrapper.SetRepository<ShiftPlan>().Add(shiftPlan);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
await _repositoryWrapper.CommitAsync(cancellationToken);
repositoryWrapper.SetRepository<ShiftPlan>().Add(shiftPlan);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
await repositoryWrapper.CommitAsync(cancellationToken);
return shiftPlan.AdaptToLDto();
}
catch (Exception )
{
await _repositoryWrapper.RollBackAsync(cancellationToken);
await repositoryWrapper.RollBackAsync(cancellationToken);
throw;
}
}

View File

@ -17,7 +17,7 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu
.Where(s => s.ComplexId == complexId);
List<ShiftPlanSDto> shiftPlans = new List<ShiftPlanSDto>();
bool setActiviesCount = false;
bool setActivitiesCount = false;
if (request.SelectedDate == 0)
{
@ -34,13 +34,13 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu
.Skip(request.Page * 15).Take(15)
.Select(ShiftPlanMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
setActiviesCount = true;
setActivitiesCount = true;
}
if (request.DateTimeQueryFilter != null)
{
setActiviesCount = true;
setActivitiesCount = true;
switch (request.DateTimeQueryFilter)
{
case DateTimeQueryFilter.Today:
@ -62,16 +62,21 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu
DateTime startOfPastWeek = DateTime.Today.AddDays(pastWeekDelta).AddDays(-7);
DateTime endOfPastWeek = startOfPastWeek.AddDays(6);
shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfPastWeek.Date && s.PlanFor.Date <= endOfPastWeek.Date)
.OrderByDescending(s => s.CreatedAt)
.OrderByDescending(s => s.PlanFor)
.Skip(request.Page * 15).Take(15)
.Select(ShiftPlanMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
break;
case DateTimeQueryFilter.NextWeek:
DateTime startOfNextWeek = DateTime.Today.AddDays(7 - (int)DateTime.Today.DayOfWeek + 1);
DateTime nextWeekToday = DateTime.Today;
int nextWeekDelta = DayOfWeek.Saturday - nextWeekToday.DayOfWeek;
if (nextWeekDelta > 0) nextWeekDelta -= 7;
DateTime startOfNextWeek = DateTime.Today.AddDays(nextWeekDelta).AddDays(+7);
DateTime endOfNextWeek = startOfNextWeek.AddDays(6);
shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfNextWeek.Date && s.PlanFor.Date <= endOfNextWeek.Date)
.OrderByDescending(s => s.CreatedAt)
.OrderByDescending(s => s.PlanFor)
.Skip(request.Page * 15).Take(15)
.Select(ShiftPlanMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
@ -83,7 +88,8 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu
DateTime startOfWeek = today.AddDays(delta);
DateTime endOfWeek = startOfWeek.AddDays(6);
shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfWeek.Date && s.PlanFor.Date <= endOfWeek.Date)
.OrderByDescending(s => s.CreatedAt)
.OrderBy(s=>s.PlanFor.TimeOfDay)
.ThenByDescending(s => s.PlanFor.Date)
.Skip(request.Page * 15).Take(15)
.Select(ShiftPlanMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
@ -93,7 +99,7 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu
DateTime startOfPastMonth = new PersianDateTime(PersianDateTime.Today.AddMonths(-1).Year, PersianDateTime.Today.AddMonths(-1).Month, 1).ToDateTime();
DateTime endOfPastMonth = startOfPastMonth.AddMonths(1);
shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfPastMonth.Date && s.PlanFor.Date < endOfPastMonth.Date)
.OrderByDescending(s => s.CreatedAt)
.OrderByDescending(s => s.PlanFor)
.Skip(request.Page * 15).Take(15)
.Select(ShiftPlanMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
@ -102,7 +108,7 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu
DateTime startOfNextMonth = new PersianDateTime(PersianDateTime.Today.AddMonths(1).Year, PersianDateTime.Today.AddMonths(1).Month, 1).ToDateTime();
DateTime endOfNextMonth = startOfNextMonth.AddMonths(1);
shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfNextMonth.Date && s.PlanFor.Date < endOfNextMonth.Date)
.OrderByDescending(s => s.CreatedAt)
.OrderByDescending(s => s.PlanFor)
.Skip(request.Page * 15).Take(15)
.Select(ShiftPlanMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
@ -112,7 +118,7 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu
DateTime startOfThisMonth = new PersianDateTime(PersianDateTime.Today.Year, PersianDateTime.Today.Month, 1).ToDateTime();
DateTime endOfThisMonth = startOfThisMonth.AddMonths(1);
shiftPlans = await baseQuery.Where(s => s.PlanFor.Date >= startOfThisMonth.Date && s.PlanFor.Date < endOfThisMonth.Date)
.OrderByDescending(s => s.CreatedAt)
.OrderByDescending(s => s.PlanFor)
.Skip(request.Page * 15).Take(15)
.Select(ShiftPlanMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
@ -125,7 +131,7 @@ public class GetShiftPlansQueryHandler(IRepositoryWrapper repositoryWrapper, ICu
}
}
if (setActiviesCount)
if (setActivitiesCount)
{
foreach (var shiftPlan in shiftPlans)

View File

@ -34,6 +34,12 @@ public class GetStaffQueryHandler(IRepositoryWrapper repositoryWrapper,ICurrentU
.Select(ActivityMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
break;
case DateTimeQueryFilter.Tomorrow:
activities = await baseQuery.Where(s => s.SetFor.Date == DateTime.Now.AddDays(1).Date)
.Skip(request.Page * count).Take(count)
.Select(ActivityMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
break;
case DateTimeQueryFilter.PastWeek:
DateTime pastWeekToday = DateTime.Today;
int pastWeekDelta = DayOfWeek.Saturday - pastWeekToday.DayOfWeek;