Skip to content

Commit

Permalink
Adapted method to sync Datadis supplies to get data just from users w…
Browse files Browse the repository at this point in the history
…ith at least one supply
  • Loading branch information
viktorKhan committed Jun 16, 2024
1 parent cf3bc18 commit 8ffd2a3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public DatadisSuppliesSyncService(GetSupplyRepository getSupplyRepository, Updat
* Synchronizes the supplies for all users retrieving data from datadis.es
*/
public void synchronizeSupplies() {
// Get all users
List<User> allUsers = getUserRepository.findAll();
// Get all users with at least one supply
List<User> allUsers = getUserRepository.findAllUsersWithAtLeastOneSupply();
Map<String, Supply> allSupplies = getSupplyRepository.findAll().stream()
.collect(Collectors.toMap(Supply::getCode, supply -> supply));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.lucoenergia.conluz.domain.admin.user.get;

import org.lucoenergia.conluz.domain.admin.user.DefaultAdminUser;
import org.lucoenergia.conluz.domain.admin.user.User;
import org.lucoenergia.conluz.domain.shared.UserId;
import org.lucoenergia.conluz.domain.shared.UserPersonalId;
Expand All @@ -25,4 +24,6 @@ public interface GetUserRepository {
List<User> findAll();

Optional<User> getDefaultAdminUser();

List<User> findAllUsersWithAtLeastOneSupply();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.lucoenergia.conluz.domain.admin.user.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

Expand All @@ -13,4 +15,7 @@ public interface UserRepository extends JpaRepository<UserEntity, UUID> {
Optional<UserEntity> findByNumberAndRole(int number, Role role);

boolean existsByPersonalId(String personalId);

@Query("SELECT u FROM users u WHERE EXISTS(SELECT s FROM supplies s WHERE s.user = u)")
List<UserEntity> findAllUsersWithAtLeastOneSupply();
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ public Optional<User> getDefaultAdminUser() {
}
return Optional.of(userEntityMapper.map(entity.get()));
}

@Override
public List<User> findAllUsersWithAtLeastOneSupply() {
return userEntityMapper.mapList(userRepository.findAllUsersWithAtLeastOneSupply());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

@Transactional
class DatadisSuppliesSyncServiceTest extends BaseIntegrationTest {
Expand Down Expand Up @@ -91,4 +90,45 @@ void synchronizeSuppliesSuccessTest() {
// Assert
Assertions.assertEquals(datadisSupply.getAddress(), getSupplyRepository.findById(SupplyId.of(supplyOne.getId())).get().getAddress());
}

@Test
void synchronizeUsersWithoutSupply() {
// Assemble
String codeOne = "codeOne";
String codeTwo = "codeTwo";
String codeThree = "codeThree";

User userOne = UserMother.randomUser();
userOne = createUserRepository.create(userOne);
User userTwo = UserMother.randomUser();
userTwo = createUserRepository.create(userTwo);

DatadisSupply datadisSupply = new DatadisSupply.Builder()
.withCups(codeOne)
.withAddress("TestAddress")
.withDistributor("EDISTRIBUCION")
.withDistributorCode("2")
.withPointType(5)
.withValidDateFrom("2024/06/01")
.build();
List<DatadisSupply> datadisSupplies = new ArrayList<>();
datadisSupplies.add(datadisSupply);

Supply supplyOne = SupplyMother.random()
.withCode(codeOne)
.withAddress("OldAddress")
.build();
supplyOne = createSupplyRepository.create(supplyOne, UserId.of(userTwo.getId()));
createSupplyRepository.create(SupplyMother.random().withCode(codeTwo).build(), UserId.of(userTwo.getId()));
createSupplyRepository.create(SupplyMother.random().withCode(codeThree).build(), UserId.of(userTwo.getId()));

when(getSupplyRepositoryDatadis.getSuppliesByUser(Mockito.any(User.class))).thenReturn(datadisSupplies);

// Act
service.synchronizeSupplies();

// Assert
Mockito.verify(getSupplyRepositoryDatadis, times(1)).getSuppliesByUser(Mockito.any(User.class));
Assertions.assertEquals(datadisSupply.getAddress(), getSupplyRepository.findById(SupplyId.of(supplyOne.getId())).get().getAddress());
}
}

0 comments on commit 8ffd2a3

Please sign in to comment.