Skip to content

Commit

Permalink
style: code refinement
Browse files Browse the repository at this point in the history
  • Loading branch information
oproprioleonardo committed Nov 8, 2024
1 parent 58fe044 commit cf89d9d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import br.com.ifsp.tickets.app.auth.IAuthManager;
import br.com.ifsp.tickets.app.auth.IAuthUtils;
import br.com.ifsp.tickets.domain.shared.exceptions.NotFoundException;
import br.com.ifsp.tickets.domain.shared.utils.ValidationUtils;
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;
Expand All @@ -25,13 +26,11 @@ public SignInUseCase(IAuthUtils authUtils, IAuthManager authManager, IUserGatewa

@Override
public SignInOutput execute(SignInInput anIn) {
EmailAddress emailAddress = null;
try {
emailAddress = new EmailAddress(anIn.login());
} catch (Exception ignored) {
if (ValidationUtils.isValidEmail(anIn.login())) {
final EmailAddress emailAddress = new EmailAddress(anIn.login());
if (this.upsertEmailGateway.existsByEmail(emailAddress))
Notification.create("Validation error").append("Activate your account first, check your email").throwPossibleErrors();
}
if (emailAddress != null && this.upsertEmailGateway.existsByEmail(emailAddress))
Notification.create("Validation error").append("Activate your account first, check your email").throwPossibleErrors();

final User user = this.userGateway.findByUsernameOrEmail(anIn.login())
.orElseThrow(() -> NotFoundException.with("User not found with login: " + anIn.login()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public PaymentService(ICreatePreferenceUseCase createPreferenceUseCase, IGetPaym
this.createPaymentUseCase = createPaymentUseCase;
}

public CreatePreferenceOutput CreatePreference(CreatePreferenceInput anIn) {
public CreatePreferenceOutput createPreference(CreatePreferenceInput anIn) {
return this.createPreferenceUseCase.execute(anIn);
}

public CreatePaymentOutput CreatePayment(CreatePaymentInput anIn) {
public CreatePaymentOutput createPayment(CreatePaymentInput anIn) {
return this.createPaymentUseCase.execute(anIn);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import br.com.ifsp.tickets.domain.event.sale.TicketSale;
import br.com.ifsp.tickets.domain.event.sale.TicketSaleID;
import br.com.ifsp.tickets.domain.shared.exceptions.NotFoundException;
import br.com.ifsp.tickets.domain.shared.exceptions.ValidationException;
import br.com.ifsp.tickets.domain.shared.validation.handler.Notification;
import br.com.ifsp.tickets.domain.ticket.TicketID;
import br.com.ifsp.tickets.domain.user.User;
import com.mercadopago.client.common.IdentificationRequest;
import com.mercadopago.client.common.PhoneRequest;
import com.mercadopago.client.preference.*;
import com.mercadopago.resources.preference.Preference;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class CreatePreferenceUseCase implements ICreatePreferenceUseCase {

private final ITicketSaleGateway ticketSaleGateway;
Expand All @@ -24,13 +28,13 @@ public CreatePreferenceUseCase(ITicketSaleGateway ticketSaleGateway) {

@Override
public CreatePreferenceOutput execute(CreatePreferenceInput anIn) {
TicketID ticketId = TicketID.with(anIn.ticketId());
User user = anIn.user();
final TicketID ticketId = TicketID.with(anIn.ticketId());
final User user = anIn.user();
final TicketSale ticketSale = this.ticketSaleGateway.findById(TicketSaleID.with(anIn.ticket_sale_id())).orElseThrow(() -> NotFoundException.with(TicketSale.class, TicketSaleID.with(anIn.ticket_sale_id())));

PreferenceClient client = new PreferenceClient();
final PreferenceClient client = new PreferenceClient();

PreferenceItemRequest itemRequest =
final PreferenceItemRequest itemRequest =
PreferenceItemRequest.builder()
.id(ticketSale.getId().getValue().toString())
.title(ticketSale.getName())
Expand All @@ -40,15 +44,15 @@ public CreatePreferenceOutput execute(CreatePreferenceInput anIn) {
.unitPrice(ticketSale.getPrice())
.build();

List<PreferenceItemRequest> items = new ArrayList<>();
final List<PreferenceItemRequest> items = new ArrayList<>();
items.add(itemRequest);

List<PreferencePaymentTypeRequest> excludedPaymentTypes = new ArrayList<>();
final List<PreferencePaymentTypeRequest> excludedPaymentTypes = new ArrayList<>();
excludedPaymentTypes.add(PreferencePaymentTypeRequest.builder().id("ticket").build());
excludedPaymentTypes.add(PreferencePaymentTypeRequest.builder().id("credit_card").build());
excludedPaymentTypes.add(PreferencePaymentTypeRequest.builder().id("debit_card").build());

PreferenceRequest preferenceRequest = PreferenceRequest.builder()
final PreferenceRequest preferenceRequest = PreferenceRequest.builder()
.expires(false)
.items(items)
.payer(
Expand All @@ -70,11 +74,12 @@ public CreatePreferenceOutput execute(CreatePreferenceInput anIn) {
.build())
.build();

Preference preference = null;
final Preference preference;
try {
preference = client.create(preferenceRequest);
} catch (Exception e) {
e.printStackTrace();
log.error("Error creating preference", e);
throw new ValidationException("Error creating preference", Notification.create(e.getMessage()));
}
return new CreatePreferenceOutput(preference.getInitPoint(), ticketId.getValue().toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@

public class ValidationUtils {


public static boolean isValidMobilePhoneNumber(String s) {
s = s.replaceAll("[^0-9]", "");
return s.length() == 11;
}

public static boolean isValidPhoneNumber(String s) {
s = s.replaceAll("[^0-9]", "");
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]", "");
Expand Down Expand Up @@ -128,12 +117,9 @@ public static boolean isCNPJ(String CNPJ) {
}

public static boolean isValidEmail(String email) {
if (email == null || email.isBlank()) return false;
final Pattern pattern = Pattern.compile("^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
return pattern.matcher(email).matches();
}

public static boolean isValidDocument(String document) {
return isCPF(document) || isCNPJ(document);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ResponseEntity<String> createUpsertEnrollment(CreateUpsertEnrollmentReque
final UserJpaEntity authenticatedUser = (UserJpaEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

final CreatePreferenceInput input = CreatePreferenceInput.of(authenticatedUser.toAggregate(), request.ticketSaleId());
CreatePreferenceOutput preferenceOutput = paymentService.CreatePreference(input);
final CreatePreferenceOutput preferenceOutput = paymentService.createPreference(input);

final CreateUpsertEnrollmentInput in = CreateUpsertEnrollmentInput.of(
authenticatedUser.toAggregate(),
Expand All @@ -90,7 +90,7 @@ public ResponseEntity<Void> webhook(CreatePaymentRequest request) {
if (!p.status().equalsIgnoreCase("approved"))
return ResponseEntity.ok().build();

GetUpsertEnrollmentOutput output = this.enrollmentService.getUpsertEnrollment(in);
final GetUpsertEnrollmentOutput output = this.enrollmentService.getUpsertEnrollment(in);
final CreateEnrollmentInput input = CreateEnrollmentInput.of(
output.userID(),
output.name(),
Expand All @@ -101,7 +101,8 @@ public ResponseEntity<Void> webhook(CreatePaymentRequest request) {
output.ticketSaleId(),
output.ticketID()
);
CreateEnrollmentOutput out = this.enrollmentService.create(input);

this.enrollmentService.create(input);
return ResponseEntity.ok().build();
}
}

0 comments on commit cf89d9d

Please sign in to comment.