Exemple #1
0
        /// <summary>
        /// Get schedule for rooms.
        /// </summary>
        /// <param name="search">Object containing search query and time.</param>
        /// <param name="rooms">Room emails.</param>
        /// <param name="token">User Active Directory access token.</param>
        /// <returns>List of schedule for rooms.</returns>
        public async Task <RoomScheduleResponse> GetRoomScheduleAsync(ScheduleSearch search, IList <string> rooms, string token)
        {
            DateTime.TryParse(search.Time, out DateTime localTime);
            var startDateTime = localTime.AddMinutes(Constants.DurationGapFromNow.Minutes);
            var endDateTime   = startDateTime.AddMinutes(Constants.DefaultMeetingDuration.Minutes);

            ScheduleRequest schedule = new ScheduleRequest
            {
                StartDateTime = new DateTimeAndTimeZone()
                {
                    DateTime = startDateTime, TimeZone = search.TimeZone
                },
                EndDateTime = new DateTimeAndTimeZone()
                {
                    DateTime = endDateTime, TimeZone = search.TimeZone
                },
                Schedules = new List <string>(),
            };

            schedule.Schedules.AddRange(rooms);
            return(await this.meetingProvider.GetRoomsScheduleAsync(schedule, token).ConfigureAwait(false));
        }
        /// <summary>
        /// Get top 'N' rooms from table storage where 'N' is room count.
        /// </summary>
        /// <param name="search">Schedule search object.</param>
        /// <returns>Returns list of rooms.</returns>
        public async Task <ActionResult <List <SearchResult> > > TopNRoomsAsync([FromBody] ScheduleSearch search)
        {
            try
            {
                var claims = this.GetUserClaims();
                var token  = await this.tokenHelper.GetUserTokenAsync(claims.FromId).ConfigureAwait(false);

                if (string.IsNullOrEmpty(token))
                {
                    this.telemetryClient.TrackTrace($"Azure Active Directory access token for user {claims.UserObjectIdentifer} is empty. Cannot get rooms.");
                    return(this.Unauthorized());
                }

                var allRooms = await this.roomCollectionStorageProvider.GetNRoomsAsync(this.initialRoomCount).ConfigureAwait(false);

                if (allRooms == null)
                {
                    return(this.StatusCode(StatusCodes.Status500InternalServerError, "Unable to fetch rooms from storage"));
                }

                if (search.IsScheduleRequired)
                {
                    var conversionResult = DateTime.TryParse(search.Time, out DateTime localTime);
                    var startUTCDateTime = localTime.AddMinutes(Constants.DurationGapFromNow.Minutes);
                    var startDateTime    = TimeZoneInfo.ConvertTimeFromUtc(startUTCDateTime, TimeZoneInfo.FindSystemTimeZoneById(TZConvert.IanaToWindows(search.TimeZone)));
                    search.Time = startDateTime.ToString("yyyy-MM-dd HH:mm:ss");
                    var rooms            = allRooms.Select(room => room.RowKey).ToList();
                    var scheduleResponse = await this.meetingHelper.GetRoomScheduleAsync(search, rooms, token).ConfigureAwait(false);

                    if (scheduleResponse.ErrorResponse != null)
                    {
                        // Graph API returned error message.
                        return(this.StatusCode((int)scheduleResponse.StatusCode, scheduleResponse.ErrorResponse.Error.ErrorMessage));
                    }

                    var searchResults = allRooms.Select(searchResult => new SearchResult(searchResult)
                    {
                        Label    = searchResult.RoomName,
                        Value    = searchResult.RowKey,
                        Sublabel = searchResult.BuildingName,
                        Status   = scheduleResponse?.Schedules.Where(schedule => schedule.ScheduleId == searchResult.RowKey).FirstOrDefault()?.ScheduleItems?.Count > 0 ? Strings.Unavailable : Strings.Available,
                    }).ToList();

                    return(this.Ok(searchResults));
                }
                else
                {
                    var searchResults = allRooms.Select(searchResult => new SearchResult(searchResult)
                    {
                        Label    = searchResult.RoomName,
                        Value    = searchResult.RowKey,
                        Sublabel = searchResult.BuildingName,
                    }).ToList();

                    return(this.Ok(searchResults));
                }
            }
            catch (Exception ex)
            {
                this.telemetryClient.TrackException(ex);
                return(this.StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
        /// <summary>
        /// Get rooms/buildings using Azure search along with their schedule.
        /// </summary>
        /// <param name="search">Schedule object.</param>
        /// <returns>Returns list of rooms.</returns>
        public async Task <IActionResult> SearchRoomAsync([FromBody] ScheduleSearch search)
        {
            try
            {
                var claims = this.GetUserClaims();
                this.telemetryClient.TrackTrace($"Received search request for user {claims.UserObjectIdentifer}. Search query : {search?.Query}");

                var token = await this.tokenHelper.GetUserTokenAsync(claims.FromId).ConfigureAwait(false);

                if (string.IsNullOrEmpty(token))
                {
                    this.telemetryClient.TrackTrace($"Azure Active Directory access token for user {claims.UserObjectIdentifer} is empty. Cannot search rooms.");
                    return(this.StatusCode(
                               StatusCodes.Status401Unauthorized,
                               new Error
                    {
                        StatusCode = SignInErrorCode,
                        ErrorMessage = "Azure Active Directory access token for user is found empty.",
                    }));
                }

                var searchServiceResults = await this.searchService.SearchRoomsAsync(search.Query).ConfigureAwait(false);

                if (searchServiceResults == null)
                {
                    return(this.StatusCode(StatusCodes.Status500InternalServerError));
                }

                if (search.IsScheduleRequired)
                {
                    var conversionResult = DateTime.TryParse(search.Time, out DateTime localTime);
                    var startUTCDateTime = localTime.AddMinutes(Constants.DurationGapFromNow.Minutes);
                    var startDateTime    = TimeZoneInfo.ConvertTimeFromUtc(startUTCDateTime, TimeZoneInfo.FindSystemTimeZoneById(TZConvert.IanaToWindows(search.TimeZone)));
                    search.Time = startDateTime.ToString("yyyy-MM-dd HH:mm:ss");
                    var rooms            = searchServiceResults.Select(room => room.RowKey).ToList();
                    var scheduleResponse = await this.meetingHelper.GetRoomScheduleAsync(search, rooms, token).ConfigureAwait(false);

                    if (scheduleResponse.ErrorResponse != null)
                    {
                        return(this.StatusCode((int)scheduleResponse.StatusCode, scheduleResponse.ErrorResponse.Error.ErrorMessage));
                    }

                    var searchedRooms = searchServiceResults.Select(searchResult => new SearchResult(searchResult)
                    {
                        Label    = searchResult.RoomName,
                        Value    = searchResult.RowKey,
                        Sublabel = searchResult.BuildingName,
                        Status   = scheduleResponse.Schedules.Where(schedule => schedule.ScheduleId == searchResult.RowKey).FirstOrDefault()?.ScheduleItems?.Count > 0 ? Strings.Unavailable : Strings.Available,
                    }).ToList();

                    return(this.Ok(searchedRooms));
                }
                else
                {
                    var searchedRooms = searchServiceResults.Select(searchResult => new SearchResult(searchResult)
                    {
                        Label    = searchResult.RoomName,
                        Value    = searchResult.RowKey,
                        Sublabel = searchResult.BuildingName,
                    }).ToList();

                    return(this.Ok(searchedRooms));
                }
            }
            catch (Exception ex)
            {
                this.telemetryClient.TrackException(ex);
                return(this.StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }