Skip to content

Commit

Permalink
[240] Add the ability to delete an account
Browse files Browse the repository at this point in the history
Bug: #240
Signed-off-by: Stéphane Bégaudeau <[email protected]>
  • Loading branch information
sbegaudeau committed Sep 6, 2023
1 parent ca98e62 commit d8af1a5
Show file tree
Hide file tree
Showing 46 changed files with 1,246 additions and 262 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.svalyn.studio.application.controllers.account.dto.AccountDTO;
import com.svalyn.studio.application.controllers.account.dto.CreateAccountInput;
import com.svalyn.studio.application.controllers.account.dto.DeleteAccountInput;
import com.svalyn.studio.application.controllers.dto.IPayload;
import com.svalyn.studio.application.controllers.dto.PageInfoWithCount;
import com.svalyn.studio.application.services.account.api.IAccountService;
Expand Down Expand Up @@ -68,4 +69,9 @@ public Connection<AccountDTO> accounts(@Argument int page, @Argument int rowsPer
public IPayload createAccount(@Argument @Valid CreateAccountInput input) {
return this.accountService.createAccount(input);
}

@MutationMapping
public IPayload deleteAccount(@Argument @Valid DeleteAccountInput input) {
return this.accountService.deleteAccount(input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 Stéphane Bégaudeau.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.svalyn.studio.application.controllers.account.dto;

import com.svalyn.studio.application.controllers.dto.IInput;
import jakarta.validation.constraints.NotNull;

import java.util.UUID;

/**
* Input used to delete an account.
*
* @author sbegaudeau
*/
public record DeleteAccountInput(@NotNull UUID id, @NotNull String username) implements IInput {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2023 Stéphane Bégaudeau.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.svalyn.studio.application.listeners.activity;

import com.svalyn.studio.domain.account.events.AccountDeletedEvent;
import com.svalyn.studio.domain.activity.repositories.IActivityEntryRepository;
import com.svalyn.studio.domain.activity.repositories.IOrganizationActivityEntryRepository;
import com.svalyn.studio.domain.activity.repositories.IProjectActivityEntryRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.Objects;

/**
* Used to delete account activities.
*
* @author sbegaudeau
*/
@Service
public class ActivityCleanerOnAccountDeletedEvent {

private final IActivityEntryRepository activityEntryRepository;

private final IOrganizationActivityEntryRepository organizationActivityEntryRepository;

private final IProjectActivityEntryRepository projectActivityEntryRepository;

private final Logger logger = LoggerFactory.getLogger(ActivityCleanerOnAccountDeletedEvent.class);

public ActivityCleanerOnAccountDeletedEvent(IActivityEntryRepository activityEntryRepository, IOrganizationActivityEntryRepository organizationActivityEntryRepository, IProjectActivityEntryRepository projectActivityEntryRepository) {
this.activityEntryRepository = Objects.requireNonNull(activityEntryRepository);
this.organizationActivityEntryRepository = Objects.requireNonNull(organizationActivityEntryRepository);
this.projectActivityEntryRepository = Objects.requireNonNull(projectActivityEntryRepository);
}

@Transactional
@TransactionalEventListener
public void onAccountDeletedEvent(AccountDeletedEvent event) {
logger.info(event.toString());
this.organizationActivityEntryRepository.deleteAllByUserId(event.account().getId());
this.projectActivityEntryRepository.deleteAllByUserId(event.account().getId());
this.activityEntryRepository.deleteAllByUserId(event.account().getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
public class ActivityListener {

private final IActivityEntryRepository activityEntryRepository;

private final IOrganizationActivityEntryRepository organizationActivityEntryRepository;

private final IProjectActivityEntryRepository projectActivityEntryRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.Objects;

/**
* Used to deleted the resources used by a change.
* Used to delete the resources used by a change.
*
* @author sbegaudeau
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2023 Stéphane Bégaudeau.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.svalyn.studio.application.listeners.organization;

import com.svalyn.studio.domain.account.events.AccountDeletedEvent;
import com.svalyn.studio.domain.organization.Organization;
import com.svalyn.studio.domain.organization.repositories.IOrganizationRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.Objects;

/**
* Used to delete the organizations owned by an account.
*
* @author sbegaudeau
*/
@Service
public class OrganizationCleanerOnAccountDeletedEvent {
private final IOrganizationRepository organizationRepository;

private final Logger logger = LoggerFactory.getLogger(OrganizationCleanerOnAccountDeletedEvent.class);

public OrganizationCleanerOnAccountDeletedEvent(IOrganizationRepository organizationRepository) {
this.organizationRepository = Objects.requireNonNull(organizationRepository);
}

@Transactional
@TransactionalEventListener
public void onAccountDeletedEvent(AccountDeletedEvent event) {
logger.info(event.toString());
var organizations = this.organizationRepository.findAllOwnedBy(event.account().getId());
organizations.forEach(Organization::dispose);
this.organizationRepository.deleteAll(organizations);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import com.svalyn.studio.application.controllers.account.dto.AccountDTO;
import com.svalyn.studio.application.controllers.account.dto.CreateAccountInput;
import com.svalyn.studio.application.controllers.account.dto.CreateAccountSuccessPayload;
import com.svalyn.studio.application.controllers.account.dto.DeleteAccountInput;
import com.svalyn.studio.application.controllers.dto.ErrorPayload;
import com.svalyn.studio.application.controllers.dto.IPayload;
import com.svalyn.studio.application.controllers.dto.ProfileDTO;
import com.svalyn.studio.application.controllers.dto.SuccessPayload;
import com.svalyn.studio.application.controllers.viewer.Viewer;
import com.svalyn.studio.application.services.account.api.IAccountService;
import com.svalyn.studio.application.services.account.api.IAvatarUrlService;
Expand All @@ -33,6 +35,7 @@
import com.svalyn.studio.domain.account.Account;
import com.svalyn.studio.domain.account.repositories.IAccountRepository;
import com.svalyn.studio.domain.account.services.api.IAccountCreationService;
import com.svalyn.studio.domain.account.services.api.IAccountDeletionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
Expand All @@ -55,11 +58,14 @@ public class AccountService implements IAccountService {

private final IAccountCreationService accountCreationService;

private final IAccountDeletionService accountDeletionService;

private final IAvatarUrlService avatarUrlService;

public AccountService(IAccountRepository accountRepository, IAccountCreationService accountCreationService, IAvatarUrlService avatarUrlService) {
public AccountService(IAccountRepository accountRepository, IAccountCreationService accountCreationService, IAccountDeletionService accountDeletionService, IAvatarUrlService avatarUrlService) {
this.accountRepository = Objects.requireNonNull(accountRepository);
this.accountCreationService = accountCreationService;
this.accountCreationService = Objects.requireNonNull(accountCreationService);
this.accountDeletionService = Objects.requireNonNull(accountDeletionService);
this.avatarUrlService = Objects.requireNonNull(avatarUrlService);
}

Expand Down Expand Up @@ -112,4 +118,18 @@ public IPayload createAccount(CreateAccountInput input) {

return payload;
}

@Override
public IPayload deleteAccount(DeleteAccountInput input) {
IPayload payload = null;

var result = this.accountDeletionService.deleteAccount(input.username());
if (result instanceof Failure<Void> failure) {
payload = new ErrorPayload(input.id(), failure.message());
} else if (result instanceof Success<Void>) {
payload = new SuccessPayload(input.id());
}

return payload;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.svalyn.studio.application.controllers.account.dto.AccountDTO;
import com.svalyn.studio.application.controllers.account.dto.CreateAccountInput;
import com.svalyn.studio.application.controllers.account.dto.DeleteAccountInput;
import com.svalyn.studio.application.controllers.dto.IPayload;
import com.svalyn.studio.application.controllers.dto.ProfileDTO;
import com.svalyn.studio.application.controllers.viewer.Viewer;
Expand All @@ -43,4 +44,6 @@ public interface IAccountService {
Page<AccountDTO> findAll(int page, int rowsPerPage);

IPayload createAccount(CreateAccountInput input);

IPayload deleteAccount(DeleteAccountInput input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public IPayload deleteProject(DeleteProjectInput input) {
var result = this.projectDeletionService.deleteProject(input.projectIdentifier());
if (result instanceof Failure<Void> failure) {
payload = new ErrorPayload(input.id(), failure.message());
} else if (result instanceof Success<Void> success) {
} else if (result instanceof Success<Void>) {
payload = new SuccessPayload(input.id());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ type EnumerationLiteral {

type Mutation {
createAccount(input: CreateAccountInput!): CreateAccountPayload! @validated
deleteAccount(input: DeleteAccountInput!): DeleteAccountPayload! @validated
createAuthenticationToken(input: CreateAuthenticationTokenInput!): CreateAuthenticationTokenPayload! @validated
updateAuthenticationTokensStatus(input: UpdateAuthenticationTokensStatusInput!): UpdateAuthenticationTokensStatusPayload! @validated
createOrganization(input: CreateOrganizationInput!): CreateOrganizationPayload! @validated
Expand Down Expand Up @@ -534,6 +535,13 @@ type CreateAccountSuccessPayload {
account: Account!
}

input DeleteAccountInput {
id: ID!
username: String!
}

union DeleteAccountPayload = ErrorPayload | SuccessPayload

input CreateAuthenticationTokenInput {
id: ID!
name: String!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.svalyn.studio.domain.AbstractValidatingAggregateRoot;
import com.svalyn.studio.domain.Profile;
import com.svalyn.studio.domain.account.events.AccountCreatedEvent;
import com.svalyn.studio.domain.account.events.AccountDeletedEvent;
import com.svalyn.studio.domain.account.events.AccountModifiedEvent;
import com.svalyn.studio.domain.account.events.AuthenticationTokenCreatedEvent;
import com.svalyn.studio.domain.account.events.AuthenticationTokenModifiedEvent;
Expand Down Expand Up @@ -171,6 +172,22 @@ public void updateAuthenticationTokensStatus(List<UUID> authenticationTokenIds,
});
}

public void dispose() {
var randomId = UUID.randomUUID().toString();
this.name = "Deleted Account";
this.username = "deleted_account-" + randomId;
this.email = randomId;
this.role = AccountRole.USER;
this.image = null;
this.imageContentType = null;
this.authenticationTokens = Set.of();
this.passwordCredentials = Set.of();
this.oAuth2Metadata = Set.of();

var createdBy = ProfileProvider.get();
this.registerEvent(new AccountDeletedEvent(UUID.randomUUID(), Instant.now(), createdBy, this));
}

public static Builder newAccount() {
return new Builder();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2023 Stéphane Bégaudeau.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.svalyn.studio.domain.account.events;

import com.svalyn.studio.domain.Profile;
import com.svalyn.studio.domain.account.Account;
import jakarta.validation.constraints.NotNull;

import java.time.Instant;
import java.util.UUID;

/**
* Event fired when an account is deleted.
*
* @author sbegaudeau
*/
public record AccountDeletedEvent(
@NotNull UUID id,
@NotNull Instant createdOn,
@NotNull Profile createdBy,
@NotNull Account account) implements IAccountEvent {
}
Loading

0 comments on commit d8af1a5

Please sign in to comment.