Skip to content

Commit

Permalink
Merge branch 'main' into chore/remove-rabbitmq
Browse files Browse the repository at this point in the history
  • Loading branch information
arealmaas committed Apr 8, 2024
2 parents 2366132 + 85fdf92 commit c42f412
Show file tree
Hide file tree
Showing 72 changed files with 306 additions and 207 deletions.
70 changes: 66 additions & 4 deletions docs/swagger/V1/swagger.verified.json
Original file line number Diff line number Diff line change
Expand Up @@ -2602,6 +2602,12 @@
"items": {
"$ref": "#/components/schemas/SearchDialogContentDtoSO"
}
},
"seenSinceLastUpdate": {
"type": "array",
"items": {
"$ref": "#/components/schemas/SearchDialogDialogSeenRecordDtoSO"
}
}
}
},
Expand All @@ -2620,6 +2626,31 @@
}
}
},
"SearchDialogDialogSeenRecordDtoSO": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "string",
"format": "guid"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"endUserIdHash": {
"type": "string"
},
"endUserName": {
"type": "string",
"nullable": true
},
"isCurrentEndUser": {
"type": "boolean",
"nullable": true
}
}
},
"GetDialogDtoSO": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -2721,6 +2752,12 @@
"items": {
"$ref": "#/components/schemas/GetDialogDialogActivityDtoSO"
}
},
"seenSinceLastUpdate": {
"type": "array",
"items": {
"$ref": "#/components/schemas/GetDialogDialogSeenRecordDtoSO"
}
}
}
},
Expand Down Expand Up @@ -2961,6 +2998,31 @@
}
}
},
"GetDialogDialogSeenRecordDtoSO": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "string",
"format": "guid"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"endUserIdHash": {
"type": "string"
},
"endUserName": {
"type": "string",
"nullable": true
},
"isCurrentEndUser": {
"type": "boolean",
"nullable": true
}
}
},
"CreateDialogCommand": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -3471,7 +3533,7 @@
"$ref": "#/components/schemas/SearchDialogContentDto"
}
},
"seenLog": {
"seenSinceLastUpdate": {
"type": "array",
"items": {
"$ref": "#/components/schemas/SearchDialogDialogSeenRecordDto"
Expand Down Expand Up @@ -3559,7 +3621,7 @@
"type": "string",
"nullable": true
},
"isAuthenticatedUser": {
"isCurrentEndUser": {
"type": "boolean"
}
}
Expand Down Expand Up @@ -3658,7 +3720,7 @@
"$ref": "#/components/schemas/GetDialogDialogActivityDto"
}
},
"seenLog": {
"seenSinceLastUpdate": {
"type": "array",
"items": {
"$ref": "#/components/schemas/GetDialogDialogSeenRecordDto"
Expand Down Expand Up @@ -3924,7 +3986,7 @@
"type": "string",
"nullable": true
},
"isAuthenticatedUser": {
"isCurrentEndUser": {
"type": "boolean"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static IServiceCollection AddApplication(this IServiceCollection services
.AddScoped<IDialogTokenGenerator, DialogTokenGenerator>()

// Transient
.AddTransient<IStringHasher, RandomSaltStringHasher>()
.AddTransient<IUserOrganizationRegistry, UserOrganizationRegistry>()
.AddTransient<IUserResourceRegistry, UserResourceRegistry>()
.AddTransient<IUserNameRegistry, UserNameRegistry>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static bool TryGetOrgNumber(this Claim? consumerClaim, [NotNullWhen(true)

public static bool TryGetAuthenticationLevel(this ClaimsPrincipal claimsPrincipal, [NotNullWhen(true)] out int? authenticationLevel)
{
string[] claimTypes = { "acr", "urn:altinn:authlevel" };
string[] claimTypes = ["acr", "urn:altinn:authlevel"];

foreach (var claimType in claimTypes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation;

internal static class FluentValidation_DateTimeOffset_Extensions
internal static class FluentValidationDateTimeOffsetExtensions
{
public const string InPastMessage = "'{PropertyName}' must be in the past.";
public const string InFutureMessage = "'{PropertyName}' must be in the future.";
Expand Down Expand Up @@ -37,4 +37,4 @@ public static IRuleBuilderOptions<T, DateTimeOffset> IsInPast<T>(this IRuleBuild
.LessThanOrEqualTo(DateTimeOffset.UtcNow)
.WithMessage(InPastMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation;

internal static class FluentValidation_Enumerable_Extensions
internal static class FluentValidationEnumerableExtensions
{
public static IRuleBuilderOptions<T, IEnumerable<TProperty>> UniqueBy<T, TProperty, TKey>(
this IRuleBuilder<T, IEnumerable<TProperty>> ruleBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation;

internal static class FluentValidation_String_Extensions
internal static class FluentValidationStringExtensions
{
public static IRuleBuilderOptions<T, string?> IsValidUri<T>(this IRuleBuilder<T, string?> ruleBuilder)
{
return ruleBuilder
.Must(uri => uri is null || Uri.IsWellFormedUriString(uri, UriKind.RelativeOrAbsolute))
.WithMessage("'{PropertyName}' is not a well formated URI.");
.WithMessage("'{PropertyName}' is not a well formatted URI.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace Digdir.Domain.Dialogporten.Application.Common.Extensions;

internal static class OneOfExtensions
{
private static readonly ConcurrentDictionary<(Type, Type), MethodInfo?> _oneOfFactoryByType = new();
private static readonly ConcurrentDictionary<(Type, Type), MethodInfo?> OneOfFactoryByType = new();

public static bool TryConvertToOneOf<TOneOf>(object value, [NotNullWhen(true)] out TOneOf? result)
{
ArgumentNullException.ThrowIfNull(value);
var oneOfFactory = _oneOfFactoryByType.GetOrAdd(
var oneOfFactory = OneOfFactoryByType.GetOrAdd(
key: new(typeof(TOneOf), value.GetType()),
valueFactory: GetOneOfFactoryOrNull);
result = (TOneOf)oneOfFactory?.Invoke(null, [value])! ?? default;
Expand All @@ -20,12 +20,12 @@ public static bool TryConvertToOneOf<TOneOf>(object value, [NotNullWhen(true)] o

private static MethodInfo? GetOneOfFactoryOrNull((Type, Type) arg)
{
const string ImplicitOperatorName = "op_Implicit";
const BindingFlags ImplicitOperatorFlags = BindingFlags.Static | BindingFlags.Public;
const string implicitOperatorName = "op_Implicit";
const BindingFlags implicitOperatorFlags = BindingFlags.Static | BindingFlags.Public;
var (oneOf, type) = arg;
var oneOfImplicitOperator = oneOf.GetMethod(
name: ImplicitOperatorName,
bindingAttr: ImplicitOperatorFlags,
name: implicitOperatorName,
bindingAttr: implicitOperatorFlags,
types: [type]);
return oneOfImplicitOperator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ namespace Digdir.Domain.Dialogporten.Application.Common.Extensions;
public static class QueryableExtensions
{
public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool predicate, Expression<Func<TSource, bool>> queryPredicate)
{
return predicate ? source.Where(queryPredicate) : source;
}
=> predicate ? source.Where(queryPredicate) : source;

private static readonly Type DialogType = typeof(DialogEntity);
private static readonly PropertyInfo DialogIdPropertyInfo = DialogType.GetProperty(nameof(DialogEntity.Id))!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
internal static class TypeExtensions
{
public static bool IsNullableType(this Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
=> type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Buffers.Text;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Options;
using NSec.Cryptography;

Expand Down
28 changes: 19 additions & 9 deletions src/Digdir.Domain.Dialogporten.Application/Common/MappingUtils.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.Text;

namespace Digdir.Domain.Dialogporten.Application.Common;

internal static class MappingUtils
internal interface IStringHasher
{
internal static byte[] GetHashSalt(int size = 16) => RandomNumberGenerator.GetBytes(size);
[return: NotNullIfNotNull(nameof(personIdentifier))]
string? Hash(string? personIdentifier);
}

internal class RandomSaltStringHasher : IStringHasher
{
private const int SaltSize = 16;
private readonly Lazy<byte[]> _lazySalt = new(() => RandomNumberGenerator.GetBytes(SaltSize));

internal static string HashPid(string personIdentifier, byte[] salt)
public string? Hash(string? personIdentifier)
{
if (string.IsNullOrWhiteSpace(personIdentifier))
{
return null;
}

var identifierBytes = Encoding.UTF8.GetBytes(personIdentifier);
Span<byte> buffer = stackalloc byte[identifierBytes.Length + salt.Length];
Span<byte> buffer = stackalloc byte[identifierBytes.Length + _lazySalt.Value.Length];
identifierBytes.CopyTo(buffer);
salt.CopyTo(buffer[identifierBytes.Length..]);
_lazySalt.Value.CopyTo(buffer[identifierBytes.Length..]);

var hashBytes = SHA256.HashData(buffer);

return BitConverter
.ToString(hashBytes, 0, 5)
.Replace("-", "")
.ToLowerInvariant();
return BitConverter.ToString(hashBytes, 0, 5).Replace("-", "").ToLowerInvariant();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ namespace Digdir.Domain.Dialogporten.Application.Common.Pagination.Extensions;
internal static class OrderExtensions
{
public static OrderSet<TOrderDefinition, TTarget> DefaultIfNull<TOrderDefinition, TTarget>(this OrderSet<TOrderDefinition, TTarget>? orderSet)
where TOrderDefinition : IOrderDefinition<TTarget>
{
return orderSet ?? OrderSet<TOrderDefinition, TTarget>.Default;
}
where TOrderDefinition : IOrderDefinition<TTarget> => orderSet ?? OrderSet<TOrderDefinition, TTarget>.Default;

public static IQueryable<T> ApplyOrder<T>(this IQueryable<T> query, IOrderSet<T> orderSet)
{
Expand Down Expand Up @@ -81,9 +78,9 @@ public static IQueryable<T> ApplyCondition<T>(this IQueryable<T> query, IOrderSe
// where a, b, c, ... are the order by expressions and an, bn, cn, ...
// are the values from the continuation token. It's a repeating pattern
// where the previous properties are equal and the next property is less than
// or greater than the next value, depending on the sort order.
// or greater than the next value, depending on the sort order.
// See https://phauer.com/2018/web-api-pagination-timestamp-id-continuation-token/ for more info.
// The algorithm is as follows:
// The algorithm is as follows:
// equalParst = [ a = an, b = bn, c = cn, ... ]
// ltGtParts = [ a < an, b < bn, c < cn, ... ]
// lgGtEqualParst = [ a < an, a = an AND b < bn, a = an AND b = bn AND c < cn, ... ]
Expand Down Expand Up @@ -139,8 +136,8 @@ when x.orderBody.Type.IsNullableType()
}

/// <summary>
/// The not null "block" is added when the continuation token is null in descending
/// order, meaning we are still in the beginning where the null values are and
/// The not null "block" is added when the continuation token is null in descending
/// order, meaning we are still in the beginning where the null values are and
/// have not reached the set where the not null values start.
/// </summary>
private static BinaryExpression IncludeNotNullsBlock(Expression orderBody)
Expand All @@ -150,8 +147,8 @@ private static BinaryExpression IncludeNotNullsBlock(Expression orderBody)
}

/// <summary>
/// The null "block" is added when a nullable continuation token is not null in
/// ascending order, meaning we have not reached the end of the pagination
/// The null "block" is added when a nullable continuation token is not null in
/// ascending order, meaning we have not reached the end of the pagination
/// where the null values are.
/// </summary>
private static BinaryExpression IncludeNullsBlock(Expression orderBody, Expression valueExpression)
Expand All @@ -171,9 +168,6 @@ public ParameterReplacer(ParameterExpression newParameter)
_newParameter = newParameter;
}

protected override Expression VisitParameter(ParameterExpression node)
{
return _newParameter;
}
protected override Expression VisitParameter(ParameterExpression node) => _newParameter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Digdir.Domain.Dialogporten.Application.Common.Pagination.Extensions;

public static class TryParseExtensions
{
private static readonly ConcurrentDictionary<Type, MethodInfo?> _tryParseByType = new();
private static readonly ConcurrentDictionary<Type, MethodInfo?> TryParseByType = new();

public static bool TryParse(Type type, string? value, out object? result)
{
Expand All @@ -27,7 +27,7 @@ public static bool TryParse(Type type, string? value, out object? result)
return true;
}

var method = _tryParseByType.GetOrAdd(type, t => TryFindTryParseMethod(t, out var m) ? m : null);
var method = TryParseByType.GetOrAdd(type, t => TryFindTryParseMethod(t, out var m) ? m : null);
if (method is null)
{
return false;
Expand All @@ -53,11 +53,11 @@ private static bool TryFindTryParseMethod(Type type, [NotNullWhen(true)] out Met
var method = (MethodInfo)m;
if (method.Name != tryParseMethodName) return false;
if (method.ReturnParameter.ParameterType != typeof(bool)) return false;
var parms = method.GetParameters();
if (parms.Length != 2) return false;
if (parms[0].ParameterType != typeof(string)) return false;
if (parms[1].ParameterType != type.MakeByRefType()) return false;
if (!parms[1].IsOut) return false;
var parameters = method.GetParameters();
if (parameters.Length != 2) return false;
if (parameters[0].ParameterType != typeof(string)) return false;
if (parameters[1].ParameterType != type.MakeByRefType()) return false;
if (!parameters[1].IsOut) return false;

return true;

Expand All @@ -70,4 +70,4 @@ private static bool TryFindTryParseMethod(Type type, [NotNullWhen(true)] out Met

return method is not null;
}
}
}
Loading

0 comments on commit c42f412

Please sign in to comment.