Exemplo n.º 1
0
        public async Task <GetNewShiftActionsResponse> Handle(GetNewShiftActionsRequest request, CancellationToken cancellationToken)
        {
            Dictionary <NewTaskAction, List <int> > taskActions = new Dictionary <NewTaskAction, List <int> >();

            GetRequestHelpFormVariantResponse requestJourney = _repository.GetRequestHelpFormVariant(request.ReferringGroupId, request.Source ?? "", cancellationToken);

            if (requestJourney == null)
            {
                throw new Exception("null response from GetRequestHelpFormVariant");
            }

            List <int> targetGroups = GetAvailableGroups(requestJourney, request.ReferringGroupId, request.Source, cancellationToken);

            taskActions.Add(NewTaskAction.MakeAvailableToGroups, targetGroups);
            taskActions.Add(NewTaskAction.SendRequestorConfirmation, null);

            if (requestJourney.RequestsRequireApproval)
            {
                taskActions.Add(NewTaskAction.NotifyGroupAdmins, new List <int> {
                    request.ReferringGroupId
                });
            }
            else
            {
                taskActions.Add(NewTaskAction.SetStatusToOpen, null);
                taskActions.Add(NewTaskAction.NotifyMatchingVolunteers, targetGroups);
            }

            return(new GetNewShiftActionsResponse()
            {
                TaskActions = taskActions
            });
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]
            [RequestBodyType(typeof(GetRequestHelpFormVariantRequest), "get request help form variant")] GetRequestHelpFormVariantRequest req,
            CancellationToken cancellationToken)
        {
            try
            {
                if (req.IsValid(out var validationResults))
                {
                    GetRequestHelpFormVariantResponse response = await _mediator.Send(req, cancellationToken);

                    return(new OkObjectResult(ResponseWrapper <GetRequestHelpFormVariantResponse, GroupServiceErrorCode> .CreateSuccessfulResponse(response)));
                }
                else
                {
                    return(new ObjectResult(ResponseWrapper <GetRequestHelpFormVariantResponse, GroupServiceErrorCode> .CreateUnsuccessfulResponse(GroupServiceErrorCode.ValidationError, validationResults))
                    {
                        StatusCode = 422
                    });
                }
            }
            catch (Exception ex)
            {
                _logger.LogErrorAndNotifyNewRelic($"Unhandled error in GetRequestHelpFormVariant", ex);
                return(new ObjectResult(ResponseWrapper <GetRequestHelpFormVariantResponse, GroupServiceErrorCode> .CreateUnsuccessfulResponse(GroupServiceErrorCode.InternalServerError, "Internal Error"))
                {
                    StatusCode = StatusCodes.Status500InternalServerError
                });
            }
        }
Exemplo n.º 3
0
        private List <int> GetAvailableGroups(GetRequestHelpFormVariantResponse requestJourney, int referringGroupId, string source, CancellationToken cancellationToken)
        {
            int  targetGroupId;
            bool includeChildGroups;

            switch (requestJourney.TargetGroups)
            {
            case TargetGroups.GenericGroup:
                targetGroupId      = -1;
                includeChildGroups = false;
                break;

            case TargetGroups.ParentGroup:
                targetGroupId      = _repository.GetGroupById(referringGroupId, cancellationToken).ParentGroupId.Value;
                includeChildGroups = false;
                break;

            case TargetGroups.SiblingsAndParentGroup:
                targetGroupId      = _repository.GetGroupById(referringGroupId, cancellationToken).ParentGroupId.Value;
                includeChildGroups = true;
                break;

            case TargetGroups.ThisGroup:
                targetGroupId      = referringGroupId;
                includeChildGroups = false;
                break;

            case TargetGroups.ThisGroupAndChildren:
                targetGroupId      = referringGroupId;
                includeChildGroups = true;
                break;

            default:
                throw new Exception($"Unexpected TargetGroups value {requestJourney.TargetGroups}");
            }

            List <int> targetGroups;

            if (includeChildGroups)
            {
                targetGroups = _repository.GetGroupAndChildGroups(targetGroupId, cancellationToken);

                if (targetGroups == null)
                {
                    throw new Exception("Null response from GetGroupAndChildGroups");
                }
            }
            else
            {
                targetGroups = new List <int>()
                {
                    targetGroupId
                };
            }

            return(targetGroups);
        }
Exemplo n.º 4
0
        private void SetupGroupService()
        {
            _groupService = new Mock <IGroupService>();
            Dictionary <Guid, TaskAction> actions = new Dictionary <Guid, TaskAction>();

            actions.Add(Guid.NewGuid(), new TaskAction()
            {
                TaskActions = new Dictionary <NewTaskAction, List <int> >()
            });

            _getNewRequestActionsResponse = new GetNewRequestActionsResponse()
            {
                Actions = actions
            };


            _groupService.Setup(x => x.GetNewRequestActions(It.IsAny <GetNewRequestActionsRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => _getNewRequestActionsResponse);

            _getGroupMembersResponse = new GetGroupMembersResponse()
            {
                Users = new List <int>()
                {
                    1, 2
                }
            };

            _groupService.Setup(x => x.GetGroupMembers(It.IsAny <int>()))
            .ReturnsAsync(() => _getGroupMembersResponse);

            _formVariantResponse = new GetRequestHelpFormVariantResponse()
            {
                AccessRestrictedByRole           = false,
                RequestorDefinedByGroup          = false,
                RequestHelpFormVariant           = RequestHelpFormVariant.Default,
                TargetGroups                     = TargetGroups.GenericGroup,
                SuppressRecipientPersonalDetails = true
            };

            _groupService.Setup(x => x.GetRequestHelpFormVariant(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => _formVariantResponse);

            _groupService.Setup(x => x.GetGroupMember(It.IsAny <GetGroupMemberRequest>()))
            .ReturnsAsync(() => _getGroupMemberResponse);

            _groupService.Setup(x => x.GetUserRoles(It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => _getUserRolesResponse);

            _groupService.Setup(x => x.GetGroup(It.IsAny <int>()))
            .ReturnsAsync(() => _getGroupResponse);
        }
Exemplo n.º 5
0
        private void SetupGroupService()
        {
            _groupService = new Mock <IGroupService>();

            _formVariantResponse = new GetRequestHelpFormVariantResponse()
            {
                AccessRestrictedByRole  = false,
                RequestorDefinedByGroup = true,
                RequestHelpFormVariant  = RequestHelpFormVariant.Default,
                TargetGroups            = TargetGroups.GenericGroup
            };

            _groupService.Setup(x => x.GetRequestHelpFormVariant(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => _formVariantResponse);

            _groupService.Setup(x => x.GetNewShiftActions(It.IsAny <GetNewShiftActionsRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => _getNewShiftActionsResponse);
        }
Exemplo n.º 6
0
        public async Task WhenIPostRequest_WithMissingSuppressRecipientPersonalDetail_IGetRejected()
        {
            _validPostcode = true;

            _formVariantResponse = new GetRequestHelpFormVariantResponse()
            {
                AccessRestrictedByRole           = false,
                RequestorDefinedByGroup          = false,
                RequestHelpFormVariant           = RequestHelpFormVariant.Default,
                TargetGroups                     = TargetGroups.GenericGroup,
                SuppressRecipientPersonalDetails = null
            };

            var request = new PostNewRequestForHelpRequest
            {
                HelpRequest = new HelpRequest
                {
                    RequestorType = RequestorType.Myself,
                    Requestor     = new RequestPersonalDetails
                    {
                        Address = new Address
                        {
                            Postcode = "test",
                        }
                    }
                },
                NewJobsRequest = new NewJobsRequest
                {
                    Jobs = new List <Job>
                    {
                        new Job
                        {
                            HealthCritical  = true,
                            DueDays         = 5,
                            SupportActivity = SupportActivities.Shopping
                        }
                    }
                }
            };

            var response = await _classUnderTest.Handle(request, new CancellationToken());

            Assert.AreEqual(Fulfillable.Rejected_ConfigurationError, response.Fulfillable);
        }
Exemplo n.º 7
0
        public async Task <GetNewRequestActionsResponse> Handle(GetNewRequestActionsRequest request, CancellationToken cancellationToken)
        {
            Dictionary <Guid, TaskAction>           actions            = new Dictionary <Guid, TaskAction>();
            Dictionary <NewTaskAction, List <int> > requestTaskActions = new Dictionary <NewTaskAction, List <int> >();

            GetRequestHelpFormVariantResponse requestJourney = _repository.GetRequestHelpFormVariant(request.HelpRequest.ReferringGroupId, request.HelpRequest.Source ?? "", cancellationToken);

            if (requestJourney == null)
            {
                throw new Exception("null response from GetRequestHelpFormVariant");
            }

            List <int> targetGroups = GetTargetGroups(requestJourney, request.HelpRequest.ReferringGroupId, request.HelpRequest.Source, cancellationToken);

            if (requestJourney.RequestsRequireApproval)
            {
                requestTaskActions.Add(NewTaskAction.NotifyGroupAdmins, new List <int> {
                    request.HelpRequest.ReferringGroupId
                });
            }
            else
            {
                requestTaskActions.Add(NewTaskAction.SetStatusToOpen, null);
            }
            requestTaskActions.Add(NewTaskAction.SendRequestorConfirmation, null);
            requestTaskActions.Add(NewTaskAction.MakeAvailableToGroups, targetGroups);

            RequestType requestType = request.NewJobsRequest.Jobs.First().SupportActivity.RequestType();

            if (!requestJourney.RequestsRequireApproval && requestType == RequestType.Task)
            {
                requestTaskActions.Add(NewTaskAction.NotifyMatchingVolunteers, targetGroups);
            }

            return(new GetNewRequestActionsResponse()
            {
                Actions = actions,
                RequestTaskActions = requestTaskActions
            });
        }
Exemplo n.º 8
0
        public async Task WhenAccessRestrictedByRole_ReturnsCorrectResponse(int createdByUserId, GroupRoles role, Fulfillable fulfillable, int timesGroupMemberCalled)
        {
            requestId      = 1;
            _validPostcode = true;
            _emailSent     = true;
            int referringGroupId = -20;

            Guid guid    = Guid.NewGuid();
            var  request = new PostNewRequestForHelpRequest
            {
                HelpRequest = new HelpRequest
                {
                    RequestorType = RequestorType.Myself,
                    Recipient     = new RequestPersonalDetails
                    {
                        Address = new Address
                        {
                            Postcode = "test",
                        }
                    },
                    VolunteerUserId  = 1,
                    CreatedByUserId  = createdByUserId,
                    ReferringGroupId = referringGroupId
                },
                NewJobsRequest = new NewJobsRequest
                {
                    Jobs = new List <Job>
                    {
                        new Job
                        {
                            Guid            = guid,
                            HealthCritical  = true,
                            DueDays         = 5,
                            SupportActivity = SupportActivities.Shopping
                        }
                    }
                }
            };

            _formVariantResponse = new GetRequestHelpFormVariantResponse()
            {
                AccessRestrictedByRole   = true,
                RequestorDefinedByGroup  = true,
                RequestHelpFormVariant   = RequestHelpFormVariant.Default,
                TargetGroups             = TargetGroups.GenericGroup,
                RequestorPersonalDetails = new RequestPersonalDetails()
                {
                    FirstName    = "First",
                    LastName     = "Last",
                    EmailAddress = "Email",
                    MobileNumber = "Mobile",
                    OtherNumber  = "Other"
                },
                SuppressRecipientPersonalDetails = true
            };

            _getGroupMemberResponse = new GetGroupMemberResponse()
            {
                UserInGroup = new UserInGroup()
                {
                    UserId     = 1,
                    GroupId    = 1,
                    GroupRoles = new List <GroupRoles>()
                    {
                        role
                    }
                }
            };

            _getGroupResponse = new GetGroupResponse()
            {
                Group = new Group()
                {
                    GroupId = referringGroupId
                }
            };

            _getUserRolesResponse = new GetUserRolesResponse()
            {
                UserGroupRoles = new Dictionary <int, List <int> >()
            };

            _getNewRequestActionsResponse = new GetNewRequestActionsResponse()
            {
                Actions = new Dictionary <Guid, TaskAction>(), RequestTaskActions = new Dictionary <NewTaskAction, List <int> >()
            };
            _getNewRequestActionsResponse.Actions.Add(guid, new TaskAction()
            {
                TaskActions = new Dictionary <NewTaskAction, List <int> >()
            });
            _getNewRequestActionsResponse.Actions[guid].TaskActions.Add(NewTaskAction.AssignToVolunteer, new List <int>()
            {
                1
            });

            var response = await _classUnderTest.Handle(request, new CancellationToken());

            _groupService.Verify(x => x.GetRequestHelpFormVariant(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <CancellationToken>()), Times.Once);
            _groupService.Verify(x => x.GetGroupMember(It.IsAny <GetGroupMemberRequest>()), Times.Exactly(timesGroupMemberCalled));
            Assert.AreEqual(fulfillable, response.Fulfillable);
        }
Exemplo n.º 9
0
        public async Task WhenRequestorDefinedByGroup_Populate()
        {
            _validPostcode = true;
            _emailSent     = true;
            Guid guid    = Guid.NewGuid();
            var  request = new PostNewRequestForHelpRequest
            {
                HelpRequest = new HelpRequest
                {
                    RequestorType = RequestorType.Myself,
                    Recipient     = new RequestPersonalDetails
                    {
                        Address = new Address
                        {
                            Postcode = "test",
                        }
                    },
                    VolunteerUserId = 1,
                },
                NewJobsRequest = new NewJobsRequest
                {
                    Jobs = new List <Job>
                    {
                        new Job
                        {
                            Guid            = guid,
                            HealthCritical  = true,
                            DueDays         = 5,
                            SupportActivity = SupportActivities.Shopping
                        }
                    }
                }
            };

            _formVariantResponse = new GetRequestHelpFormVariantResponse()
            {
                AccessRestrictedByRole   = false,
                RequestorDefinedByGroup  = true,
                RequestHelpFormVariant   = RequestHelpFormVariant.Default,
                TargetGroups             = TargetGroups.GenericGroup,
                RequestorPersonalDetails = new RequestPersonalDetails()
                {
                    FirstName    = "First",
                    LastName     = "Last",
                    EmailAddress = "Email",
                    MobileNumber = "Mobile",
                    OtherNumber  = "Other"
                },
                SuppressRecipientPersonalDetails = true
            };



            _getNewRequestActionsResponse = new GetNewRequestActionsResponse()
            {
                Actions = new Dictionary <Guid, TaskAction>(), RequestTaskActions = new Dictionary <NewTaskAction, List <int> >()
            };
            _getNewRequestActionsResponse.Actions.Add(guid, new TaskAction()
            {
                TaskActions = new Dictionary <NewTaskAction, List <int> >()
            });
            _getNewRequestActionsResponse.Actions[guid].TaskActions.Add(NewTaskAction.AssignToVolunteer, new List <int>()
            {
                1
            });

            var response = await _classUnderTest.Handle(request, new CancellationToken());

            _groupService.Verify(x => x.GetRequestHelpFormVariant(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <CancellationToken>()), Times.Once);
            Assert.AreEqual(_formVariantResponse.RequestorPersonalDetails, request.HelpRequest.Requestor);
            Assert.AreEqual(Fulfillable.Accepted_DiyRequest, response.Fulfillable);
        }