public IHttpActionResult ApproveChildcareRequest(int requestId, ChildcareRequestDto childcareRequest) { if (!ModelState.IsValid) { var errors = ModelState.Values.SelectMany(val => val.Errors).Aggregate("", (current, err) => current + err.Exception.Message); var dataError = new ApiErrorDto("Save Request Data Invalid", new InvalidOperationException("Invalid Save request Data" + errors)); throw new HttpResponseException(dataError.HttpResponseMessage); } return(Authorized(token => { try { _childcareService.ApproveChildcareRequest(requestId, childcareRequest, token); return Ok(); } catch (EventMissingException e) { var errors = new DateError() { Errors = e.MissingDateTimes, Message = e.Message }; var json = JsonConvert.SerializeObject(errors, Formatting.Indented); var message = new HttpResponseMessage(HttpStatusCode.RequestedRangeNotSatisfiable); message.Content = new StringContent(json); throw new HttpResponseException(message); } catch (ChildcareDatesMissingException e) { var json = JsonConvert.SerializeObject(e.Message, Formatting.None); var message = new HttpResponseMessage(HttpStatusCode.NotAcceptable); message.Content = new StringContent(json); throw new HttpResponseException(message); } catch (DuplicateChildcareEventsException e) { var error = new DateError() { Error = e.RequestedDate, Message = e.Message }; var json = JsonConvert.SerializeObject(error, Formatting.Indented); var message = new HttpResponseMessage(HttpStatusCode.MultipleChoices); message.Content = new StringContent(json); throw new HttpResponseException(message); } catch (Exception e) { var apiError = new ApiErrorDto("Approve Childcare Request Failed", e); throw new HttpResponseException(apiError.HttpResponseMessage); } })); }
public void CreateChildcareRequest(ChildcareRequestDto request, String token) { var mpRequest = request.ToMPChildcareRequest(); var childcareRequestId = _childcareRequestService.CreateChildcareRequest(mpRequest); _childcareRequestService.CreateChildcareRequestDates(childcareRequestId, mpRequest, token); try { var childcareRequest = _childcareRequestService.GetChildcareRequest(childcareRequestId, token); SendChildcareRequestNotification(childcareRequest); } catch (Exception ex) { _logger.Error(string.Format("Save Request failed"), ex); } }
public void UpdateChildcareRequest(ChildcareRequestDto request, string token) { var mpRequest = request.ToMPChildcareRequest(); _childcareRequestService.UpdateChildcareRequest(mpRequest); //delete the current childcare request dates _childcareRequestService.DeleteAllChildcareRequestDates(request.ChildcareRequestId); //add the new dates _childcareRequestService.CreateChildcareRequestDates(request.ChildcareRequestId, mpRequest, token); try { var childcareRequest = _childcareRequestService.GetChildcareRequest(request.ChildcareRequestId, token); SendChildcareRequestNotification(childcareRequest); } catch (Exception ex) { _logger.Error("Update Request failed", ex); } }
public void RejectChildcareRequest(int childcareRequestId, ChildcareRequestDto childcareRequest, string token) { try { //set the approved column for dates to false var childcareDates = _childcareRequestService.GetChildcareRequestDates(childcareRequestId); foreach (var ccareDates in childcareDates) { _childcareRequestService.DecisionChildcareRequestDate(ccareDates.ChildcareRequestDateId, false); } _childcareRequestService.DecisionChildcareRequest(childcareRequestId, _configurationWrapper.GetConfigIntValue("ChildcareRequestRejected"), childcareRequest.ToMPChildcareRequest()); var templateId = _configurationWrapper.GetConfigIntValue("ChildcareRequestRejectionNotificationTemplate"); SendChildcareRequestDecisionNotification(childcareRequestId, childcareDates, childcareRequest, templateId, token); } catch (Exception ex) { _logger.Error(string.Format("Update Request failed"), ex); throw new Exception("Reject Childcare failed", ex); } }
public IHttpActionResult RejectChildcareRequest(int requestId, ChildcareRequestDto childcareRequest) { if (!ModelState.IsValid) { var errors = ModelState.Values.SelectMany(val => val.Errors).Aggregate("", (current, err) => current + err.Exception.Message); var dataError = new ApiErrorDto("Save Request Data Invalid", new InvalidOperationException("Invalid Save request Data" + errors)); throw new HttpResponseException(dataError.HttpResponseMessage); } return(Authorized(token => { try { _childcareService.RejectChildcareRequest(requestId, childcareRequest, token); return Ok(); } catch (Exception e) { var apiError = new ApiErrorDto("Reject Childcare Request Failed", e); throw new HttpResponseException(apiError.HttpResponseMessage); } })); }
private void SendChildcareRequestDecisionNotification(int requestId, List <MpChildcareRequestDate> childcareRequestDates, ChildcareRequestDto childcareRequest, int templateId, String token) { var childcareRequestEmail = _childcareRequestService.GetChildcareRequest(requestId, token);; var template = _communicationService.GetTemplate(templateId); var decisionNotes = childcareRequest.DecisionNotes ?? "N/A"; var authorUserId = _configurationWrapper.GetConfigIntValue("DefaultUserAuthorId"); var datesList = childcareRequestDates.Select(dateRec => dateRec.RequestDate).Select(requestDate => BuildParagraph("", requestDate.ToShortDateString())).ToList(); var styles = Styles(); var htmlCell = new HtmlElement("td", styles).Append(datesList); var htmlRow = new HtmlElement("tr", styles).Append(htmlCell); var htmlTBody = new HtmlElement("tbody", styles).Append(htmlRow); var htmlTable = new HtmlElement("table", styles).Append(htmlTBody); var mergeData = new Dictionary <string, object> { { "Group", childcareRequestEmail.GroupName }, { "ChildcareSession", childcareRequestEmail.ChildcareSession }, { "DecisionNotes", decisionNotes }, { "Frequency", childcareRequest.Frequency }, { "Dates", htmlTable.Build() }, { "RequestId", childcareRequestEmail.RequestId }, { "Base_Url", _configurationWrapper.GetConfigValue("BaseMPUrl") }, { "Congregation", childcareRequestEmail.CongregationName } }; var toContactsList = new List <MpContact> { new MpContact { ContactId = childcareRequestEmail.RequesterId, EmailAddress = childcareRequestEmail.RequesterEmail } }; var communication = new MinistryPlatform.Translation.Models.MpCommunication { AuthorUserId = authorUserId, EmailBody = template.Body, EmailSubject = template.Subject, FromContact = new MpContact { ContactId = childcareRequestEmail.ChildcareContactId, EmailAddress = childcareRequestEmail.ChildcareContactEmail }, ReplyToContact = new MpContact { ContactId = childcareRequestEmail.ChildcareContactId, EmailAddress = childcareRequestEmail.ChildcareContactEmail }, ToContacts = toContactsList, MergeData = mergeData }; try { _communicationService.SendMessage(communication); } catch (Exception ex) { _logger.Error(string.Format("Send Childcare request approval notification email failed"), ex); } }
// TODO: Should we merge childcareRequestId into the childcareRequestDto? public void ApproveChildcareRequest(int childcareRequestId, ChildcareRequestDto childcareRequest, string token) { try { var request = GetChildcareRequestForReview(childcareRequestId, token); var datesFromRequest = _childcareRequestService.GetChildcareRequestDates(childcareRequestId); var requestedDates = childcareRequest.DatesList.Select(date => GetChildcareDateFromList(datesFromRequest, date)).ToList(); if (requestedDates.Count == 0) { throw new ChildcareDatesMissingException(childcareRequestId); } var events = _childcareRequestService.FindChildcareEvents(childcareRequestId, requestedDates, request); var childcareEvents = GetChildcareEventsfortheDates(events, requestedDates, request); var missingDates = requestedDates.Where(childcareRequestDate => !childcareEvents.ContainsKey(childcareRequestDate.ChildcareRequestDateId)).ToList(); if (missingDates.Count > 0) { var dateList = missingDates.Aggregate("", (current, date) => current + ", " + date.RequestDate.ToShortDateString()); _logger.Debug("Missing events for dates: ${dateList}"); var dateMap = missingDates.Select(c => c.RequestDate).ToList(); throw new EventMissingException(dateMap); } //set the approved column for dates to true foreach (var ccareDates in requestedDates) { _childcareRequestService.DecisionChildcareRequestDate(ccareDates.ChildcareRequestDateId, true); var eventId = childcareEvents.Where((ev) => ev.Key == ccareDates.ChildcareRequestDateId).Select((ev) => ev.Value).SingleOrDefault(); var eventGroup = new MpEventGroup() { Closed = false, Created = true, EventId = eventId, GroupId = request.GroupId }; var currentGroups = _eventService.GetGroupsForEvent(eventId).Select((g) => g.GroupId).ToList(); if (!currentGroups.Contains(request.GroupId)) { _eventService.CreateEventGroup(eventGroup, token); } } var requestStatusId = GetApprovalStatus(datesFromRequest, requestedDates); _childcareRequestService.DecisionChildcareRequest(childcareRequestId, requestStatusId, childcareRequest.ToMPChildcareRequest()); var templateId = GetApprovalEmailTemplate(requestStatusId); SendChildcareRequestDecisionNotification(childcareRequestId, requestedDates, childcareRequest, templateId, token); } catch (EventMissingException ex) { throw; } catch (ChildcareDatesMissingException ex) { throw; } catch (DuplicateChildcareEventsException ex) { throw; } catch (Exception ex) { _logger.Error(string.Format("Update Request failed"), ex); throw new Exception("Approve Childcare failed", ex); } }