fix(request-limits): 🐛 Fixed the issue where we were calculating Tv Request limit reset date incorrectly

pull/4317/head
tidusjar 3 years ago
parent e302cf685f
commit ceaec3feb0

@ -6,6 +6,7 @@ using Ombi.Core.Authentication;
using Ombi.Core.Engine; using Ombi.Core.Engine;
using Ombi.Core.Models; using Ombi.Core.Models;
using Ombi.Core.Services; using Ombi.Core.Services;
using Ombi.Helpers;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests; using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository; using Ombi.Store.Repository;
@ -277,7 +278,7 @@ namespace Ombi.Core.Tests.Engine
.With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true) .With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true)
.And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2) .And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2)
.And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1) .And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1)
.And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddDays(1).AddHours(-1)) .And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddDays(1).Date)
); );
} }
@ -318,7 +319,7 @@ namespace Ombi.Core.Tests.Engine
.With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true) .With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true)
.And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2) .And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2)
.And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(0) .And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(0)
.And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddDays(1).AddHours(-2)) .And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddDays(1).Date)
); );
} }
@ -359,7 +360,7 @@ namespace Ombi.Core.Tests.Engine
.With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true) .With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true)
.And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(10) .And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(10)
.And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1) .And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1)
.And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddDays(1).AddHours(-2)) .And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddDays(1).Date)
); );
} }
@ -426,7 +427,7 @@ namespace Ombi.Core.Tests.Engine
.With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true) .With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true)
.And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2) .And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2)
.And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1) .And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1)
.And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddDays(7)) .And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.FirstDateInWeek().AddDays(7).Date)
); );
} }
@ -467,7 +468,7 @@ namespace Ombi.Core.Tests.Engine
.With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true) .With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true)
.And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2) .And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2)
.And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(0) .And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(0)
.And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddDays(6)) .And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.FirstDateInWeek().AddDays(7).Date)
); );
} }
@ -508,7 +509,7 @@ namespace Ombi.Core.Tests.Engine
.With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true) .With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true)
.And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(10) .And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(10)
.And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1) .And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1)
.And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddDays(6)) .And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.FirstDateInWeek().AddDays(7).Date)
); );
} }
@ -575,7 +576,7 @@ namespace Ombi.Core.Tests.Engine
.With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true) .With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true)
.And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2) .And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2)
.And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1) .And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1)
.And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddMonths(1)) .And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(new DateTime(today.Year, today.Month, 1).AddMonths(1).Date)
); );
} }
@ -616,7 +617,7 @@ namespace Ombi.Core.Tests.Engine
.With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true) .With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true)
.And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2) .And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(2)
.And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(0) .And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(0)
.And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddMonths(1).AddDays(-1)) .And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(new DateTime(today.Year, today.Month, 1).AddMonths(1).Date)
); );
} }
@ -657,7 +658,7 @@ namespace Ombi.Core.Tests.Engine
.With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true) .With.Property(nameof(RequestQuotaCountModel.HasLimit)).EqualTo(true)
.And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(10) .And.Property(nameof(RequestQuotaCountModel.Limit)).EqualTo(10)
.And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1) .And.Property(nameof(RequestQuotaCountModel.Remaining)).EqualTo(1)
.And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(today.AddMonths(1).AddDays(-1)) .And.Property(nameof(RequestQuotaCountModel.NextRequest)).EqualTo(new DateTime(today.Year, today.Month, 1).AddMonths(1).Date)
); );
} }

@ -25,16 +25,11 @@
// ************************************************************************/ // ************************************************************************/
#endregion #endregion
using System;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Ombi.Core.Authentication;
using Ombi.Core.Rule.Interfaces; using Ombi.Core.Rule.Interfaces;
using Ombi.Core.Services; using Ombi.Core.Services;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests; using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository;
namespace Ombi.Core.Rule.Rules.Request namespace Ombi.Core.Rule.Rules.Request
{ {

@ -1,8 +1,6 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Ombi.Core.Authentication; using Ombi.Core.Authentication;
using Ombi.Core.Engine.Interfaces;
using Ombi.Core.Models; using Ombi.Core.Models;
using Ombi.Core.Rule.Interfaces;
using Ombi.Helpers; using Ombi.Helpers;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests; using Ombi.Store.Entities.Requests;
@ -246,6 +244,7 @@ namespace Ombi.Core.Services
}; };
} }
var now = DateTime.UtcNow;
switch (user.EpisodeRequestLimitType) switch (user.EpisodeRequestLimitType)
{ {
case RequestLimitType.Day: case RequestLimitType.Day:
@ -260,10 +259,10 @@ namespace Ombi.Core.Services
.OrderBy(x => x.RequestDate) .OrderBy(x => x.RequestDate)
.Select(x => x.RequestDate) .Select(x => x.RequestDate)
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
nextRequest = oldestRequestedAt.AddDays(1); nextRequest = oldestRequestedAt.AddDays(1).Date;
break; break;
case RequestLimitType.Week: case RequestLimitType.Week:
var fdow = now.FirstDateInWeek();
filteredLog = log.Where(x => x.RequestDate >= DateTime.UtcNow.Date.AddDays(-7)); filteredLog = log.Where(x => x.RequestDate >= DateTime.UtcNow.Date.AddDays(-7));
// Needed, due to a bug which would cause all episode counts to be 0 // Needed, due to a bug which would cause all episode counts to be 0
zeroEpisodeCount = await filteredLog.Where(x => x.EpisodeCount == 0).Select(x => x.EpisodeCount).CountAsync(); zeroEpisodeCount = await filteredLog.Where(x => x.EpisodeCount == 0).Select(x => x.EpisodeCount).CountAsync();
@ -274,9 +273,10 @@ namespace Ombi.Core.Services
.OrderBy(x => x.RequestDate) .OrderBy(x => x.RequestDate)
.Select(x => x.RequestDate) .Select(x => x.RequestDate)
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
nextRequest = oldestRequestedAt.AddDays(7); nextRequest = fdow.AddDays(7).Date;
break; break;
case RequestLimitType.Month: case RequestLimitType.Month:
var firstDayOfMonth = new DateTime(now.Year, now.Month, 1);
filteredLog = log.Where(x => x.RequestDate >= DateTime.UtcNow.Date.AddMonths(-1)); filteredLog = log.Where(x => x.RequestDate >= DateTime.UtcNow.Date.AddMonths(-1));
// Needed, due to a bug which would cause all episode counts to be 0 // Needed, due to a bug which would cause all episode counts to be 0
zeroEpisodeCount = await filteredLog.Where(x => x.EpisodeCount == 0).Select(x => x.EpisodeCount).CountAsync(); zeroEpisodeCount = await filteredLog.Where(x => x.EpisodeCount == 0).Select(x => x.EpisodeCount).CountAsync();
@ -287,7 +287,7 @@ namespace Ombi.Core.Services
.OrderBy(x => x.RequestDate) .OrderBy(x => x.RequestDate)
.Select(x => x.RequestDate) .Select(x => x.RequestDate)
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
nextRequest = oldestRequestedAt.AddMonths(1); nextRequest = firstDayOfMonth.AddMonths(1).Date;
break; break;
} }

Loading…
Cancel
Save