-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
LastActivityDate
on installation token refresh
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace Bit.Core.Platform.Installations; | ||
|
||
/// <summary> | ||
/// Command interface responsible for updating data on an `Installation` | ||
/// record. | ||
/// </summary> | ||
/// <remarks> | ||
/// This interface is implemented by `UpdateInstallationCommand` | ||
/// </remarks> | ||
/// <seealso cref="Bit.Core.Platform.Installations.UpdateInstallationCommand"/> | ||
public interface IUpdateInstallationCommand | ||
{ | ||
Task UpdateLastActivityDateAsync(Guid installationId); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace Bit.Core.Platform.Installations; | ||
|
||
/// <summary> | ||
/// Commands responsible for updating an installation from | ||
/// `InstallationRepository`. | ||
/// </summary> | ||
/// <remarks> | ||
/// If referencing: you probably want the interface | ||
/// `IUpdateInstallationCommand` instead of directly calling this class. | ||
/// </remarks> | ||
/// <seealso cref="IUpdateInstallationCommand"/> | ||
public class UpdateInstallationCommand : IUpdateInstallationCommand | ||
{ | ||
private readonly IGetInstallationQuery _getInstallationQuery; | ||
private readonly IInstallationRepository _installationRepository; | ||
private readonly TimeProvider _timeProvider; | ||
|
||
public UpdateInstallationCommand( | ||
IGetInstallationQuery getInstallationQuery, | ||
IInstallationRepository installationRepository, | ||
TimeProvider timeProvider | ||
) | ||
{ | ||
_getInstallationQuery = getInstallationQuery; | ||
_installationRepository = installationRepository; | ||
_timeProvider = timeProvider; | ||
} | ||
|
||
public async Task UpdateLastActivityDateAsync(Guid installationId) | ||
{ | ||
if (installationId == default) | ||
{ | ||
throw new Exception | ||
( | ||
"Tried to update the last activity date for " + | ||
"an installation, but an invalid installation id was " + | ||
"provided." | ||
); | ||
Check warning on line 38 in src/Core/Platform/Installations/Commands/UpdateInstallationActivityDateCommand/UpdateInstallationCommand.cs Codecov / codecov/patchsrc/Core/Platform/Installations/Commands/UpdateInstallationActivityDateCommand/UpdateInstallationCommand.cs#L32-L38
|
||
} | ||
var installation = await _getInstallationQuery.GetByIdAsync(installationId); | ||
if (installation == null) | ||
{ | ||
throw new Exception | ||
( | ||
"Tried to update the last activity date for " + | ||
$"installation {installationId.ToString()}, but no " + | ||
"installation was found for that id." | ||
); | ||
Check warning on line 48 in src/Core/Platform/Installations/Commands/UpdateInstallationActivityDateCommand/UpdateInstallationCommand.cs Codecov / codecov/patchsrc/Core/Platform/Installations/Commands/UpdateInstallationActivityDateCommand/UpdateInstallationCommand.cs#L42-L48
|
||
} | ||
installation.LastActivityDate = _timeProvider.GetUtcNow().UtcDateTime; | ||
await _installationRepository.UpsertAsync(installation); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace Bit.Core.Platform.Installations; | ||
|
||
/// <summary> | ||
/// Queries responsible for fetching an installation from | ||
/// `InstallationRepository`. | ||
/// </summary> | ||
/// <remarks> | ||
/// If referencing: you probably want the interface `IGetInstallationQuery` | ||
/// instead of directly calling this class. | ||
/// </remarks> | ||
/// <seealso cref="IGetInstallationQuery"/> | ||
public class GetInstallationQuery : IGetInstallationQuery | ||
{ | ||
private readonly IInstallationRepository _installationRepository; | ||
|
||
public GetInstallationQuery(IInstallationRepository installationRepository) | ||
{ | ||
_installationRepository = installationRepository; | ||
} | ||
|
||
/// <inheritdoc cref="IGetInstallationQuery.GetByIdAsync"/> | ||
public async Task<Installation> GetByIdAsync(Guid installationId) | ||
{ | ||
Check warning on line 23 in src/Core/Platform/Installations/Queries/GetInstallationQuery/GetInstallationQuery.cs Codecov / codecov/patchsrc/Core/Platform/Installations/Queries/GetInstallationQuery/GetInstallationQuery.cs#L23
|
||
if (installationId == default(Guid)) | ||
{ | ||
return null; | ||
Check warning on line 26 in src/Core/Platform/Installations/Queries/GetInstallationQuery/GetInstallationQuery.cs Codecov / codecov/patchsrc/Core/Platform/Installations/Queries/GetInstallationQuery/GetInstallationQuery.cs#L25-L26
|
||
} | ||
return await _installationRepository.GetByIdAsync(installationId); | ||
} | ||
Check warning on line 29 in src/Core/Platform/Installations/Queries/GetInstallationQuery/GetInstallationQuery.cs Codecov / codecov/patchsrc/Core/Platform/Installations/Queries/GetInstallationQuery/GetInstallationQuery.cs#L28-L29
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Bit.Core.Platform.Installations; | ||
|
||
/// <summary> | ||
/// Query interface responsible for fetching an installation from | ||
/// `InstallationRepository`. | ||
/// </summary> | ||
/// <remarks> | ||
/// This interface is implemented by `GetInstallationQuery` | ||
/// </remarks> | ||
/// <seealso cref="GetInstallationQuery"/> | ||
public interface IGetInstallationQuery | ||
{ | ||
/// <summary> | ||
/// Retrieves an installation from the `InstallationRepository` by its id. | ||
/// </summary> | ||
/// <param name="installationId">The GUID id of the installation.</param> | ||
/// <returns>A task containing an `Installation`.</returns> | ||
/// <seealso cref="T:Bit.Core.Platform.Installations.Repositories.IInstallationRepository"/> | ||
Task<Installation> GetByIdAsync(Guid installationId); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Bit.Core.Platform.Installations; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bit.Core.Platform; | ||
|
||
public static class PlatformServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Extend DI to include commands and queries exported from the Platform | ||
/// domain. | ||
/// </summary> | ||
public static IServiceCollection AddPlatformServices(this IServiceCollection services) | ||
{ | ||
services.AddScoped<IGetInstallationQuery, GetInstallationQuery>(); | ||
services.AddScoped<IUpdateInstallationCommand, UpdateInstallationCommand>(); | ||
|
||
return services; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Bit.Test.Common.AutoFixture; | ||
using Bit.Test.Common.AutoFixture.Attributes; | ||
using Microsoft.Extensions.Time.Testing; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Bit.Core.Platform.Installations.Tests; | ||
|
||
[SutProviderCustomize] | ||
public class UpdateInstallationCommandTests | ||
{ | ||
[Theory] | ||
[BitAutoData] | ||
public async Task UpdateLastActivityDateAsync_ShouldUpdateLastActivityDate( | ||
Installation installation | ||
) | ||
{ | ||
// Arrange | ||
var sutProvider = new SutProvider<UpdateInstallationCommand>() | ||
.WithFakeTimeProvider() | ||
.Create(); | ||
|
||
var someDate = new DateTime(2014, 11, 3, 18, 27, 0, DateTimeKind.Utc); | ||
sutProvider.GetDependency<FakeTimeProvider>().SetUtcNow(someDate); | ||
|
||
sutProvider | ||
.GetDependency<IGetInstallationQuery>() | ||
.GetByIdAsync(installation.Id) | ||
.Returns(installation); | ||
|
||
// Act | ||
await sutProvider.Sut.UpdateLastActivityDateAsync(installation.Id); | ||
|
||
// Assert | ||
await sutProvider | ||
.GetDependency<IInstallationRepository>() | ||
.Received(1) | ||
.UpsertAsync(Arg.Is<Installation>(inst => inst.LastActivityDate == someDate)); | ||
} | ||
} |