diff --git a/Modules/Devices/src/Devices.Application/DTOs/IdentityDeletionProcessDTO.cs b/Modules/Devices/src/Devices.Application/DTOs/IdentityDeletionProcessDTO.cs new file mode 100644 index 0000000000..42d61dc62d --- /dev/null +++ b/Modules/Devices/src/Devices.Application/DTOs/IdentityDeletionProcessDTO.cs @@ -0,0 +1,62 @@ +using Backbone.DevelopmentKit.Identity.ValueObjects; +using Backbone.Modules.Devices.Domain.Entities.Identities; + +namespace Backbone.Modules.Devices.Application.DTOs; + +public class IdentityDeletionProcessDTO +{ + public IdentityDeletionProcessDTO(IdentityDeletionProcess process) + { + Id = process.Id; + AuditLog = process.AuditLog + .Select(e => new IdentityDeletionProcessAuditLogEntryDTO(e)) + .ToList(); + Status = process.Status; + CreatedAt = process.CreatedAt; + ApprovalReminder1SentAt = process.ApprovalReminder1SentAt; + ApprovalReminder2SentAt = process.ApprovalReminder2SentAt; + ApprovalReminder3SentAt = process.ApprovalReminder3SentAt; + ApprovedAt = process.ApprovedAt; + ApprovedByDevice = process.ApprovedByDevice; + GracePeriodEndsAt = process.GracePeriodEndsAt; + GracePeriodReminder1SentAt = process.GracePeriodReminder1SentAt; + GracePeriodReminder2SentAt = process.GracePeriodReminder2SentAt; + GracePeriodReminder3SentAt = process.GracePeriodReminder3SentAt; + } + + public string Id { get; set; } + public List AuditLog { get; set; } + public DeletionProcessStatus Status { get; set; } + public DateTime CreatedAt { get; set; } + + public DateTime? ApprovalReminder1SentAt { get; set; } + public DateTime? ApprovalReminder2SentAt { get; set; } + public DateTime? ApprovalReminder3SentAt { get; set; } + + public DateTime? ApprovedAt { get; set; } + public DeviceId? ApprovedByDevice { get; set; } + + public DateTime? GracePeriodEndsAt { get; set; } + + public DateTime? GracePeriodReminder1SentAt { get; set; } + public DateTime? GracePeriodReminder2SentAt { get; set; } + public DateTime? GracePeriodReminder3SentAt { get; set; } +} + +public class IdentityDeletionProcessAuditLogEntryDTO +{ + public IdentityDeletionProcessAuditLogEntryDTO(IdentityDeletionProcessAuditLogEntry entry) + { + Id = entry.Id; + CreatedAt = entry.CreatedAt; + Message = entry.Message; + OldStatus = entry.OldStatus; + NewStatus = entry.NewStatus; + } + + public string Id { get; set; } + public DateTime CreatedAt { get; set; } + public string Message { get; set; } + public DeletionProcessStatus? OldStatus { get; set; } + public DeletionProcessStatus NewStatus { get; set; } +} diff --git a/Modules/Devices/src/Devices.Application/Identities/Queries/GetDeletionProcess/GetDeletionProcessQuery.cs b/Modules/Devices/src/Devices.Application/Identities/Queries/GetDeletionProcess/GetDeletionProcessQuery.cs new file mode 100644 index 0000000000..3f54f3e752 --- /dev/null +++ b/Modules/Devices/src/Devices.Application/Identities/Queries/GetDeletionProcess/GetDeletionProcessQuery.cs @@ -0,0 +1,9 @@ +using Backbone.Modules.Devices.Application.DTOs; +using MediatR; + +namespace Backbone.Modules.Devices.Application.Identities.Queries.GetDeletionProcess; + +public class GetDeletionProcessQuery : IRequest +{ + public required string Id { get; set; } +} diff --git a/Modules/Devices/src/Devices.Application/Identities/Queries/GetDeletionProcess/Handler.cs b/Modules/Devices/src/Devices.Application/Identities/Queries/GetDeletionProcess/Handler.cs new file mode 100644 index 0000000000..91e547dce5 --- /dev/null +++ b/Modules/Devices/src/Devices.Application/Identities/Queries/GetDeletionProcess/Handler.cs @@ -0,0 +1,29 @@ +using Backbone.BuildingBlocks.Application.Abstractions.Exceptions; +using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.UserContext; +using Backbone.Modules.Devices.Application.DTOs; +using Backbone.Modules.Devices.Application.Infrastructure.Persistence.Repository; +using Backbone.Modules.Devices.Domain.Entities.Identities; +using MediatR; + +namespace Backbone.Modules.Devices.Application.Identities.Queries.GetDeletionProcess; + +public class Handler : IRequestHandler +{ + private readonly IIdentitiesRepository _identitiesRepository; + private readonly IUserContext _userContext; + + public Handler(IIdentitiesRepository identitiesRepository, IUserContext userContext) + { + _identitiesRepository = identitiesRepository; + _userContext = userContext; + } + + public async Task Handle(GetDeletionProcessQuery request, CancellationToken cancellationToken) + { + var identity = await _identitiesRepository.FindByAddress(_userContext.GetAddress(), cancellationToken) ?? throw new NotFoundException(nameof(Identity)); + var deletionProcess = identity.DeletionProcesses.FirstOrDefault(p => p.Id == request.Id) ?? throw new NotFoundException(nameof(IdentityDeletionProcess)); + var response = new IdentityDeletionProcessDTO(deletionProcess); + + return response; + } +} diff --git a/Modules/Devices/src/Devices.ConsumerApi/Controllers/IdentitiesController.cs b/Modules/Devices/src/Devices.ConsumerApi/Controllers/IdentitiesController.cs index ac33756002..fc0647a196 100644 --- a/Modules/Devices/src/Devices.ConsumerApi/Controllers/IdentitiesController.cs +++ b/Modules/Devices/src/Devices.ConsumerApi/Controllers/IdentitiesController.cs @@ -6,6 +6,7 @@ using Backbone.Modules.Devices.Application.Identities.Commands.ApproveDeletionProcess; using Backbone.Modules.Devices.Application.Identities.Commands.CreateIdentity; using Backbone.Modules.Devices.Application.Identities.Commands.StartDeletionProcessAsOwner; +using Backbone.Modules.Devices.Application.Identities.Queries.GetDeletionProcess; using Backbone.Modules.Devices.Infrastructure.OpenIddict; using MediatR; using Microsoft.AspNetCore.Authorization; @@ -76,6 +77,15 @@ public async Task ApproveDeletionProcess([FromRoute] string id, C var response = await _mediator.Send(new ApproveDeletionProcessCommand(id), cancellationToken); return Ok(response); } + + [HttpGet("Self/DeletionProcesses/{id}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesError(StatusCodes.Status404NotFound)] + public async Task GetDeletionProcess([FromRoute] string id, CancellationToken cancellationToken) + { + var response = await _mediator.Send(new GetDeletionProcessQuery { Id = id }, cancellationToken); + return Ok(response); + } } public class CreateIdentityRequest