Exemplo n.º 1
0
        public IHttpActionResult SaveCampReservation([FromBody] CampReservationDTO campReservation, int eventId)
        {
            if (!ModelState.IsValid)
            {
                var errors    = ModelState.Values.SelectMany(val => val.Errors).Aggregate("", (current, err) => current + err.Exception.Message);
                var dataError = new ApiErrorDto("Camper Application data Invalid", new InvalidOperationException("Invalid Camper Application Data" + errors));
                throw new HttpResponseException(dataError.HttpResponseMessage);
            }

            return(Authorized(token =>
            {
                try
                {
                    var newCamperInfo = _campService.SaveCampReservation(campReservation, eventId, token);
                    return Ok(newCamperInfo);
                }
                catch (ApplicationException e)
                {
                    throw new HttpResponseException(HttpStatusCode.PreconditionFailed);
                }
                catch (Exception e)
                {
                    var apiError = new ApiErrorDto("Camp Reservation failed", e);
                    throw new HttpResponseException(apiError.HttpResponseMessage);
                }
            }));
        }
Exemplo n.º 2
0
        public CampReservationDTO SaveCampReservation(CampReservationDTO campReservation, int eventId, string token)
        {
            var nickName  = string.IsNullOrWhiteSpace(campReservation.PreferredName) ? campReservation.FirstName : campReservation.PreferredName;
            var contactId = Convert.ToInt32(campReservation.ContactId);

            var minorContact = new MpContact
            {
                FirstName           = campReservation.FirstName,
                LastName            = campReservation.LastName,
                PreferredName       = $"{campReservation.LastName}, {campReservation.FirstName}",
                MiddleName          = campReservation.MiddleName,
                MobilePhone         = campReservation.MobilePhone,
                BirthDate           = Convert.ToDateTime(campReservation.BirthDate),
                Gender              = campReservation.Gender,
                Nickname            = nickName,
                SchoolAttending     = campReservation.SchoolAttending,
                HouseholdId         = (_contactRepository.GetMyProfile(token)).Household_ID,
                HouseholdPositionId = 2
            };

            MpParticipant participant;

            if (campReservation.ContactId == null || campReservation.ContactId == 0)
            {
                var newMinorContact = _contactRepository.CreateContact(minorContact);
                contactId   = newMinorContact[0].RecordId;
                participant = _participantRepository.GetParticipant(contactId);
                campReservation.ContactId = contactId;
            }
            else
            {
                var updateToDictionary = new Dictionary <String, Object>
                {
                    { "Contact_ID", Convert.ToInt32(campReservation.ContactId) },
                    { "First_Name", minorContact.FirstName },
                    { "Last_Name", minorContact.LastName },
                    { "Middle_Name", minorContact.MiddleName },
                    { "Nickname", nickName },
                    { "Mobile_Phone", minorContact.MobilePhone },
                    { "Gender_ID", campReservation.Gender },
                    { "Date_Of_Birth", minorContact.BirthDate },
                    { "Current_School", minorContact.SchoolAttending },
                    { "Congregation_Name", (_congregationRepository.GetCongregationById(campReservation.CrossroadsSite)).Name }
                };

                _contactRepository.UpdateContact(Convert.ToInt32(campReservation.ContactId), updateToDictionary);
                participant = _participantRepository.GetParticipant(Convert.ToInt32(campReservation.ContactId));
            }

            // Save shirt size if set
            var configuration = MpObjectAttributeConfigurationFactory.Contact();

            _objectAttributeService.SaveObjectAttributes(contactId, campReservation.AttributeTypes, campReservation.SingleAttributes, configuration);

            // Save students in selected grade group
            var group = _groupRepository.GetGradeGroupForContact(contactId, _apiUserRepository.GetToken());

            if (group.Status && group.Value.GroupId != campReservation.CurrentGrade)
            {
                _groupRepository.endDateGroupParticipant(group.Value.GroupParticipantId, group.Value.GroupId, DateTime.Now);
                _groupRepository.addParticipantToGroup(participant.ParticipantId,
                                                       campReservation.CurrentGrade,
                                                       _configurationWrapper.GetConfigIntValue("Group_Role_Default_ID"),
                                                       false,
                                                       DateTime.Now);
            }
            else if (!group.Status)
            {
                _groupRepository.addParticipantToGroup(participant.ParticipantId,
                                                       campReservation.CurrentGrade,
                                                       _configurationWrapper.GetConfigIntValue("Group_Role_Default_ID"),
                                                       false,
                                                       DateTime.Now);
            }

            // Check if this person is already an event participant
            var eventParticipant = _eventParticipantRepository.GetEventParticipantEligibility(eventId, contactId);
            var currentlyActive  = (eventParticipant != null && (eventParticipant.EndDate == null || eventParticipant.EndDate >= DateTime.Now));
            var rulesPass        = true;

            if (!currentlyActive)
            {
                rulesPass = _campRules.VerifyCampRules(eventId, campReservation.Gender);
            }

            // ALL OF THE BELOW CODE SHOULD ONLY HAPPEN IF THE RULES PASS
            if (rulesPass)
            {
                //Create eventParticipant
                int eventParticipantId;

                // This is a new event participant, determine their pending timeout and create their entry in the database
                if (eventParticipant == null)
                {
                    var endDate = DetermineEndDate(eventId);
                    eventParticipantId = _eventRepository.RegisterInterestedParticipantWithEndDate(participant.ParticipantId, eventId, endDate);
                }
                else
                {
                    eventParticipantId = eventParticipant.EventParticipantId;

                    // If the participant had previously started an application which expired, update its End Date now
                    if (eventParticipant.EndDate != null && eventParticipant.EndDate < DateTime.Now)
                    {
                        var endDate = DetermineEndDate(eventId);
                        _eventRepository.UpdateParticipantEndDate(eventParticipantId, endDate);
                    }
                }
                var crossroadsSite = _congregationRepository.GetCongregationById(campReservation.CrossroadsSite);

                //form response
                var answers = new List <MpFormAnswer>
                {
                    new MpFormAnswer
                    {
                        Response           = campReservation.SchoolAttendingNext,
                        FieldId            = _configurationWrapper.GetConfigIntValue("SummerCampForm.SchoolAttendingNextYear"),
                        EventParticipantId = eventParticipantId
                    },
                    new MpFormAnswer
                    {
                        Response           = campReservation.RoomMate,
                        FieldId            = _configurationWrapper.GetConfigIntValue("SummerCampForm.PreferredRoommate"),
                        EventParticipantId = eventParticipantId
                    },
                    new MpFormAnswer
                    {
                        Response           = crossroadsSite.Name,
                        FieldId            = _configurationWrapper.GetConfigIntValue("SummerCampForm.CamperCongregation"),
                        EventParticipantId = eventParticipantId
                    }
                };

                var formId       = _configurationWrapper.GetConfigIntValue("SummerCampFormID");
                var formResponse = new MpFormResponse
                {
                    ContactId   = contactId,
                    FormId      = formId,
                    FormAnswers = answers,
                    EventId     = eventId
                };

                _formSubmissionRepository.SubmitFormResponse(formResponse);
                return(campReservation);
            }
            throw new ApplicationException("Rules do not pass!");
        }