Product deleted
This commit is contained in:
parent
e32e84ac4a
commit
f57eb9e924
|
|
@ -19,6 +19,8 @@ import java.time.Instant;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Pushes Dolibarr products to PrestaShop, creating or updating by SKU.
|
||||
|
|
@ -57,6 +59,11 @@ public class ProductSyncService {
|
|||
List<DolibarrProductDto> products = dolibarrClient.getProducts();
|
||||
log.info("ProductSync: {} products to process", products.size());
|
||||
|
||||
Set<String> dolibarrSkus = products.stream()
|
||||
.map(DolibarrProductDto::ref)
|
||||
.filter(ref -> ref != null && !ref.isBlank())
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
for (DolibarrProductDto product : products) {
|
||||
if (product.ref() == null || product.ref().isBlank()) {
|
||||
log.warn("Skipping Dolibarr product id={} with no ref", product.id());
|
||||
|
|
@ -74,6 +81,23 @@ public class ProductSyncService {
|
|||
markError(product.ref(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
for (ProductMapping mapping : productMappingRepository.findAll()) {
|
||||
if (!dolibarrSkus.contains(mapping.getSku()) && mapping.getPrestashopId() != null) {
|
||||
try {
|
||||
prestashopClient.deleteProduct(mapping.getPrestashopId());
|
||||
productMappingRepository.delete(mapping);
|
||||
processed++;
|
||||
log.info("ProductSync: deleted PS product SKU={} psId={} (no longer in Dolibarr)",
|
||||
mapping.getSku(), mapping.getPrestashopId());
|
||||
} catch (PrestashopApiException | RestClientException | HttpMessageConversionException e) {
|
||||
failed++;
|
||||
String msg = "Delete SKU %s: %s".formatted(mapping.getSku(), e.getMessage());
|
||||
errors.add(msg);
|
||||
log.warn("ProductSync delete failed: {}", msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
failed++;
|
||||
String msg = "Failed to fetch products from Dolibarr: " + e.getMessage();
|
||||
|
|
|
|||
Loading…
Reference in New Issue