Skip to content

Commit

Permalink
Merge pull request #470 from Strongminds/release/11.1.4
Browse files Browse the repository at this point in the history
Release/11.1.4
  • Loading branch information
mrjsawdk authored Nov 29, 2022
2 parents 8f3d7d5 + 67b72a4 commit eb46968
Show file tree
Hide file tree
Showing 298 changed files with 13,351 additions and 3,068 deletions.
131 changes: 75 additions & 56 deletions Core.ApplicationServices/AdviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using Core.DomainServices.Extensions;
using Core.DomainServices.Time;
using Infrastructure.Services.DataAccess;

using Core.DomainModel.Shared;
using Core.DomainServices.Notifications;
using Core.DomainModel.Notification;
Expand Down Expand Up @@ -134,29 +133,37 @@ public bool SendAdvice(int id)
var advice = _adviceRepository.AsQueryable().ById(id);
if (advice != null)
{
if (advice.AdviceType == AdviceType.Immediate || IsAdviceInScope(advice))
if (IsDeactivated(advice))
{
_logger.Warning("SendAdvice has been invoked for deactivated Advice with id: {adviceId}. The hangfire jobs should have been deleted during deactivation. Check the logs.", id);
DeleteJobFromHangfire(advice);
}
else
{
if (DispatchEmails(advice))
if (advice.AdviceType == AdviceType.Immediate || IsAdviceInScope(advice))
{
_adviceRepository.Update(advice);
if (DispatchEmails(advice))
{
_adviceRepository.Update(advice);

_adviceSentRepository.Insert(new AdviceSent { AdviceId = id, AdviceSentDate = _operationClock.Now });
_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);
}
if (advice.AdviceType == AdviceType.Immediate)
{
advice.IsActive = false;
}
else if (IsAdviceExpired(advice))
{
advice.IsActive = false;
DeleteJobFromHangfire(advice);
}

_adviceRepository.Save();
_adviceSentRepository.Save();
transaction.Commit();
_adviceRepository.Save();
_adviceSentRepository.Save();
transaction.Commit();
}
}
else
{
Expand All @@ -173,6 +180,11 @@ public bool SendAdvice(int id)
}
}

private static bool IsDeactivated(Advice advice)
{
return !advice.IsActive;
}

private bool IsAdviceExpired(Advice advice)
{
return advice.StopDate != null && advice.StopDate.Value.Date < _operationClock.Now.Date;
Expand Down Expand Up @@ -397,49 +409,56 @@ public void CreateOrUpdateJob(int adviceId)
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)
if (IsDeactivated(advice))
{
var jobId = adviceTrigger.PartitionId.Match(partitionId => Advice.CreatePartitionJobId(adviceId, partitionId), () => advice.JobId);
_hangfireApi.AddOrUpdateRecurringJob(jobId, () => SendAdvice(adviceId), adviceTrigger.Cron);
_logger.Warning("Advice with id: {adviceId} will not be scheduled since it has been deactivated.", adviceId);
}

if (advice.StopDate.HasValue)
else
{
//Schedule deactivation to happen the day after the stop date (stop date is "last day alive" for the advice)
var deactivateAt = advice.StopDate.Value.Date == DateTime.MaxValue.Date ? DateTime.MaxValue.Date : advice.StopDate.Value.Date.AddDays(1);
_hangfireApi.Schedule(() => DeactivateById(advice.Id), new DateTimeOffset(deactivateAt));
}
var adviceAlarmDate = advice.AlarmDate.Value;
var adviceScheduling = advice.Scheduling.Value;

//If time has passed the trigger time, Hangfire will not fire until the next trigger date so we must force it.
if (adviceAlarmDate.Date.Equals(_operationClock.Now.Date))
{
switch (adviceScheduling)
var adviceTriggers = AdviceTriggerFactory.CreateFrom(adviceAlarmDate, adviceScheduling);

foreach (var adviceTrigger in adviceTriggers)
{
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;
var jobId = adviceTrigger.PartitionId.Match(partitionId => Advice.CreatePartitionJobId(adviceId, partitionId), () => advice.JobId);
_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)
var deactivateAt = advice.StopDate.Value.Date == DateTime.MaxValue.Date ? DateTime.MaxValue.Date : advice.StopDate.Value.Date.AddDays(1);
_hangfireApi.Schedule(() => DeactivateById(advice.Id), new DateTimeOffset(deactivateAt));
}

//If time has passed the trigger time, Hangfire will not fire until the next trigger date 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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ public bool AllowDelete(IEntity entity)
OrganizationRight right =>
// Only global admin can set other users as global admins
AllowAdministerOrganizationRight(right),
OrganizationUnit unit =>
IsGlobalAdmin() || IsLocalAdmin(unit.OrganizationId),
_ => true
};
}
Expand Down
7 changes: 6 additions & 1 deletion Core.ApplicationServices/Contract/IItContractService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Core.ApplicationServices.Model.Contracts;
using Core.DomainModel.GDPR;
using Core.DomainModel.ItContract;
using Core.DomainModel.Organization;
using Core.DomainServices.Queries;


Expand All @@ -26,6 +27,10 @@ public interface IItContractService
Maybe<OperationError> ValidateNewName(int contractId, string name);
IQueryable<ItContract> Query(params IDomainQuery<ItContract>[] conditions);
Result<ContractOptions, OperationError> GetAssignableContractOptions(int organizationId);
Result<IEnumerable<(int year, int quarter)>,OperationError> GetAppliedProcurementPlans(int organizationId);
Result<IEnumerable<(int year, int quarter)>, OperationError> GetAppliedProcurementPlans(int organizationId);
Maybe<OperationError> SetResponsibleUnit(int contractId, Guid targetUnitUuid);
Maybe<OperationError> RemoveResponsibleUnit(int contractId);
Maybe<OperationError> RemovePaymentResponsibleUnits(int contractId, bool isInternal, IEnumerable<int> paymentIds);
Maybe<OperationError> TransferPayments(int contractId, Guid targetUnitUuid, bool isInternal, IEnumerable<int> paymentIds);
}
}
Loading

0 comments on commit eb46968

Please sign in to comment.