Skip to content

Commit

Permalink
Extract GetDateRange into ApiHelpers util
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Avery committed Oct 9, 2023
1 parent ba36b29 commit 98b865b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 51 deletions.
35 changes: 7 additions & 28 deletions src/Api/Controllers/EventsController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bit.Api.Models.Response;
using Bit.Api.Utilities;
using Bit.Core.Context;
using Bit.Core.Exceptions;
using Bit.Core.Models.Data;
Expand Down Expand Up @@ -41,7 +42,7 @@ public EventsController(
public async Task<ListResponseModel<EventResponseModel>> GetUser(
[FromQuery] DateTime? start = null, [FromQuery] DateTime? end = null, [FromQuery] string continuationToken = null)
{
var dateRange = GetDateRange(start, end);
var dateRange = ApiHelpers.GetDateRange(start, end);
var userId = _userService.GetProperUserId(User).Value;
var result = await _eventRepository.GetManyByUserAsync(userId, dateRange.Item1, dateRange.Item2,
new PageOptions { ContinuationToken = continuationToken });
Expand Down Expand Up @@ -75,7 +76,7 @@ public async Task<ListResponseModel<EventResponseModel>> GetCipher(string id,
throw new NotFoundException();
}

var dateRange = GetDateRange(start, end);
var dateRange = ApiHelpers.GetDateRange(start, end);
var result = await _eventRepository.GetManyByCipherAsync(cipher, dateRange.Item1, dateRange.Item2,
new PageOptions { ContinuationToken = continuationToken });
var responses = result.Data.Select(e => new EventResponseModel(e));
Expand All @@ -92,7 +93,7 @@ public async Task<ListResponseModel<EventResponseModel>> GetOrganization(string
throw new NotFoundException();
}

var dateRange = GetDateRange(start, end);
var dateRange = ApiHelpers.GetDateRange(start, end);
var result = await _eventRepository.GetManyByOrganizationAsync(orgId, dateRange.Item1, dateRange.Item2,
new PageOptions { ContinuationToken = continuationToken });
var responses = result.Data.Select(e => new EventResponseModel(e));
Expand All @@ -110,7 +111,7 @@ public async Task<ListResponseModel<EventResponseModel>> GetOrganizationUser(str
throw new NotFoundException();
}

var dateRange = GetDateRange(start, end);
var dateRange = ApiHelpers.GetDateRange(start, end);
var result = await _eventRepository.GetManyByOrganizationActingUserAsync(organizationUser.OrganizationId,
organizationUser.UserId.Value, dateRange.Item1, dateRange.Item2,
new PageOptions { ContinuationToken = continuationToken });
Expand All @@ -127,7 +128,7 @@ public async Task<ListResponseModel<EventResponseModel>> GetProvider(Guid provid
throw new NotFoundException();
}

var dateRange = GetDateRange(start, end);
var dateRange = ApiHelpers.GetDateRange(start, end);
var result = await _eventRepository.GetManyByProviderAsync(providerId, dateRange.Item1, dateRange.Item2,
new PageOptions { ContinuationToken = continuationToken });
var responses = result.Data.Select(e => new EventResponseModel(e));
Expand All @@ -145,33 +146,11 @@ public async Task<ListResponseModel<EventResponseModel>> GetProviderUser(Guid pr
throw new NotFoundException();
}

var dateRange = GetDateRange(start, end);
var dateRange = ApiHelpers.GetDateRange(start, end);
var result = await _eventRepository.GetManyByProviderActingUserAsync(providerUser.ProviderId,
providerUser.UserId.Value, dateRange.Item1, dateRange.Item2,
new PageOptions { ContinuationToken = continuationToken });
var responses = result.Data.Select(e => new EventResponseModel(e));
return new ListResponseModel<EventResponseModel>(responses, result.ContinuationToken);
}

private Tuple<DateTime, DateTime> GetDateRange(DateTime? start, DateTime? end)
{
if (!end.HasValue || !start.HasValue)
{
end = DateTime.UtcNow.Date.AddDays(1).AddMilliseconds(-1);
start = DateTime.UtcNow.Date.AddDays(-30);
}
else if (start.Value > end.Value)
{
var newEnd = start;
start = end;
end = newEnd;
}

if ((end.Value - start.Value) > TimeSpan.FromDays(367))
{
throw new BadRequestException("Range too large.");
}

return new Tuple<DateTime, DateTime>(start.Value, end.Value);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bit.Api.Models.Response;
using Bit.Api.Utilities;
using Bit.Core.Exceptions;
using Bit.Core.Models.Data;
using Bit.Core.Repositories;
Expand Down Expand Up @@ -42,34 +43,12 @@ public async Task<ListResponseModel<EventResponseModel>> GetServiceAccountEvents
throw new NotFoundException();
}

var dateRange = GetDateRange(start, end);
var dateRange = ApiHelpers.GetDateRange(start, end);

var result = await _eventRepository.GetManyByServiceAccountAsync(serviceAccount.OrganizationId,
serviceAccount.Id, dateRange.Item1, dateRange.Item2,
new PageOptions { ContinuationToken = continuationToken });
var responses = result.Data.Select(e => new EventResponseModel(e));
return new ListResponseModel<EventResponseModel>(responses, result.ContinuationToken);
}

private Tuple<DateTime, DateTime> GetDateRange(DateTime? start, DateTime? end)
{
if (!end.HasValue || !start.HasValue)
{
end = DateTime.UtcNow.Date.AddDays(1).AddMilliseconds(-1);
start = DateTime.UtcNow.Date.AddDays(-30);
}
else if (start.Value > end.Value)
{
var newEnd = start;
start = end;
end = newEnd;
}

if (end.Value - start.Value > TimeSpan.FromDays(367))
{
throw new BadRequestException("Range too large.");
}

return new Tuple<DateTime, DateTime>(start.Value, end.Value);
}
}
23 changes: 23 additions & 0 deletions src/Api/Utilities/ApiHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text.Json;
using Azure.Messaging.EventGrid;
using Azure.Messaging.EventGrid.SystemEvents;
using Bit.Core.Exceptions;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Mvc;

Expand Down Expand Up @@ -69,4 +70,26 @@ public async static Task<ObjectResult> HandleAzureEvents(HttpRequest request,

return new OkObjectResult(response);
}

public static Tuple<DateTime, DateTime> GetDateRange(DateTime? start, DateTime? end)
{
if (!end.HasValue || !start.HasValue)
{
end = DateTime.UtcNow.Date.AddDays(1).AddMilliseconds(-1);
start = DateTime.UtcNow.Date.AddDays(-30);
}
else if (start.Value > end.Value)
{
var newEnd = start;
start = end;
end = newEnd;
}

if ((end.Value - start.Value) > TimeSpan.FromDays(367))
{
throw new BadRequestException("Range too large.");
}

return new Tuple<DateTime, DateTime>(start.Value, end.Value);
}
}

0 comments on commit 98b865b

Please sign in to comment.