Exemple #1
0
        public void NotifyOthers(NotificationType type, string senderId, int postId)
        {
            var sender = GetUserById(senderId);

            if (sender == null)
            {
                throw new ArgumentException("Sender doesn't exist");
            }

            var subscribers = new PostRepository(_context).GetPostSubscriptions(postId).ToList();

            foreach (var subscriber in subscribers)
            {
                if (subscriber.IsOff)
                {
                    continue;
                }

                var notification = _context.Notifications
                                   .FirstOrDefault(n => n.ReceiverId.Equals(subscriber.UserId) && n.SenderId.Equals(sender.Id) && n.NotificationType == type && n.PostId == postId);

                var post = new PostRepository(_context).GetPost(postId);

                if (post == null)
                {
                    throw new ArgumentException("Post doesn't exist");
                }

                if (subscriber.UserId.Equals(sender.Id))
                {
                    continue;
                }

                if (notification == null || type == NotificationType.Comment)
                {
                    var newNotification = new Notification
                    {
                        DateTime         = DateTime.UtcNow,
                        IsRead           = false,
                        PostId           = post.Id,
                        SenderId         = sender.Id,
                        ReceiverId       = subscriber.UserId,
                        NotificationType = type,
                        OwnershipType    = OwnershipType.Other
                    };
                    _context.Notifications.Add(newNotification);

                    NotificationsHub.Notify(subscriber.UserId, sender, newNotification);
                }
            }
        }
Exemple #2
0
        public IHttpActionResult NotifyAdmin([FromUri] string email)
        {
            Notification notification = new Notification();

            notification.Text = "New user registrated: " + email;
            notification.Date = DateTime.Now;

            if (PostNotification(notification))
            {
                NotificationsHub.Notify(notification);
                return(Ok("Success"));
            }

            NotificationsHub.Notify(null);
            return(Ok("Failure"));
        }
Exemple #3
0
        public IHttpActionResult NotifyAdmin([FromUri] string serviceName, [FromUri] int id)
        {
            Notification notification = new Notification();

            notification.Text = "New service added with name: " + serviceName + " and id: " + id;
            notification.Date = DateTime.Now;

            if (PostNotification(notification))
            {
                NotificationsHub.Notify(notification);
                return(Ok("Success"));
            }

            NotificationsHub.Notify(null);
            return(Ok("Failure"));
        }
Exemple #4
0
        public void Notify(string receiverId, NotificationType type, string senderId, int?postId)
        {
            var sender = GetUserById(senderId);

            if (sender == null)
            {
                throw new ArgumentException("Sender doesn't exist");
            }

            if (postId != null)
            {
                var post = new PostRepository(_context).GetPost((int)postId);

                if (post == null)
                {
                    throw new ArgumentException("Post doesn't exist");
                }

                var notification = _context.Notifications.
                                   FirstOrDefault(n => n.ReceiverId.Equals(post.ApplicationUserId) && n.SenderId.Equals(sender.Id) && n.NotificationType == type && n.PostId == postId);

                if (!post.ApplicationUserId.Equals(sender.Id) && (notification == null || type == NotificationType.Comment))
                {
                    var newNotification = new Notification
                    {
                        DateTime         = DateTime.UtcNow,
                        IsRead           = false,
                        PostId           = post.Id,
                        SenderId         = sender.Id,
                        ReceiverId       = post.ApplicationUserId,
                        NotificationType = type,
                        OwnershipType    = OwnershipType.Owner
                    };
                    _context.Notifications.Add(newNotification);

                    NotificationsHub.Notify(post.ApplicationUserId, sender, newNotification);
                }
                NotifyOthers(type, senderId, (int)postId);
            }

            if (!receiverId.IsNullOrWhiteSpace())
            {
                var receiver = GetUserById(receiverId);

                if (receiver == null)
                {
                    throw new ArgumentException("Receiver doesn't exist");
                }

                var notification = _context.Notifications.FirstOrDefault(n => n.ReceiverId.Equals(receiver.Id) && n.SenderId.Equals(sender.Id) && n.NotificationType == type);

                if (notification == null)
                {
                    var newNotification = new Notification
                    {
                        DateTime         = DateTime.UtcNow,
                        IsRead           = false,
                        SenderId         = sender.Id,
                        ReceiverId       = receiver.Id,
                        NotificationType = NotificationType.Follow,
                        OwnershipType    = OwnershipType.Owner
                    };
                    _context.Notifications.Add(newNotification);

                    NotificationsHub.Notify(receiver.Id, sender, newNotification);
                }
            }
        }
Exemple #5
0
        public void Execute()
        {
            if (ApplicationDbContext.options != null && !isRunning)
            {
                isRunning = true;
                ApplicationDbContext appDbContext = new ApplicationDbContext(ApplicationDbContext.options);

                try
                {
                    // Load relevant constants from Configration file (appSettings)
                    int    minQuestionsRecommend = ConfigurationManager.Instance.GetValue("MIN_QUESTIONS_RECOMMEND", Consts.MIN_QUESTIONS_RECOMMEND);
                    double maxQuestionRank       = ConfigurationManager.Instance.GetValue("MAX_QUESTION_RANK", Consts.MAX_QUESTION_RANK);
                    double minDaysAfterRejected  = ConfigurationManager.Instance.GetValue("NUM_DAYS_AFTER_REJECTED", Consts.NUM_DAYS_AFTER_REJECTED);

                    // Load relevant candidates (which enabled to send their resume) and solved at least MIN_QUESTIONS_RECOMMEND
                    IEnumerable <CandidateUser> candidates = appDbContext.Set <CandidateUser>()
                                                             .Include("Questions.Question")
                                                             .Where(c => c.AllowSendResume &&
                                                                    c.Questions.Where(q => q.IsDone).Count() >= minQuestionsRecommend);

                    // Load relevant recruiters (which enabled to receive notifications)
                    Dictionary <string, RecruiterUser> recruiters = appDbContext.Set <RecruiterUser>()
                                                                    .Include(r => r.Identity)
                                                                    .Where(r => r.ReceiveNotifications).ToDictionary(r => r.IdentityId, r => r);

                    // Load only OPEN positions which created by recruiters who enabled notifications
                    IEnumerable <Position> positions = appDbContext.Set <Position>()
                                                       .Include(p => p.PositionSkills)
                                                       .Where(p => p.Status == (int)PositionStatus.Opened && recruiters.ContainsKey(p.CreatedBy));


                    // Load recommendations which is relevant to filter users

                    /*
                     * It is uses in order to prevent from sending recommendations to recruiters
                     * about candidates which sent a recommendation already
                     */
                    var recommendations = appDbContext.Set <RecommendationNotification>()
                                          .Where(r => !(r.Approved == false && r.DateResponded.Value.AddDays(minDaysAfterRejected) < DateTime.Now))
                                          .ToLookup(r => r.PositionId, r => r);


                    foreach (Position position in positions)
                    {
                        IEnumerable <PositionSkill> positionSkills = position.PositionSkills;
                        Dictionary <int, double>    matchingNumbersOfCandidates = new Dictionary <int, double>();

                        foreach (CandidateUser candidate in candidates)
                        {
                            bool skipToNextCandidate = false;
                            //if(recommendations.Contains(position.Id))
                            //{

                            //}
                            foreach (RecommendationNotification recommendation in recommendations[position.Id])
                            {
                                if (recommendation.CandidateId == candidate.Id)
                                {
                                    skipToNextCandidate = true;
                                    break;
                                }
                            }
                            if (skipToNextCandidate)
                            {
                                continue;
                            }

                            double matchingNumber = 0;
                            foreach (PositionSkill positionSkill in positionSkills)
                            {
                                double skillGrade       = 0;
                                double maxTotalRank     = 0;
                                double totalRank        = 0;
                                double sumQuestionsRank = 0;

                                // Load questions which done by the candidate, and has at least one skill of this position
                                var relevantQuestions = candidate.Questions.Where(cq => cq.IsDone && cq.Question.Rank > 0);
                                var questionsWithAtLeastOneRelevantSkill = new List <CandidateQuestion>();

                                foreach (CandidateQuestion cq in relevantQuestions)
                                {
                                    if (Utils.ConvertStringIdsToList(cq.Question.TestedSkills).Contains(positionSkill.Id))
                                    {
                                        questionsWithAtLeastOneRelevantSkill.Add(cq);
                                    }
                                }
                                relevantQuestions = questionsWithAtLeastOneRelevantSkill;

                                maxTotalRank = relevantQuestions.Count() * maxQuestionRank;

                                if (maxTotalRank > 0)
                                {
                                    foreach (var relevantQuestion in relevantQuestions)
                                    {
                                        sumQuestionsRank += relevantQuestion.Question.Rank / maxQuestionRank;
                                        totalRank        += relevantQuestion.Question.Rank;
                                    }

                                    skillGrade      = sumQuestionsRank * (totalRank / maxTotalRank) * positionSkill.SkillWeight;
                                    matchingNumber += skillGrade;
                                }
                            }
                            if (matchingNumber > 0)
                            {
                                matchingNumbersOfCandidates.Add(candidate.Id, matchingNumber);
                            }
                        }

                        // Getting the recommended candidate which his matching number is the heighest
                        double max         = 0;
                        int    candidateId = -1;
                        foreach (var matchingNumber in matchingNumbersOfCandidates)
                        {
                            if (matchingNumber.Value > max)
                            {
                                max         = matchingNumber.Value;
                                candidateId = matchingNumber.Key;
                            }
                        }

                        if (candidateId != -1 && recruiters.ContainsKey(position.CreatedBy))
                        {
                            RecruiterUser relevantRecruiter = recruiters[position.CreatedBy];

                            // Update recommentdation table
                            Notification notificiation = new Notification()
                            {
                                Type        = (int)NotificationType.Recommendation,
                                DateCreated = DateTime.Now,
                                IsViewed    = false,
                                Recipent    = relevantRecruiter.IdentityId,
                            };

                            RecommendationNotification recommendation = new RecommendationNotification()
                            {
                                Approved     = null,
                                Notification = notificiation,
                                CandidateId  = candidateId,
                                PositionId   = position.Id,
                            };
                            var otherDbContext = new ApplicationDbContext(ApplicationDbContext.options);
                            new RecommendationNotificationsRepository(otherDbContext).Add(recommendation);
                            otherDbContext.SaveChanges();


                            var genericNotificationToClient = new GenericNotification()
                            {
                                NotificationId = recommendation.Notification.Id,
                                Type           = recommendation.Notification.Type,
                                DateCreated    = recommendation.Notification.DateCreated,
                                IsViewed       = recommendation.Notification.IsViewed,
                                Approved       = recommendation.Approved,
                                CandidateId    = recommendation.CandidateId,
                            };

                            // Send the recommendation
                            NotificationsHub.Notify(relevantRecruiter.Identity.Email, genericNotificationToClient);
                        }

                        //double maxMatchingNumber = matchingNumbersOfCandidates.Values.Max();
                        //int candidateId = matchingNumbersOfCandidates.Where((key, value) => value == maxMatchingNumber).First().Key;
                    }
                }
                catch (Exception)
                {
                    // TODO: write to log
                }
                finally
                {
                    isRunning = false;
                }
            }
        }