public async Task <ActionResult <List <ShiftAvailabilityDto> > > GetAvailability(int locationId, DateTimeOffset start, DateTimeOffset end) { if (start >= end) { return(BadRequest("Start date was on or after end date.")); } if (end.Subtract(start).TotalDays > 30) { return(BadRequest("End date and start date are more than 30 days apart.")); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } var shiftAvailability = await ShiftService.GetShiftAvailability(start, end, locationId : locationId); if (!User.HasPermission(Permission.ViewAllFutureShifts)) { var location = await Db.Location.AsNoTracking().FirstOrDefaultAsync(l => l.Id == locationId); var timezone = location.Timezone; var restrictionDays = int.Parse(Configuration.GetNonEmptyValue("ViewShiftRestrictionDays")); var currentDate = DateTimeOffset.UtcNow.ConvertToTimezone(timezone).DateOnly(); var endDate = currentDate.TranslateDateForDaylightSavings(timezone, restrictionDays + 1); foreach (var sa in shiftAvailability) { sa.Conflicts = sa.Conflicts.WhereToList(c => c.Start < endDate); } } return(Ok(shiftAvailability.Adapt <List <ShiftAvailabilityDto> >())); }
public async Task <ActionResult <List <ShiftDto> > > GetShifts(int locationId, DateTimeOffset start, DateTimeOffset end, bool includeDuties = false) { if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } if (!User.HasPermission(Permission.ViewDutyRoster)) { includeDuties = false; } var shifts = await ShiftService.GetShiftsForLocation(locationId, start, end, includeDuties); if (!User.HasPermission(Permission.ViewAllFutureShifts)) { var location = await Db.Location.AsNoTracking().FirstOrDefaultAsync(l => l.Id == locationId); var timezone = location.Timezone; var restrictionDays = int.Parse(Configuration.GetNonEmptyValue("ViewShiftRestrictionDays")); var currentDate = DateTimeOffset.UtcNow.ConvertToTimezone(timezone).DateOnly(); var endDate = currentDate.TranslateDateForDaylightSavings(timezone, restrictionDays + 1); shifts = shifts.WhereToList(s => s.StartDate < endDate); } return(Ok(shifts.Adapt <List <ShiftDto> >())); }
public async Task <ActionResult <List <AuditDto> > > ViewRoleHistory(Guid sheriffId) { var sheriff = await SheriffService.GetSheriff(sheriffId, null); if (sheriff == null) { return(NotFound(CouldNotFindSheriffError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, sheriff.HomeLocationId)) { return(Forbid()); } var userRoleIds = Db.UserRole.AsNoTracking().Where(ur => ur.UserId == sheriffId).Select(ur => ur.Id); var roleHistory = Db.Audit.AsNoTracking().Include(a => a.CreatedBy).Where(e => e.TableName == "UserRole" && userRoleIds.Contains(e.KeyValues.RootElement.GetProperty("Id") .GetInt32())) .ToList(); //Have to select, because we have adapt ignore on these properties. return(Ok(roleHistory.Select(s => { var audit = s.Adapt <AuditDto>(); audit.CreatedBy = s.CreatedBy.Adapt <SheriffDto>(); audit.CreatedOn = s.CreatedOn; audit.CreatedById = s.CreatedById; return audit; }))); }
public async Task <ActionResult <AssignmentDto> > UpdateAssignment(UpdateAssignmentDto assignmentDto) { if (assignmentDto == null) { return(BadRequest(InvalidAssignmentError)); } if (assignmentDto.Start >= assignmentDto.End) { return(BadRequest("Start time was on or after end time.")); } var savedAssignment = await AssignmentService.GetAssignment(assignmentDto.Id); if (savedAssignment == null) { return(NotFound()); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, savedAssignment.LocationId)) { return(Forbid()); } var updateAssignment = assignmentDto.Adapt <Assignment>(); var updatedAssignment = await AssignmentService.UpdateAssignment(updateAssignment); return(Ok(updatedAssignment.Adapt <AssignmentDto>())); }
public async Task <ActionResult <List <ShiftAvailabilityDto> > > GetDistributeScheduleForLocation(int locationId, DateTimeOffset start, DateTimeOffset end, bool includeWorkSection) { if (start >= end) { return(BadRequest("Start date was on or after end date.")); } if (end.Subtract(start).TotalDays > 30) { return(BadRequest("End date and start date are more than 30 days apart.")); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } if (!User.HasPermission(Permission.ViewDutyRoster)) { includeWorkSection = false; } var shiftAvailability = await ShiftService.GetShiftAvailability(start, end, locationId : locationId); var shiftsWithDuties = await DistributeScheduleService.GetDistributeSchedule(shiftAvailability, includeWorkSection, start, end, locationId); if (!User.HasPermission(Permission.ViewAllFutureShifts) || !User.HasPermission(Permission.ViewDutyRosterInFuture)) { var location = await Db.Location.AsNoTracking().FirstOrDefaultAsync(l => l.Id == locationId); var timezone = location.Timezone; var currentDate = DateTimeOffset.UtcNow.ConvertToTimezone(timezone).DateOnly(); if (!User.HasPermission(Permission.ViewAllFutureShifts)) { var shiftRestrictionDays = int.Parse(Configuration.GetNonEmptyValue("ViewShiftRestrictionDays")); var endDateShift = currentDate.TranslateDateForDaylightSavings(timezone, shiftRestrictionDays + 1); foreach (var sa in shiftsWithDuties) { sa.Conflicts = sa.Conflicts.WhereToList(c => c.Start < endDateShift); } } if (!User.HasPermission(Permission.ViewDutyRosterInFuture)) { var dutyRestrictionHours = float.Parse(Configuration.GetNonEmptyValue("ViewDutyRosterRestrictionHours")); var endDateDuties = currentDate.TranslateDateForDaylightSavings(timezone, hoursToShift: dutyRestrictionHours); foreach (var sa in shiftsWithDuties) { sa.Conflicts.WhereToList(c => c.Start > endDateDuties) .ForEach(c => c.WorkSection = null); } } } return(Ok(shiftsWithDuties.Adapt <List <ShiftAvailabilityDto> >())); }
public async Task <ActionResult <List <AssignmentDto> > > GetAssignments(int locationId, DateTimeOffset?start, DateTimeOffset?end) { if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } var assignments = await AssignmentService.GetAssignments(locationId, start, end); return(Ok(assignments.Adapt <List <AssignmentDto> >())); }
public async Task <ActionResult <List <DutyDto> > > GetDuties(int locationId, DateTimeOffset start, DateTimeOffset end) { if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } var duties = await DutyRosterService.GetDutiesForLocation(locationId, start, end); return(Ok(duties.Adapt <List <DutyDto> >())); }
public async Task <ActionResult <ImportedShiftsDto> > ImportWeeklyShifts(int locationId, DateTimeOffset start) { if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } var shifts = await ShiftService.ImportWeeklyShifts(locationId, start); return(Ok(shifts.Adapt <ImportedShiftsDto>())); }
private async Task CheckForAccessToSheriffByLocation(Guid?id, string badgeNumber = null) { var savedSheriff = await SheriffService.GetSheriff(id, badgeNumber); if (savedSheriff == null) { throw new NotFoundException(CouldNotFindSheriffError); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, savedSheriff.HomeLocationId)) { throw new NotAuthorizedException(); } }
public async Task <ActionResult <SheriffDto> > AddSheriff(CreateSheriffDto sheriffDto) { if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, sheriffDto.HomeLocationId)) { return(Forbid()); } var sheriff = sheriffDto.Adapt <Sheriff>(); sheriff = await SheriffService.AddSheriff(sheriff); return(Ok(sheriff.Adapt <SheriffDto>())); }
public async Task <ActionResult <List <ShiftDto> > > GetShifts(int locationId, DateTimeOffset start, DateTimeOffset end, bool includeDuties = false) { if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } if (!User.HasPermission(Permission.ViewDuties)) { includeDuties = false; } var shifts = await ShiftService.GetShiftsForLocation(locationId, start, end, includeDuties); return(Ok(shifts.Adapt <List <ShiftDto> >())); }
public async Task <ActionResult <DutyDto> > AddDuty(AddDutyDto newDuty) { if (newDuty == null) { return(BadRequest(InvalidDutyErrorMessage)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, newDuty.LocationId)) { return(Forbid()); } var duty = await DutyRosterService.AddDuty(newDuty.Adapt <Duty>()); return(Ok(duty.Adapt <DutyDto>())); }
public async Task <ActionResult <LookupCodeDto> > Add(AddLookupCodeDto lookupCodeDto) { if (lookupCodeDto == null) { return(BadRequest(InvalidLookupCodeError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, lookupCodeDto.LocationId)) { return(Forbid()); } var lookupCode = await ManageTypesService.Add(lookupCodeDto); return(Ok(lookupCode.Adapt <LookupCodeDto>())); }
public async Task <ActionResult> UpdateSortOrders(SortOrdersDto sortOrdersDto) { if (sortOrdersDto == null) { return(BadRequest(InvalidLookupCodeError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, sortOrdersDto.SortOrderLocationId)) { return(Forbid()); } await ManageTypesService.UpdateSortOrders(sortOrdersDto); return(NoContent()); }
public async Task <ActionResult <SheriffDto> > GetSheriffForTeam(Guid id) { var sheriff = await SheriffService.GetFilteredSheriffForTeams(id); if (sheriff == null) { return(NotFound(CouldNotFindSheriffError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, sheriff.HomeLocationId)) { return(Forbid()); } return(Ok(sheriff.Adapt <SheriffDto>())); }
public async Task <ActionResult <AssignmentDto> > AddAssignment(AddAssignmentDto assignmentDto) { if (assignmentDto == null) { return(BadRequest(InvalidAssignmentError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, assignmentDto.LocationId)) { return(Forbid()); } var assignment = assignmentDto.Adapt <Assignment>(); var createdAssignment = await AssignmentService.CreateAssignment(assignment); return(Ok(createdAssignment.Adapt <AssignmentDto>())); }
public async Task <ActionResult <ShiftDto> > ExpireShifts(List <int> ids) { var locationIds = await ShiftService.GetShiftsLocations(ids); if (locationIds.Count != 1) { return(BadRequest(CannotUpdateCrossLocationError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationIds.First())) { return(Forbid()); } await ShiftService.ExpireShifts(ids); return(NoContent()); }
public async Task <ActionResult> ExpireDuty(int id) { var duty = await DutyRosterService.GetDuty(id); if (duty == null) { return(NotFound()); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, duty.LocationId)) { return(Forbid()); } await DutyRosterService.ExpireDuty(id); return(NoContent()); }
public async Task <ActionResult <DutyDto> > MoveSheriffFromDutySlot(int fromDutySlotId, int toDutyId, DateTimeOffset?separationTime = null) { var duty = await DutyRosterService.GetDutyByDutySlot(fromDutySlotId); if (duty == null) { return(NotFound()); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, duty.LocationId)) { return(Forbid()); } duty = await DutyRosterService.MoveSheriffFromDutySlot(fromDutySlotId, toDutyId, separationTime); return(Ok(duty.Adapt <DutyDto>())); }
public async Task <ActionResult> ExpireAssignment(int id, string expiryReason, DateTimeOffset?expiryDate = null) { var savedAssignment = await AssignmentService.GetAssignment(id); if (savedAssignment == null) { return(NotFound()); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, savedAssignment.LocationId)) { return(Forbid()); } await AssignmentService.ExpireAssignment(id, expiryReason, expiryDate); return(NoContent()); }
public async Task <ActionResult <LookupCodeDto> > UnExpire(int id) { var entity = await ManageTypesService.Find(id); if (entity == null) { return(NotFound()); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, entity.LocationId)) { return(Forbid()); } var lookupCode = await ManageTypesService.Unexpire(id); return(Ok(lookupCode.Adapt <LookupCodeDto>())); }
public async Task <ActionResult <SheriffWithIdirDto> > GetSheriffForTeam(Guid id) { var sheriff = await SheriffService.GetFilteredSheriffForTeams(id); if (sheriff == null) { return(NotFound(CouldNotFindSheriffError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, sheriff.HomeLocationId)) { return(Forbid()); } var sheriffDto = sheriff.Adapt <SheriffWithIdirDto>(); //Prevent exposing Idirs to regular users. sheriffDto.IdirName = User.HasPermission(Permission.EditIdir) ? sheriff.IdirName : null; return(Ok(sheriffDto)); }
public async Task <ActionResult> UpdateDutyComment(int dutyId, string comment) { var locationIds = await DutyRosterService.GetDutiesLocations(new List <int> { dutyId }); if (locationIds.Count != 1) { return(BadRequest(CannotUpdateCrossLocationError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationIds.First())) { return(Forbid()); } await DutyRosterService.UpdateDutyComment(dutyId, comment); return(NoContent()); }
private async Task CheckForAccessToSheriffByLocation <T>(int id) where T : SheriffEvent { var sheriffEvent = await SheriffService.GetSheriffEvent <T>(id); if (sheriffEvent == null) { throw new NotFoundException(CouldNotFindSheriffEventError); } var savedSheriff = await SheriffService.GetSheriff(sheriffEvent.SheriffId, null); if (savedSheriff == null) { throw new NotFoundException(CouldNotFindSheriffError); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, savedSheriff.HomeLocationId)) { throw new NotAuthorizedException(); } }
public async Task <ActionResult <List <ShiftAvailabilityDto> > > GetAvailability(int locationId, DateTimeOffset start, DateTimeOffset end) { if (start >= end) { throw new BusinessLayerException("Start date was on or after end date."); } if (end.Subtract(start).TotalDays > 30) { throw new BusinessLayerException("End date and start date are more than 30 days apart."); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } var shiftAvailability = await ShiftService.GetShiftAvailability(locationId, start, end); return(Ok(shiftAvailability.Adapt <List <ShiftAvailabilityDto> >())); }
public async Task <ActionResult <List <DutyDto> > > GetDuties(int locationId, DateTimeOffset start, DateTimeOffset end) { if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } var duties = await DutyRosterService.GetDutiesForLocation(locationId, start, end); if (!User.HasPermission(Permission.ViewDutyRosterInFuture)) { var location = await Db.Location.AsNoTracking().FirstOrDefaultAsync(l => l.Id == locationId); var timezone = location.Timezone; var currentDate = DateTimeOffset.UtcNow.ConvertToTimezone(timezone).DateOnly(); var restrictionHours = float.Parse(Configuration.GetNonEmptyValue("ViewDutyRosterRestrictionHours")); var endDate = currentDate.TranslateDateForDaylightSavings(timezone, hoursToShift: restrictionHours); duties = duties.WhereToList(d => d.StartDate < endDate); } return(Ok(duties.Adapt <List <DutyDto> >())); }
public async Task <ActionResult <List <DutyDto> > > UpdateDuties(List <UpdateDutyDto> editDuties, bool overrideValidation = false) { if (editDuties == null) { return(BadRequest(InvalidDutyErrorMessage)); } var locationIds = await DutyRosterService.GetDutiesLocations(editDuties.SelectToList(d => d.Id)); if (locationIds.Count != 1) { return(BadRequest(CannotUpdateCrossLocationError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationIds.First())) { return(Forbid()); } var duties = await DutyRosterService.UpdateDuties(editDuties.Adapt <List <Duty> >(), overrideValidation); return(Ok(duties.Adapt <List <DutyDto> >())); }
public async Task <ActionResult> ExpireDuties(List <int> ids) { if (ids == null) { return(BadRequest(InvalidDutyErrorMessage)); } var locationIds = await DutyRosterService.GetDutiesLocations(ids); if (locationIds.Count != 1) { return(BadRequest(CannotUpdateCrossLocationError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationIds.First())) { return(Forbid()); } await DutyRosterService.ExpireDuties(ids); return(NoContent()); }
public async Task <ActionResult <List <ShiftDto> > > UpdateShifts(List <UpdateShiftDto> shiftDtos) { if (shiftDtos == null) { return(BadRequest(InvalidShiftError)); } var locationIds = await ShiftService.GetShiftsLocations(shiftDtos.SelectDistinctToList(s => s.Id)); if (locationIds.Count != 1) { return(BadRequest(CannotUpdateCrossLocationError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationIds.First())) { return(Forbid()); } var shift = await ShiftService.UpdateShifts(shiftDtos.Adapt <List <Shift> >()); return(Ok(shift.Adapt <List <ShiftDto> >())); }
public async Task <ActionResult <List <DutyDto> > > AddDuties(List <AddDutyDto> newDuties) { if (newDuties == null) { return(BadRequest(InvalidDutyErrorMessage)); } var locationIds = newDuties.SelectDistinctToList(d => d.LocationId); if (locationIds.Count != 1) { return(BadRequest(CannotUpdateCrossLocationError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationIds.First())) { return(Forbid()); } var duty = await DutyRosterService.AddDuties(newDuties.Adapt <List <Duty> >()); return(Ok(duty.Adapt <List <DutyDto> >())); }