Ejemplo n.º 1
0
        public async Task <IActionResult> CompleteWorkItem(CompleteWorkItemDto dto)
        {
            var workItem = (await _workItemRepository.GetWorkItems(_clock.UtcNow.UtcDateTime, User.UserId(), workItemId: dto.Id)).Single();

            if (workItem.CompletedAt.HasValue || workItem.CanceledAt.HasValue)
            {
                return(Problem("Work item is already complete"));
            }
            else if (User.IsInPatrol(workItem.PatrolId))
            {
                if (workItem.CanComplete || workItem.CanAdmin || User.RoleInPatrol(workItem.PatrolId).CanMaintainWorkItems())
                {
                    //normal user completion
                    if (workItem.CanComplete && !dto.ForUserId.HasValue)
                    {
                        await _workItemService.CompleteWorkItem(dto.Id, User.UserId(), dto.WorkNotes);
                    }
                    //admin force completion
                    else if ((workItem.CanAdmin || User.RoleInPatrol(workItem.PatrolId).CanMaintainWorkItems()) &&
                             !dto.ForUserId.HasValue)
                    {
                        await _workItemService.CompleteWorkItem(dto.Id, User.UserId(), dto.WorkNotes, force : true);
                    }
                    //admin proxy completion
                    else if ((workItem.CanAdmin || User.RoleInPatrol(workItem.PatrolId).CanMaintainWorkItems()) &&
                             dto.ForUserId.HasValue && (workItem.CompletionMode == CompletionMode.Any || workItem.Assignments.Any(y => y.UserId == dto.ForUserId.Value)))
                    {
                        await _workItemService.CompleteWorkItem(dto.Id, User.UserId(), dto.WorkNotes, dto.ForUserId.Value);
                    }
                    //admin force proxy completion
                    else if ((workItem.CanAdmin || User.RoleInPatrol(workItem.PatrolId).CanMaintainWorkItems()) &&
                             dto.ForUserId.HasValue && workItem.Assignments.Any(y => y.UserId == dto.ForUserId.Value))
                    {
                        await _workItemService.CompleteWorkItem(dto.Id, User.UserId(), dto.WorkNotes, dto.ForUserId.Value, true);
                    }
                    else
                    {
                        throw new InvalidOperationException("Invalid completion paramaters");
                    }
                    return(Ok());
                }
                else
                {
                    return(Forbid());
                }
            }
            else
            {
                return(Forbid());
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CancelWorkItem(CompleteWorkItemDto dto)
        {
            var workItem = (await _workItemRepository.GetWorkItems(_clock.UtcNow.UtcDateTime, User.UserId(), workItemId: dto.Id)).Single();

            if (User.IsInPatrol(workItem.PatrolId))
            {
                if (workItem.CanAdmin || User.RoleInPatrol(workItem.PatrolId).CanMaintainWorkItems())
                {
                    await _workItemService.CancelWorkItem(dto.Id, User.UserId());

                    return(Ok());
                }
                else
                {
                    return(Forbid());
                }
            }
            else
            {
                return(Forbid());
            }
        }