-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new endpoint for unit contact points (#151)
* new endpoint for unit contact points * added UnitProfileClientTest * test implemented, but not working * fixed unit test for unitclient * fixed code smell * still fixing code smells * fixed final code smell * Update src/Altinn.Profile.Integrations/SblBridge/UnitProfileClient.cs * Fixed pr comments
- Loading branch information
Showing
20 changed files
with
640 additions
and
7 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/Altinn.Profile.Core/Integrations/IUnitProfileClient.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,17 @@ | ||
using Altinn.Profile.Core.Unit.ContactPoints; | ||
|
||
namespace Altinn.Profile.Core.Integrations | ||
{ | ||
/// <summary> | ||
/// Interface describing a client for the user profile service | ||
/// </summary> | ||
public interface IUnitProfileClient | ||
{ | ||
/// <summary> | ||
/// Provides a list of user registered contact points based on the lookup criteria | ||
/// </summary> | ||
/// <param name="lookup">Lookup object containing a list of organizations and a resource</param> | ||
/// <returns>A list of unit contact points</returns> | ||
Task<Result<UnitContactPointsList, bool>> GetUserRegisteredContactPoints(UnitContactPointLookup lookup); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/Altinn.Profile.Core/Unit.ContactPoints/IUnitContactPoints.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,16 @@ | ||
using Altinn.Profile.Core.Unit.ContactPoints; | ||
|
||
namespace Altinn.Profile.Core.User.ContactPoints; | ||
|
||
/// <summary> | ||
/// Class describing the methods required for user contact point service | ||
/// </summary> | ||
public interface IUnitContactPoints | ||
{ | ||
/// <summary> | ||
/// Method for retriveing user registered contact points for a unit | ||
/// </summary> | ||
/// <param name="lookup">A lookup object containing a list of organisation numbers and the resource to lookup contact points for</param> | ||
/// <returns>The users' contact points and reservation status or a boolean if failure.</returns> | ||
Task<Result<UnitContactPointsList, bool>> GetUserRegisteredContactPoints(UnitContactPointLookup lookup); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Altinn.Profile.Core/Unit.ContactPoints/UnitContactPointLookup.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,18 @@ | ||
namespace Altinn.Profile.Core.Unit.ContactPoints | ||
{ | ||
/// <summary> | ||
/// A class describing the query model for contact points for units | ||
/// </summary> | ||
public class UnitContactPointLookup | ||
{ | ||
/// <summary> | ||
/// Gets or sets the list of organisation numbers to lookup contact points for | ||
/// </summary> | ||
public List<string> OrganizationNumbers { get; set; } = []; | ||
|
||
/// <summary> | ||
/// Gets or sets the resource id to filter the contact points by | ||
/// </summary> | ||
public string ResourceId { get; set; } = string.Empty; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Altinn.Profile.Core/Unit.ContactPoints/UnitContactPointService.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,28 @@ | ||
using Altinn.Profile.Core.Integrations; | ||
using Altinn.Profile.Core.User.ContactPoints; | ||
|
||
namespace Altinn.Profile.Core.Unit.ContactPoints | ||
{ | ||
/// <summary> | ||
/// Implementation of the <see cref="IUnitContactPoints"/> interface using a REST client to retrieve profile data "/> | ||
/// </summary> | ||
public class UnitContactPointService : IUnitContactPoints | ||
{ | ||
private readonly IUnitProfileClient _unitClient; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="UnitContactPointService"/> class. | ||
/// </summary> | ||
public UnitContactPointService(IUnitProfileClient unitClient) | ||
{ | ||
_unitClient = unitClient; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<Result<UnitContactPointsList, bool>> GetUserRegisteredContactPoints(UnitContactPointLookup lookup) | ||
{ | ||
Result<UnitContactPointsList, bool> result = await _unitClient.GetUserRegisteredContactPoints(lookup); | ||
return result; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Altinn.Profile.Core/Unit.ContactPoints/UnitContactPoints.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,36 @@ | ||
using Altinn.Profile.Core.User.ContactPoints; | ||
|
||
namespace Altinn.Profile.Core.Unit.ContactPoints | ||
{ | ||
/// <summary> | ||
/// Class describing the user registered contact points for a unit | ||
/// </summary> | ||
public class UnitContactPoints | ||
{ | ||
/// <summary> | ||
/// Gets or sets the organization number of the organization. | ||
/// </summary> | ||
public string OrganizationNumber { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets the party id of the organization. | ||
/// </summary> | ||
public int PartyId { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a list of multiple contanct points associated with the organisation. | ||
/// </summary> | ||
public List<UserContactPoints> UserContactPoints { get; set; } = []; | ||
} | ||
|
||
/// <summary> | ||
/// A list representation of <see cref="UnitContactPoints"/> | ||
/// </summary> | ||
public class UnitContactPointsList | ||
{ | ||
/// <summary> | ||
/// A list containing contact points for users | ||
/// </summary> | ||
public List<UnitContactPoints> ContactPointsList { get; set; } = []; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/Altinn.Profile.Core/User.ContactPoints/UserContactPoints.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
75 changes: 75 additions & 0 deletions
75
src/Altinn.Profile.Integrations/SblBridge/PartyNotificationContactPoints.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,75 @@ | ||
using Altinn.Profile.Core.Unit.ContactPoints; | ||
using Altinn.Profile.Core.User.ContactPoints; | ||
|
||
namespace Altinn.Profile.Integrations.SblBridge | ||
{ | ||
/// <summary> | ||
/// Model describing a container for a list of contact points. | ||
/// </summary> | ||
public class PartyNotificationContactPoints | ||
{ | ||
/// <summary> | ||
/// Gets or sets the party id of the organisation. | ||
/// </summary> | ||
public Guid? PartyId { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the legacy id of the organisation. | ||
/// </summary> | ||
public int LegacyPartyId { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the organization number of the organisation. | ||
/// </summary> | ||
public string OrganizationNumber { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets a list of multiple contanct points associated with the organisation. | ||
/// </summary> | ||
public List<UserRegisteredContactPoint> ContactPoints { get; set; } = new List<UserRegisteredContactPoint>(); | ||
|
||
/// <summary> | ||
/// Maps a list of <see cref="PartyNotificationContactPoints"/> to a list of <see cref="UnitContactPoints"/>. | ||
/// </summary> | ||
public static UnitContactPointsList MapToUnitContactPoints(List<PartyNotificationContactPoints> source) | ||
{ | ||
List<UnitContactPoints> contactPoints = source.Select(partyNotificationEndpoint => new UnitContactPoints | ||
{ | ||
OrganizationNumber = partyNotificationEndpoint.OrganizationNumber, | ||
PartyId = partyNotificationEndpoint.LegacyPartyId, | ||
UserContactPoints = partyNotificationEndpoint.ContactPoints.Select(contactPoint => new UserContactPoints | ||
{ | ||
UserId = contactPoint.LegacyUserId, | ||
Email = contactPoint.Email, | ||
MobileNumber = contactPoint.MobileNumber | ||
}).ToList() | ||
}).ToList(); | ||
|
||
return new UnitContactPointsList() { ContactPointsList = contactPoints }; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Model describing the contact information that a user has associated with a party they can represent. | ||
/// </summary> | ||
public class UserRegisteredContactPoint | ||
{ | ||
/// <summary> | ||
/// Gets or sets the legacy user id for the owner of this party notification endpoint. | ||
/// </summary> | ||
/// <remarks> | ||
/// This was named as legacy for consistency. Property for UUID will probably never be added. | ||
/// </remarks> | ||
public int LegacyUserId { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the email address for this contact point. | ||
/// </summary> | ||
public string Email { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets the mobile number for this contact point. | ||
/// </summary> | ||
public string MobileNumber { get; set; } = string.Empty; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/Altinn.Profile.Integrations/SblBridge/UnitProfileClient.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,70 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
using Altinn.Profile.Core; | ||
using Altinn.Profile.Core.Integrations; | ||
using Altinn.Profile.Core.Unit.ContactPoints; | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Altinn.Profile.Integrations.SblBridge; | ||
|
||
/// <summary> | ||
/// Represents an implementation of <see cref="IUnitContactPointClient"/> using SBLBridge to obtain unit profile information. | ||
/// </summary> | ||
public class UnitProfileClient : IUnitProfileClient | ||
{ | ||
private readonly ILogger<UnitProfileClient> _logger; | ||
private readonly HttpClient _client; | ||
private readonly JsonSerializerOptions _serializerOptions; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="UnitProfileClient"/> class | ||
/// </summary> | ||
/// <param name="httpClient">HttpClient from default http client factory</param> | ||
/// <param name="logger">the logger</param> | ||
/// <param name="settings">the sbl bridge settings</param> | ||
public UnitProfileClient( | ||
HttpClient httpClient, | ||
ILogger<UnitProfileClient> logger, | ||
IOptions<SblBridgeSettings> settings) | ||
{ | ||
_logger = logger; | ||
_client = httpClient; | ||
_client.BaseAddress = new Uri(settings.Value.ApiProfileEndpoint); | ||
|
||
_serializerOptions = new JsonSerializerOptions | ||
{ | ||
WriteIndented = true, | ||
PropertyNameCaseInsensitive = true, | ||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) } | ||
}; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<Result<UnitContactPointsList, bool>> GetUserRegisteredContactPoints(UnitContactPointLookup lookup) | ||
{ | ||
string endpoint = $"units/contactpointslookup"; | ||
|
||
StringContent requestBody = new StringContent(JsonSerializer.Serialize(lookup), Encoding.UTF8, "application/json"); | ||
|
||
HttpResponseMessage response = await _client.PostAsync(endpoint, requestBody); | ||
|
||
if (!response.IsSuccessStatusCode) | ||
{ | ||
_logger.LogError( | ||
"// UnitClient // GetUserRegisteredContactPoints // Unexpected response. Failed with {StatusCode} and message {Message}", | ||
response.StatusCode, | ||
await response.Content.ReadAsStringAsync()); | ||
|
||
return false; | ||
} | ||
|
||
string content = await response.Content.ReadAsStringAsync(); | ||
List<PartyNotificationContactPoints> partyNotificationEndpoints = JsonSerializer.Deserialize<List<PartyNotificationContactPoints>>(content, _serializerOptions)!; | ||
|
||
return PartyNotificationContactPoints.MapToUnitContactPoints(partyNotificationEndpoints); | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
src/Altinn.Profile/Controllers/UnitContactPointController.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,45 @@ | ||
using System.Threading.Tasks; | ||
|
||
using Altinn.Profile.Core; | ||
using Altinn.Profile.Core.Unit.ContactPoints; | ||
using Altinn.Profile.Core.User.ContactPoints; | ||
|
||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Altinn.Profile.Controllers | ||
{ | ||
/// <summary> | ||
/// Controller for unit profile contact point API endpoints for internal consumption (e.g. Notifications) requiring neither authenticated user token nor access token authorization. | ||
/// </summary> | ||
[Route("profile/api/v1/units/contactpoint")] | ||
[ApiExplorerSettings(IgnoreApi = true)] | ||
[Consumes("application/json")] | ||
[Produces("application/json")] | ||
public class UnitContactPointController : ControllerBase | ||
{ | ||
private readonly IUnitContactPoints _contactPointService; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="UnitContactPointController"/> class. | ||
/// </summary> | ||
public UnitContactPointController(IUnitContactPoints contactPointsService) | ||
{ | ||
_contactPointService = contactPointsService; | ||
} | ||
|
||
/// <summary> | ||
/// Endpoint looking up the contact points for the units provided in the lookup object in the request body | ||
/// </summary> | ||
/// <returns>Returns an overview of the user registered contact points for the provided units and given resource</returns> | ||
[HttpPost("lookup")] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
public async Task<ActionResult<UnitContactPointsList>> PostLookup([FromBody] UnitContactPointLookup unitContactPointLookup) | ||
{ | ||
Result<UnitContactPointsList, bool> result = await _contactPointService.GetUserRegisteredContactPoints(unitContactPointLookup); | ||
return result.Match<ActionResult<UnitContactPointsList>>( | ||
success => Ok(success), | ||
_ => Problem("Could not retrieve contact points")); | ||
} | ||
} | ||
} |
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
4 changes: 3 additions & 1 deletion
4
...r.ContactPoints/UserContactPointLookup.cs → ....Profile/Models/UserContactPointLookup.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
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
Oops, something went wrong.