Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ public Long sumOperationErrorWithGroupBy(String fieldName,
.sum();
}

@Override
public Boolean isExistByField(String fieldName, Object value, Long from, Long to) {
List<CheckedPayment> checkedPayments = localStorage.get();
int count = (int) checkedPayments.stream()
.filter(checkedPayment -> checkedPayment.getEventTime() >= from
&& checkedPayment.getEventTime() <= to
&& paymentFieldValueFilter.filter(fieldName, value, checkedPayment))
.count();
log.debug("LocalResultStorageRepository countOperationByField: {}", count);
return count != 0;
}

private boolean filterByStatusAndFields(
Long from,
Long to,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ Long sumOperationErrorWithGroupBy(
Long to,
List<FieldModel> fieldModels);


Boolean isExistByField(String fieldName, Object value, Long from, Long to);
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,15 @@ public Long sumOperationErrorWithGroupBy(String fieldName, Object value, Long fr
return sumOperationErrorWithGroupBy(fieldName, value, from, to, fieldModels, null);
}

@Override
public Boolean isExistByField(String fieldName, Object value, Long from, Long to) {
return aggregationGeneralRepository.countOperationByField(
EventSource.FRAUD_EVENTS_UNIQUE.getTable(),
fieldName,
value,
from,
to
) != 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,20 @@ public Long sumOperationErrorWithGroupBy(String fieldName,
return jdbcTemplate.query(resultSql.toString(), params.toArray(), new SumExtractor());
}

@Override
public Boolean isExistByField(String fieldName, Object value, Long from, Long to) {
String sql = String.format("""
select 1 as cnt
from %2$s
where timestamp >= ?
and timestamp <= ?
and eventTime >= ?
and eventTime <= ?
and %1$s = ?
limit 1""", fieldName, TABLE);
List<Object> params = AggregationUtil.generateParams(from, to, value);
log.debug("AggregationGeneralRepositoryImpl isExistByField sql: {} params: {}", sql, params);
return jdbcTemplate.query(sql, params.toArray(), new CountExtractor()) != 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.vality.fraudbusters.util.ReferenceKeyGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.Instant;
Expand All @@ -22,13 +23,14 @@ public class ShopManagementService {
private final Pool<String> referencePoolImpl;
private final Pool<String> groupReferencePoolImpl;

@Cacheable(value = "isNewShop")
public boolean isNewShop(String partyId, String shopId) {
if (hasReferenceInPools(partyId, shopId)) {
return false;
}
Long to = Instant.now().toEpochMilli();
Long from = Instant.now().minus(properties.getCountToCheckDays(), ChronoUnit.DAYS).toEpochMilli();
return repository.countOperationByField("shopId", shopId, from, to) == 0;
return repository.isExistByField("shopId", shopId, from, to);
}

private boolean hasReferenceInPools(String partyId, String shopId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static dev.vality.fraudbusters.extension.ClickHouseContainerExtension.CLICKHOUSE_CONTAINER;
import static dev.vality.fraudbusters.util.BeanUtil.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Slf4j
@ActiveProfiles("full-prod")
Expand Down Expand Up @@ -77,6 +78,12 @@ public void countOperationByPhoneTest() throws SQLException {
assertEquals(1, count);
}

@Test
public void isExistTest() throws SQLException {
Boolean isExist = paymentRepository.isExistByField(EventField.phone.name(), PHONE, FROM, TO);
assertTrue(isExist);
}

@Test
public void countOperationByEmailTestWithGroupBy() throws SQLException {
PaymentModel paymentModel = createFraudModelSecond();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ public void testCreateDefaultReference() {

@Test
public void testIsNewShop() {
when(fraudResultRepository.countOperationByField(anyString(), anyString(), anyLong(), anyLong())).thenReturn(0);
when(fraudResultRepository.isExistByField(anyString(), anyString(), anyLong(), anyLong())).thenReturn(false);
shopManagementService.isNewShop("partyId", "s1");
verify(fraudResultRepository).countOperationByField(anyString(), anyString(), anyLong(), anyLong());
verify(fraudResultRepository).isExistByField(anyString(), anyString(), anyLong(), anyLong());

referencePoolImpl.add("partyId", "test");
boolean newShop = shopManagementService.isNewShop("partyId", "s1");
assertFalse(newShop);

referencePoolImpl.remove("partyId");
when(fraudResultRepository.isExistByField(anyString(), anyString(), anyLong(), anyLong())).thenReturn(true);
newShop = shopManagementService.isNewShop("partyId", "s1");
assertTrue(newShop);
}
Expand Down
Loading