update to create key by EncodeEnvelopeReceiverId

This commit is contained in:
tekh 2025-09-01 15:42:33 +02:00
parent 87c5e7e4de
commit 20d312a84e
3 changed files with 46 additions and 30 deletions

View File

@ -1,30 +1,38 @@
using Microsoft.Extensions.Logging;
using System.Text;
using System.Text;
namespace EnvelopeGenerator.Application.Extensions
namespace EnvelopeGenerator.Application.Extensions;
/// <summary>
/// Provides extension methods for decoding and extracting information from an envelope receiver ID.
/// </summary>
public static class EncodingExtensions
{
/// <summary>
/// Provides extension methods for decoding and extracting information from an envelope receiver ID.
///
/// </summary>
public static class EncodingExtensions
/// <param name="readOnlyId"></param>
/// <returns></returns>
public static string EncodeEnvelopeReceiverId(this long readOnlyId)
{
public static string EncodeEnvelopeReceiverId(this long readOnlyId)
{
//The random number is used as a salt to increase security but it is not saved in the database.
string combinedString = $"{Random.Shared.Next()}::{readOnlyId}::{Random.Shared.Next()}";
byte[] bytes = Encoding.UTF8.GetBytes(combinedString);
string base64String = Convert.ToBase64String(bytes);
//The random number is used as a salt to increase security but it is not saved in the database.
string combinedString = $"{Random.Shared.Next()}::{readOnlyId}::{Random.Shared.Next()}";
byte[] bytes = Encoding.UTF8.GetBytes(combinedString);
string base64String = Convert.ToBase64String(bytes);
return base64String;
}
return base64String;
}
public static string EncodeEnvelopeReceiverId(this (string envelopeUuid, string receiverSignature) input)
{
string combinedString = $"{input.envelopeUuid}::{input.receiverSignature}";
byte[] bytes = Encoding.UTF8.GetBytes(combinedString);
string base64String = Convert.ToBase64String(bytes);
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string EncodeEnvelopeReceiverId(this (string envelopeUuid, string receiverSignature) input)
{
string combinedString = $"{input.envelopeUuid}::{input.receiverSignature}";
byte[] bytes = Encoding.UTF8.GetBytes(combinedString);
string base64String = Convert.ToBase64String(bytes);
return base64String;
}
return base64String;
}
}

View File

@ -1,4 +1,6 @@
using DigitalData.Core.Abstraction.Application.Repository;
using AutoMapper;
using DigitalData.Core.Abstraction.Application.Repository;
using EnvelopeGenerator.Application.Dto.Receiver;
using EnvelopeGenerator.Domain.Entities;
using MediatR;
using Microsoft.AspNetCore.Mvc;
@ -13,7 +15,7 @@ namespace EnvelopeGenerator.Application.Receivers.Commands;
///
/// </summary>
[ApiExplorerSettings(IgnoreApi = true)]
public record CreateReceiverCommand : IRequest<(int Id, bool AlreadyExists)>
public record CreateReceiverCommand : IRequest<(ReceiverReadDto Receiver, bool AlreadyExists)>
{
/// <summary>
///
@ -52,20 +54,23 @@ public record CreateReceiverCommand : IRequest<(int Id, bool AlreadyExists)>
/// <summary>
///
/// </summary>
public class CreateReceiverCommandHandler : IRequestHandler<CreateReceiverCommand, (int Id, bool AlreadyExists)>
public class CreateReceiverCommandHandler : IRequestHandler<CreateReceiverCommand, (ReceiverReadDto Receiver, bool AlreadyExists)>
{
/// <summary>
///
/// </summary>
private readonly IRepository<Receiver> _repo;
private readonly IMapper _mapper;
/// <summary>
///
/// </summary>
/// <param name="repo"></param>
public CreateReceiverCommandHandler(IRepository<Receiver> repo)
public CreateReceiverCommandHandler(IRepository<Receiver> repo, IMapper mapper)
{
_repo = repo;
_mapper = mapper;
}
/// <summary>
@ -74,7 +79,7 @@ public class CreateReceiverCommandHandler : IRequestHandler<CreateReceiverComman
/// <param name="request"></param>
/// <param name="cancel"></param>
/// <returns></returns>
public async Task<(int Id, bool AlreadyExists)> Handle(CreateReceiverCommand request, CancellationToken cancel)
public async Task<(ReceiverReadDto Receiver, bool AlreadyExists)> Handle(CreateReceiverCommand request, CancellationToken cancel)
{
var receiver = await _repo.ReadOnly()
.Where(r => r.EmailAddress == request.EmailAddress)
@ -85,6 +90,7 @@ public class CreateReceiverCommandHandler : IRequestHandler<CreateReceiverComman
if (!alreadyExists)
receiver = await _repo.CreateAsync(request, cancel);
return (receiver!.Id, alreadyExists);
var receiverDto = _mapper.Map<ReceiverReadDto>(receiver);
return (receiverDto, alreadyExists);
}
}

View File

@ -1,4 +1,5 @@
using EnvelopeGenerator.Application.Histories.Commands;
using EnvelopeGenerator.Application.Extensions;
using EnvelopeGenerator.Application.Histories.Commands;
using EnvelopeGenerator.Application.Histories.Queries;
using EnvelopeGenerator.Domain.Constants;
@ -25,13 +26,14 @@ public class HistoryTests : TestBase
/// Arrange
// Create envelope
var createEnvelopeCmd = FakeCreateEnvelopeCommand;
var envelope = await Mediator.Send(createEnvelopeCmd);
var envelope = await Mediator.Send(createEnvelopeCmd).ThrowIfNull(Exceptions.NotFound);
// Create receiver
var createReceiverCmd = this.CreateReceiverCommand();
(int receiverId, _) = await Mediator.Send(createReceiverCmd);
(var receiver, _) = await Mediator.Send(createReceiverCmd);
var key = (envelope.Uuid, receiver.Signature).EncodeEnvelopeReceiverId();
var key = string.Empty;
var createCmd = Fake.Provider.CreateHistoryCommand(key);
// Act