Example #1
0
        public async Task <IHttpActionResult> SaveSpiritualGrowth([FromBody] SpiritualGrowthDTO spiritualGrowth)
        {
            if (ModelState.IsValid)
            {
                return(await Authorized(token =>
                {
                    try
                    {
                        _groupLeaderService.SaveSpiritualGrowth(spiritualGrowth)
                        .Concat(_groupLeaderService.SetApplied(token)).Wait();

                        _groupLeaderService.GetApplicationData(spiritualGrowth.ContactId).Subscribe((res) =>
                        {
                            if ((string)res["referenceContactId"] != "0")
                            {
                                _groupLeaderService.SendReferenceEmail(res).Subscribe(CancellationToken.None);
                            }
                            else
                            {
                                _groupLeaderService.SendNoReferenceEmail(res).Subscribe(CancellationToken.None);
                            }

                            if (((string)res["studentLeaderRequest"]).ToUpper() == "TRUE")
                            {
                                _groupLeaderService.SendStudentMinistryRequestEmail(res).Subscribe(CancellationToken.None);
                            }
                        });
                        return Ok();
                    }
                    catch (Exception e)
                    {
                        var apiError = new ApiErrorDto("Saving SpiritualGrowth 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("Spiritual Growth Data Invalid", new InvalidOperationException("Invalid Spiritual Growth Data" + errors));

            throw new HttpResponseException(dataError.HttpResponseMessage);
        }
Example #2
0
        public IObservable <int> SaveSpiritualGrowth(SpiritualGrowthDTO spiritualGrowth)
        {
            var form = new MpFormResponse()
            {
                ContactId   = spiritualGrowth.ContactId,
                FormId      = _configWrapper.GetConfigIntValue("GroupLeaderFormId"),
                FormAnswers = new List <MpFormAnswer>
                {
                    new MpFormAnswer
                    {
                        FieldId  = _configWrapper.GetConfigIntValue("GroupLeaderFormStoryFieldId"),
                        Response = spiritualGrowth.Story
                    },
                    new MpFormAnswer
                    {
                        FieldId  = _configWrapper.GetConfigIntValue("GroupLeaderFormTaughtFieldId"),
                        Response = spiritualGrowth.Taught
                    }
                }
            };

            return(Observable.Create <int>(observer =>
            {
                var responseId = _formSubmissionRepository.SubmitFormResponse(form);
                if (responseId == 0)
                {
                    observer.OnError(new ApplicationException("Unable to submit Spiritual Growth form for Group Leader"));
                }

                SendConfirmationEmail(spiritualGrowth.ContactId, spiritualGrowth.EmailAddress);

                observer.OnNext(responseId);
                observer.OnCompleted();
                return Disposable.Create(() => Console.WriteLine("Observable Destroyed"));
            }));
        }