Skip to content

add rp filter #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/main/java/org/fiware/iam/tmforum/OrganizationResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ public class OrganizationResolver {

//TODO Cache me if you can
public Mono<String> getDID(String organizationId) {
return apiClient.retrieveOrganization(organizationId, FIELD_NAME_PARTY_CHARACTERISTIC)
return apiClient.retrieveOrganization(organizationId, null)
.filter(response -> response.getStatus().equals(HttpStatus.OK))
.map(HttpResponse::body)
.map(ovo ->
getDidFromExternalReference(ovo.getExternalReference())
.or(() -> getDidFromPartyCharacteristics(ovo.getPartyCharacteristic()))
.orElseThrow(() -> new TMForumException("Could not find organizations DID (%s) in response.".formatted(organizationId)))
.map(ovo -> {
String did = getDidFromExternalReference(ovo.getExternalReference())
.or(() -> getDidFromPartyCharacteristics(ovo.getPartyCharacteristic()))
.orElseThrow(() -> new TMForumException("Could not find organizations DID (%s) in response.".formatted(organizationId)));
log.info("Did is {}", did);
return did;
}
);
}

Expand All @@ -54,6 +57,7 @@ private Optional<String> getDidFromPartyCharacteristics(List<CharacteristicVO> c
}

private Optional<String> getDidFromExternalReference(List<ExternalReferenceVO> externalReferenceVOList) {
log.warn("External refs {}", externalReferenceVOList);
if (externalReferenceVOList == null) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fiware.iam.tmforum.handlers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpResponseFactory;
Expand Down Expand Up @@ -46,6 +47,8 @@ public class ProductOrderEventHandler implements EventHandler {
private static final String STATE_VERIFIED = "dspace:VERIFIED";
private static final String STATE_FINALIZED = "dspace:FINALIZED";

private static final String CUSTOMER_ROLE = "Customer";

private final ObjectMapper objectMapper;
private final GeneralProperties generalProperties;
private final OrganizationResolver organizationResolver;
Expand All @@ -70,12 +73,7 @@ public Mono<HttpResponse<?>> handleEvent(String eventType, Map<String, Object> e
.map(ProductOrderCreateEventPayloadVO::getProductOrder)
.map(ProductOrderVO::getRelatedParty)
.filter(Objects::nonNull)
.map(rpl -> {
if (rpl.size() != 1) {
throw new IllegalArgumentException("Expected exactly one ordering organization.");
}
return rpl.get(0);
})
.map(rpl -> getCustomer(rpl).orElseThrow(() -> new IllegalArgumentException("Exactly one ordering related party is expected.")))
.map(RelatedPartyVO::getId)
.findAny()
.orElseThrow(() -> new IllegalArgumentException("The ProductOrder-Event does not include a valid organization id."));
Expand All @@ -89,6 +87,22 @@ public Mono<HttpResponse<?>> handleEvent(String eventType, Map<String, Object> e

}

private Optional<RelatedPartyVO> getCustomer(List<RelatedPartyVO> relatedPartyVOS) {
if (relatedPartyVOS == null || relatedPartyVOS.isEmpty()) {
return Optional.empty();
}
if (relatedPartyVOS.size() == 1) {
String role = relatedPartyVOS.getFirst().getRole();
if (role == null || role.equals(CUSTOMER_ROLE)) {
return Optional.of(relatedPartyVOS.getFirst());
}
}
return relatedPartyVOS.stream()
.filter(relatedPartyVO -> relatedPartyVO.getRole() != null)
.filter(relatedPartyVO -> relatedPartyVO.getRole().equals(CUSTOMER_ROLE))
.findFirst();
}

private Mono<HttpResponse<?>> handelCreateEvent(String organizationId, Map<String, Object> event) {
ProductOrderCreateEventVO productOrderCreateEventVO = objectMapper.convertValue(event, ProductOrderCreateEventVO.class);

Expand Down
Loading