Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSCTTV-3637 automatic publishing of new Orcid data. TTV model update. Keycloak configuration changes. #236

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions aspnetcore/src/api.Tests/Services_Tests/UserProfileServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,5 +539,26 @@ public void GetProfileEditorSource()
Assert.Equal(expectedProfileEditorSource.Organization.NameSv, actualProfileEditorSource.Organization.NameSv);
Assert.Equal(expectedProfileEditorSource.Organization.SectorId, actualProfileEditorSource.Organization.SectorId);
}

[Fact(DisplayName = "ProfileSettings from DimUserProfile")]
public void GetProfileSettings()
{
// Arrange
UserProfileService userProfileService = new();
DimUserProfile dimUserProfile = new()
{
Hidden = true,
PublishNewOrcidData = true
};
ProfileSettings expectedProfileSettings = new ProfileSettings() {
Hidden = true,
PublishNewOrcidData = true
};
// Act
ProfileSettings actualProfileSettings = userProfileService.GetProfileSettings(dimUserProfile);
// Assert
Assert.Equal(expectedProfileSettings.Hidden, actualProfileSettings.Hidden);
Assert.Equal(expectedProfileSettings.PublishNewOrcidData, actualProfileSettings.PublishNewOrcidData);
}
}
}
117 changes: 48 additions & 69 deletions aspnetcore/src/api/Controllers/ProfileSettingsController.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
using api.Services;
using api.Models.Api;
using api.Models.Common;
using api.Models.Ttv;
using api.Models.ProfileEditor;
using api.Services;
using api.Models.Api;
using api.Models.Common;
using api.Models.Ttv;
using api.Models.ProfileEditor;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.AspNetCore.Http;
using System;
using Microsoft.Extensions.Logging;
using api.Models.Log;
using api.Models.ProfileEditor.Items;
using static api.Models.Common.Constants;

namespace api.Controllers
{
Expand Down Expand Up @@ -48,6 +43,15 @@ public async Task<IActionResult> GetSettings()
// Get ORCID id
string orcidId = GetOrcidId();

// Get user profile
DimUserProfile dimUserProfile = await _userProfileService.GetUserprofile(orcidId);

// Check that userprofile exists.
if (dimUserProfile == null)
{
return Ok(new ApiResponse(success: false, reason: "profile not found"));
}

// Cache key
string cacheKey = _userProfileService.GetCMemoryCacheKey_ProfileSettings(orcidId);

Expand All @@ -57,40 +61,25 @@ public async Task<IActionResult> GetSettings()
return Ok(new ApiResponseProfileSettingsGet(success: true, reason: "", data: cachedProfileSettings, fromCache: true));
}

// Cached response was not found, get data
DimUserProfile dimUserProfile;
ProfileSettings profileSettings = new();
try
{
dimUserProfile = await _userProfileService.GetUserprofile(orcidId);
profileSettings.Hidden = dimUserProfile.Hidden;
}
catch (Exception ex)
{
_logger.LogInformation(
LogContent.MESSAGE_TEMPLATE,
this.GetLogUserIdentification(),
new LogApiInfo(
action: LogContent.Action.SETTINGS_GET,
error: true,
state: LogContent.ActionState.FAILED,
message: $"{ex.ToString()}"));
return Ok(new ApiResponse(success: false, reason: "profile not found", fromCache: false));
}
// Get settings data
ProfileSettings profileSettings = _userProfileService.GetProfileSettings(dimUserProfile);

// Store data into cache
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
// Keep in cache for this time, reset time if accessed.
.SetSlidingExpiration(TimeSpan.FromSeconds(Constants.Cache.MEMORY_CACHE_EXPIRATION_SECONDS));
_cache.Set(cacheKey, profileSettings, cacheEntryOptions);

return Ok(new ApiResponseProfileSettingsGet(success: true, reason: "", data: profileSettings, fromCache: false));
return Ok(
new ApiResponseProfileSettingsGet(
success: true,
reason: "",
data: profileSettings,
fromCache: false));
}

/// <summary>
/// Set profile settings.
/// Setting "hidden" to true removes profile from Elasticsearch and disables Elasticsearch sync for the profile.
/// Setting "hidden" to false adds profile to Elasticsearch and enables Elasticsearch sync for the profile.
/// </summary>
[HttpPost]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)]
Expand All @@ -104,55 +93,45 @@ public async Task<IActionResult> SetSettings([FromBody] ProfileSettings profileS
// Get ORCID id
string orcidId = GetOrcidId();

// Get user profile
DimUserProfile dimUserProfile = await _userProfileService.GetUserprofileTracking(orcidId);

// Check that userprofile exists.
if (dimUserProfile == null)
{
return Ok(new ApiResponse(success: false, reason: "profile not found"));
}

// Remove cached data
string cacheKey = _userProfileService.GetCMemoryCacheKey_ProfileSettings(orcidId);
_cache.Remove(cacheKey);

LogUserIdentification logUserIdentification = this.GetLogUserIdentification();

// Handle setting "hidden"
if (profileSettings.Hidden != null)
{
if (profileSettings.Hidden == true)
{
// Hide profile
await _userProfileService.HideProfile(orcidId: orcidId, logUserIdentification: logUserIdentification);
}
else if (profileSettings.Hidden == false)
{
// Reveal profile
await _userProfileService.RevealProfile(orcidId: orcidId, logUserIdentification: logUserIdentification);
}
}

return Ok(new ApiResponse());
}
string settingsToLogMessage = $"hidden={profileSettings.Hidden}, publishNewOrcidData={profileSettings.PublishNewOrcidData}";

_logger.LogInformation(
LogContent.MESSAGE_TEMPLATE,
logUserIdentification,
new LogApiInfo(
action: LogContent.Action.SETTINGS_SET,
state: LogContent.ActionState.START,
message: settingsToLogMessage));

// Save settings
await _userProfileService.SaveProfileSettings(
orcidId: orcidId,
dimUserProfile: dimUserProfile,
profileSettings: profileSettings,
logUserIdentification: logUserIdentification);

/// <summary>
/// DEPRECATED
/// Set profile state to "hidden".
/// Profile is removed from Elasticsearch index.
/// </summary>
[HttpGet]
[Route("hideprofile")]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)]
public async Task<IActionResult> HideProfileFromPortal()
{
// Get ORCID id
string orcidId = GetOrcidId();

LogUserIdentification logUserIdentification = this.GetLogUserIdentification();
_logger.LogInformation(
LogContent.MESSAGE_TEMPLATE,
logUserIdentification,
new LogApiInfo(
action: LogContent.Action.PROFILE_HIDE,
state: LogContent.ActionState.START));

// Set profile state to "hidden"
await _userProfileService.HideProfile(orcidId: orcidId, logUserIdentification: logUserIdentification);
action: LogContent.Action.SETTINGS_SET,
state: LogContent.ActionState.COMPLETE,
message: settingsToLogMessage));

return Ok(new ApiResponse());
}
Expand Down
10 changes: 10 additions & 0 deletions aspnetcore/src/api/Models/ProfileEditor/ProfileSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ public ProfileSettings()
{
}

/// <summary>
/// When true, removes profile from Elasticsearch and disables Elasticsearch sync for the profile.
/// When false, adds profile to Elasticsearch and enables Elasticsearch sync for the profile.
/// </summary>
public bool? Hidden { get; set; }

/// <summary>
/// When true, new ORCID data items are automatically made public.
/// When false, user must manually set new ORCID data items public.
/// </summary>
public bool? PublishNewOrcidData { get; set; }
}
}
1 change: 1 addition & 0 deletions aspnetcore/src/api/Models/StructuredLog/LogContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static class Action
public const string ORCID_WEBHOOK_UNREGISTER = "ORCID: webhook: unregister";
public const string ORCID_WEBHOOK_RECEIVED = "ORCID: webhook: received";
public const string SETTINGS_GET = "Settings: get";
public const string SETTINGS_SET = "Settings: set";
}

public static class ActionState
Expand Down
74 changes: 36 additions & 38 deletions aspnetcore/src/api/Models/Ttv/DimPid.cs
Original file line number Diff line number Diff line change
@@ -1,83 +1,81 @@
using System;
using System.Collections.Generic;

namespace api.Models.Ttv;

public partial class DimPid
{
public int Id { get; set; }

public string PidContent { get; set; }

public string PidType { get; set; }

public int DimOrganizationId { get; set; }

public int DimKnownPersonId { get; set; }

public int DimPublicationId { get; set; }

public int DimServiceId { get; set; }

public int DimInfrastructureId { get; set; }

public int DimPublicationChannelId { get; set; }

public int DimResearchDatasetId { get; set; }

public int DimResearchDataCatalogId { get; set; }

public int DimResearchActivityId { get; set; }

public int DimEventId { get; set; }

public int DimProfileOnlyPublicationId { get; set; }

public string SourceId { get; set; }

public string SourceDescription { get; set; }

public DateTime? Created { get; set; }

public DateTime? Modified { get; set; }

public int? DimProfileOnlyDatasetId { get; set; }

public int? DimProfileOnlyFundingDecisionId { get; set; }

public int? DimResearchProjectId { get; set; }

public int? DimResearchCommunityId { get; set; }

public virtual DimEvent DimEvent { get; set; }

public virtual DimInfrastructure DimInfrastructure { get; set; }

public virtual DimKnownPerson DimKnownPerson { get; set; }

public virtual DimOrganization DimOrganization { get; set; }

public virtual DimProfileOnlyDataset DimProfileOnlyDataset { get; set; }

public virtual DimProfileOnlyFundingDecision DimProfileOnlyFundingDecision { get; set; }

public virtual DimProfileOnlyPublication DimProfileOnlyPublication { get; set; }

public virtual DimPublication DimPublication { get; set; }

public virtual DimPublicationChannel DimPublicationChannel { get; set; }

public virtual DimResearchActivity DimResearchActivity { get; set; }

public virtual DimResearchCommunity DimResearchCommunity { get; set; }

public virtual DimResearchDataCatalog DimResearchDataCatalog { get; set; }

public virtual DimResearchDataset DimResearchDataset { get; set; }

public virtual DimService DimService { get; set; }

public virtual ICollection<FactFieldValue> FactFieldValueDimPidIdOrcidPutCodeNavigations { get; set; } = new List<FactFieldValue>();

public virtual ICollection<FactFieldValue> FactFieldValueDimPids { get; set; } = new List<FactFieldValue>();
}
Loading
Loading