Exemplo n.º 1
0
        public async Task <IHttpActionResult> SaveProfile([FromBody] GroupLeaderProfileDTO profile)
        {
            if (ModelState.IsValid)
            {
                return(await Authorized(token =>
                {
                    try
                    {
                        _groupLeaderService.SaveReferences(profile).Zip <int, IList <Unit>, int>(_groupLeaderService.SaveProfile(token, profile),
                                                                                                 (int first, IList <Unit> second) => first).Wait();

                        return Ok();
                    }
                    catch (Exception e)
                    {
                        var apiError = new ApiErrorDto("Saving Leader Profile failed:", e);
                        throw new HttpResponseException(apiError.HttpResponseMessage);
                    }
                }));
            }
            var errors    = ModelState.Values.SelectMany(val => val.Errors).Aggregate("", (current, err) => current + err.ErrorMessage);
            var dataError = new ApiErrorDto("Registration Data Invalid", new InvalidOperationException("Invalid Registration Data" + errors));

            throw new HttpResponseException(dataError.HttpResponseMessage);
        }
Exemplo n.º 2
0
 private static Person PersonMock(GroupLeaderProfileDTO leaderDto)
 {
     return(new Person
     {
         FirstName = leaderDto.NickName,
         LastName = leaderDto.LastName,
         NickName = leaderDto.NickName,
         EmailAddress = leaderDto.Email
     });
 }
Exemplo n.º 3
0
        public void ShouldThrowExceptionWhenSaveProfileFails()
        {
            const string fakeToken  = "letmein";
            var          leaderDto  = new GroupLeaderProfileDTO();
            var          fakePerson = PersonMock(leaderDto);

            _personService.Setup(m => m.GetLoggedInUserProfile(fakeToken)).Returns(fakePerson);
            _userRepo.Setup(m => m.GetUserIdByUsername(It.IsAny <string>()));
            _contactMock.Setup(m => m.UpdateContact(It.IsAny <int>(), It.IsAny <Dictionary <string, object> >())).Throws <ApplicationException>();

            Assert.Throws <ApplicationException>(() => _fixture.SaveProfile(fakeToken, leaderDto).Wait());
        }
Exemplo n.º 4
0
        public IObservable <int> SaveReferences(GroupLeaderProfileDTO leader)
        {
            var form = new MpFormResponse
            {
                ContactId   = leader.ContactId,
                FormId      = _configWrapper.GetConfigIntValue("GroupLeaderFormId"),
                FormAnswers = new List <MpFormAnswer>
                {
                    new MpFormAnswer
                    {
                        FieldId  = _configWrapper.GetConfigIntValue("GroupLeaderReferenceFieldId"),
                        Response = leader.ReferenceContactId
                    },
                    new MpFormAnswer
                    {
                        FieldId  = _configWrapper.GetConfigIntValue("GroupLeaderReferenceNameFieldId"),
                        Response = leader.ReferenceDisplayName ?? ""
                    },
                    new MpFormAnswer
                    {
                        FieldId  = _configWrapper.GetConfigIntValue("GroupLeaderHuddleFieldId"),
                        Response = leader.HuddleResponse
                    },
                    new MpFormAnswer
                    {
                        FieldId  = _configWrapper.GetConfigIntValue("GroupLeaderStudentFieldId"),
                        Response = leader.LeadStudents
                    }
                }
            };

            return(Observable.Create <int>(observer =>
            {
                var responseId = _formSubmissionRepository.SubmitFormResponse(form);
                if (responseId == 0)
                {
                    observer.OnError(new ApplicationException("Unable to submit form response for Group Leader"));
                }
                observer.OnNext(responseId);
                observer.OnCompleted();
                return Disposable.Create(() => Console.WriteLine("Observable destroyed"));
            }));
        }
Exemplo n.º 5
0
        public IObservable <IList <Unit> > SaveProfile(string token, GroupLeaderProfileDTO leader)
        {
            // get the current contact data....
            var currentPerson = _personService.GetLoggedInUserProfile(token);

            currentPerson.CongregationId = leader.Site;
            currentPerson.NickName       = leader.NickName;
            currentPerson.LastName       = leader.LastName;
            currentPerson.EmailAddress   = leader.Email;
            currentPerson.DateOfBirth    = leader.BirthDate.ToShortDateString();
            currentPerson.HouseholdId    = leader.HouseholdId;
            currentPerson.MobilePhone    = leader.MobilePhone;
            currentPerson.AddressId      = leader.AddressId;

            var personDict  = getDictionary(currentPerson.GetContact());
            var userUpdates = currentPerson.GetUserUpdateValues();
            var household   = new MpHousehold
            {
                Address_ID      = currentPerson.AddressId,
                Congregation_ID = currentPerson.CongregationId,
                Home_Phone      = currentPerson.HomePhone,
                Household_ID    = currentPerson.HouseholdId
            };

            try
            {
                userUpdates["User_ID"] = _userRepository.GetUserIdByUsername(leader.OldEmail);
            }
            catch (Exception e)
            {
                throw new Exception($"Unable to find the user account for {leader.OldEmail}", e);
            }

            return(Observable.Zip(
                       Observable.Start(() =>
            {
                _contactRepository.UpdateContact(currentPerson.ContactId, personDict);
                _contactRepository.UpdateHousehold(household);
            }),
                       Observable.Start(() => _userRepository.UpdateUser(userUpdates))));
        }