-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial draft * Some more initial changes * Delete user funnctionality * Prepend comments to source files * Fixed tests * Removed hardcoded code for testing --------- Co-authored-by: GitHub Action <[email protected]>
- Loading branch information
1 parent
142fdcb
commit f4719a8
Showing
18 changed files
with
343 additions
and
14 deletions.
There are no files selected for viewing
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
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
40 changes: 40 additions & 0 deletions
40
backend/core/src/Core.Application/Commands/CustomerCommands/DeleteCustomerCommand.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,40 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
using Core.Domain.Repositories; | ||
using MediatR; | ||
|
||
namespace Core.Application.Commands.CustomerCommands | ||
{ | ||
public class DeleteCustomerCommand : IRequest | ||
{ | ||
public string CustomerCode { get; set; } | ||
|
||
public string IP { get; set; } | ||
|
||
public DeleteCustomerCommand(string customerCode, string iP) | ||
{ | ||
CustomerCode = customerCode; | ||
IP = iP; | ||
} | ||
} | ||
|
||
public class DeleteCustomerCommandHandler : IRequestHandler<DeleteCustomerCommand> | ||
{ | ||
private readonly ICustomerRepository _customerRepository; | ||
|
||
public DeleteCustomerCommandHandler( | ||
ICustomerRepository customerRepository) | ||
{ | ||
_customerRepository = customerRepository; | ||
} | ||
|
||
public async Task Handle(DeleteCustomerCommand request, CancellationToken cancellationToken) | ||
{ | ||
var customer = await _customerRepository.GetAsync(request.CustomerCode, cancellationToken); | ||
|
||
await _customerRepository.DeleteAsync(customer, request.IP); | ||
} | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
backend/core/src/Core.Infrastructure/AzureB2CGraphService/B2CGraphService.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,67 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
using Azure.Identity; | ||
using Core.Domain.Exceptions; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Graph; | ||
|
||
namespace Core.Infrastructure.AzureB2CGraphService | ||
{ | ||
public class B2CGraphService : IB2CGraphService | ||
{ | ||
private readonly GraphServiceClient _graphServiceClient; | ||
private readonly ILogger<B2CGraphService> _logger; | ||
|
||
public B2CGraphService( | ||
GraphServiceClient graphServiceClient, | ||
IOptions<B2CServiceOptions> options, | ||
ILogger<B2CGraphService> logger) | ||
{ | ||
_graphServiceClient = graphServiceClient; | ||
_logger = logger; | ||
|
||
var clientId = options.Value.ClientId; | ||
var clientSecret = options.Value.ClientSecret; | ||
var tenant = options.Value.Tenant; | ||
|
||
var clientSecretCredential = new ClientSecretCredential(tenant, clientId, clientSecret); | ||
|
||
var scopes = new[] { "https://graph.microsoft.com/.default" }; | ||
|
||
_graphServiceClient = new GraphServiceClient(clientSecretCredential, scopes); | ||
} | ||
|
||
public async Task DeleteUserAsync(string customerCode) | ||
{ | ||
_logger.LogInformation("Attempting to delete user {CustomerId} from B2C.", customerCode); | ||
|
||
try | ||
{ | ||
var user = await _graphServiceClient.Users[customerCode].GetAsync(); | ||
|
||
if (user != null) | ||
{ | ||
await _graphServiceClient.Users[customerCode].DeleteAsync(); | ||
_logger.LogInformation("User {CustomerId} deleted successfully.", customerCode); | ||
} | ||
else | ||
{ | ||
_logger.LogInformation("User {CustomerId} not found.", customerCode); | ||
} | ||
} | ||
catch (ServiceException ex) when (ex.IsMatch(GraphErrorCode.Request_ResourceNotFound.ToString())) | ||
{ | ||
_logger.LogInformation("User {CustomerId} not found.", customerCode); | ||
throw new CustomErrorsException("B2CGraphService", customerCode, "User not found."); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "An unexpected error occurred while deleting user {CustomerId}.", customerCode); | ||
throw new CustomErrorsException("B2CGraphService", customerCode, "An unexpected error occurred while deleting user."); | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/core/src/Core.Infrastructure/AzureB2CGraphService/B2CServiceOptions.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,20 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Core.Infrastructure.AzureB2CGraphService | ||
{ | ||
public class B2CServiceOptions | ||
{ | ||
[Required] | ||
public required string ClientId { get; set; } | ||
|
||
[Required] | ||
public required string ClientSecret { get; set; } | ||
|
||
[Required] | ||
public required string Tenant { get; set; } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
backend/core/src/Core.Infrastructure/AzureB2CGraphService/IB2CGraphService.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,13 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
using Core.Domain.Entities.CustomerAggregate; | ||
|
||
namespace Core.Infrastructure.AzureB2CGraphService | ||
{ | ||
public interface IB2CGraphService | ||
{ | ||
public Task DeleteUserAsync(string customerCode); | ||
} | ||
} |
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
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.