Skip to content

Commit

Permalink
#129 Accessibility - overriding accessibilities by period of time and…
Browse files Browse the repository at this point in the history
… employee
  • Loading branch information
kgniazdowski committed Dec 1, 2021
1 parent 36addda commit 361c6b3
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 118 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System.Net;
using System.Threading.Tasks;
using Accessibility.Api.Availabilities;
using Accessibility.Application.Availabilities.Commands.UpdateInPeriodOfTime;
using Accessibility.Application.Availabilities.Queries;
using Accessibility.Application.Availabilities.Queries.GetAvailabilites;
using Accessibility.Domain.Schedules;
using Accessibility.Domain.SharedKernel;
using Core.Queries;
using MediatR;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -38,26 +35,18 @@ public async Task<IActionResult> Get(

[HttpPatch]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> UpdateInPeriodOfTime(
[FromRoute] Guid facilityId,
[FromRoute] Guid scheduleId,
[FromBody] UpdateAvailabilitiesInPeriodOfTimeRequest request)
[FromBody] UpdateAvailabilitiesDto dto)
{
var result = await mediator.Send(new UpdateAvailabilitiesInPeriodOfTimeCommand(
new FacilityId(facilityId),
new ScheduleId(scheduleId),
request.DateFrom,
request.DateTo,
request.Availabilities
facilityId,
scheduleId,
dto
));

if (!result.Success)
{
return NotFound();
}

return Ok(result.Result);
return Ok();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;

namespace Accessibility.Application.Availabilities.Commands.UpdateInPeriodOfTime
{
public record UpdateAvailabilitiesDto(
DateTime DateFrom,
DateTime DateTo,
Guid EmployeeId,
Guid CreatorId,
IEnumerable<UpdatedAvailability> Availabilities
);

public record UpdatedAvailability(
DateTime StartTime,
DateTime EndTime
);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using System;
using System.Collections.Generic;
using Accessibility.Domain.Schedules;
using Accessibility.Domain.SharedKernel;
using Core.Commands;
using MediatR;

namespace Accessibility.Application.Availabilities.Commands.UpdateInPeriodOfTime
{
public record UpdateAvailabilitiesInPeriodOfTimeCommand(
FacilityId FacilityId,
ScheduleId ScheduleId,
DateTime DateFrom,
DateTime DateTo,
IEnumerable<AvailabilityDto> Availabilities
) : IRequest<CommandResult<int>>;
Guid FacilityId,
Guid ScheduleId,
UpdateAvailabilitiesDto Dto
) : ICommand;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
using System.Threading;
using System.Threading.Tasks;
using Accessibility.Domain.Schedules;
using Accessibility.Domain.Schedules.Availabilities;
using Accessibility.Domain.SharedKernel;
using MediatR;
using Core.Domain.UnitOfWork;
using Core.Commands;
using System;

namespace Accessibility.Application.Availabilities.Commands.UpdateInPeriodOfTime
{
public class UpdateAvailabilitiesInPeriodOfTimeCommandHandler : IRequestHandler<UpdateAvailabilitiesInPeriodOfTimeCommand, CommandResult<int>>
public class UpdateAvailabilitiesInPeriodOfTimeCommandHandler : ICommandHandler<UpdateAvailabilitiesInPeriodOfTimeCommand>
{
private readonly IScheduleRepository repository;
private readonly IUnitOfWork unitOfWork;
Expand All @@ -21,28 +21,30 @@ public UpdateAvailabilitiesInPeriodOfTimeCommandHandler(IScheduleRepository repo
this.unitOfWork = unitOfWork;
}

public async Task<CommandResult<int>> Handle(UpdateAvailabilitiesInPeriodOfTimeCommand request, CancellationToken cancellationToken)
public async Task<Unit> Handle(UpdateAvailabilitiesInPeriodOfTimeCommand request, CancellationToken cancellationToken)
{
var groupedAvailabilities = request.Availabilities.GroupBy(a => a.StartTime.Date);
var facilityId = new FacilityId(request.FacilityId);
var scheduleId = new ScheduleId(request.ScheduleId);
var employeeId = new EmployeeId(request.Dto.EmployeeId);
var creatorId = new EmployeeId(request.Dto.CreatorId);

var schedule = await repository.GetByIdAsync(request.ScheduleId, request.FacilityId);
var schedule = await repository.GetByIdAsync(scheduleId, facilityId);

if (schedule == null)
return new CommandResult<int>(false, "Schedule does not exist.");
throw new Exception("Schedule does not exist.");

schedule.OverrideAvailabilitiesInPeriodOfTime(
new PeriodOfTime(request.DateFrom, request.DateTo),
request.Availabilities.Select(a => new AvailabilityData(
new EmployeeId(a.EmployeeId),
new PeriodOfTime(a.StartTime, a.EndTime),
new EmployeeId(a.CreatorId)
)));
new PeriodOfTime(request.Dto.DateFrom, request.Dto.DateTo),
employeeId,
creatorId,
request.Dto.Availabilities.Select(a => new PeriodOfTime(
a.StartTime,
a.EndTime
))
);

schedule.IncreaseVersion();

await unitOfWork.CommitAsync();

return new CommandResult<int>(true, request.Availabilities.Count());
return Unit.Value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.Linq;
using Accessibility.Domain.SharedKernel;
using Core.Domain;

namespace Accessibility.Domain.Schedules.Rules
{
public class AvailabilityCanNotDuplicateInPeriodOfTimeRule : IBusinessRule
{
private readonly IEnumerable<PeriodOfTime> availabilities;

public AvailabilityCanNotDuplicateInPeriodOfTimeRule(IEnumerable<PeriodOfTime> availabilities)
{
this.availabilities = availabilities;
}

public string Message => $"Employee can not have more than one record in the same period of time in rage of one schedule.";

public bool IsBroken()
{
int skipCount = 1;

foreach (var availability in availabilities)
{
if (availabilities
.Skip(skipCount)
.Any(a => a.HasCommonPeriodWithEdges(availability)))
{
return true;
}

skipCount++;
}

return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System.Collections.Generic;
using System.Linq;
using Accessibility.Domain.Schedules.Availabilities;
using Core.Domain;
using Accessibility.Domain.SharedKernel;

namespace Accessibility.Domain.Schedules.Rules
{
public class OverridingAvailabilitiesMustFitPeriodOfTimeRule : IBusinessRule
public class OverridenAvailabilitiesMustFitPeriodOfTimeRule : IBusinessRule
{
private readonly PeriodOfTime periodOfTime;
private readonly IEnumerable<AvailabilityData> availabilities;
private readonly IEnumerable<PeriodOfTime> availabilities;

public OverridingAvailabilitiesMustFitPeriodOfTimeRule(PeriodOfTime periodOfTime, IEnumerable<AvailabilityData> availabilities)
public OverridenAvailabilitiesMustFitPeriodOfTimeRule(PeriodOfTime periodOfTime, IEnumerable<PeriodOfTime> availabilities)
{
this.periodOfTime = periodOfTime;
this.availabilities = availabilities;
Expand All @@ -20,6 +19,6 @@ public OverridingAvailabilitiesMustFitPeriodOfTimeRule(PeriodOfTime periodOfTime
public string Message => "Not all availabilities are within specified period of time.";

public bool IsBroken() =>
availabilities.Any(a => !a.PeriodOfTime.IsInRange(periodOfTime));
availabilities.Any(a => !a.IsInRange(periodOfTime));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,32 @@ public Schedule(
AddDomainEvent(new ScheduleCreatedEvent(Id));
}

public void OverrideAvailabilitiesInPeriodOfTime(PeriodOfTime periodOfTime, IEnumerable<AvailabilityData> availabilities)
public void OverrideAvailabilitiesInPeriodOfTime(
PeriodOfTime periodOfTime,
EmployeeId employeeId,
EmployeeId creatorId,
IEnumerable<PeriodOfTime> availabilities)
{
CheckRule(new OverridingAvailabilitiesMustFitPeriodOfTimeRule(periodOfTime, availabilities));
CheckRule(new WorkerAvailabilityCanNotDuplicateInPeriodOfTimeRule(availabilities));

var oldAvailabilities = this.availabilities.Where(a => a.PeriodOfTime.IsInRange(periodOfTime)).ToList();

CheckRule(new OverridenAvailabilitiesMustFitPeriodOfTimeRule(periodOfTime, availabilities));
CheckRule(new AvailabilityCanNotDuplicateInPeriodOfTimeRule(availabilities));

var oldAvailabilities = this.availabilities
.Where(a =>
a.PeriodOfTime.IsInRange(periodOfTime) &&
a.EmployeeId == employeeId)
.ToList();

var foundAvailabilities = new List<Availability>();

foreach (var availability in availabilities)
{
var old = oldAvailabilities.FirstOrDefault(a =>
a.EmployeeId == availability.EmployeeId &&
a.PeriodOfTime.Equals(availability.PeriodOfTime));

a.PeriodOfTime.Equals(availability));

if (old == null)
{
this.availabilities.Add(Availability.Create(availability.EmployeeId, availability.PeriodOfTime, availability.CreatorId));
}
this.availabilities.Add(Availability.Create(employeeId, availability, creatorId));
else
{
foundAvailabilities.Add(old);
}
}

foreach (var old in oldAvailabilities)
Expand All @@ -87,7 +90,7 @@ public void Modify(

public void CreateCorrection(List<AvailabilityData> corrections)
{
CheckRule(new WorkerAvailabilityCanNotDuplicateInPeriodOfTimeRule(corrections));
CheckRule(new AvailabilityCanNotDuplicateInPeriodOfTimeRule(corrections.Select(c => c.PeriodOfTime)));

var currentPriority = availabilities.Max(a => a.Priority);
var nextPriority = (short)(currentPriority + 1);
Expand Down

0 comments on commit 361c6b3

Please sign in to comment.