Exemplo n.º 1
0
        /// <summary>
        /// DRY.  loops through user ids creating them in the repository. does not save changes to the repo
        /// </summary>
        /// <param name="userId">array of user id's</param>
        /// <param name="notificationType">type of notification</param>
        /// <param name="entityId">the project id</param>
        private void sendNewNotices(int[] userId, NotificationType notificationType, int entityId, EntityType entityType)
        {
            try
            {

                Notification newNotice;
                for (int i = 0; i < userId.Length; i++)
                {
                    newNotice = new Notification
                    {
                        Count = 1,
                        LastEditTimestamp = DateTime.Now,
                        NotificationType = notificationType,
                        EntityId = entityId,
                        EntityType = entityType,
                        RecipientId = userId[i],
                        Read = false
                    };

                    // only two entity types right now. so should hopefully not puke.
                    if (entityType == EntityType.Project)
                    {
                        BCModel.Projects.Project theProject = _repo.GetProject(entityId);
                        if (theProject == null)
                        {
                            throw new Exception("cannot find project");
                        }
                        switch (notificationType)
                        {
                            case NotificationType.BidSubmitted:
                                newNotice.Message = string.Format(_BidSubmissionMsg, theProject.Title);
                                break;
                            case NotificationType.InvitationRequest:
                                newNotice.Message = string.Format(_InvitationRequestMsg, theProject.Title);
                                break;
                            case NotificationType.InvitationResponse:
                                newNotice.Message = string.Format(_ResponseToInvatiationMsg, theProject.Title);
                                break;
                            case NotificationType.InvitationToBid:
                                newNotice.Message = string.Format(_InvitationToBidMsg, theProject.Title);
                                break;
                            case NotificationType.ProjectChange:
                                newNotice.Message = string.Format(_ChangesToProjectMsg, theProject.Title);
                                break;
                        }
                    }
                    else if (entityType == EntityType.Company)
                    {
                        CompanyProfile theCompany = _repo.GetCompanyProfile(entityId);
                        if (theCompany == null)
                        {
                            throw new Exception("cannot find the company");
                        }
                        switch (notificationType)
                        {
                            case NotificationType.ConnectionAccepted:
                                newNotice.Message = string.Format(_ConnectionAccepted, theCompany.CompanyName);
                                break;
                        }
                    }

                    if (newNotice.Message == null || newNotice.Message == string.Empty)
                    {
                        throw new Exception("message is emtpy");
                    }

                    _repo.Create(newNotice);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// sends an internal notification to a user
        /// </summary>
        /// <param name="recipientId">userid</param>
        /// <param name="notificationType">type of notification</param>
        /// <param name="projectId">the project the notification is for</param>
        /// <returns></returns>
        public bool SendNotification(int recipientId, RecipientType recipientType, BCModel.NotificationType notificationType, int entityId, EntityType entityType)
        {
            try
            {
                switch (recipientType)
                {
                    // if company, then loop through each of the companies users
                    case RecipientType.company:
                        List<Notification> existingNotices = (from r in _repo.Query()
                                                              where r.NotificationType == notificationType      // notification type y
                                                              && r.Recipient.CompanyId == recipientId                     // sent to company z
                                                              && r.EntityId == entityId                       // for project x
                                                              && r.EntityType == entityType
                                                              && !r.Read                                        // not read yet
                                                              && EntityFunctions.DiffDays(r.LastEditTimestamp, DateTime.Now).Value == 0  // from today
                                                              select r).ToList();
                        List<UserProfile> users = _repo.QueryUserProfiles().Where(x => x.CompanyId == recipientId).ToList();

                        if (existingNotices.Count == users.Count) // if there is an unread notice for each user of comany z
                        {
                            appendExistingNotices(existingNotices.ToArray());
                            _repo.Save(); // save changes
                            return true;
                        }
                        else if (existingNotices.Count > 0 && existingNotices.Count < users.Count) // if some users have read the notice already
                        {
                            // update unread
                            appendExistingNotices(existingNotices.ToArray());

                            // create new notices
                            sendNewNotices(users.Where(x => !existingNotices.Select(e => e.RecipientId).Contains(x.UserId)).Select(s => s.UserId).ToArray(), notificationType, entityId, entityType);

                            // save changes
                            _repo.Save();
                            return true;
                        }
                        else // else no notices exist
                        {
                            // create new notices
                            sendNewNotices(users.Select(s => s.UserId).ToArray(), notificationType, entityId, entityType);
                            _repo.Save();
                            return true;
                        }

                    // if user, only do the single notice
                    case RecipientType.user:
                        // find out if there is already a notification type for this project from today
                        Notification existingNotice = (from r in _repo.Query()
                                                       where r.NotificationType == notificationType      // notification type y
                                                       && r.RecipientId == recipientId                     // sent to user z
                                                       && r.EntityId == entityId                       // for project x
                                                       && r.EntityType == entityType
                                                       && !r.Read                                        // not read yet
                                                       && r.LastEditTimestamp.Date == DateTime.Now.Date  // from today
                                                       select r).SingleOrDefault();

                        // if there is not an existing notice
                        if (existingNotice != null)
                        {
                            // pull the notice out of the list
                            existingNotice.Count = existingNotice.Count++;      // increase notice count
                            existingNotice.LastEditTimestamp = DateTime.Now;    // reset timestamp
                            _repo.Update(existingNotice);                       // update notice
                            _repo.Save();                                       // save changes
                            return true;
                        }
                        else // there is no existing notice
                        {
                            Notification theNotice = new Notification
                            {
                                Count = 1,
                                LastEditTimestamp = DateTime.Now,
                                NotificationType = notificationType,
                                EntityId = entityId,
                                EntityType = entityType,
                                Read = false,
                                RecipientId = recipientId
                            };                      // draft the notice
                            _repo.Create(theNotice);    // add to queue
                            _repo.Save();               // send/save
                            return true;
                        }
                    default:
                        throw new ArgumentException("Unknown notification type");
                }

            }
            catch (Exception)
            {
                return false;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// DRY. updates existing notices. does not save changes to the repository
        /// </summary>
        /// <param name="notices"></param>
        private void appendExistingNotices(Notification[] notices)
        {
            try
            {
                for (int i = 0; i < notices.Length; i++)
                {
                    notices[i].LastEditTimestamp = DateTime.Now;    // reset timestamp
                    notices[i].Count += 1;                          // increase count of changes
                    _repo.Update(notices[i]);                       // update record
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }