/// <summary> /// Saves or updates a checkin /// </summary> /// <param name="c"></param> /// <returns></returns> public static Checkin CheckIn(Checkin c) { using (EntityContext ctx = new EntityContext()) { c.ValidateAndRaise(); // Validate user has only one active checkin. if (ctx.Checkins.Where(ci => !ci.EndTime.HasValue && ci.UserId == c.UserId).Any()) { throw new CheckinExistsException(i18n.API_ErrorCheckinExists); } // Validate space is not being used if (ctx.Checkins.Where(ci => ci.SpaceId == c.SpaceId && !ci.EndTime.HasValue).Any()) { throw new CheckinExistsException(i18n.API_ErrorSpaceUsed); } c.CheckInId = 0; c.StartTime = c.StartTime.ToCommonTime(); c.EndTime = null; ctx.Checkins.AddObject(c); ctx.SaveChanges(); // Notify users. Notify(NotificationType.Checkin, c); return(c); } }
/// <summary> /// Saves or updates a checkin /// </summary> /// <param name="c"></param> /// <returns></returns> public static Checkin CheckIn(Checkin c) { using (EntityContext ctx = new EntityContext()) { c.ValidateAndRaise(); // Validate user has only one active checkin. if (ctx.Checkins.Where(ci => !ci.EndTime.HasValue && ci.UserId == c.UserId).Any()) { throw new CheckinExistsException(i18n.API_ErrorCheckinExists); } // Validate space is not being used if (ctx.Checkins.Where(ci => ci.SpaceId == c.SpaceId && !ci.EndTime.HasValue).Any()) { throw new CheckinExistsException(i18n.API_ErrorSpaceUsed); } c.CheckInId = 0; c.StartTime = c.StartTime.ToCommonTime(); c.EndTime = null; ctx.Checkins.AddObject(c); ctx.SaveChanges(); // Notify users. Notify(NotificationType.Checkin, c); return c; } }
/// <summary> /// Deletes a checkin /// </summary> /// <param name="id"></param> /// <returns></returns> public static Checkin Delete(int id) { using (EntityContext ctx = new EntityContext()) { Checkin c = ctx.Checkins.Where(cx => cx.CheckInId == id).FirstOrDefault(); ctx.Checkins.DeleteObject(c); ctx.SaveChanges(); return(c); } }
/// <summary> /// Checks out a user /// </summary> /// <param name="userId"></param> /// <returns></returns> public static Checkin CheckOutByUserId(int userId) { Checkin c = Checkin.GetLastForUser(userId); if (c == null) { return(null); } if (c.EndTime == null) { return(CheckOut(c.CheckInId)); } return(c); }
/// <summary> /// Notifies users that have set up their notification alert setting. /// </summary> /// <param name="baseCheckin"></param> private static void NotifyWaitingList(Checkin baseCheckin) { Pubnub pub = PubnubFactory.GetInstance(); //pub.Publish(PubnubFactory.Channels.GeneralNotification, new { Class = "AvailableNotification", UserId = user.UserId }); using (EntityContext ctx = new EntityContext()) { List <int> checkedInUsers = ctx.Checkins.Where(c => !c.EndTime.HasValue).Select(c => c.UserId).ToList(); List <int> availableUsers = ctx.UserInfos.Where(u => !checkedInUsers.Contains(u.UserId) && u.NotificationsAvailability.HasValue && u.NotificationsAvailability.Value && u.UserId != baseCheckin.UserId) .Select(u => u.UserId) .ToList(); availableUsers.ForEach(user => { pub.Publish(PubnubFactory.Channels.GeneralNotification, new { Class = "AvailableNotification", UserId = user }); }); } }
/// <summary> /// Sends messages based on the notification type. /// </summary> /// <param name="notify"></param> /// <param name="checkin"></param> public static void Notify(NotificationType notify, Checkin checkin) { Pubnub nub = PubnubFactory.GetInstance(); nub.Publish(PubnubFactory.Channels.CheckinHistory, new CheckinNotification(checkin, notify)); nub.Publish(PubnubFactory.Channels.CheckinCurrent, checkin); switch (notify) { case NotificationType.Checkin: // Insert custom actions based on the type here. break; case NotificationType.Checkout: // Insert custom actions based on the type here. NotifyBlockingUsers(checkin, checkin.GetCheckinsIBlock()); NotifyWaitingList(checkin); break; } }
/// <summary> /// Notifies the users that are blocking the specified checkin/space. /// At the moment it sends IM and Email regardless of the users' preferences /// </summary> /// <param name="baseCheckin"></param> /// <param name="checkins"></param> private static void NotifyBlockingUsers(Checkin baseCheckin, List <Checkin> checkins) { List <UserInfo> userInfos = new List <UserInfo>(); List <User> users = new List <User>(); List <int> userIds = checkins.Select(c => c.UserId).ToList(); UserInfo requestingUser; using (EntityContext ctx = new EntityContext()) { requestingUser = ctx.UserInfos.Where(ui => ui.UserId == baseCheckin.UserId).FirstOrDefault(); userInfos = ctx.UserInfos.Where(ui => userIds.Contains(ui.UserId)).ToList(); users = ctx.Users.Where(ui => userIds.Contains(ui.UserId)).ToList(); } Pubnub pub = PubnubFactory.GetInstance(); users.ForEach(user => { // Notify via email EmailerFactory.SendMail(user.Email, i18n.Notification_EmailTitle, string.Format(i18n.Notification_EmailMessage, requestingUser.FullName)); // Notify via UI (if the user has it open) pub.Publish(PubnubFactory.Channels.GeneralNotification, new { Class = "BlockNotification", UserId = user.UserId, RequestingUser = requestingUser.UserId }); }); userInfos.ForEach(ui => { if (!string.IsNullOrEmpty(ui.ContactEmail)) { // Notify via IM MessageQueue.Save(new MessageQueue() { To = ui.ContactEmail, Text = string.Format(i18n.Notification_IMMessage, requestingUser.FullName) }); } }); TropoFactory.CreateSession(); }
/// <summary> /// Checks out a current checkin. /// </summary> /// <param name="checkinId"></param> /// <returns></returns> public static Checkin CheckOut(int checkinId) { using (EntityContext ctx = new EntityContext()) { Checkin c = ctx.Checkins.Where(ch => ch.CheckInId == checkinId).FirstOrDefault(); if (c == null) { return(null); } // Validate if users are blocking you. c.EndTime = DateTime.Now.ToCommonTime(); ctx.SaveChanges(); // Notify users Notify(NotificationType.Checkout, c); return(c); } }
partial void DeleteCheckin(Checkin instance);
partial void UpdateCheckin(Checkin instance);
partial void InsertCheckin(Checkin instance);
/// <summary> /// Create a new Checkin object. /// </summary> /// <param name="checkInId">Initial value of the CheckInId property.</param> /// <param name="startTime">Initial value of the StartTime property.</param> /// <param name="spaceId">Initial value of the SpaceId property.</param> /// <param name="userId">Initial value of the UserId property.</param> /// <param name="registeredFrom">Initial value of the RegisteredFrom property.</param> /// <param name="registeredBy">Initial value of the RegisteredBy property.</param> public static Checkin CreateCheckin(global::System.Int32 checkInId, global::System.DateTime startTime, global::System.Int32 spaceId, global::System.Int32 userId, global::System.Int32 registeredFrom, global::System.Int32 registeredBy) { Checkin checkin = new Checkin(); checkin.CheckInId = checkInId; checkin.StartTime = startTime; checkin.SpaceId = spaceId; checkin.UserId = userId; checkin.RegisteredFrom = registeredFrom; checkin.RegisteredBy = registeredBy; return checkin; }
/// <summary> /// Deprecated Method for adding a new object to the Checkins EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToCheckins(Checkin checkin) { base.AddObject("Checkins", checkin); }
/// <summary> /// Notifies users that have set up their notification alert setting. /// </summary> /// <param name="baseCheckin"></param> private static void NotifyWaitingList(Checkin baseCheckin) { Pubnub pub = PubnubFactory.GetInstance(); //pub.Publish(PubnubFactory.Channels.GeneralNotification, new { Class = "AvailableNotification", UserId = user.UserId }); using (EntityContext ctx = new EntityContext()) { List<int> checkedInUsers = ctx.Checkins.Where(c => !c.EndTime.HasValue).Select(c => c.UserId).ToList(); List<int> availableUsers = ctx.UserInfos.Where(u => !checkedInUsers.Contains(u.UserId) && u.NotificationsAvailability.HasValue && u.NotificationsAvailability.Value && u.UserId != baseCheckin.UserId) .Select(u => u.UserId) .ToList(); availableUsers.ForEach(user => { pub.Publish(PubnubFactory.Channels.GeneralNotification, new { Class = "AvailableNotification", UserId = user }); }); } }
/// <summary> /// Notifies the users that are blocking the specified checkin/space. /// At the moment it sends IM and Email regardless of the users' preferences /// </summary> /// <param name="baseCheckin"></param> /// <param name="checkins"></param> private static void NotifyBlockingUsers(Checkin baseCheckin, List<Checkin> checkins) { List<UserInfo> userInfos = new List<UserInfo>(); List<User> users = new List<User>(); List<int> userIds = checkins.Select(c => c.UserId).ToList(); UserInfo requestingUser; using (EntityContext ctx = new EntityContext()) { requestingUser = ctx.UserInfos.Where(ui => ui.UserId == baseCheckin.UserId).FirstOrDefault(); userInfos = ctx.UserInfos.Where(ui => userIds.Contains(ui.UserId)).ToList(); users = ctx.Users.Where(ui => userIds.Contains(ui.UserId)).ToList(); } Pubnub pub = PubnubFactory.GetInstance(); users.ForEach(user => { // Notify via email EmailerFactory.SendMail(user.Email, i18n.Notification_EmailTitle, string.Format(i18n.Notification_EmailMessage, requestingUser.FullName)); // Notify via UI (if the user has it open) pub.Publish(PubnubFactory.Channels.GeneralNotification, new { Class = "BlockNotification", UserId = user.UserId, RequestingUser = requestingUser.UserId }); }); userInfos.ForEach(ui => { if (!string.IsNullOrEmpty(ui.ContactEmail)) { // Notify via IM MessageQueue.Save(new MessageQueue() { To = ui.ContactEmail, Text = string.Format(i18n.Notification_IMMessage, requestingUser.FullName) }); } }); TropoFactory.CreateSession(); }