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 @@ -5,6 +5,7 @@
import dev.vality.fistful.base.Cash;
import dev.vality.fistful.withdrawal.TimestampedChange;
import dev.vality.fistful.withdrawal.WithdrawalState;
import dev.vality.fistful.withdrawal.adjustment.BodyChangePlan;
import dev.vality.fistful.withdrawal.status.Status;
import dev.vality.swag.wallets.webhook.events.model.*;
import dev.vality.wallets.hooker.domain.WebHookModel;
Expand All @@ -22,6 +23,7 @@

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

@Slf4j
@Component
Expand Down Expand Up @@ -56,7 +58,8 @@ protected WebhookMessage generateMessage(
messageGenParams.getEventId(),
messageGenParams.getCreatedAt(),
messageGenParams.getExternalId(),
messageGenParams.getWithdrawalState());
messageGenParams.getWithdrawalState(),
event.getChange().isSetAdjustment() ? event.getChange().getAdjustment().getId() : null);

WebhookMessage webhookMessage = generatorService.generate(event, model, messageGenParams);
webhookMessage.setParentEventId(initParenId(model, messageGenParams.getParentId()));
Expand Down Expand Up @@ -91,7 +94,8 @@ private String initRequestBody(
Long eventId,
String createdAt,
String externalId,
WithdrawalState withdrawalState) throws JsonProcessingException {
WithdrawalState withdrawalState,
String adjustmentId) throws JsonProcessingException {
if (status.isSetFailed()) {
WithdrawalFailed withdrawalFailed = new WithdrawalFailed()
.withdrawalID(withdrawalId)
Expand All @@ -107,7 +111,7 @@ private String initRequestBody(
.withdrawalID(withdrawalId)
.externalID(externalId)
.fee(fee)
.body(initNewBody(withdrawalState));
.body(initNewBody(withdrawalState, adjustmentId));
withdrawalSucceeded.setEventType(Event.EventTypeEnum.WITHDRAWAL_SUCCEEDED);
withdrawalSucceeded.setEventID(eventId.toString());
withdrawalSucceeded.setOccuredAt(OffsetDateTime.parse(createdAt));
Expand Down Expand Up @@ -135,16 +139,27 @@ private Fee calculateFee(String withdrawalId, Long eventId, WithdrawalState with
return null;
}

private WithdrawalBody initNewBody(WithdrawalState withdrawalState) {
private WithdrawalBody initNewBody(WithdrawalState withdrawalState, String adjustmentId) {
if (!amountChanged(withdrawalState)) {
return null;
}
return initBody(withdrawalState.getNewBody(), withdrawalState.getBody());
if (withdrawalState.isSetNewBody()) {
return initBody(withdrawalState.getNewBody(), withdrawalState.getBody());
}

return withdrawalState.getAdjustments().stream()
.filter(adjustmentState -> Objects.equals(adjustmentState.getId(), adjustmentId))
.map(adjustment -> adjustment.getChangesPlan().getNewBody())
.filter(Objects::nonNull)
.map(BodyChangePlan::getNewBody)
.findFirst()
.map(newBody -> initBody(newBody, withdrawalState.getBody()))
.orElse(null);
}

private boolean amountChanged(WithdrawalState withdrawalState) {
return withdrawalState != null
&& withdrawalState.isSetNewBody();
&& (withdrawalState.isSetNewBody() || withdrawalState.isSetAdjustments());
}

private WithdrawalBody initBody(Cash newBody, Cash oldBody) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import dev.vality.fistful.destination.Destination;
import dev.vality.fistful.destination.TimestampedChange;
import dev.vality.fistful.withdrawal.*;
import dev.vality.fistful.withdrawal.adjustment.AdjustmentState;
import dev.vality.fistful.withdrawal.adjustment.BodyChangePlan;
import dev.vality.fistful.withdrawal.adjustment.ChangesPlan;
import dev.vality.fistful.withdrawal.status.Status;
import dev.vality.fistful.withdrawal.status.Succeeded;
import dev.vality.kafka.common.serialization.ThriftSerializer;
Expand Down Expand Up @@ -175,6 +178,17 @@ public static WithdrawalState createWithdrawalStateWithNewBody() {
.setPostings(List.of(createFeePosting())));
}

public static WithdrawalState createWithdrawalStateWithAdjustmentState() {
return createWithdrawalState()
.setStatus(Status.succeeded(new Succeeded()))
.setAdjustments(List.of(new AdjustmentState()
.setChangesPlan(new ChangesPlan()
.setNewBody(new BodyChangePlan()
.setNewBody(createCash(1500, "USD"))))))
.setEffectiveFinalCashFlow(new FinalCashFlow()
.setPostings(List.of(createFeePosting())));
}

public static WebHookModel createWebhookModel() {
LinkedHashSet<EventType> eventTypes = new LinkedHashSet<>();
eventTypes.add(EventType.WITHDRAWAL_CREATED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,26 @@ void handleWithdrawalAdjustmentSucceededWebhook() {
.anyMatch(body -> body.contains("\"eventType\":\"WithdrawalSucceeded\"")
&& body.contains("\"body\":{\"amount\":1000,\"changedAmount\":1500,\"currency\":\"USD\"}")));
}

@Test
void handleWithdrawalAdjustmentWithAdjustmentStateSucceededWebhook() {
WebHookModel webhook = TestBeanFactory.createWebhookModel();
when(withdrawalClient.getWithdrawalInfo(eq(TestBeanFactory.WITHDRAWAL_ID), anyLong()))
.thenReturn(TestBeanFactory.createWithdrawalStateWithAdjustmentState());

webHookDao.create(webhook);

withdrawalEventService.handleEvents(List.of(TestBeanFactory.createWithdrawalEvent()));
withdrawalEventService.handleEvents(List.of(TestBeanFactory.createWithdrawalSucceeded(69L)));
withdrawalEventService.handleEvents(List.of(TestBeanFactory.createWithdrawalAdjustmentChange(70L)));

ArgumentCaptor<WebhookMessage> captor = ArgumentCaptor.forClass(WebhookMessage.class);
verify(webHookMessageSenderService, timeout(1000L).times(3))
.send(captor.capture());
Assertions.assertTrue(captor.getAllValues().stream()
.map(WebhookMessage::getRequestBody)
.map(String::new)
.anyMatch(body -> body.contains("\"eventType\":\"WithdrawalSucceeded\"")
&& body.contains("\"body\":{\"amount\":1000,\"changedAmount\":1500,\"currency\":\"USD\"}")));
}
}
Loading