Skip to content

Commit

Permalink
Skip Datadis consumption sync when supply does not have distributor code
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorKhan committed Jun 16, 2024
1 parent 8ffd2a3 commit 0359282
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public void synchronizeSupplies() {
.collect(Collectors.toMap(Supply::getCode, supply -> supply));

for (User user : allUsers) {

LOGGER.info("Processing users with ID {}", user.getId());

List<DatadisSupply> datadisSupplies = getSupplyRepositoryDatadis.getSuppliesByUser(user);
for (DatadisSupply datadisSupply : datadisSupplies) {
Supply supply = allSupplies.get(datadisSupply.getCups());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.lucoenergia.conluz.domain.consumption.datadis.sync;

import org.apache.commons.lang3.StringUtils;
import org.lucoenergia.conluz.domain.admin.supply.Supply;
import org.lucoenergia.conluz.domain.admin.supply.get.GetSupplyRepository;
import org.lucoenergia.conluz.domain.consumption.datadis.DatadisConsumption;
Expand Down Expand Up @@ -49,6 +50,11 @@ public void synchronizeConsumptions() {

for (Supply supply : allSupplies) {

if (StringUtils.isBlank(supply.getDatadisDistributorCode())) {
LOGGER.warn("Skipping supply with ID: {} because does not have distributor code", supply.getId());
continue;
}

LOGGER.info("Processing supply with ID: {}", supply.getId());

// Get validity date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public List<DatadisConsumption> getHourlyConsumptionsByMonth(@NotNull Supply sup
result.addAll(consumptions);
} else {
LOGGER.error("Unable to get consumptions for supply with ID {}. Code {}, message: {}",
supply.getCode(), response.code(), response.body() != null ? response.body().string() : response.message());
supply.getId(), response.code(), response.body() != null ? response.body().string() : response.message());
}
} catch (IOException e) {
LOGGER.error("Unable to get consumptions from datadis.es", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ void testSynchronizeConsumptionsValidDateFromNull() {
Supply supplyWithMoreThanOneYearValidDateFrom = SupplyMother.random()
.withDatadisValidDateFrom(LocalDate.now().minusMonths(24)).build();
supplyWithMoreThanOneYearValidDateFrom = createSupplyRepository.create(supplyWithMoreThanOneYearValidDateFrom, UserId.of(user.getId()));
Supply supplyWithoutDistributorCode = SupplyMother.random()
.withDatadisDistributorCode(null)
.withDatadisValidDateFrom(LocalDate.now().minusMonths(24)).build();
supplyWithoutDistributorCode = createSupplyRepository.create(supplyWithoutDistributorCode, UserId.of(user.getId()));

List<DatadisConsumption> consumptions = List.of(mock(DatadisConsumption.class));
when(getDatadisConsumptionRepository.getHourlyConsumptionsByMonth(any(Supply.class), any(Month.class), anyInt()))
Expand All @@ -76,6 +80,8 @@ void testSynchronizeConsumptionsValidDateFromNull() {
.getHourlyConsumptionsByMonth(eq(supplyWithNotNullValidDateFrom), any(Month.class), anyInt());
verify(getDatadisConsumptionRepository, times(12))
.getHourlyConsumptionsByMonth(eq(supplyWithMoreThanOneYearValidDateFrom), any(Month.class), anyInt());
verify(getDatadisConsumptionRepository, times(0))
.getHourlyConsumptionsByMonth(eq(supplyWithoutDistributorCode), any(Month.class), anyInt());
verify(persistDatadisConsumptionRepository, times(21)).persistConsumptions(anyList());
}
}

0 comments on commit 0359282

Please sign in to comment.