Exemple #1
0
        public async Task <Dictionary <string, MeetingSlot> > FindMeetingTimes(MeetingRequestInput meetingRequest, string AccessToken)
        {
            string queryParameter = "/findMeetingTimes";

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, graphUrl + queryParameter))
                {
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);

                    FindMeetingTimesRequest fmtr = BuildFMTR(meetingRequest);

                    var postBody = await Task.Run(() => JsonConvert.SerializeObject(fmtr));

                    var httpContent = new StringContent(postBody, Encoding.UTF8, "application/json");
                    request.Content = httpContent;

                    using (var response = await client.SendAsync(request))
                    {
                        string bodyText = await response.Content.ReadAsStringAsync();

                        if (response.IsSuccessStatusCode)
                        {
                            Trace.TraceInformation("Find Meeting Request complete");
                            var findMeetingResponse = JsonConvert.DeserializeObject <FindMeetingTimesResponse>(bodyText);

                            Dictionary <string, MeetingSlot> possibleTimes = new Dictionary <string, MeetingSlot>();

                            foreach (var item in findMeetingResponse.meetingTimeSuggestions)
                            {
                                // HACK: This assumes we're offsetting from Pacific Standard Time
                                string   timeZoneId = "Pacific Standard Time";
                                DateTime startUtc   = item.meetingTimeSlot.start.dateTime;
                                DateTime start      = TimeZoneInfo.ConvertTimeFromUtc(startUtc, TimeZoneInfo.FindSystemTimeZoneById(timeZoneId));

                                possibleTimes.Add(start.ToString(), new MeetingSlot()
                                {
                                    Start = start
                                });
                            }
                            return(possibleTimes);
                        }
                        else
                        {
                            Trace.TraceError(bodyText);
                            return(null);
                        }
                    }
                }
            }
        }
Exemple #2
0
        private FindMeetingTimesRequest BuildFMTR(MeetingRequestInput mri)
        {
            DateTime preferredDate   = mri.RequestedDateTime.Value;
            int      meetingDuration = mri.MeetingDuration.Value;


            FindMeetingTimesRequest fmtr = new FindMeetingTimesRequest();

            // Attendees
            // In this example, we're only looking at the organzier's
            // calendar.
            var me  = new Attendee();
            var ema = new Emailaddress()
            {
                address = mri.OrganizerEmail,
                name    = mri.OrganizerName
            };

            me.emailAddress = ema;
            me.type         = "Required";

            // Location Constraint
            var lc = new Locationconstraint();

            var l = new Location()
            {
                displayName = "Skype"
            };

            lc.locations       = new Location[] { l };
            lc.isRequired      = bool.FalseString;
            lc.suggestLocation = bool.TrueString;

            // Time Constraint
            var tc      = new Timeconstraint();
            var ztStart = new ZonedDateTime();
            var ztEnd   = new ZonedDateTime();

            // TODO: This assumes we're offsetting from Pacific Time
            // https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/resources/dateTimeTimeZone
            var    current      = new DateTime(preferredDate.Year, preferredDate.Month, preferredDate.Day, 17, 0, 0, DateTimeKind.Utc);
            string timeZoneText = "Pacific Standard Time";

            ztStart.dateTime = current;
            ztStart.timeZone = timeZoneText;
            ztEnd.dateTime   = current.AddDays(1).AddHours(1);
            ztEnd.timeZone   = timeZoneText;

            var ts = new Timeslot()
            {
                start = ztStart,
                end   = ztEnd
            };

            tc.timeslots = new Timeslot[] { ts };

            fmtr.attendees          = new Attendee[] { me };
            fmtr.locationConstraint = lc;
            // https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_findmeetingtimes
            // The length of the meeting, denoted in ISO8601 format.
            fmtr.meetingDuration = XmlConvert.ToString(TimeSpan.FromSeconds(meetingDuration));
            fmtr.timeConstraint  = tc;
            fmtr.maxCandidates   = 3;

            return(fmtr);
        }
        public async Task <FindMeetingTimeResponse> PostFindMeetingTimes(string accessToken, FindMeetingTimesRequest findMeetingTimesRequest)
        {
            string endpoint = "https://graph.microsoft.com/v1.0/me/findMeetingTimes";

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                    findMeetingTimesRequest.meetingDuration = "PT1H";

                    request.Content = new StringContent(JsonConvert.SerializeObject(findMeetingTimesRequest), Encoding.UTF8, "application/json");

                    using (var response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            string stringResult = await response.Content.ReadAsStringAsync();

                            FindMeetingTimeResponse findMeetingTimeResponse = JsonConvert.DeserializeObject <FindMeetingTimeResponse>(stringResult);
                            return(findMeetingTimeResponse);
                        }
                        return(null);
                    }
                }
            }
        }
Exemple #4
0
        public async Task <GetRoomResponse> GetSuggestedMeetings(string token, string nombreLista, string entrevistadorMail, DateTime fecha, int centroId, int?oficinaId, bool ignorarDisponibilidad)
        {
            GetRoomResponse response    = new GetRoomResponse();
            var             fechaInicio = DateTime.Parse(fecha.ToUniversalTime().ToString());
            var             fechaFin    = fechaInicio;

            try
            {
                string accessToken = token;


                FindMeetingTimesRequest findMeetingTimeRequest = new FindMeetingTimesRequest();
                var attendees = new List <ApiRooms.Graph.Responses.FindMeetingTimeRequest.Attendee>();

                List <string> possivelAttendees = new List <string>();

                if (!ignorarDisponibilidad)
                {
                    possivelAttendees.Add(entrevistadorMail);
                }

                foreach (var item in possivelAttendees)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        attendees.Add(new ApiRooms.Graph.Responses.FindMeetingTimeRequest.Attendee()
                        {
                            emailAddress = new ApiRooms.Graph.Responses.FindMeetingTimeRequest.EmailAddress()
                            {
                                address = item, name = item
                            },
                            type = "required"
                        });
                    }
                }

                findMeetingTimeRequest.timeConstraint = new TimeConstraint()
                {
                    timeslots = new List <Timeslot>()
                    {
                        new Timeslot()
                        {
                            start = new ApiRooms.Graph.Responses.FindMeetingTimeRequest.Start()
                            {
                                dateTime = fechaInicio, timeZone = "UTC"
                            },
                            end = new ApiRooms.Graph.Responses.FindMeetingTimeRequest.End()
                            {
                                dateTime = fechaFin, timeZone = "UTC"
                            }
                        }
                    }
                };



                // Getting the rooms in the list
                FindRoomsListResponse          listaSalas    = new FindRoomsListResponse();
                GetStringExcludedRoomsResponse excludedRooms = new GetStringExcludedRoomsResponse();
                if (!string.IsNullOrEmpty(nombreLista))
                {
                    listaSalas = await _roomsService.FindRooms(accessToken, nombreLista);

                    excludedRooms = _roomsService.GetStringExcludedRooms(centroId, oficinaId);
                }
                if (excludedRooms.IsValid && !string.IsNullOrEmpty(excludedRooms.StringExcludedRooms))
                {
                    listaSalas = FilterListSalas(listaSalas, excludedRooms.StringExcludedRooms);
                }

                for (int i = 0; i < listaSalas.value.Count; i++)
                {
                    if (i < 18 && i < listaSalas.value.Count)
                    {
                        attendees.Add(new ApiRooms.Graph.Responses.FindMeetingTimeRequest.Attendee()
                        {
                            emailAddress = new ApiRooms.Graph.Responses.FindMeetingTimeRequest.EmailAddress()
                            {
                                address = listaSalas.value[i].address
                            },
                            type = "resource"
                        });
                    }
                    else
                    {
                        break;
                    }
                }


                findMeetingTimeRequest.returnSuggestionReasons = true;
                findMeetingTimeRequest.isOrganizerOptional     = true;
                findMeetingTimeRequest.maxCandidates           = 100;

                findMeetingTimeRequest.attendees = attendees;


                // Get the the meeting times
                var findMeetingTimes = await _roomsService.PostFindMeetingTimes(accessToken, findMeetingTimeRequest);

                if (findMeetingTimes.emptySuggestionsReason.Equals("AttendeesUnavailable"))
                {
                    response.IsValid = false;
                    return(response);
                }

                List <FindMeetingTimesRowViewModel> suggestions = FilterSuggestions(findMeetingTimes, listaSalas);

                if (suggestions.Count == 0)
                {
                    response.IsValid = false;
                    return(response);
                }

                response.IsValid = true;
                response.salas   = new List <SelectListItem>();

                foreach (var sugg in suggestions)
                {
                    foreach (var sala in sugg.Salas)
                    {
                        if (sala.Name != null && sala.Email != null)
                        {
                            SelectListItem listaSala = new SelectListItem();
                            listaSala.Value = sala.Email;
                            listaSala.Text  = sala.Name;

                            response.salas.Add(listaSala);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                response.IsValid      = false;
                response.ErrorMessage = e.Message;
            }
            return(response);
        }