/// <summary> /// Updates the ZoneConcurrentList for UserSession's zone-enter and zone-last-seen timestamps /// </summary> /// <returns></returns> private async Task UpdateZoneConcurrentListAsync() { try { bool isDataModified = false; _zoneConcurrentList = await _dataClient?.TransientData?.ObtainZoneConcurrentListAsync(); if (_zoneConcurrentList != null) { // Retrieve the User's respective UserSession var userSession = _zoneConcurrentList.ObtainZoneSession(_locationDevice.ZoneID.Value)?.UserConcurrentList?.ObtainUserSession(_userID.Value); if (userSession != null) { userSession.EnteredIntoZoneAt = userSession.EnteredIntoZoneAt ?? DateTime.UtcNow; userSession.LastSeenInZoneAt = DateTime.UtcNow; isDataModified = true; } } if (isDataModified) { // Save the updated UserSessions await _dataClient.TransientData.SaveZoneConcurrentListAsync(); Context.Logger.LogLine("UserSessions updated"); } } catch (Exception ex) { Context.Logger.LogLine("ZoneConcurrentList ERROR: " + ex.Message); } }
/// <summary> /// Flushes DisplayConcurrentList /// </summary> /// <param name="dataClientConfig"></param> private async Task FlushDisplayConcurrentListAsync() { try { bool isDataModified = false; var displayConcurrentList = await _dataClient?.TransientData?.ObtainDisplayConcurrentListAsync(); if (displayConcurrentList?.DisplaySessions?.Any() ?? false) { // Get a list of all DisplayEndpoints var displayEndpoints = _dataClient?.ConfigurationData?.DisplayEndpoints.ToList(); // Traverse through all DisplaySessions in DisplayConcurrentList foreach (DisplaySession displaySession in displayConcurrentList.DisplaySessions) { // Flush the DisplaySession's show-notification information if (displaySession?.IsUserExists == true) { // Get the ZoneID of the DisplaySession long?zoneID = displayEndpoints? .SingleOrDefault(dE => dE.DisplayEndpointID == displaySession.DisplayEndpointID).ZoneID; // Reset show-notification info if there is no UserSession for the Zone's ZoneSession if (!(_zoneConcurrentList?.ObtainZoneSession(zoneID.Value)?.UserConcurrentList?.UserSessions?.Any() ?? false)) { displaySession.IsUserExists = false; displaySession.BufferedShowNotificationID = null; if (displaySession.CurrentShowNotificationExpireAt != null && displaySession.CurrentShowNotificationExpireAt.Value.ToUniversalTime() < DateTime.UtcNow) { displaySession.CurrentShowNotificationExpireAt = null; } isDataModified = true; } } // Flush the DisplaySession's touch information if (displaySession?.DisplayTouchedNotificationID != null) { bool resetTouchInfo = false; if (displaySession.DisplayTouchedAt != null) { // Reset touch info if the combination of the time DisplayEndpoint is touched and // DisplaySession touch-timeout threshold is lesser than current time var combinedTs = displaySession.DisplayTouchedAt.Value.AddSeconds(Config.DisplaySessionTouchTimeoutThreshold).ToUniversalTime(); resetTouchInfo = combinedTs < DateTime.UtcNow ? true : false; } else { resetTouchInfo = true; } // Reseting DisplaySession's touch info if (resetTouchInfo) { displaySession.DisplayTouchedNotificationID = null; displaySession.DisplayTouchedAt = null; isDataModified = true; } } // Flush the DisplaySession's LocationDevice information if (displaySession?.LocationDeviceID != null) { bool resetLocationDeviceInfo = false; if (displaySession.LocationDeviceRegisteredAt != null) { // Reset LocationDevice info if the combination of the time it's registered into session and // DisplaySession LocationDevice-timeout threshold is lesser than current time var combinedTs = displaySession.LocationDeviceRegisteredAt.Value.AddSeconds(Config.DisplaySessionLocationDeviceTimeoutThreshold).ToUniversalTime(); resetLocationDeviceInfo = combinedTs < DateTime.UtcNow ? true : false; } else { resetLocationDeviceInfo = true; } // Reseting DisplaySession's LocationDevice info if (resetLocationDeviceInfo) { displaySession.LocationDeviceID = null; displaySession.LocationDeviceRegisteredAt = null; isDataModified = true; } } } } if (isDataModified) { displayConcurrentList.LastFlushedAt = DateTime.UtcNow; // Save the updated DisplayConcurrentList await _dataClient.TransientData.SaveDisplayConcurrentListAsync(); Context.Logger.LogLine("DisplayConcurrentList updated"); } } catch (Exception ex) { Context.Logger.LogLine("DisplayConcurrentList ERROR: " + ex.Message); } }
/// <summary> /// Returns Coupons for the User (if any) /// </summary> /// <returns></returns> private async Task <List <Coupon> > GetCouponsAsync() { bool isDataModified = false; List <Coupon> couponsToReturn = null; try { if (_locationDevice != null) { // Retrieve DisplayEndpointIDs in the Zone var zoneDisplayEndpointIDs = _locationDevice?.Zone?.DisplayEndpoints? .Select(dE => dE.DisplayEndpointID); // Retrieve NotificationIDs (only the ones are touched) associated with the DisplayEndpoints in the Zone var touchedNotificationIDs = _displayConcurrentList?.DisplaySessions? .Where(dS => zoneDisplayEndpointIDs?.Contains(dS.DisplayEndpointID) ?? false) .Where(dS => dS.DisplayTouchedNotificationID != null) .Select(dS => dS.DisplayTouchedNotificationID); // Retrieve Coupons associated with the touched Notifications var coupons = _dataClient?.ConfigurationData?.Coupons? .Where(c => touchedNotificationIDs.Contains(c.NotificationID)); // Retrieve the User's respective UserSession var userSession = _zoneConcurrentList?.ObtainZoneSession(_locationDevice.ZoneID.Value)?.UserConcurrentList?.ObtainUserSession(_userID.Value); // Create a new list of Coupons from the retrieved Coupons (associated with the touched Notifications), // which are not received by the User for the UserSession yet couponsToReturn = coupons? .Where(c => !userSession.ReceivedCouponIDs.Contains(c.CouponID)).ToList(); var couponsToReturnIDs = couponsToReturn? .Select(c => c.CouponID); if (couponsToReturnIDs?.Any() ?? false) { // Update the UserSession's received-coupons-registry with the new list of Coupons userSession?.ReceivedCouponIDs?.AddRange(couponsToReturnIDs); // Add the new list of Coupons to transactional data var userCoupons = new List <UserCoupon>(); foreach (long couponID in couponsToReturnIDs) { userCoupons.Add(new UserCoupon() { UserID = _userID.Value, CouponID = couponID, ReceivedAt = DateTime.UtcNow, CouponRedempted = false }); } _dataClient?.TransactionalData?.UserCoupons?.AddRange(userCoupons); isDataModified = true; } } } catch (Exception ex) { Context.Logger.LogLine(ex.Message); } if (isDataModified) { try { // Save the updated UserSessions await _dataClient.TransientData.SaveZoneConcurrentListAsync(); Context.Logger.LogLine("UserSessions updated for received-coupons-registry"); // Save the updated UserCoupons await _dataClient.TransactionalData.SaveChangesAsync(); Context.Logger.LogLine("UserCoupons updated for transactional data"); } catch (Exception ex) { couponsToReturn = null; Context.Logger.LogLine("Data saving ERROR: " + ex.Message); } } return(couponsToReturn); }