Example #1
0
        public async Task<int> Handle(CreatePublicNotificationCommand request, CancellationToken cancellationToken)
        {
            //Verificar si el usuario tiene algun rol que permita enviar notificaciones (eg. es docente). Reemplazar por auth.
            var user = _context.Individuals.Include(x => x.Roles).ThenInclude(r => r.Role).FirstOrDefault(x => x.Id == request.PersonId);
            if (user == null || !user.CanSendNotification())
                throw new Exception("User has not been authorized to make this request");

            var entity = new Notification
            {
                SentDate = DateTime.Now,
                Title = request.Title,
                Text = request.Text,
                Type = Domain.Enumerations.NotificationTypeEnum.Public
            };

            //Agregar mensaje a cada individuo
            var individuals = _context.Individuals.Select(x => x.Id).ToList();
            foreach (var individual in individuals)
            {
                var personNotif = new PersonNotification()
                {
                    ReceiverId = individual,
                    NotificationId = entity.Id
                };

                entity.IndividualNotifications.Add(personNotif);
            }
            _context.Notifications.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return entity.Id;
        }
        public static bool Add(PersonNotification oNotification)
        {
            Database oDatabase = DatabaseFactory.CreateDatabase(DataHelpers.ConnectionString());

            using (DbConnection connection = oDatabase.CreateConnection()){
                connection.Open();
                try{
                    DbCommand oCommand = oDatabase.GetStoredProcCommand("sp_NotificationToPersonRepository_Add");
                    oDatabase.AddInParameter(oCommand, "@UniqueID", System.Data.DbType.Guid, oNotification.NotificationUniqueID);
                    oDatabase.AddInParameter(oCommand, "@PersonID", System.Data.DbType.Int64, oNotification.PersonID);
                    oDatabase.AddInParameter(oCommand, "@ID", System.Data.DbType.Guid, System.Guid.NewGuid());
                    oDatabase.AddInParameter(oCommand, "@Day", System.Data.DbType.DateTime, oNotification.Day);
                    oDatabase.AddInParameter(oCommand, "@CommunityID", System.Data.DbType.Int64, oNotification.CommunityID);
                    oDatabase.AddInParameter(oCommand, "@SentDate", System.Data.DbType.DateTime, oNotification.SentDate);
                    oCommand.Connection = connection;
                    if (oCommand.ExecuteNonQuery() == 1)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                catch (Exception ex) {
                    System.Diagnostics.EventLog.WriteEntry("NotificationToPersonRepository", ex.Message);
                    return(false);
                }
            }
        }
        public async Task SquadEvent(int eventId, EventActionType eventActionType)
        {
            List <PersonNotification> personList = null;

            DataProvider.ExecuteCmd(GetConnection, "dbo.PersonNotificationPreference_GetSquadEventNotificationPeople",
                                    inputParamMapper : delegate(SqlParameterCollection paramCollection)
            {
                paramCollection.AddWithValue("@EventActionTypeId", (int)eventActionType);
                paramCollection.AddWithValue("@EventId", eventId);
            }
                                    , map : delegate(IDataReader reader, short set)
            {
                switch (set)
                {
                case 0:
                    PersonNotification prsn = MapPersonNotification(reader);

                    if (personList == null)
                    {
                        personList = new List <PersonNotification>();
                    }
                    personList.Add(prsn);
                    break;
                }
            }
                                    );
            string eventActionString = String.Empty;

            switch (eventActionType)
            {
            case EventActionType.Created:
                eventActionString = "created";
                break;

            case EventActionType.Modified:
                eventActionString = "modified";
                break;

            case EventActionType.Cancelled:
                eventActionString = "cancelled";
                break;
            }
            if (personList != null)
            {
                string url                     = System.Web.Configuration.WebConfigurationManager.AppSettings["BaseUrl"] + "/eventdetails/" + eventId;
                string emailTitle              = personList[0].GroupName + " squad event news";
                string emailMainMessage        = "An event for the " + personList[0].GroupName + " squad has been " + eventActionString;
                string emailPleaseClickMessage = "Click the link below to view event details";
                string emailSubject            = "A Squad Event has been " + eventActionString;
                string emailMessageText        = "Click the following link to view event details: " + url;
                string smsMessageText          = "An event for the " + personList[0].GroupName + " squad has been " + eventActionString + "! Click the following link to view event details: " + url;

                await SendMessages(personList, url, emailTitle, emailMainMessage, emailPleaseClickMessage, emailSubject, emailMessageText, smsMessageText);
            }
        }
        private PersonNotification CreatePersonNotification(string userId, Character character)
        {
            var notification = new PersonNotification
            {
                PersonId  = character.PersonId,
                UserId    = userId,
                Content   = CreatePersonNotificationMessage(character),
                IsRead    = false,
                CreatedAt = DateTime.UtcNow,
                CreatedBy = _systemGuid
            };

            return(notification);
        }
        private static PersonNotification MapPersonNotification(IDataReader reader)
        {
            PersonNotification prsn = new PersonNotification();
            int ord = 0;

            prsn.Id          = reader.GetSafeInt32(ord++);
            prsn.FirstName   = reader.GetSafeString(ord++);
            prsn.MiddleName  = reader.GetSafeString(ord++);
            prsn.LastName    = reader.GetSafeString(ord++);
            prsn.PhoneNumber = reader.GetSafeString(ord++);
            prsn.Email       = reader.GetSafeString(ord++);
            prsn.SendEmail   = reader.GetSafeBool(ord++);
            prsn.SendText    = reader.GetSafeBool(ord++);
            prsn.LinkId      = reader.GetSafeInt32(ord++);
            prsn.GroupName   = reader.GetSafeString(ord++);
            return(prsn);
        }
        public async Task NewJobApplication(int applicationId)
        {
            List <PersonNotification> personList = null;

            DataProvider.ExecuteCmd(GetConnection, "dbo.PersonNotificationPreference_GetJobApplicationNotifyPeople",
                                    inputParamMapper : delegate(SqlParameterCollection paramCollection)
            {
                paramCollection.AddWithValue("@ApplicationId", applicationId);
            }
                                    , map : delegate(IDataReader reader, short set)
            {
                switch (set)
                {
                case 0:
                    PersonNotification prsn = MapPersonNotification(reader);

                    if (personList == null)
                    {
                        personList = new List <PersonNotification>();
                    }
                    personList.Add(prsn);
                    break;
                }
            }
                                    );
            if (personList != null)
            {
                string url                     = System.Web.Configuration.WebConfigurationManager.AppSettings["BaseUrl"] + "/jobpostings/" + personList[0].LinkId + "/edit";
                string emailTitle              = "New Application";
                string emailMainMessage        = "Your Job Posting has received a new application";
                string emailPleaseClickMessage = "Click the link below to view and manage applications";
                string emailSubject            = "Your Job Posting has received a new application";
                string emailMessageText        = "Click the following link to view and manage applications: " + url;
                string smsMessageText          = "Your Job Posting on Deploy has received a new application! Click the following link to view and manage applications: " + url;

                await SendMessages(personList, url, emailTitle, emailMainMessage, emailPleaseClickMessage, emailSubject, emailMessageText, smsMessageText);
            }
        }