-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update
LastActivityDate
on installation token refresh (#5081)
- Loading branch information
1 parent
cd7c4bf
commit 90f7bfe
Showing
13 changed files
with
229 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...nstallations/Commands/UpdateInstallationActivityDateCommand/IUpdateInstallationCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
53 changes: 53 additions & 0 deletions
53
...Installations/Commands/UpdateInstallationActivityDateCommand/UpdateInstallationCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
); | ||
} | ||
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." | ||
); | ||
} | ||
installation.LastActivityDate = _timeProvider.GetUtcNow().UtcDateTime; | ||
await _installationRepository.UpsertAsync(installation); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/Core/Platform/Installations/Queries/GetInstallationQuery/GetInstallationQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
if (installationId == default(Guid)) | ||
{ | ||
return null; | ||
} | ||
return await _installationRepository.GetByIdAsync(installationId); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Core/Platform/Installations/Queries/GetInstallationQuery/IGetInstallationQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
test/Core.Test/Platform/Installations/Commands/UpdateInstallationCommandTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |