From 99762667e97204c03b7ebc4b69b09643586387c1 Mon Sep 17 00:00:00 2001 From: Shane Melton Date: Wed, 24 Jan 2024 08:26:37 -0800 Subject: [PATCH] [AC-1890] Include collection permission details in PUT/POST response (#3658) * [Ac-1890] Return CollectionDetailsResponseModel for collection PUT/POST endpoints when a userId is available in the current context * [AC-1890] Fix broken tests * [AC-1890] Update to use Organization FC column --- src/Api/Controllers/CollectionsController.cs | 23 +++++++++++++++++-- .../LegacyCollectionsControllerTests.cs | 8 +++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Api/Controllers/CollectionsController.cs b/src/Api/Controllers/CollectionsController.cs index 407251801..6c1080503 100644 --- a/src/Api/Controllers/CollectionsController.cs +++ b/src/Api/Controllers/CollectionsController.cs @@ -250,7 +250,17 @@ public class CollectionsController : Controller } await _collectionService.SaveAsync(collection, groups, users); - return new CollectionResponseModel(collection); + + if (!_currentContext.UserId.HasValue) + { + return new CollectionResponseModel(collection); + } + + // If we have a user, fetch the collection to get the latest permission details + var userCollectionDetails = await _collectionRepository.GetByIdAsync(collection.Id, + _currentContext.UserId.Value, await FlexibleCollectionsIsEnabledAsync(collection.OrganizationId)); + + return new CollectionDetailsResponseModel(userCollectionDetails); } [HttpPut("{id}")] @@ -618,7 +628,16 @@ public class CollectionsController : Controller var groups = model.Groups?.Select(g => g.ToSelectionReadOnly()); var users = model.Users?.Select(g => g.ToSelectionReadOnly()); await _collectionService.SaveAsync(model.ToCollection(collection), groups, users); - return new CollectionResponseModel(collection); + + if (!_currentContext.UserId.HasValue) + { + return new CollectionResponseModel(collection); + } + + // If we have a user, fetch the collection details to get the latest permission details for the user + var updatedCollectionDetails = await _collectionRepository.GetByIdAsync(id, _currentContext.UserId.Value, await FlexibleCollectionsIsEnabledAsync(collection.OrganizationId)); + + return new CollectionDetailsResponseModel(updatedCollectionDetails); } private async Task PutUsers_vNext(Guid id, IEnumerable model) diff --git a/test/Api.Test/Controllers/LegacyCollectionsControllerTests.cs b/test/Api.Test/Controllers/LegacyCollectionsControllerTests.cs index 7b3bad676..0d2ec824f 100644 --- a/test/Api.Test/Controllers/LegacyCollectionsControllerTests.cs +++ b/test/Api.Test/Controllers/LegacyCollectionsControllerTests.cs @@ -49,6 +49,10 @@ public class LegacyCollectionsControllerTests sutProvider.GetDependency().GetByOrganizationAsync(orgId, orgUser.UserId.Value) .Returns(orgUser); + sutProvider.GetDependency() + .GetByIdAsync(Arg.Any(), orgUser.UserId.Value, Arg.Any()) + .Returns(new CollectionDetails()); + var collectionRequest = new CollectionRequestModel { Name = "encrypted_string", @@ -87,6 +91,10 @@ public class LegacyCollectionsControllerTests sutProvider.GetDependency().GetByOrganizationAsync(orgId, orgUser.UserId.Value) .Returns(orgUser); + sutProvider.GetDependency() + .GetByIdAsync(Arg.Any(), orgUser.UserId.Value, Arg.Any()) + .Returns(new CollectionDetails()); + var collectionRequest = new CollectionRequestModel { Name = "encrypted_string",