Beispiel #1
0
        public virtual HttpResponseMessage CanPlaceRegistrant(int registrantId, int registrationTemplatePlacementId, int groupId)
        {
            var rockContext = this.Service.Context as RockContext;
            RegistrationTemplatePlacementService registrationTemplatePlacementService = new RegistrationTemplatePlacementService(rockContext);

            var registrationTemplatePlacement = registrationTemplatePlacementService.Get(registrationTemplatePlacementId);

            // make sure the specified registrant, registrationTemplatePlacement, and group are found
            if (registrationTemplatePlacement == null)
            {
                return(ControllerContext.Request.CreateErrorResponse(
                           HttpStatusCode.NotFound,
                           $"Specified RegistrationTemplatePlacement not found."));
            }

            var group = new GroupService(rockContext).Get(groupId);

            if (group == null)
            {
                return(ControllerContext.Request.CreateErrorResponse(
                           HttpStatusCode.NotFound,
                           $"Specified group not found."));
            }

            var registrantInfo = new RegistrationRegistrantService(rockContext).GetSelect(registrantId, s => new
            {
                s.PersonAlias.Person,
                s.Registration.RegistrationInstance
            });

            if (registrantInfo == null)
            {
                return(ControllerContext.Request.CreateErrorResponse(
                           HttpStatusCode.NotFound,
                           $"Specified registrant not found."));
            }

            if (registrationTemplatePlacement.GroupTypeId != group.GroupTypeId)
            {
                return(ControllerContext.Request.CreateErrorResponse(
                           HttpStatusCode.BadRequest,
                           $"Specified group's group type does not match the group type of the registration placement."));
            }

            RegistrationInstanceService registrationInstanceService = new RegistrationInstanceService(rockContext);
            var sharedGroupsQuery    = registrationTemplatePlacementService.GetRegistrationTemplatePlacementPlacementGroups(registrationTemplatePlacement);
            var instanceGroupsQuery  = registrationInstanceService.GetRegistrationInstancePlacementGroups(registrantInfo.RegistrationInstance).Where(a => a.GroupTypeId == registrationTemplatePlacement.GroupTypeId);
            var placementGroupsQuery = sharedGroupsQuery.Union(instanceGroupsQuery);

            // make sure the specified group is one of the placement groups

            if (!placementGroupsQuery.Any(a => a.Id == groupId))
            {
                return(ControllerContext.Request.CreateErrorResponse(
                           HttpStatusCode.NotFound,
                           $"{group.Name} is not a placement group for this registration template or instance."));
            }

            // If multiple placements are allowed, and there are no more things to check, we can return an OK
            if (registrationTemplatePlacement.AllowMultiplePlacements)
            {
                return(new HttpResponseMessage(HttpStatusCode.OK));
            }
            else
            {
                var personAlreadyInPlacementGroup = placementGroupsQuery.Where(a => a.Id != groupId).Any(a => a.Members.Any(m => m.PersonId == registrantInfo.Person.Id));

                if (personAlreadyInPlacementGroup)
                {
                    return(ControllerContext.Request.CreateErrorResponse(
                               HttpStatusCode.BadRequest,
                               $"{registrantInfo.Person} can not be in more than one {registrationTemplatePlacement.Name} group"));
                }
            }

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }