Skip to content

Commit

Permalink
CSCTTV-3637 automatic publishing new orcid data (#234)
Browse files Browse the repository at this point in the history
* Modify profile settings handling. Add setting to automatically publish new ORCID data.

* TTV model update 15.5.2024 (#232)

* TTV model update 20.5.2024 (#233)

* TTV model update 15.5.2024 (#232)
  • Loading branch information
sarkikos authored May 20, 2024
1 parent 6fb3171 commit f6e1727
Show file tree
Hide file tree
Showing 23 changed files with 2,370 additions and 1,998 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ public void getEmptyDimPid_01()
Assert.Equal<int>(-1, actualDimPid.DimInfrastructureId);
Assert.Equal<int>(-1, actualDimPid.DimPublicationChannelId);
Assert.Equal<int>(-1, actualDimPid.DimResearchDatasetId);
Assert.Equal<int>(-1, actualDimPid.DimFundingDecisionId);
Assert.Equal<int>(-1, (int)actualDimPid.DimResearchProjectId);
Assert.Equal<int>(-1, (int)actualDimPid.DimResearchCommunityId);
Assert.Equal<int>(-1, actualDimPid.DimResearchDataCatalogId);
Assert.Equal<int>(-1, actualDimPid.DimResearchActivityId);
Assert.Equal<int>(-1, actualDimPid.DimEventId);
Expand Down Expand Up @@ -538,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
35 changes: 35 additions & 0 deletions aspnetcore/src/api/Models/Ttv/DimCallDecision.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;

namespace api.Models.Ttv;

/// <summary>
/// Rahoituspäätöspaneeli
/// </summary>
public partial class DimCallDecision
{
public int Id { get; set; }

public int DecisionMaker { get; set; }

public int DimDateIdApproval { get; set; }

public int DimCallProgrammeId { get; set; }

/// <summary>
/// Rahoituspäätöspaneeli - Haun vaihe
/// </summary>
public string CallProcessingPhase { get; set; }

public string SourceId { get; set; }

public string SourceDescription { get; set; }

public virtual DimReferencedatum DecisionMakerNavigation { get; set; }

public virtual DimCallProgramme DimCallProgramme { get; set; }

public virtual DimDate DimDateIdApprovalNavigation { get; set; }

public virtual ICollection<DimFundingDecision> DimFundingDecisions { get; set; } = new List<DimFundingDecision>();
}
12 changes: 12 additions & 0 deletions aspnetcore/src/api/Models/Ttv/DimCallProgramme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public partial class DimCallProgramme

public string CallNameDetailsSv { get; set; }

public string LocalIdentifier { get; set; }

public int? TypeOfFunding { get; set; }

public virtual ICollection<DimCallDecision> DimCallDecisions { get; set; } = new List<DimCallDecision>();

public virtual DimCallProgramme DimCallProgrammeNavigation { get; set; }

public virtual DimDate DimDateIdDueNavigation { get; set; }

public virtual DimDate DimDateIdOpenNavigation { get; set; }
Expand All @@ -75,6 +83,10 @@ public partial class DimCallProgramme

public virtual ICollection<DimWebLink> DimWebLinks { get; set; } = new List<DimWebLink>();

public virtual ICollection<DimCallProgramme> InverseDimCallProgrammeNavigation { get; set; } = new List<DimCallProgramme>();

public virtual DimReferencedatum TypeOfFundingNavigation { get; set; }

public virtual ICollection<DimCallProgramme> DimCallProgrammeId2s { get; set; } = new List<DimCallProgramme>();

public virtual ICollection<DimCallProgramme> DimCallProgrammes { get; set; } = new List<DimCallProgramme>();
Expand Down
6 changes: 6 additions & 0 deletions aspnetcore/src/api/Models/Ttv/DimDate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public partial class DimDate

public virtual ICollection<DimAffiliation> DimAffiliationStartDateNavigations { get; set; } = new List<DimAffiliation>();

public virtual ICollection<DimCallDecision> DimCallDecisions { get; set; } = new List<DimCallDecision>();

public virtual ICollection<DimCallProgramme> DimCallProgrammeDimDateIdDueNavigations { get; set; } = new List<DimCallProgramme>();

public virtual ICollection<DimCallProgramme> DimCallProgrammeDimDateIdOpenNavigations { get; set; } = new List<DimCallProgramme>();
Expand Down Expand Up @@ -57,6 +59,10 @@ public partial class DimDate

public virtual ICollection<DimResearchActivity> DimResearchActivityDimStartDateNavigations { get; set; } = new List<DimResearchActivity>();

public virtual ICollection<DimResearchProject> DimResearchProjectEndDateNavigations { get; set; } = new List<DimResearchProject>();

public virtual ICollection<DimResearchProject> DimResearchProjectStartDateNavigations { get; set; } = new List<DimResearchProject>();

public virtual ICollection<DimResearcherToResearchCommunity> DimResearcherToResearchCommunityEndDateNavigations { get; set; } = new List<DimResearcherToResearchCommunity>();

public virtual ICollection<DimResearcherToResearchCommunity> DimResearcherToResearchCommunityStartDateNavigations { get; set; } = new List<DimResearcherToResearchCommunity>();
Expand Down
18 changes: 11 additions & 7 deletions aspnetcore/src/api/Models/Ttv/DimFundingDecision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public partial class DimFundingDecision

public int DimFundingDecisionIdParentDecision { get; set; }

/// <summary>
/// Päätöksen paikallinen tunniste (tiedon toimittajan)
/// </summary>
public string FunderProjectNumber { get; set; }

public string Acronym { get; set; }
Expand Down Expand Up @@ -65,12 +68,19 @@ public partial class DimFundingDecision

public int DimRegisteredDataSourceId { get; set; }

/// <summary>
/// Rahoituspäätös - Päätöspaneeli
/// </summary>
public int? DimCallDecisionsId { get; set; }

public virtual ICollection<BrFundingConsortiumParticipation> BrFundingConsortiumParticipations { get; set; } = new List<BrFundingConsortiumParticipation>();

public virtual ICollection<BrParticipatesInFundingGroup> BrParticipatesInFundingGroups { get; set; } = new List<BrParticipatesInFundingGroup>();

public virtual ICollection<BrWordClusterDimFundingDecision> BrWordClusterDimFundingDecisions { get; set; } = new List<BrWordClusterDimFundingDecision>();

public virtual DimCallDecision DimCallDecisions { get; set; }

public virtual DimCallProgramme DimCallProgramme { get; set; }

public virtual DimDate DimDateIdApprovalNavigation { get; set; }
Expand All @@ -87,11 +97,9 @@ public partial class DimFundingDecision

public virtual DimOrganization DimOrganizationIdFunderNavigation { get; set; }

public virtual ICollection<DimPid> DimPids { get; set; } = new List<DimPid>();

public virtual DimRegisteredDataSource DimRegisteredDataSource { get; set; }

public virtual DimTypeOfFunding DimTypeOfFunding { get; set; }
public virtual DimReferencedatum DimTypeOfFunding { get; set; }

public virtual ICollection<DimWebLink> DimWebLinks { get; set; } = new List<DimWebLink>();

Expand All @@ -105,11 +113,7 @@ public partial class DimFundingDecision

public virtual ICollection<DimFundingDecision> DimFundingDecisionFroms { get; set; } = new List<DimFundingDecision>();

public virtual ICollection<DimFundingDecision> DimFundingDecisionFromsNavigation { get; set; } = new List<DimFundingDecision>();

public virtual ICollection<DimFundingDecision> DimFundingDecisionTos { get; set; } = new List<DimFundingDecision>();

public virtual ICollection<DimFundingDecision> DimFundingDecisionTosNavigation { get; set; } = new List<DimFundingDecision>();

public virtual ICollection<DimKeyword> DimKeywords { get; set; } = new List<DimKeyword>();
}
2 changes: 2 additions & 0 deletions aspnetcore/src/api/Models/Ttv/DimName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public partial class DimName

public virtual DimRegisteredDataSource DimRegisteredDataSource { get; set; }

public virtual ICollection<DimResearchProject> DimResearchProjects { get; set; } = new List<DimResearchProject>();

public virtual ICollection<FactContribution> FactContributions { get; set; } = new List<FactContribution>();

public virtual ICollection<FactFieldValue> FactFieldValues { get; set; } = new List<FactFieldValue>();
Expand Down
2 changes: 2 additions & 0 deletions aspnetcore/src/api/Models/Ttv/DimOrganization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public partial class DimOrganization

public virtual ICollection<DimResearchActivity> DimResearchActivities { get; set; } = new List<DimResearchActivity>();

public virtual ICollection<DimResearchProject> DimResearchProjects { get; set; } = new List<DimResearchProject>();

public virtual DimSector DimSector { get; set; }

public virtual ICollection<DimWebLink> DimWebLinks { get; set; } = new List<DimWebLink>();
Expand Down
Loading

0 comments on commit f6e1727

Please sign in to comment.