-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mateusz Czeladka
committed
Sep 18, 2023
1 parent
e8c69d4
commit c8e193f
Showing
9 changed files
with
118 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ain/java/org/cardano/foundation/voting/service/common/DefaultUserVerificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.cardano.foundation.voting.service.common; | ||
|
||
import io.vavr.control.Either; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.cardano.foundation.voting.domain.IsVerifiedRequest; | ||
import org.cardano.foundation.voting.domain.IsVerifiedResponse; | ||
import org.cardano.foundation.voting.service.discord.DiscordUserVerificationService; | ||
import org.cardano.foundation.voting.service.sms.SMSUserVerificationService; | ||
import org.cardano.foundation.voting.utils.CompletableFutures; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.zalando.problem.Problem; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class DefaultUserVerificationService implements UserVerificationService { | ||
|
||
private final SMSUserVerificationService smsUserVerificationService; | ||
|
||
private final DiscordUserVerificationService discordUserVerificationService; | ||
|
||
@Override | ||
@Transactional(readOnly = true) | ||
public Either<Problem, IsVerifiedResponse> isVerified(IsVerifiedRequest isVerifiedRequest) { | ||
log.info("Received isVerified request: {}", isVerifiedRequest); | ||
|
||
CompletableFuture<Either<Problem, IsVerifiedResponse>> smsVerificationFuture = CompletableFuture.supplyAsync(() -> { | ||
return smsUserVerificationService.isVerified(isVerifiedRequest); | ||
}); | ||
|
||
CompletableFuture<Either<Problem, IsVerifiedResponse>> discordVerificationFuture = CompletableFuture.supplyAsync(() -> { | ||
return discordUserVerificationService.isVerifiedBasedOnStakeAddress(isVerifiedRequest); | ||
}); | ||
|
||
var allFutures = CompletableFutures.anyResultsOf(List.of(smsVerificationFuture, discordVerificationFuture)); | ||
|
||
List<Either<Problem, IsVerifiedResponse>> allResponses = allFutures.orTimeout(30, SECONDS) | ||
.join(); | ||
|
||
var successCount = allResponses.stream().filter(Either::isRight).count(); | ||
|
||
if (successCount != 2) { | ||
var problem = allResponses.stream().filter(Either::isLeft).findFirst().orElseThrow().getLeft(); | ||
|
||
return Either.left(problem); | ||
} | ||
|
||
var successes = allResponses.stream().filter(Either::isRight).toList().stream().map(Either::get).toList(); | ||
|
||
var isVerified = successes.stream().reduce((a, b) -> { | ||
return new IsVerifiedResponse(a.isVerified() || b.isVerified()); | ||
}).orElse(new IsVerifiedResponse(false)); | ||
|
||
return Either.right(isVerified); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...e/src/main/java/org/cardano/foundation/voting/service/common/UserVerificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.cardano.foundation.voting.service.common; | ||
|
||
import io.vavr.control.Either; | ||
import org.cardano.foundation.voting.domain.IsVerifiedRequest; | ||
import org.cardano.foundation.voting.domain.IsVerifiedResponse; | ||
import org.zalando.problem.Problem; | ||
|
||
public interface UserVerificationService { | ||
|
||
Either<Problem, IsVerifiedResponse> isVerified(IsVerifiedRequest isVerifiedRequest); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters