Skip to content

Commit

Permalink
Merge pull request #840 from cabinetoffice/revert-831-feature/DP-730-…
Browse files Browse the repository at this point in the history
…UnexpectedError-ExistingIdentifierNumber

Revert "Feature/dp 730 unexpected error existing identifier number"
  • Loading branch information
jakzal authored Oct 25, 2024
2 parents 7c713e3 + 9713d76 commit 06bf383
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ public static void MapApiExceptions(
return;
}

// Capture the exception message
var exceptionMessage = aex.Result.Detail;

var errorMessage = GetErrorMessageByCode(code, exceptionMessage);
var errorMessage = GetErrorMessageByCode(code);
modelState.AddModelError(string.Empty, errorMessage);

}

private static string? ExtractErrorCode(ApiException<ProblemDetails> aex)
Expand All @@ -31,7 +29,7 @@ public static void MapApiExceptions(
: null;
}

private static string GetErrorMessageByCode(string errorCode, string? exceptionMessage = null)
private static string GetErrorMessageByCode(string errorCode)
{
return errorCode switch
{
Expand All @@ -44,7 +42,6 @@ private static string GetErrorMessageByCode(string errorCode, string? exceptionM
ErrorCodes.UNKNOWN_ORGANISATION => ErrorMessagesList.UnknownOrganisation,
ErrorCodes.BUYER_INFO_NOT_EXISTS => ErrorMessagesList.BuyerInfoNotExists,
ErrorCodes.UNKNOWN_BUYER_INFORMATION_UPDATE_TYPE => ErrorMessagesList.UnknownBuyerInformationUpdateType,
ErrorCodes.ORGANISATION_UPDATE_INVALID_INPUT => exceptionMessage ?? ErrorMessagesList.UnexpectedError,
_ => ErrorMessagesList.UnexpectedError
};
}
Expand Down
2 changes: 0 additions & 2 deletions Frontend/CO.CDP.OrganisationApp/Constants/ErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ public static class ErrorCodes
public const string PERSON_INVITE_ALREADY_CLAIMED = "PERSON_INVITE_ALREADY_CLAIMED";
public const string APIKEY_NAME_ALREADY_EXISTS = "APIKEY_NAME_ALREADY_EXISTS";
public const string EMAIL_ALREADY_EXISTS_WITHIN_ORGANISATION = "EMAIL_ALREADY_EXISTS_WITHIN_ORGANISATION";

public const string ORGANISATION_UPDATE_INVALID_INPUT = "ORGANISATION_UPDATE_INVALID_INPUT";
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,10 @@ public async Task Execute_ShouldNotInsertIdentifier_WhenIdentifierIdIsEmptyOrNul
var organisation = OrganisationWithPponIdentifier;
_organisationRepositoryMock.Setup(repo => repo.Find(_organisationId)).ReturnsAsync(organisation);

var result = await UseCase.Execute((_organisationId, command));

Func<Task> act = async () => await UseCase.Execute((_organisationId, command));

await act.Should()
.ThrowAsync<InvalidUpdateOrganisationCommand>();

result.Should().BeTrue();
_organisationRepositoryMock.Verify(repo => repo.SaveAsync(organisation, AnyOnSave()), Times.Once);

organisation.Identifiers.FirstOrDefault(i => i.Scheme == "VAT").Should().BeNull();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public static class ServiceCollectionExtensions
{ typeof(InvalidUpdateSupplierInformationCommand), (StatusCodes.Status400BadRequest, "INVALID_SUPPLIER_INFORMATION_UPDATE_ENTITY") },
{ typeof(InvalidQueryException), (StatusCodes.Status400BadRequest, "ISSUE_WITH_QUERY_PARAMETERS") },
{ typeof(DuplicateAuthenticationKeyNameException), (StatusCodes.Status400BadRequest, "APIKEY_NAME_ALREADY_EXISTS") },
{ typeof(DuplicateEmailWithinOrganisationException), (StatusCodes.Status400BadRequest, "EMAIL_ALREADY_EXISTS_WITHIN_ORGANISATION") },
{ typeof(InvalidUpdateOrganisationCommand), (StatusCodes.Status400BadRequest, "ORGANISATION_UPDATE_INVALID_INPUT") },

{ typeof(DuplicateEmailWithinOrganisationException), (StatusCodes.Status400BadRequest, "EMAIL_ALREADY_EXISTS_WITHIN_ORGANISATION") }
};

public static IServiceCollection AddOrganisationProblemDetails(this IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,36 +97,24 @@ public async Task<bool> Execute((Guid organisationId, UpdateOrganisation updateO

break;
case OrganisationUpdateType.AdditionalIdentifiers:
if (updateObject.AdditionalIdentifiers == null || !updateObject.AdditionalIdentifiers.Any())
if (updateObject.AdditionalIdentifiers == null)
{
throw new InvalidUpdateOrganisationCommand("Missing additional identifiers.");
}

foreach (var identifier in updateObject.AdditionalIdentifiers)
{
if (string.IsNullOrWhiteSpace(identifier.Id))
{
throw new InvalidUpdateOrganisationCommand($"Missing Identifier Number for scheme '{identifier.Scheme}'.");
}

// Check if Identifier number already exists
var organisationIdentifier = organisationRepository.FindByIdentifier(identifier.Scheme, identifier.Id);
if (organisationIdentifier.Result != null)
{
throw new InvalidUpdateOrganisationCommand($"The identifier '{identifier.Id}' you have entered belongs to a different organization that already exists.");
}

var existingIdentifier = organisation.Identifiers.FirstOrDefault(i => i.Scheme == identifier.Scheme);

if (existingIdentifier != null)
{
// Update existing identifier
existingIdentifier.IdentifierId = identifier.Id;
existingIdentifier.LegalName = identifier.LegalName;
if (!string.IsNullOrEmpty(identifier.Id))
{
existingIdentifier.IdentifierId = identifier.Id;
existingIdentifier.LegalName = identifier.LegalName;
}
}
else
else if (!string.IsNullOrEmpty(identifier.Id))
{
// Add new identifier
organisation.Identifiers.Add(new OrganisationInformation.Persistence.Organisation.Identifier
{
IdentifierId = identifier.Id,
Expand Down

0 comments on commit 06bf383

Please sign in to comment.