Ejemplo n.º 1
0
        private async Task <bool> CreateNotification(NotificationModel model)
        {
            #region Create Schedule

            //TODO: It will be better to use automapper.
            var entity = new ScheduleInfo
            {
                RecurseId         = model.RecurseId,
                Module            = model.Module,
                ExpirationDate    = model.ExpirationDate,
                MessageTemplateId = (long)model.MessageTemplate,
                JsonData          = model.JsonData,
                UserSchedules     = model.UserIds.Select(id => new UserSchedule
                {
                    UserId = id,
                    Status = NotificationStatus.None
                }).ToList()
            };

            await _scheduleRepository.AddAsync(entity);

            await _scheduleRepository.SaveAsync();

            #endregion

            #region Create jobs and save created jobs data

            var jobDays   = GetJobDatesByMessageTemplate(model.MessageTemplate);
            var fireDates = jobDays.Select(d => entity.ExpirationDate.AddDays(-d));
            entity.ScheduleJobs = await StartJobs(fireDates, entity);

            return(await _scheduleRepository.SaveAsync());

            #endregion
        }
Ejemplo n.º 2
0
        private async Task <bool> CreateNotification(CreateNotificationModel model)
        {
            #region Create Schedule

            //TODO: It will be better to use automapper.
            var entity = new ScheduleInfo
            {
                RecurseId         = model.RecurseId,
                Module            = model.Module,
                ExpirationDate    = model.ExpirationDate,
                MessageTemplateId = (long)model.MessageTemplate,
                JsonData          = model.JsonData,
                UserSchedules     = model.UserIds.Select(id => new UserSchedule()
                {
                    UserId = id,
                    Status = NotificationStatus.None
                }).ToList()
            };

            await _scheduleRepository.AddAsync(entity);

            await _scheduleRepository.SaveAsync();

            #endregion

            #region Create Jobs for schedule

            //TODO: Store job fire interval in DB or in some configuration file
            var jobDates = new List <int> {
                2, 10, 15
            };
            var createdJobs = new List <ScheduleJob>();
            foreach (var date in jobDates)
            {
                var fireDate = entity.ExpirationDate.AddDays(-date);
                var jobId    = await _scheduledJobService.StartJobAsync(new NotificationJob(_serviceProvider), //TODO-Question: Why we create new obj?
                                                                                                               //TODO: Change this logic when Hangfire will change parameter type to DateTime.
                                                                        fireDate - DateTime.Now,
                                                                        new NotificationJobParameter
                {
                    ScheduleId = entity.Id
                });

                createdJobs.Add(new ScheduleJob()
                {
                    JobId    = jobId,
                    FireDate = fireDate,
                });
            }

            #endregion

            #region Save created jobs data

            entity.ScheduleJobs = createdJobs;
            return(await this._scheduleRepository.SaveAsync());

            #endregion
        }