Skip to content

Commit

Permalink
feat: added trigger endpoint & cleanup mock request
Browse files Browse the repository at this point in the history
  • Loading branch information
eschrewe committed Jul 10, 2024
1 parent 2055141 commit c06032b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.puris.backend.common.domain.model.measurement.ItemUnitEnumeration;
import org.eclipse.tractusx.puris.backend.common.util.VariablesService;
import org.eclipse.tractusx.puris.backend.erpadapter.domain.model.ErpAdapterRequest;
import org.eclipse.tractusx.puris.backend.erpadapter.logic.service.ErpAdapterRequestService;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner;
Expand All @@ -38,8 +37,6 @@
import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductItemStock;
import org.eclipse.tractusx.puris.backend.stock.domain.model.ReportedMaterialItemStock;
import org.eclipse.tractusx.puris.backend.stock.domain.model.ReportedProductItemStock;
import org.eclipse.tractusx.puris.backend.common.domain.model.measurement.ItemUnitEnumeration;
import org.eclipse.tractusx.puris.backend.stock.logic.dto.itemstocksamm.DirectionCharacteristic;
import org.eclipse.tractusx.puris.backend.stock.logic.service.MaterialItemStockService;
import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductItemStockService;
import org.eclipse.tractusx.puris.backend.stock.logic.service.ReportedMaterialItemStockService;
Expand Down Expand Up @@ -78,8 +75,6 @@ public class DataInjectionCommandLineRunner implements CommandLineRunner {

@Autowired
private VariablesService variablesService;
@Autowired
private ErpAdapterRequestService erpAdapterRequestService;

private ObjectMapper objectMapper;

Expand Down Expand Up @@ -246,20 +241,6 @@ private void setupCustomerRole() throws JsonProcessingException {
.locationBpna(supplierPartner.getSites().first().getAddresses().first().getBpna())
.build();
reportedProductItemStockService.create(reportedProductItemStock);

// TODO: remove mock
ErpAdapterRequest mockRequest = ErpAdapterRequest
.builder()
.partnerBpnl(supplierPartner.getBpnl())
.requestDate(new Date(System.currentTimeMillis()-3*60*60*1000))
.ownMaterialNumber(semiconductorMaterial.getOwnMaterialNumber())
.directionCharacteristic(DirectionCharacteristic.INBOUND)
.requestType("ItemStock")
.sammVersion("2.0")
.responseCode(201)
.build();
mockRequest = erpAdapterRequestService.create(mockRequest);
log.info("Created mocked ErpAdapterRequest: \n{}", mockRequest);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.puris.backend.common.edc.domain.model.AssetType;
import org.eclipse.tractusx.puris.backend.erpadapter.logic.service.ErpAdapterTriggerService;
import org.eclipse.tractusx.puris.backend.erpadapter.logic.service.ItemStockErpAdapterService;
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService;
import org.eclipse.tractusx.puris.backend.stock.logic.dto.itemstocksamm.DirectionCharacteristic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -49,11 +52,40 @@ public class ErpAdapterController {
@Autowired
private ItemStockErpAdapterService itemStockErpAdapterService;

@Autowired
private ErpAdapterTriggerService erpAdapterTriggerService;

@Autowired
private MaterialPartnerRelationService mprService;

@Operation(description = "This endpoint is used to trigger scheduled updates from the ErpAdapter. This is useful " +
"if you are expecting a specific request from a partner in the near future and want to make a best-effort attempt to ensure " +
"that your PURIS backend has already obtained current data to respond to that expected request, when it arrives.")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "accepted"),
@ApiResponse(responseCode = "404", description = "bad request")
})
@PostMapping("/trigger")
public ResponseEntity<?> frontEndtriggerRecevier(
@RequestParam("partner-bpnl") String bpnl,
@RequestParam("own-materialnumber") String materialNumber,
@RequestParam("asset-type") AssetType assetType,
@RequestParam(required = false, value = "direction") DirectionCharacteristic directionCharacteristic
) {
boolean valid = BPNL_PATTERN.matcher(bpnl).matches()
&& NON_EMPTY_NON_VERTICAL_WHITESPACE_PATTERN.matcher(materialNumber).matches();
if (valid && mprService.find(bpnl, materialNumber) != null) {
erpAdapterTriggerService.notifyPartnerRequest(bpnl, materialNumber, assetType, directionCharacteristic);
return ResponseEntity.status(201).build();
} else {
return ResponseEntity.badRequest().build();
}
}



@Operation(description = "This endpoint accepts responses from the ERP adapter. Incoming messages are expected to " +
"carry a SAMM of the previously requested type. \n\nPlease note that this version currently accepts multiple responses " +
"addressing the same request-id for testing purposes. However, in the near future, this will be enforced strictly. " +
"I.e. only the first response for a given request-id will be accepted. All later responses addressing the same request-id" +
" will be rejected (status code 409)\n\n" +
"carry a SAMM of the previously requested type. \n\n" +
"Currently supported: \n\n" +
"| response-type | samm-version |\n" +
"|---------------|--------------|\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,21 @@ public void createAndSend(ErpAdapterRequest erpAdapterRequest) {
if (erpAdapterRequest != null) {
Integer responseCode = erpAdapterRequestClient.sendRequest(erpAdapterRequest);
if (responseCode != null) {
erpAdapterRequest.setResponseCode(responseCode);
update(erpAdapterRequest);
if (responseCode >= 200 && responseCode < 400) {
log.info("Successfully sent request to ERP Adapter, got status code {} for request:\n{}", responseCode, erpAdapterRequest);
} else {
log.warn("Received status code {} from ERP Adapter for request:\n{}", responseCode, erpAdapterRequest);
}
erpAdapterRequest.setResponseCode(responseCode);
update(erpAdapterRequest);
} else {
log.error("Failed to send request to ERP Adapter:\n{}", erpAdapterRequest);
}
}
}

public ErpAdapterRequest get(UUID id) {
// TODO: Remove when mock is removed
return repository.findById(id).orElse(repository.findAll().getFirst());
// return repository.findById(id).orElse(null);
return repository.findById(id).orElse(null);
}

public ErpAdapterRequest update(ErpAdapterRequest erpAdapterRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ public int receiveItemStockUpdate(ErpAdapterController.Dto dto) {
log.error("Unknown request-id {}", dto.requestId());
return 404;
}
// TODO: uncomment the following block when removing mock request, also edit swagger description in ErpAdapterController
// if (request.getResponseReceivedDate() != null) {
// log.error("Received duplicate response for messageId {}", request.getId());
// return 409;
// }
if (request.getResponseReceivedDate() != null) {
log.error("Received duplicate response for messageId {}", request.getId());
return 409;
}
if (request.getResponseCode() == null || request.getResponseCode() < 200 || request.getResponseCode() >= 400) {
log.error("Unexpected response, erp adapter had not confirmed request");
return 404;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ public interface MaterialPartnerRelationRepository extends JpaRepository<Materia
List<MaterialPartnerRelation> findAllByPartnerAndPartnerMaterialNumberAndPartnerBuysMaterialIsTrue(Partner partner, String partnerMaterialNumber);

List<MaterialPartnerRelation> findAllByPartnerAndAndPartnerCXNumber(Partner partner, String partnerCXNumber);

List<MaterialPartnerRelation> findAllByPartner_BpnlAndMaterial_OwnMaterialNumber(String bpnl, String ownMaterialNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ public interface MaterialPartnerRelationService {
List<MaterialPartnerRelation> findAllByCustomerPartnerAndPartnerMaterialNumber(Partner partner, String partnerMaterialNumber);

MaterialPartnerRelation findByPartnerAndPartnerCXNumber(Partner partner, String partnerCXNumber);

MaterialPartnerRelation find(String bpnl, String ownMaterialNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -619,4 +619,16 @@ public MaterialPartnerRelation findByPartnerAndPartnerCXNumber(Partner partner,
return null;
}
}

@Override
public MaterialPartnerRelation find(String bpnl, String ownMaterialNumber) {
var results = mprRepository.findAllByPartner_BpnlAndMaterial_OwnMaterialNumber(bpnl, ownMaterialNumber);
if (results.isEmpty()) {
return null;
}
if (results.size() > 1) {
log.warn("Found more than one MPR for {} and ownMaterialNumber {}", bpnl, ownMaterialNumber);

Check failure

Code scanning / CodeQL

Log Injection High

This log entry depends on a
user-provided value
.

Check failure

Code scanning / CodeQL

Log Injection High

This log entry depends on a
user-provided value
.
}
return results.getFirst();
}
}

0 comments on commit c06032b

Please sign in to comment.