Exemple #1
0
 public void Deactivate(Advice advice)
 {
     DeleteJobFromHangfire(advice);
     advice.IsActive = false;
     AdviceRepository.Update(advice);
     AdviceRepository.Save();
 }
Exemple #2
0
        /// <summary>
        /// Public only to allow HangFire to access it
        /// </summary>
        /// <param name="adviceId"></param>
        public void DeactivateById(int adviceId)
        {
            var advice = AdviceRepository.AsQueryable().ById(adviceId);

            if (advice != null)
            {
                Deactivate(advice);
            }
        }
Exemple #3
0
        /// <summary>
        /// Public only to allow HangFire to access it
        /// </summary>
        /// <param name="adviceId"></param>
        public void CreateOrUpdateJob(int adviceId)
        {
            var advice = AdviceRepository.GetByKey(adviceId);

            if (advice == null || advice.Scheduling == null || advice.AlarmDate == null)
            {
                throw new ArgumentException(nameof(adviceId) + " does not point to a valid id or points to an advice without alarm date or scheduling");
            }

            var adviceAlarmDate  = advice.AlarmDate.Value;
            var adviceScheduling = advice.Scheduling.Value;

            var adviceTriggers = AdviceTriggerFactory.CreateFrom(adviceAlarmDate, adviceScheduling);

            foreach (var adviceTrigger in adviceTriggers)
            {
                var prefix = advice.JobId;
                var jobId  = adviceTrigger.PartitionId.Match(partitionId => CreatePartitionJobId(prefix, partitionId), () => prefix);
                HangfireApi.AddOrUpdateRecurringJob(jobId, () => SendAdvice(adviceId), adviceTrigger.Cron);
            }

            if (advice.StopDate.HasValue)
            {
                //Schedule deactivation to happen the day after the stop date (stop date is "last day alive" for the advice)
                HangfireApi.Schedule(() => DeactivateById(advice.Id), new DateTimeOffset(advice.StopDate.Value.Date.AddDays(1)));
            }

            //If time has passed the trigger time, Hangfire will not fire until the next trigger data so we must force it.
            if (adviceAlarmDate.Date.Equals(OperationClock.Now.Date))
            {
                switch (adviceScheduling)
                {
                case Scheduling.Day:
                case Scheduling.Week:
                case Scheduling.Month:
                case Scheduling.Year:
                case Scheduling.Quarter:
                case Scheduling.Semiannual:
                    var mustScheduleAdviceToday =
                        advice.AdviceSent.Where(x => x.AdviceSentDate.Date == adviceAlarmDate.Date).Any() == false &&
                        WillTriggerInvokeToday() == false;
                    if (mustScheduleAdviceToday)
                    {
                        //Send the first advice now
                        HangfireApi.Schedule(() => SendAdvice(adviceId));
                    }
                    break;

                //Intentional fallthrough - no corrections here
                case Scheduling.Hour:
                case Scheduling.Immediate:
                default:
                    break;
                }
            }
        }
Exemple #4
0
 public void BulkDeleteAdvice(IEnumerable <Advice> toBeDeleted)
 {
     using var transaction = TransactionManager.Begin(IsolationLevel.ReadCommitted);
     foreach (var advice in toBeDeleted)
     {
         RemoveAdviceAndItsRelatedEntities(advice);
     }
     AdviceRepository.Save();
     transaction.Commit();
 }
Exemple #5
0
 public IQueryable <Advice> GetAdvicesAccessibleToCurrentUser()
 {
     return(OrganizationalUserContext.IsGlobalAdmin()
         ? AdviceRepository.AsQueryable()
         : OrganizationalUserContext
            .OrganizationIds
            .Select(GetAdvicesForOrg)
            .Aggregate <IQueryable <Advice>, IQueryable <Advice> >
            (
                null,
                (acc, next) => acc == null ? next : acc.Concat(next)
            ));
 }
Exemple #6
0
        public bool SendAdvice(int id)
        {
            using var transaction = TransactionManager.Begin(IsolationLevel.ReadCommitted);
            try
            {
                var advice = AdviceRepository.AsQueryable().ById(id);
                if (advice != null)
                {
                    if (advice.AdviceType == AdviceType.Immediate || IsAdviceInScope(advice))
                    {
                        if (DispatchEmails(advice))
                        {
                            AdviceRepository.Update(advice);

                            AdviceSentRepository.Insert(new AdviceSent {
                                AdviceId = id, AdviceSentDate = OperationClock.Now
                            });
                        }
                    }

                    if (advice.AdviceType == AdviceType.Immediate)
                    {
                        advice.IsActive = false;
                    }
                    else if (IsAdviceExpired(advice))
                    {
                        advice.IsActive = false;
                        DeleteJobFromHangfire(advice);
                    }

                    AdviceRepository.Save();
                    AdviceSentRepository.Save();
                    transaction.Commit();
                }
                return(true);
            }
            catch (Exception e)
            {
                Logger?.Error(e, "General error sending emails in advice service");
                transaction.Rollback();
                throw;
            }
        }
Exemple #7
0
 public Maybe <Advice> GetAdviceById(int id)
 {
     return(AdviceRepository.GetByKey(id));
 }
Exemple #8
0
 private void RemoveAdviceAndItsRelatedEntities(Advice advice)
 {
     DeleteJobFromHangfire(advice);
     AdviceRepository.DeleteByKeyWithReferencePreload(advice.Id);
 }
Exemple #9
0
 public void Delete(Advice advice)
 {
     RemoveAdviceAndItsRelatedEntities(advice);
     AdviceRepository.Save();
 }
Exemple #10
0
        public IQueryable <Advice> GetAdvicesForOrg(int orgKey)
        {
            var result = AdviceRepository.SQL($"SELECT a.* FROM[kitos].[dbo].[Advice] a Join ItContract c on c.Id = a.RelationId Where c.OrganizationId = {orgKey} and a.Type = 0 Union SELECT a.* FROM[kitos].[dbo].[Advice] a Join ItProject p on p.Id = a.RelationId Where p.OrganizationId = {orgKey} and a.Type = 2 Union SELECT a.* FROM[kitos].[dbo].[Advice] a Join ItSystemUsage u on u.Id = a.RelationId Where u.OrganizationId = {orgKey} and a.Type = 1 Union SELECT a.* FROM[kitos].[dbo].[Advice] a Join ItInterface i on i.Id = a.RelationId Where i.OrganizationId = {orgKey} and a.Type = 3 Union SELECT a.* FROM[kitos].[dbo].[Advice] a Join DataProcessingRegistrations i on i.Id = a.RelationId Where i.OrganizationId = {orgKey} and a.Type = 4");

            return(result.AsQueryable());
        }
Exemple #11
0
 public void CreateAdvice(Advice advice)
 {
     ScheduleAdvice(advice);
     AdviceRepository.Update(advice);
     AdviceRepository.Save();
 }