Skip to content

Commit

Permalink
Confirm Account page is ready (#998)
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Muravja <[email protected]>
  • Loading branch information
kyberorg committed Feb 17, 2023
1 parent 159c05e commit 0f1333c
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/main/java/pm/axe/db/dao/AccountDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ public interface AccountDao extends CrudRepository<Account, Long> {
*/
Optional<Account> findByUserAndType(User user, AccountType accountType);

/**
* Searching for User's Account by its {@link AccountType}.
*
* @param user account's owner
* @param accountType type of account
* @return true - if found or false - if not.
*/
boolean existsByUserAndType(User user, AccountType accountType);

/**
* Lists all {@link Account}s of given {@link AccountType}.
*
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/pm/axe/services/user/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ public Optional<Account> getAccount(final User user, final AccountType accountTy
return accountDao.findByUserAndType(user, accountType);
}

/**
* Checks if Account with given {@link User} and {@link AccountType} exists.
*
* @param user account's owner
* @param accountType account's type
* @return true if exists, false if not.
*/
public boolean isAccountExist(final User user, final AccountType accountType) {
return accountDao.existsByUserAndType(user, accountType);
}

/**
* Searches {@link Account} by plain-text {@link Account} name and {@link AccountType}.
*
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/pm/axe/services/user/TokenService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
public class TokenService {
private static final String TAG = "[" + TokenService.class.getSimpleName() + "]";

private static final String ERR_USER_ALREADY_HAS_TOKEN = "User already has token";

private final TokenDao tokenDao;

/**
Expand Down Expand Up @@ -174,7 +172,13 @@ public Optional<Token> getToken(final String tokenString) {
return token.isPresent() ? returnOnlyValidToken(token.get()) : Optional.empty();
}

public Optional<Token> getToken(final User user, final TokenType tokenType) {
if (user == null) throw new IllegalArgumentException("user cannot be null");
if (tokenType == null) throw new IllegalArgumentException("token type cannot be null");

Token token = tokenDao.findByTokenTypeAndUser(tokenType, user);
return token != null ? returnOnlyValidToken(token) : Optional.empty();
}

/**
* Provides {@link User}'s {@link TokenType#TELEGRAM_CONFIRMATION_TOKEN} token.
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/pm/axe/services/user/UserOperationsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,20 @@ public OperationResult deleteUser(final User user, final boolean force) {
EventBus.getDefault().post(UserDeletedEvent.createWith(user));
return deletionResult;
}

public OperationResult deleteAccountOnly(final Account account) {
if (account == null) throw new IllegalArgumentException("Account cannot be null");
if (account.getUser() == null) throw new IllegalStateException("Account has no owner");

Optional<Token> accountConfirmationToken = tokenService.getToken(account.getUser(),
TokenType.ACCOUNT_CONFIRMATION_TOKEN);
if (accountConfirmationToken.isPresent()) {
//because we need to delete it in @Sync manner
OperationResult tokenDeletionResult = tokenService.deleteToken(accountConfirmationToken.get().getToken());
if (tokenDeletionResult.notOk()) {
return tokenDeletionResult;
}
}
return accountService.deleteAccount(account);
}
}
36 changes: 29 additions & 7 deletions src/main/java/pm/axe/ui/pages/user/ConfirmAccountPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.component.html.H3;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.EmailField;
Expand Down Expand Up @@ -83,25 +85,30 @@ public void beforeEnter(final BeforeEnterEvent event) {
}

private void initPage() {
hasEmail = accountService.getAccount(user, AccountType.EMAIL).isPresent();
hasEmail = accountService.isAccountExist(user, AccountType.EMAIL);

if (hasEmail) {
hasConfirmedEmail = accountService.isCurrentEmailConfirmed(user);
}

H3 title = new H3("Confirm your Account");

Component emailSectionContent = hasConfirmedEmail ?
createAccountConfirmedSection(AccountType.EMAIL) : emailSectionContent();

Section emailSection = new Section("Using Email");
emailSection.setCustomContent(emailSectionContent());
emailSection.setCustomContent(emailSectionContent);

//TG account is already confirmed
boolean hasConfirmedTelegram = accountService.isAccountExist(user, AccountType.TELEGRAM);

Component telegramSectionContent = hasConfirmedTelegram ?
createAccountConfirmedSection(AccountType.TELEGRAM) : telegramSectionContent();

telegramSection = new Section("Using Telegram");
telegramSection.setCustomContent(telegramSectionContent());
telegramSection.setCustomContent(telegramSectionContent);

add(title, emailSection, telegramSection);

if (hasConfirmedEmail) {
emailSection.setVisible(false);
}
}

private Component emailSectionContent() {
Expand Down Expand Up @@ -172,6 +179,21 @@ private Component telegramSectionContent() {
return TelegramSpan.create(tgToken);
}

private Component createAccountConfirmedSection(final AccountType accountType) {
if (accountType == null) throw new IllegalArgumentException("accountType cannot be null");
Icon successMark = VaadinIcon.CHECK.create();
successMark.setColor("green");
Span text = new Span("You have successfully confirmed your ");
Span accType = new Span(StringUtils.capitalize(accountType.name().toLowerCase()));
Span account = new Span(" account");

Span textSpan = new Span(text, accType, account);
HorizontalLayout layout = new HorizontalLayout(successMark, textSpan);
layout.setAlignItems(Alignment.CENTER);
layout.setMaxHeight("100%");
return layout;
}

private void onEmailChanged(final AbstractField.ComponentValueChangeEvent<EmailField, String> e) {
if (!e.isFromClient()) return;
final String email = e.getValue().trim();
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/pm/axe/ui/pages/user/profile/tabs/ProfileTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,16 @@ private Optional<Token> getTelegramToken() {

private void deleteEmail() {
Optional<Account> emailAccount = accountService.getAccount(user, AccountType.EMAIL);
emailAccount.ifPresent(accountService::deleteAccount);
if (emailAccount.isPresent()) {
OperationResult result = userOpsService.deleteAccountOnly(emailAccount.get());
if (result.ok()) {
AppUtils.showSuccessNotification("Email successfully deleted");
//TODO update status to NONE
} else {
ErrorUtils.showErrorNotification("Failed to delete email. System error");
//TODO update status to FAILED
}
}

}
}

0 comments on commit 0f1333c

Please sign in to comment.