Skip to content

Commit

Permalink
AO-802: Updated newOrderable method to add a price change
Browse files Browse the repository at this point in the history
  • Loading branch information
sradziszewski committed Dec 5, 2023
1 parent bb80e35 commit 49c0651
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

import com.jayway.restassured.response.Response;
import guru.nidi.ramltester.junit.RamlMatchers;
import java.time.ZoneId;
Expand Down Expand Up @@ -74,6 +73,7 @@
import org.openlmis.referencedata.domain.RightName;
import org.openlmis.referencedata.dto.OrderableChildDto;
import org.openlmis.referencedata.dto.OrderableDto;
import org.openlmis.referencedata.dto.PriceChangeDto;
import org.openlmis.referencedata.dto.ProgramOrderableDto;
import org.openlmis.referencedata.dto.VersionIdentityDto;
import org.openlmis.referencedata.exception.UnauthorizedException;
Expand Down Expand Up @@ -344,7 +344,8 @@ public void shouldCreateNewOrderableWithProgramOrderable() {
.createNew(Code.code("orderableDisplayCategoryCode"));
orderableDisplayCategory.setId(UUID.randomUUID());
ProgramOrderable programOrderable = new ProgramOrderable(program, orderable, 1, true,
orderableDisplayCategory, true, 1, Money.of(CurrencyUnit.USD, 10.0));
orderableDisplayCategory, true, 1, Money.of(CurrencyUnit.USD, 10.0),
Collections.emptyList());
orderable.setProgramOrderables(Collections.singletonList(programOrderable));
orderable.export(orderableDto);

Expand Down Expand Up @@ -929,7 +930,8 @@ public void shouldGetAuditLog() {

private ProgramOrderableDto generateProgramOrderable() {
return new ProgramOrderableDto(UUID.randomUUID(), UUID.randomUUID(),
null, null, true, true, 0, 1, Money.of(CurrencyUnit.USD, 10.0));
null, null, true, true, 0, 1, Money.of(CurrencyUnit.USD, 10.0),
Collections.singletonList(new PriceChangeDto()));
}

private void checkIfEquals(PageDto response, List<OrderableDto> expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@

import static org.springframework.util.CollectionUtils.isEmpty;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.joda.money.Money;
import org.openlmis.referencedata.domain.Orderable;
import org.openlmis.referencedata.domain.OrderableChild;
import org.openlmis.referencedata.domain.PriceChange;
import org.openlmis.referencedata.domain.Program;
import org.openlmis.referencedata.domain.ProgramOrderable;
import org.openlmis.referencedata.domain.User;
import org.openlmis.referencedata.exception.NotFoundException;
import org.openlmis.referencedata.repository.OrderableRepository;
import org.openlmis.referencedata.repository.ProgramRepository;
import org.openlmis.referencedata.repository.UserRepository;
import org.openlmis.referencedata.service.AuthenticationHelper;
import org.openlmis.referencedata.util.messagekeys.UserMessageKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -40,6 +48,12 @@ public class OrderableBuilder {
@Autowired
private OrderableRepository orderableRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private AuthenticationHelper authenticationHelper;

/**
* Creates new instance based on data from {@link Orderable.Importer}.
*
Expand Down Expand Up @@ -67,6 +81,31 @@ public Orderable newOrderable(Orderable.Importer importer, Orderable persistedOr
programOrderable.setProgram(program);
programOrderable.setProduct(orderable);

List<PriceChange> priceChanges = item.getPriceChanges().stream()
.map(priceChange -> {
User author = userRepository.findById(priceChange.getAuthor().getId())
.orElseThrow(() -> new NotFoundException(UserMessageKeys.ERROR_NOT_FOUND));
PriceChange priceChangeItem = PriceChange.newInstance(priceChange, author);
priceChangeItem.setProgramOrderable(programOrderable);
return priceChangeItem;
})
.collect(Collectors.toList());

if (persistedOrderable != null) {
boolean priceHasChanged = true;
Money newPrice = programOrderable.getPricePerPack();
if (persistedOrderable.getProgramOrderable(program) != null) {
Money currentPrice = persistedOrderable.getProgramOrderable(program)
.getPricePerPack();
priceHasChanged = !currentPrice.equals(newPrice);
}

if (priceHasChanged) {
addPriceChange(programOrderable, newPrice, priceChanges);
}
}

programOrderable.setPriceChanges(priceChanges);
return programOrderable;
})
.collect(Collectors.toList());
Expand Down Expand Up @@ -97,4 +136,15 @@ public Orderable newOrderable(Orderable.Importer importer, Orderable persistedOr
return orderable;
}

private void addPriceChange(ProgramOrderable programOrderable, Money newPrice,
List<PriceChange> priceChanges) {
PriceChange priceChange = new PriceChange();
priceChange.setAuthor(authenticationHelper.getCurrentUser());
priceChange.setPrice(newPrice);
priceChange.setOccurredDate(ZonedDateTime.now());
priceChange.setProgramOrderable(programOrderable);

priceChanges.add(priceChange);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Matchers.any;
Expand All @@ -28,6 +29,8 @@
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
Expand All @@ -44,6 +47,8 @@
import org.openlmis.referencedata.dto.ProgramOrderableDto;
import org.openlmis.referencedata.repository.OrderableRepository;
import org.openlmis.referencedata.repository.ProgramRepository;
import org.openlmis.referencedata.service.AuthenticationHelper;
import org.openlmis.referencedata.testbuilder.UserDataBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
Expand All @@ -57,6 +62,9 @@ public class OrderableBuilderTest {
@Mock
private OrderableRepository orderableRepository;

@Mock
private AuthenticationHelper authenticationHelper;

@InjectMocks
private OrderableBuilder orderableBuilder;

Expand Down Expand Up @@ -123,6 +131,19 @@ public void shouldIncrementVersionNumber() {
assertThat(updatedOrderable.getVersionNumber(), is(2L));
}

@Test
public void shouldAddNewPriceChange() {
Program program = createProgram("test_program");
Orderable orderable = createOrderable(program.getId());
orderable.getProgramOrderable(program).setPricePerPack(Money.of(CurrencyUnit.of("USD"), 10));
OrderableDto orderableDto = createOrderableDto(program.getId());
when(authenticationHelper.getCurrentUser()).thenReturn(new UserDataBuilder().build());

Orderable updatedOrderable = orderableBuilder.newOrderable(orderableDto, orderable);

assertThat(updatedOrderable.getProgramOrderable(program).getPriceChanges(), hasSize(1));
}

private Program createProgram(String code) {
Program program = new Program(UUID.randomUUID());
program.setCode(Code.code(code));
Expand Down

0 comments on commit 49c0651

Please sign in to comment.