Skip to content

Commit

Permalink
refactor: change from CPF to RG
Browse files Browse the repository at this point in the history
  • Loading branch information
oproprioleonardo committed Oct 6, 2024
1 parent c473cc3 commit b219941
Show file tree
Hide file tree
Showing 26 changed files with 165 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public UserOutput execute(GetUserByIdInput anIn) {
final UserID userID = UserID.with(anIn.id());
final User user = this.userGateway.findById(userID).orElseThrow(() -> NotFoundException.with(User.class, userID));
final User authenticatedUser = anIn.authenticatedUser();
return UserOutput.from(user, authenticatedUser.getRole().getPermissions());
return UserOutput.from(user, userID, authenticatedUser.getRole().getPermissions());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package br.com.ifsp.tickets.app.auth.get;

import br.com.ifsp.tickets.domain.user.User;
import br.com.ifsp.tickets.domain.user.UserID;
import br.com.ifsp.tickets.domain.user.vo.role.PermissionType;
import br.com.ifsp.tickets.domain.user.vo.role.Role;

Expand All @@ -15,12 +16,12 @@ public record UserOutput(
String username,
RoleOutputData role,
LocalDate birthDate,
String cpfInitials,
String documentInitials,
String phoneNumberInitials,
String companyID
) {
public static UserOutput from(User user, List<PermissionType> authorities) {
if (authorities.contains(PermissionType.MANAGE_ANY_USER) || authorities.contains(PermissionType.MANAGE_COMPANY_ENROLLMENTS))
public static UserOutput from(User user, UserID applicant, List<PermissionType> authorities) {
if (authorities.contains(PermissionType.MANAGE_ANY_USER) || authorities.contains(PermissionType.MANAGE_COMPANY_ENROLLMENTS) || applicant.equals(user.getId()))
return new UserOutput(
user.getId().getValue().toString(),
user.getName(),
Expand All @@ -29,7 +30,7 @@ public static UserOutput from(User user, List<PermissionType> authorities) {
user.getUsername(),
RoleOutputData.from(user.getRole()),
user.getBirthDate(),
user.getCpf().getInitials(),
user.getDocument() == null ? null : user.getDocument().getInitials(),
user.getPhoneNumber().getInitials(),
user.getCompanyID().getValue() == null ? null : user.getCompanyID().getValue().toString()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public record UserOutputData(
String username,
RoleOutputData role,
LocalDate birthDate,
String cpfInitials,
String documentInitials,
String phoneNumberInitials,
String companyID
) {
Expand All @@ -38,7 +38,7 @@ public static UserOutputData from(User user) {
user.getUsername(),
RoleOutputData.from(user.getRole()),
user.getBirthDate(),
user.getCpf().getInitials(),
user.getDocument() == null ? null : user.getDocument().getInitials(),
user.getPhoneNumber().getInitials(),
user.getCompanyID().getValue() == null ? null : user.getCompanyID().getValue().toString()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public record SignUpInput(
String username,
String password,
LocalDate birthDate,
String cpf,
String document,
String phoneNumber
) {

Expand All @@ -28,14 +28,14 @@ public record SignUpInput(
if (birthDate == null)
throw new IllegalArgumentException("field 'birth_date' cannot be null");

if (cpf == null || cpf.isBlank())
throw new IllegalArgumentException("field 'cpf' cannot be null or empty");
if (document == null || document.isBlank())
throw new IllegalArgumentException("field 'document' cannot be null or empty");

if (phoneNumber == null || phoneNumber.isBlank())
throw new IllegalArgumentException("field 'phone_number' cannot be null or empty");
}

public static SignUpInput of(String name, String email, String username, String password, LocalDate birthDate, String cpf, String phoneNumber) {
return new SignUpInput(name, email, username, password, birthDate, cpf, phoneNumber);
public static SignUpInput of(String name, String email, String username, String password, LocalDate birthDate, String document, String phoneNumber) {
return new SignUpInput(name, email, username, password, birthDate, document, phoneNumber);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import br.com.ifsp.tickets.domain.user.User;
import br.com.ifsp.tickets.domain.user.email.IUpsertEmailGateway;
import br.com.ifsp.tickets.domain.user.email.UpsertEmail;
import br.com.ifsp.tickets.domain.user.vo.CPF;
import br.com.ifsp.tickets.domain.user.vo.EmailAddress;
import br.com.ifsp.tickets.domain.user.vo.PhoneNumber;
import br.com.ifsp.tickets.domain.user.vo.RG;
import br.com.ifsp.tickets.domain.user.vo.role.Role;

import java.time.LocalDate;
Expand All @@ -41,7 +41,7 @@ public SignUpUseCase(IUserGateway userGateway, IAuthUtils authUtils, IUpsertEmai
public SignUpOutput execute(SignUpInput anIn) {
final String name = anIn.name();
final String username = anIn.username();
final CPF cpf = new CPF(anIn.cpf());
final RG rg = new RG(anIn.document());
final EmailAddress emailAddress = new EmailAddress(anIn.email());
final PhoneNumber phoneNumber = new PhoneNumber(anIn.phoneNumber());
final LocalDate birthDate = anIn.birthDate();
Expand All @@ -54,7 +54,7 @@ public SignUpOutput execute(SignUpInput anIn) {
phoneNumber,
username,
passwordEncoded,
cpf,
rg,
birthDate
);
user.changeEmail(emailAddress);
Expand All @@ -65,7 +65,7 @@ public SignUpOutput execute(SignUpInput anIn) {
notification
.throwPossibleErrors()
.uniqueness(() -> this.userGateway.existsByUsername(username), "username")
.uniqueness(() -> this.userGateway.existsByEncryptedCPF(cpf), "cpf")
.uniqueness(() -> this.userGateway.existsByEncryptedDocument(rg), "document")
.uniqueness(() -> this.userGateway.existsByEmail(emailAddress), "email")
.uniqueness(() -> this.upsertEmailGateway.existsByEmail(emailAddress), "email")
.throwPossibleErrors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public CreateEnrollmentOutput execute(CreateEnrollmentInput anIn) {
name = user.getName();
emailString = user.getEmail().getValue();
birthDate = user.getBirthDate();
document = user.getCpf().getValue();
document = user.getDocument().getValue();
alreadyExists = this.enrollmentGateway.existsByUserIDAndEventID(user.getId(), eventID);
} else alreadyExists = this.enrollmentGateway.existsByDocumentAndEventID(document, eventID);

Expand Down
4 changes: 2 additions & 2 deletions database/inserir_padroes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ DECLARE
id_company uuid;
id_event uuid;
BEGIN
INSERT INTO users (id, name, role_id, username, email, phone, password, cpf, birth_date, active)
INSERT INTO users (id, name, role_id, username, email, phone, password, document, birth_date, active)
VALUES (uuid_generate_v4(),
'Leonardo',
2,
'c_manager',
'l.6042silva@gmail.com',
'11999999999',
'$2y$10$C2LFrm1SlAx8H13NsZDns.DOj4eHVBz4mfYVwI.MaGKD6/IKAZItG',
'63640027060',
'219555783',
'1999-04-02',
true)
RETURNING id INTO id_usuario;
Expand Down
2 changes: 1 addition & 1 deletion database/tabelas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ create table users
id uuid not null
primary key,
bio varchar(255),
cpf varchar(255) not null
document varchar(255)
unique,
email varchar(255)
unique,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package br.com.ifsp.tickets.domain.shared.exceptions;

public class IllegalDocumentException extends DomainException {

public IllegalDocumentException(String cnpj) {
super("Document '%s' is not valid".formatted(cnpj));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ public static boolean isValidPhoneNumber(String s) {
return s.length() == 10;
}

public static boolean isRG(String rg) {
// Remove todos os caracteres não numéricos (pontos, hífens, espaços)
final String cleanRG = rg.replaceAll("[^\\dXx]", "");

// Verifica se o tamanho do RG é válido (8 dígitos + dígito verificador opcional)
if (cleanRG.length() < 8 || cleanRG.length() > 9) {
return false;
}

return Pattern.matches("^[0-9]{7,9}[0-9Xx]?$", cleanRG);
}

public static boolean isCPF(String CPF) {
CPF = CPF.replaceAll("[^0-9]", "");
// considera-se erro CPF's formados por uma sequencia de numeros iguais
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import br.com.ifsp.tickets.domain.shared.search.AdvancedSearchQuery;
import br.com.ifsp.tickets.domain.shared.search.Pagination;
import br.com.ifsp.tickets.domain.user.vo.CPF;
import br.com.ifsp.tickets.domain.user.vo.Document;
import br.com.ifsp.tickets.domain.user.vo.EmailAddress;
import br.com.ifsp.tickets.domain.user.vo.PhoneNumber;

Expand All @@ -18,7 +18,7 @@ public interface IUserGateway {

Optional<User> findByUsernameOrEmail(String login);

Optional<User> findByCPF(CPF cpf);
Optional<User> findByDocument(Document document);

Optional<User> findById(UserID id);

Expand All @@ -36,6 +36,6 @@ public interface IUserGateway {

boolean existsByPhoneNumber(PhoneNumber phoneNumber);

boolean existsByEncryptedCPF(CPF cpf);
boolean existsByEncryptedDocument(Document document);

}
20 changes: 10 additions & 10 deletions domain/src/main/java/br/com/ifsp/tickets/domain/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import br.com.ifsp.tickets.domain.shared.AggregateRoot;
import br.com.ifsp.tickets.domain.shared.exceptions.IllegalEntityIdException;
import br.com.ifsp.tickets.domain.shared.validation.IValidationHandler;
import br.com.ifsp.tickets.domain.user.vo.CPF;
import br.com.ifsp.tickets.domain.user.vo.Document;
import br.com.ifsp.tickets.domain.user.vo.EmailAddress;
import br.com.ifsp.tickets.domain.user.vo.PhoneNumber;
import br.com.ifsp.tickets.domain.user.vo.role.PermissionType;
Expand All @@ -26,13 +26,13 @@ public class User extends AggregateRoot<UserID> {
private PhoneNumber phoneNumber;
private String username;
private String password;
private CPF cpf;
private Document document;
private LocalDate birthDate;
private LocalDate passwordDate;
private boolean active;
private CompanyID companyID;

protected User(UserID userID, String name, Role role, String bio, EmailAddress email, PhoneNumber phoneNumber, String username, String password, CPF cpf, LocalDate birthDate, LocalDate passwordDate, boolean active, CompanyID companyID) {
protected User(UserID userID, String name, Role role, String bio, EmailAddress email, PhoneNumber phoneNumber, String username, String password, Document document, LocalDate birthDate, LocalDate passwordDate, boolean active, CompanyID companyID) {
super(userID);
this.name = name;
this.role = role;
Expand All @@ -41,25 +41,25 @@ protected User(UserID userID, String name, Role role, String bio, EmailAddress e
this.phoneNumber = phoneNumber;
this.username = username;
this.password = password;
this.cpf = cpf;
this.document = document;
this.birthDate = birthDate;
this.passwordDate = passwordDate;
this.active = active;
this.companyID = companyID == null ? new CompanyID(null) : companyID;
}

public static User with(UserID userID, String name, Role role, String bio, EmailAddress email, PhoneNumber phoneNumber, String username, String password, CPF cpf, LocalDate birthDate, LocalDate passwordDate, boolean active, CompanyID companyID) {
return new User(userID, name, role, bio, email, phoneNumber, username, password, cpf, birthDate, passwordDate, active, companyID);
public static User with(UserID userID, String name, Role role, String bio, EmailAddress email, PhoneNumber phoneNumber, String username, String password, Document document, LocalDate birthDate, LocalDate passwordDate, boolean active, CompanyID companyID) {
return new User(userID, name, role, bio, email, phoneNumber, username, password, document, birthDate, passwordDate, active, companyID);
}

public static User create(String name, Role role, PhoneNumber phoneNumber, String username, String password, CPF cpf, LocalDate birthDate) {
return new User(UserID.unique(), name, role, null, new EmailAddress(null), phoneNumber, username, password, cpf, birthDate, LocalDate.now(), true, null);
public static User create(String name, Role role, PhoneNumber phoneNumber, String username, String password, Document document, LocalDate birthDate) {
return new User(UserID.unique(), name, role, null, new EmailAddress(null), phoneNumber, username, password, document, birthDate, LocalDate.now(), true, null);
}

public void updateProfile(String name, String bio, CPF cpf, LocalDate birthDate) {
public void updateProfile(String name, String bio, Document document, LocalDate birthDate) {
this.name = name;
this.bio = bio;
this.cpf = cpf;
this.document = document;
this.birthDate = birthDate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void validate() {
this.validateBio();
this.validateUsername();
this.validatePhoneNumber();
this.validateCPF();
this.validateDocument();
this.validateBirthDate();
this.validateRole();
}
Expand Down Expand Up @@ -63,9 +63,9 @@ private void validatePhoneNumber() {
error("Phone number is required");
}

private void validateCPF() {
if (user.getCpf() == null)
error("CPF is required");
private void validateDocument() {
if (user.getDocument() == null)
error("document is required");
}

private void validateBirthDate() {
Expand Down
22 changes: 8 additions & 14 deletions domain/src/main/java/br/com/ifsp/tickets/domain/user/vo/CPF.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
package br.com.ifsp.tickets.domain.user.vo;

import br.com.ifsp.tickets.domain.shared.ValueObject;
import br.com.ifsp.tickets.domain.shared.exceptions.IllegalCPFException;
import br.com.ifsp.tickets.domain.shared.exceptions.IllegalDocumentException;
import br.com.ifsp.tickets.domain.shared.utils.ValidationUtils;
import lombok.Getter;

@Getter
public class CPF extends ValueObject {

private final String value;
public class CPF extends Document {

public CPF(String value) {
if (value == null || value.isBlank())
throw new IllegalCPFException(value);
super(value);

if (!ValidationUtils.isCPF(value))
throw new IllegalCPFException(value);

this.value = value;
throw new IllegalDocumentException(value);
}

public String getInitials() {
return value.substring(0, this.value.length() - 5) + "*****";
return this.getValue().substring(0, this.getValue().length() - 5) + "*****";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

CPF cpf = (CPF) o;
final CPF cpf = (CPF) o;

return value.equals(cpf.value);
return this.getValue().equals(cpf.getValue());
}

@Override
public int hashCode() {
return value.hashCode();
return this.getValue().hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package br.com.ifsp.tickets.domain.user.vo;

import br.com.ifsp.tickets.domain.shared.ValueObject;
import br.com.ifsp.tickets.domain.shared.exceptions.IllegalDocumentException;
import lombok.Getter;

@Getter
public class Document extends ValueObject {

private final String value;

public Document(String value) {
if (value == null || value.isBlank())
throw new IllegalDocumentException(value);

this.value = value;
}

public String getInitials() {
return this.value.substring(0, this.value.length() - 5) + "*****";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

final Document doc = (Document) o;

return this.value.equals(doc.value);
}

@Override
public int hashCode() {
return this.value.hashCode();
}
}
Loading

0 comments on commit b219941

Please sign in to comment.