Skip to content

Commit

Permalink
feat: update user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
oproprioleonardo committed Oct 6, 2024
1 parent b219941 commit 8257abb
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import br.com.ifsp.tickets.app.auth.signup.ISignUpUseCase;
import br.com.ifsp.tickets.app.auth.signup.SignUpInput;
import br.com.ifsp.tickets.app.auth.signup.SignUpOutput;
import br.com.ifsp.tickets.app.auth.update.IUpdateUserUseCase;
import br.com.ifsp.tickets.app.auth.update.UpdateUserInput;

public class AuthService {

Expand All @@ -24,14 +26,16 @@ public class AuthService {
private final IRecoveryUseCase recoveryUseCase;
private final IActivationUseCase activationUseCase;
private final IGetUserByIdUseCase getUserByIdUseCase;
private final IUpdateUserUseCase updateUserUseCase;

public AuthService(ISignInUseCase signInUseCase, ISignUpUseCase signUpUseCase, IRecoveryRequestUseCase recoveryRequestUseCase, IRecoveryUseCase recoveryUseCase, IActivationUseCase activationUseCase, IGetUserByIdUseCase getUserByIdUseCase) {
public AuthService(ISignInUseCase signInUseCase, ISignUpUseCase signUpUseCase, IRecoveryRequestUseCase recoveryRequestUseCase, IRecoveryUseCase recoveryUseCase, IActivationUseCase activationUseCase, IGetUserByIdUseCase getUserByIdUseCase, IUpdateUserUseCase updateUserUseCase) {
this.signInUseCase = signInUseCase;
this.signUpUseCase = signUpUseCase;
this.recoveryRequestUseCase = recoveryRequestUseCase;
this.recoveryUseCase = recoveryUseCase;
this.activationUseCase = activationUseCase;
this.getUserByIdUseCase = getUserByIdUseCase;
this.updateUserUseCase = updateUserUseCase;
}

public void accountRecovery(RecoveryInput inputData) {
Expand All @@ -50,6 +54,10 @@ public SignUpOutput register(SignUpInput inputData) {
return this.signUpUseCase.execute(inputData);
}

public UserOutput update(UpdateUserInput input) {
return this.updateUserUseCase.execute(input);
}

public void activate(ActivationInput inputData) {
this.activationUseCase.execute(inputData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import br.com.ifsp.tickets.app.auth.signin.SignInUseCase;
import br.com.ifsp.tickets.app.auth.signup.ISignUpUseCase;
import br.com.ifsp.tickets.app.auth.signup.SignUpUseCase;
import br.com.ifsp.tickets.app.auth.update.IUpdateUserUseCase;
import br.com.ifsp.tickets.app.auth.update.UpdateUserUseCase;
import br.com.ifsp.tickets.domain.communication.email.IEmailGateway;
import br.com.ifsp.tickets.domain.communication.message.IMessageGateway;
import br.com.ifsp.tickets.domain.user.IUserGateway;
Expand All @@ -36,7 +38,17 @@ public static AuthService create(IAuthManager authManager,
final IRecoveryUseCase recoveryUseCase = new RecoveryUseCase(passwordRecoveryTokenGateway, authUtils, userGateway);
final IActivationUseCase activationUseCase = new ActivationUseCase(userGateway, upsertEmailGateway);
final IGetUserByIdUseCase getUserByIdUseCase = new GetUserByIdUseCase(userGateway);
authService = new AuthService(signInUseCase, signUpUseCase, recoveryRequestUseCase, recoveryUseCase, activationUseCase, getUserByIdUseCase);
final IUpdateUserUseCase updateUserUseCase = new UpdateUserUseCase(userGateway);

authService = new AuthService(
signInUseCase,
signUpUseCase,
recoveryRequestUseCase,
recoveryUseCase,
activationUseCase,
getUserByIdUseCase,
updateUserUseCase
);
}
return authService;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package br.com.ifsp.tickets.app.auth.update;

import br.com.ifsp.tickets.app.IUseCase;
import br.com.ifsp.tickets.app.auth.get.UserOutput;

public interface IUpdateUserUseCase extends IUseCase<UpdateUserInput, UserOutput> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package br.com.ifsp.tickets.app.auth.update;

import br.com.ifsp.tickets.domain.user.User;

import java.time.LocalDate;

public record UpdateUserInput(
User author,
String id,
String name,
String bio,
LocalDate birthDate,
String document
) {

public static UpdateUserInput of(User author, String id, String name, String bio, LocalDate birthDate, String document) {
return new UpdateUserInput(author, id, name, bio, birthDate, document);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package br.com.ifsp.tickets.app.auth.update;

import br.com.ifsp.tickets.app.auth.get.UserOutput;
import br.com.ifsp.tickets.domain.shared.exceptions.IllegalResourceAccessException;
import br.com.ifsp.tickets.domain.shared.exceptions.NotFoundException;
import br.com.ifsp.tickets.domain.shared.validation.handler.Notification;
import br.com.ifsp.tickets.domain.user.IUserGateway;
import br.com.ifsp.tickets.domain.user.User;
import br.com.ifsp.tickets.domain.user.UserID;
import br.com.ifsp.tickets.domain.user.vo.RG;

import java.time.LocalDate;

public class UpdateUserUseCase implements IUpdateUserUseCase {

private final IUserGateway userGateway;

public UpdateUserUseCase(IUserGateway userGateway) {
this.userGateway = userGateway;
}

@Override
public UserOutput execute(UpdateUserInput anIn) {
final User author = anIn.author();
final UserID target = UserID.with(anIn.id());
final String name = anIn.name();
final String bio = anIn.bio();
final LocalDate birthDate = anIn.birthDate();
final RG document = new RG(anIn.document());

final Notification notification = Notification.create("Could not update aggregate User");

final User targetUser;
if (author.getId().equals(target)) targetUser = author;
else if (!author.canManageAnyUsers())
throw new IllegalResourceAccessException("User does not have permission to manage other users");
else
targetUser = this.userGateway.findById(target).orElseThrow(() -> NotFoundException.with(User.class, target));

targetUser.updateProfile(name, bio, document, birthDate);

targetUser.validate(notification);
notification.throwPossibleErrors();

return UserOutput.from(this.userGateway.update(targetUser), author.getId(), author.getRole().getPermissions());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public boolean canManageEvents() {
return this.role.getPermissions().contains(PermissionType.MANAGE_COMPANY_EVENTS);
}

public boolean canManageAnyUsers() {
return this.role.getPermissions().contains(PermissionType.MANAGE_ANY_USER);
}

public boolean canManageAnyTicket() {
return this.role.getPermissions().contains(PermissionType.MANAGE_ANY_TICKET);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import br.com.ifsp.tickets.infra.contexts.user.models.login.LoginRequest;
import br.com.ifsp.tickets.infra.contexts.user.models.login.LoginResponse;
import br.com.ifsp.tickets.infra.contexts.user.models.register.RegisterRequest;
import br.com.ifsp.tickets.infra.contexts.user.models.update.UpdateUserRequest;
import br.com.ifsp.tickets.infra.contexts.user.models.user.UserResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
Expand Down Expand Up @@ -38,6 +39,15 @@ public interface AuthAPI {
})
ResponseEntity<UserResponse> getUserById(@PathVariable String id);

@PutMapping(value = "/{id}")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User updated successfully"),
@ApiResponse(responseCode = "401", description = "Invalid credentials"),
@ApiResponse(responseCode = "403", description = "Access denied"),
@ApiResponse(responseCode = "404", description = "User not found")
})
ResponseEntity<UserResponse> updateUser(@PathVariable String id, @RequestBody UpdateUserRequest request);

@PostMapping(value = "/activate/{token}")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "User activated successfully"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import br.com.ifsp.tickets.app.auth.signin.SignInOutput;
import br.com.ifsp.tickets.app.auth.signup.SignUpInput;
import br.com.ifsp.tickets.app.auth.signup.SignUpOutput;
import br.com.ifsp.tickets.app.auth.update.UpdateUserInput;
import br.com.ifsp.tickets.infra.api.AuthAPI;
import br.com.ifsp.tickets.infra.contexts.user.contexts.recovery.models.RecoveryRequest;
import br.com.ifsp.tickets.infra.contexts.user.models.login.LoginRequest;
import br.com.ifsp.tickets.infra.contexts.user.models.login.LoginResponse;
import br.com.ifsp.tickets.infra.contexts.user.models.register.RegisterRequest;
import br.com.ifsp.tickets.infra.contexts.user.models.update.UpdateUserRequest;
import br.com.ifsp.tickets.infra.contexts.user.models.user.UserResponse;
import br.com.ifsp.tickets.infra.contexts.user.persistence.UserJpaEntity;
import br.com.ifsp.tickets.infra.contexts.user.presenters.AuthApiPresenter;
Expand Down Expand Up @@ -53,6 +55,13 @@ public ResponseEntity<UserResponse> getUserById(String id) {
return ResponseEntity.ok(AuthApiPresenter.present(this.authService.getUserById(input)));
}

@Override
public ResponseEntity<UserResponse> updateUser(String id, UpdateUserRequest request) {
final UserJpaEntity authenticatedUser = (UserJpaEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
final UpdateUserInput input = UpdateUserInput.of(authenticatedUser.toAggregate(), id, request.name(), request.bio(), request.getBirthDate(), request.document());
return ResponseEntity.ok(AuthApiPresenter.present(this.authService.update(input)));
}

@Override
public ResponseEntity<Void> activate(String token) {
final ActivationInput input = ActivationInput.of(token);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package br.com.ifsp.tickets.infra.contexts.user.models.update;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public record UpdateUserRequest(
@JsonProperty("name")
String name,
@JsonProperty("bio")
String bio,
@JsonProperty("birth_date")
String birthDate,
@JsonProperty("document")
String document
) {

public LocalDate getBirthDate() {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
return LocalDate.parse(birthDate, formatter);
}

}

0 comments on commit 8257abb

Please sign in to comment.