public async Task <IActionResult> CreateLicenceEvent([FromBody] ViewModels.LicenceEvent item) { MicrosoftDynamicsCRMadoxioEvent dynamicsEvent = new MicrosoftDynamicsCRMadoxioEvent(); if (item?.Status == LicenceEventStatus.Submitted) { bool alwaysAuthorization; try { var licence = _dynamicsClient.Licenceses.GetByKey(item.LicenceId); alwaysAuthorization = licence.AdoxioIseventapprovalalwaysrequired == null ? false : (bool)licence.AdoxioIseventapprovalalwaysrequired; } catch (HttpOperationException ex) { _logger.LogError(ex, "Error creating event"); return(BadRequest()); } item.EventClass = item.DetermineEventClass(alwaysAuthorization); if (item.EventClass != EventClass.Authorization || item.EventCategory == EventCategory.Market) { item.Status = LicenceEventStatus.Approved; } else { item.Status = LicenceEventStatus.InReview; } } dynamicsEvent.CopyValues(item); string temp = _httpContextAccessor.HttpContext.Session.GetString("UserSettings"); UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp); dynamicsEvent.AccountODataBind = _dynamicsClient.GetEntityURI("accounts", userSettings.AccountId); if (!string.IsNullOrEmpty(item.LicenceId)) { dynamicsEvent.LicenceODataBind = _dynamicsClient.GetEntityURI("adoxio_licenceses", item.LicenceId); } try { dynamicsEvent = _dynamicsClient.Events.Create(dynamicsEvent); } catch (HttpOperationException ex) { _logger.LogError(ex, "Error creating event"); return(BadRequest()); } LicenceEvent viewModelEvent = dynamicsEvent.ToViewModel(_dynamicsClient); if (item.Schedules != null && item.Schedules.Count > 0) { viewModelEvent.Schedules = new List <LicenceEventSchedule>(); foreach (LicenceEventSchedule eventSchedule in item.Schedules) { MicrosoftDynamicsCRMadoxioEventschedule dynamicsSchedule = new MicrosoftDynamicsCRMadoxioEventschedule(); dynamicsSchedule.CopyValues(eventSchedule); dynamicsSchedule.EventODataBind = _dynamicsClient.GetEntityURI("adoxio_events", dynamicsEvent.AdoxioEventid); MicrosoftDynamicsCRMadoxioEventschedule newEventSchedule = _dynamicsClient.Eventschedules.Create(dynamicsSchedule); viewModelEvent.Schedules.Add(newEventSchedule.ToViewModel()); } } return(new JsonResult(viewModelEvent)); }
public async Task <IActionResult> UpdateLicenceEvent([FromBody] ViewModels.LicenceEvent item, string id) { if (string.IsNullOrEmpty(id)) { return(BadRequest()); } MicrosoftDynamicsCRMadoxioEvent dynamicsEvent = _dynamicsClient.GetEventByIdWithChildren(id); if (dynamicsEvent == null || !CurrentUserHasAccessToEventOwnedBy(dynamicsEvent.AdoxioAccount.Accountid)) { return(new NotFoundResult()); } // not updating security plan if (item?.SecurityPlanSubmitted == null && item?.Status == LicenceEventStatus.Submitted) { // determine event class bool alwaysAuthorization; try { var licence = _dynamicsClient.Licenceses.GetByKey(item.LicenceId); alwaysAuthorization = licence.AdoxioIseventapprovalalwaysrequired == null ? false : (bool)licence.AdoxioIseventapprovalalwaysrequired; } catch (HttpOperationException ex) { _logger.LogError(ex, "Error updating event"); return(BadRequest()); } item.EventClass = item.DetermineEventClass(alwaysAuthorization); if (item.EventClass != EventClass.Authorization) { item.Status = LicenceEventStatus.Approved; } else { item.Status = LicenceEventStatus.InReview; } } MicrosoftDynamicsCRMadoxioEvent patchEvent = new MicrosoftDynamicsCRMadoxioEvent(); patchEvent.CopyValues(item); if (!string.IsNullOrEmpty(item.LicenceId) && item.LicenceId != dynamicsEvent._adoxioLicenceValue) { patchEvent.LicenceODataBind = _dynamicsClient.GetEntityURI("adoxio_licenceses", item.LicenceId); } try { _dynamicsClient.Events.Update(id, patchEvent); } catch (HttpOperationException httpOperationException) { _logger.LogError(httpOperationException, "Error updating event"); } dynamicsEvent = _dynamicsClient.GetEventByIdWithChildren(id); /* Get current event schedules and delete */ LicenceEvent viewModelEvent = dynamicsEvent.ToViewModel(_dynamicsClient); if (viewModelEvent.Schedules != null && viewModelEvent.Schedules.Count > 0) { foreach (LicenceEventSchedule eventSchedule in viewModelEvent.Schedules) { _dynamicsClient.Eventschedules.Delete(eventSchedule.Id); } } /* Create new event schedules */ if (item.Schedules != null && item.Schedules.Count > 0) { viewModelEvent.Schedules = new List <LicenceEventSchedule>(); foreach (LicenceEventSchedule eventSchedule in item.Schedules) { MicrosoftDynamicsCRMadoxioEventschedule dynamicsSchedule = new MicrosoftDynamicsCRMadoxioEventschedule(); dynamicsSchedule.CopyValues(eventSchedule); dynamicsSchedule.EventODataBind = _dynamicsClient.GetEntityURI("adoxio_events", dynamicsEvent.AdoxioEventid); MicrosoftDynamicsCRMadoxioEventschedule newEventSchedule = _dynamicsClient.Eventschedules.Create(dynamicsSchedule); viewModelEvent.Schedules.Add(newEventSchedule.ToViewModel()); } } if (dynamicsEvent != null) { return(new JsonResult(dynamicsEvent.ToViewModel(_dynamicsClient))); } return(new NotFoundResult()); }