From 0f1333ceaf12a9ce9efdad336cc54a69cb510925 Mon Sep 17 00:00:00 2001 From: Aleksandr Muravja Date: Fri, 17 Feb 2023 11:04:00 +0200 Subject: [PATCH] Confirm Account page is ready (#998) Signed-off-by: Aleksandr Muravja --- src/main/java/pm/axe/db/dao/AccountDao.java | 9 +++++ .../pm/axe/services/user/AccountService.java | 11 ++++++ .../pm/axe/services/user/TokenService.java | 8 +++-- .../services/user/UserOperationsService.java | 16 +++++++++ .../axe/ui/pages/user/ConfirmAccountPage.java | 36 +++++++++++++++---- .../pages/user/profile/tabs/ProfileTab.java | 12 ++++++- 6 files changed, 82 insertions(+), 10 deletions(-) diff --git a/src/main/java/pm/axe/db/dao/AccountDao.java b/src/main/java/pm/axe/db/dao/AccountDao.java index 4f900536c..d25408fde 100644 --- a/src/main/java/pm/axe/db/dao/AccountDao.java +++ b/src/main/java/pm/axe/db/dao/AccountDao.java @@ -21,6 +21,15 @@ public interface AccountDao extends CrudRepository { */ Optional 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}. * diff --git a/src/main/java/pm/axe/services/user/AccountService.java b/src/main/java/pm/axe/services/user/AccountService.java index a5e94a819..68342cfd3 100644 --- a/src/main/java/pm/axe/services/user/AccountService.java +++ b/src/main/java/pm/axe/services/user/AccountService.java @@ -209,6 +209,17 @@ public Optional 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}. * diff --git a/src/main/java/pm/axe/services/user/TokenService.java b/src/main/java/pm/axe/services/user/TokenService.java index cf3720f88..81a57c8cf 100644 --- a/src/main/java/pm/axe/services/user/TokenService.java +++ b/src/main/java/pm/axe/services/user/TokenService.java @@ -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; /** @@ -174,7 +172,13 @@ public Optional getToken(final String tokenString) { return token.isPresent() ? returnOnlyValidToken(token.get()) : Optional.empty(); } + public Optional 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. diff --git a/src/main/java/pm/axe/services/user/UserOperationsService.java b/src/main/java/pm/axe/services/user/UserOperationsService.java index f65c10775..b615e2c02 100644 --- a/src/main/java/pm/axe/services/user/UserOperationsService.java +++ b/src/main/java/pm/axe/services/user/UserOperationsService.java @@ -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 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); + } } diff --git a/src/main/java/pm/axe/ui/pages/user/ConfirmAccountPage.java b/src/main/java/pm/axe/ui/pages/user/ConfirmAccountPage.java index 92d4f5128..d54ef6e78 100644 --- a/src/main/java/pm/axe/ui/pages/user/ConfirmAccountPage.java +++ b/src/main/java/pm/axe/ui/pages/user/ConfirmAccountPage.java @@ -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; @@ -83,7 +85,7 @@ 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); @@ -91,17 +93,22 @@ private void initPage() { 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() { @@ -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 e) { if (!e.isFromClient()) return; final String email = e.getValue().trim(); diff --git a/src/main/java/pm/axe/ui/pages/user/profile/tabs/ProfileTab.java b/src/main/java/pm/axe/ui/pages/user/profile/tabs/ProfileTab.java index fa36a2a7c..2576dd6d5 100644 --- a/src/main/java/pm/axe/ui/pages/user/profile/tabs/ProfileTab.java +++ b/src/main/java/pm/axe/ui/pages/user/profile/tabs/ProfileTab.java @@ -447,6 +447,16 @@ private Optional getTelegramToken() { private void deleteEmail() { Optional 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 + } + } + } }