mirror of
https://github.com/valitydev/cashier.git
synced 2024-11-06 09:55:17 +00:00
BJ-872: add shop check (#7)
This commit is contained in:
parent
096044d6c6
commit
16f48081f0
@ -1,9 +1,13 @@
|
||||
package com.rbkmoney.cashier.handler;
|
||||
|
||||
import com.rbkmoney.cashier.domain.CashRegister;
|
||||
import com.rbkmoney.cashier.domain.InvoiceChangeWithMetadata;
|
||||
import com.rbkmoney.cashier.handler.inventory.HandlerInventory;
|
||||
import com.rbkmoney.cashier.parser.SourceEventParser;
|
||||
import com.rbkmoney.cashier.repository.CashRegisterRepository;
|
||||
import com.rbkmoney.cashier.repository.InvoiceAggregateRepository;
|
||||
import com.rbkmoney.damsel.payment_processing.EventPayload;
|
||||
import com.rbkmoney.damsel.payment_processing.Invoice;
|
||||
import com.rbkmoney.machinegun.eventsink.MachineEvent;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -19,13 +23,15 @@ import static java.util.stream.Collectors.toList;
|
||||
@RequiredArgsConstructor
|
||||
public class EventsHandler {
|
||||
|
||||
private final InvoiceAggregateRepository invoiceAggregateRepository;
|
||||
private final CashRegisterRepository cashRegisterRepository;
|
||||
private final HandlerInventory handlerInventory;
|
||||
private final SourceEventParser eventParser;
|
||||
|
||||
public void handle(List<MachineEvent> machineEvents) {
|
||||
machineEvents.stream()
|
||||
.map(machineEvent -> Map.entry(eventParser.parse(machineEvent), machineEvent))
|
||||
.filter(entry -> entry.getKey().isSetInvoiceChanges())
|
||||
.filter(entry -> entry.getKey().isSetInvoiceChanges() && hasShopInDataBase(entry.getValue()))
|
||||
.map(this::toInvoiceChangesWithMetadata)
|
||||
.flatMap(List::stream)
|
||||
.sorted(byEventId())
|
||||
@ -53,4 +59,20 @@ public class EventsHandler {
|
||||
private Comparator<InvoiceChangeWithMetadata> byEventId() {
|
||||
return comparingLong(InvoiceChangeWithMetadata::getEventId);
|
||||
}
|
||||
|
||||
private boolean hasShopInDataBase(MachineEvent machineEvent) {
|
||||
String invoiceId = machineEvent.getSourceId();
|
||||
long eventId = machineEvent.getEventId();
|
||||
|
||||
Invoice aggregate = invoiceAggregateRepository.findByInvoiceIdAndEventId(
|
||||
invoiceId,
|
||||
eventId);
|
||||
|
||||
List<CashRegister> cashRegisters = cashRegisterRepository.findByPartyIdAndShopId(
|
||||
aggregate.getInvoice().getOwnerId(),
|
||||
aggregate.getInvoice().getShopId());
|
||||
|
||||
return (cashRegisters != null && !cashRegisters.isEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,16 @@
|
||||
package com.rbkmoney.cashier;
|
||||
|
||||
import com.rbkmoney.cashier.domain.CashRegister;
|
||||
import com.rbkmoney.cashier.handler.EventsHandler;
|
||||
import com.rbkmoney.cashier.repository.CashRegisterRepository;
|
||||
import com.rbkmoney.cashier.repository.InvoiceAggregateRepository;
|
||||
import com.rbkmoney.cashier.util.JsonMapper;
|
||||
import com.rbkmoney.damsel.cashreg.processing.ManagementSrv;
|
||||
import com.rbkmoney.damsel.domain.*;
|
||||
import com.rbkmoney.damsel.payment_processing.*;
|
||||
import com.rbkmoney.damsel.payment_processing.Invoice;
|
||||
import com.rbkmoney.damsel.payment_processing.InvoicePayment;
|
||||
import com.rbkmoney.damsel.payment_processing.InvoicePaymentRefund;
|
||||
import com.rbkmoney.damsel.payment_processing.*;
|
||||
import com.rbkmoney.geck.serializer.kit.mock.MockMode;
|
||||
import com.rbkmoney.geck.serializer.kit.mock.MockTBaseProcessor;
|
||||
import com.rbkmoney.geck.serializer.kit.tbase.TBaseHandler;
|
||||
@ -18,6 +20,7 @@ import org.apache.thrift.TBase;
|
||||
import org.apache.thrift.TException;
|
||||
import org.apache.thrift.TSerializer;
|
||||
import org.apache.thrift.protocol.TBinaryProtocol;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -33,7 +36,9 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ -89,8 +94,10 @@ public class IntegrationTestWithNoKafka {
|
||||
|
||||
when(invoiceAggregateRepository.findByInvoiceIdAndEventId("invoiceId", 0L))
|
||||
.thenReturn(aggregate);
|
||||
|
||||
List<CashRegister> cashRegisters = prepareListCashRegisters();
|
||||
when(cashRegisterRepository.findByPartyIdAndShopId(anyString(), anyString()))
|
||||
.thenReturn(List.of());
|
||||
.thenReturn(cashRegisters);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -181,4 +188,28 @@ public class IntegrationTestWithNoKafka {
|
||||
return new MockTBaseProcessor(MockMode.RANDOM, 15, 1)
|
||||
.process(tBase, new TBaseHandler<>(InvoiceChange.class));
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
private List<CashRegister> prepareListCashRegisters() {
|
||||
List<CashRegister> cashRegisters = new ArrayList<>();
|
||||
CashRegister cashRegister = CashRegister.builder()
|
||||
.id("id")
|
||||
.partyId("partyId")
|
||||
.shopId("shopId")
|
||||
.providerId(1)
|
||||
.providerParams(JsonMapper.toJson(prepareProviderParams()))
|
||||
.build();
|
||||
cashRegisters.add(cashRegister);
|
||||
return cashRegisters;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Map<String, String> prepareProviderParams() {
|
||||
return Map.of(
|
||||
"name", "pupa",
|
||||
"password", "lupa",
|
||||
"tel", "88005553535",
|
||||
"url", "https://pupalupa.com/");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user