Skip to content

Commit

Permalink
Errors output is extended for the registration request (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
getmansky authored Aug 25, 2022
1 parent 3026f41 commit aa5718a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AutoMapper;
using FluentValidation.Results;
using MediatR;
using Microsoft.AspNetCore.Identity;
using VirtoCommerce.CustomerModule.Core;
using VirtoCommerce.CustomerModule.Core.Model;
using VirtoCommerce.CustomerModule.Core.Services;
Expand All @@ -23,6 +24,7 @@
using VirtoCommerce.ProfileExperienceApiModule.Data.Validators;
using VirtoCommerce.NotificationsModule.Core.Types;
using VirtoCommerce.NotificationsModule.Core.Model;
using VirtoCommerce.Platform.Security;
using VirtoCommerce.ProfileExperienceApiModule.Data.Models.RegisterOrganization;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Commands
Expand Down Expand Up @@ -107,7 +109,7 @@ private async Task<RegisterOrganizationResult> ProcessRequestAsync(RegisterReque
{
var errors = validationResults
.SelectMany(x => x.Errors)
.Select(x => $"{x.ErrorCode}: {x.ErrorMessage}".TrimEnd(' ', ':'))
.Select(x => new RegistrationError{Code = x.ErrorCode, Description = x.ErrorMessage})
.ToList();

SetErrorResult(result, errors, tokenSource);
Expand All @@ -120,7 +122,7 @@ private async Task<RegisterOrganizationResult> ProcessRequestAsync(RegisterReque

if (store == null)
{
SetErrorResult(result, $"Store {request.StoreId} has not been found", tokenSource);
SetErrorResult(result, "Store not found",$"Store {request.StoreId} has not been found", tokenSource);
return result;
}

Expand All @@ -129,7 +131,7 @@ private async Task<RegisterOrganizationResult> ProcessRequestAsync(RegisterReque
var maintainerRole = await _accountService.FindRoleById(MaintainerRoleId);
if (maintainerRole == null)
{
SetErrorResult(result, $"Organization maintainer role with id {MaintainerRoleId} not found", tokenSource);
SetErrorResult(result, "Role not found",$"Organization maintainer role with id {MaintainerRoleId} not found", tokenSource);
return result;
}

Expand Down Expand Up @@ -167,14 +169,7 @@ private async Task<RegisterOrganizationResult> ProcessRequestAsync(RegisterReque
account.CreatedBy = Creator;

var identityResult = await _accountService.CreateAccountAsync(account);
result.AccountCreationResult = new AccountCreationResult
{
Succeeded = identityResult.Succeeded,
Errors = identityResult.Errors
.Select(x => $"{x.Code}: {x.Description}".TrimEnd(' ', ':'))
.ToList(),
AccountName = account.UserName
};
result.AccountCreationResult = GetAccountCreationResult(identityResult, account);

if (!result.AccountCreationResult.Succeeded)
{
Expand All @@ -186,6 +181,21 @@ private async Task<RegisterOrganizationResult> ProcessRequestAsync(RegisterReque
return result;
}

private static AccountCreationResult GetAccountCreationResult(IdentityResult identityResult, ApplicationUser account)
{
return new AccountCreationResult
{
Succeeded = identityResult.Succeeded,
AccountName = account.UserName,
Errors = identityResult.Errors.Select(x => new RegistrationError
{
Code = x.Code,
Description = x.Description,
Parameter = x is CustomIdentityError error ? error.ErrorParameter.ToString() : null
}).ToList()
};
}

private static void FillContactFields(Contact contact)
{
contact.FullName = contact.FirstName + " " + contact.LastName;
Expand Down Expand Up @@ -214,12 +224,12 @@ private Task SetDynamicPropertiesAsync(IList<DynamicPropertyValue> dynamicProper
return Task.CompletedTask;
}

private static void SetErrorResult(RegisterOrganizationResult result, string errorMessage, CancellationTokenSource source)
private static void SetErrorResult(RegisterOrganizationResult result, string errorCode, string errorMessage, CancellationTokenSource source)
{
SetErrorResult(result, new List<string> { errorMessage }, source);
SetErrorResult(result, new List<RegistrationError>{new() {Code = errorCode, Description = errorMessage}}, source);
}

private static void SetErrorResult(RegisterOrganizationResult result, List<string> errors, CancellationTokenSource source)
private static void SetErrorResult(RegisterOrganizationResult result, List<RegistrationError> errors, CancellationTokenSource source)
{
result.AccountCreationResult = new AccountCreationResult
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace VirtoCommerce.ProfileExperienceApiModule.Data.Models.RegisterOrganizat
public class AccountCreationResult
{
public string AccountName { get; set; }
public List<string> Errors { get; set; }
public List<RegistrationError> Errors { get; set; }
public bool Succeeded { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace VirtoCommerce.ProfileExperienceApiModule.Data.Models.RegisterOrganization
{
public class RegistrationError
{
public string Code { get; set; }
public string Description { get; set; }
public string Parameter { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AccountCreationResultType: ObjectGraphType<AccountCreationResult>
public AccountCreationResultType()
{
Field(x => x.Succeeded);
Field<ListGraphType<StringGraphType>>("errors", "The errors that occurred during the operation.", resolve: context => context.Source.Errors);
Field<ListGraphType<RegistrationErrorType>>("errors", "The errors that occurred during the operation.", resolve: context => context.Source.Errors);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using GraphQL.Types;
using VirtoCommerce.ProfileExperienceApiModule.Data.Models.RegisterOrganization;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Schemas.RegisterCompany
{
public class RegistrationErrorType : ObjectGraphType<RegistrationError>
{
public RegistrationErrorType()
{
Field(x => x.Code, true);
Field(x => x.Description, true);
Field(x => x.Parameter, true);
}
}
}

0 comments on commit aa5718a

Please sign in to comment.