diff --git a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java index 54bffa7..9735506 100644 --- a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java +++ b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java @@ -3,7 +3,6 @@ package com.teterialosjuanjos.tfg.sync_service.integration.prestashop; import com.teterialosjuanjos.tfg.sync_service.config.IntegrationProperties; import com.teterialosjuanjos.tfg.sync_service.integration.prestashop.dto.*; import lombok.extern.slf4j.Slf4j; -import com.fasterxml.jackson.databind.JsonNode; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; @@ -86,19 +85,18 @@ public class PrestashopClient { } /** - * Creates a new product in PrestaShop. - * PS WebService requires XML request body even when {@code output_format=JSON} is set. + * Creates a new product in PrestaShop and returns it with its assigned PS id. * - *

The POST response JSON wraps multilingual fields differently from GET - * (e.g. {@code {"language":[...]}} instead of a flat array), so the response - * is parsed as a raw {@link JsonNode} and only the assigned {@code id} is extracted. - * All other fields are taken from the input DTO.

+ *

PS WebService requires XML request body even when {@code output_format=JSON} is set. + * The POST response JSON wraps multilingual fields in a different structure than GET + * responses, so the response body is discarded and a subsequent GET by reference is used + * to retrieve the created product with its id.

* - * @return the created product with its assigned PS {@code id} + * @return the created product with its assigned PS {@code id}, or {@code null} if not found after creation */ public PrestashopProductDto createProduct(PrestashopProductDto dto) { log.debug("Creating PrestaShop product reference={}", dto.reference()); - JsonNode response = restClient.post() + restClient.post() .uri(u -> u.path("/products") .queryParam("ws_key", wsKey) .queryParam("output_format", "JSON") @@ -106,17 +104,9 @@ public class PrestashopClient { .contentType(MediaType.APPLICATION_XML) .body(toProductXml(dto).getBytes(StandardCharsets.UTF_8)) .retrieve() - .body(JsonNode.class); + .toBodilessEntity(); - Integer psId = null; - if (response != null && response.has("product")) { - JsonNode product = response.get("product"); - if (product.has("id")) { - psId = product.get("id").asInt(); - } - } - return new PrestashopProductDto(psId, dto.reference(), dto.price(), dto.active(), - dto.idCategoryDefault(), dto.name(), dto.description(), dto.descriptionShort()); + return getProductByReference(dto.reference()).orElse(null); } /** Updates an existing product by its PrestaShop internal ID. */