Skip to content

Commit

Permalink
fix: nullable things
Browse files Browse the repository at this point in the history
  • Loading branch information
oproprioleonardo committed Sep 25, 2024
1 parent e71f7ed commit 0739c9d
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
import br.com.ifsp.tickets.domain.event.IEventGateway;
import br.com.ifsp.tickets.domain.shared.file.IFileStorage;
import br.com.ifsp.tickets.domain.ticket.ITicketGateway;
import br.com.ifsp.tickets.domain.user.IUserGateway;

public class EnrollmentServiceFactory {
private static EnrollmentService enrollmentService;

public static EnrollmentService create(
IEmailGateway emailGateway,
IUserGateway userGateway,
IMessageGateway messageGateway,
IEventGateway eventGateway,
IEnrollmentGateway enrollmentGateway,
Expand All @@ -30,7 +28,6 @@ public static EnrollmentService create(
if (enrollmentService == null) {
final ICreateEnrollmentUseCase createEnrollmentUseCase = new CreateEnrollmentUseCase(
eventGateway,
userGateway,
enrollmentGateway,
ticketGateway,
messageGateway,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
import br.com.ifsp.tickets.domain.shared.validation.handler.Notification;
import br.com.ifsp.tickets.domain.ticket.ITicketGateway;
import br.com.ifsp.tickets.domain.ticket.Ticket;
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 java.time.LocalDate;

public class CreateEnrollmentUseCase implements ICreateEnrollmentUseCase {

private final IEventGateway eventGateway;
private final IUserGateway userGateway;
private final IEnrollmentGateway enrollmentGateway;
private final ITicketGateway ticketGateway;
private final IMessageGateway messageGateway;
Expand All @@ -36,9 +35,8 @@ public class CreateEnrollmentUseCase implements ICreateEnrollmentUseCase {
private final IFileStorage fileProvider;
private final ITicketQRGenerator ticketGenerator;

public CreateEnrollmentUseCase(IEventGateway eventGateway, IUserGateway userGateway, IEnrollmentGateway enrollmentGateway, ITicketGateway ticketGateway, IMessageGateway messageGateway, ICompanyGateway companyGateway, IEmailGateway emailGateway, IFileStorage fileProvider, ITicketQRGenerator ticketGenerator) {
public CreateEnrollmentUseCase(IEventGateway eventGateway, IEnrollmentGateway enrollmentGateway, ITicketGateway ticketGateway, IMessageGateway messageGateway, ICompanyGateway companyGateway, IEmailGateway emailGateway, IFileStorage fileProvider, ITicketQRGenerator ticketGenerator) {
this.eventGateway = eventGateway;
this.userGateway = userGateway;
this.enrollmentGateway = enrollmentGateway;
this.ticketGateway = ticketGateway;
this.messageGateway = messageGateway;
Expand All @@ -60,23 +58,25 @@ public CreateEnrollmentOutput execute(CreateEnrollmentInput anIn) {
final Event event = this.eventGateway.findById(eventID).orElseThrow(() -> NotFoundException.with(Event.class, eventID));
final Company company = this.companyGateway.findById(event.getCompanyID()).orElseThrow(() -> NotFoundException.with(Company.class, event.getCompanyID()));

if(user != null){
if (user != null) {
name = user.getName();
emailString = String.valueOf(user.getEmail());
emailString = user.getEmail().getValue();
birthDate = user.getBirthDate();
document = String.valueOf(user.getCpf());
document = user.getCpf().getValue();
alreadyExists = this.enrollmentGateway.existsByUserIDAndEventID(user.getId(), eventID);
}else alreadyExists = this.enrollmentGateway.existsByDocumentAndEventID(document, eventID);
} else alreadyExists = this.enrollmentGateway.existsByDocumentAndEventID(document, eventID);

if (alreadyExists) {
Notification.create("Validation Error").append("User already enrolled in this event").throwPossibleErrors();
}
final UserID userID = user != null ? user.getId() : new UserID(null);

final Enrollment enrollment = Enrollment
.newEnrollment(name, emailString, document, birthDate,
user != null ? user.getId() : null, event.getId());
userID, event.getId());

final LocalDate expiredIn = event.getEndDate().plusDays(1);
final Ticket ticket = Ticket.newTicket(user.getId(), document, event, "Ingresso sem limite de acompanhantes", event.getInitDate(), expiredIn);
final Ticket ticket = Ticket.newTicket(userID, document, event, "Ingresso sem limite de acompanhantes", event.getInitDate(), expiredIn);
final Message message = this.messageGateway.findBySubjectAndType(MessageSubject.EVENT_TICKET, MessageType.HTML).orElseThrow(() -> NotFoundException.with("Email template not found"));
final Notification notification = Notification.create();
enrollment.validate(notification);
Expand All @@ -85,7 +85,7 @@ public CreateEnrollmentOutput execute(CreateEnrollmentInput anIn) {
final Enrollment createdEnrollment = this.enrollmentGateway.create(enrollment);
final Ticket createdTicket = this.ticketGateway.create(ticket);

final Email email = Email.createDynamic(user.getEmail().toString(), message, user.getName(), company.getName());
final Email email = Email.createDynamic(emailString, message, name, company.getName());
email.appendAttachment("qr-code.png", ticketGenerator.generateQRCodeToBase64(createdTicket.getId()), fileProvider);
email.validate(notification);
notification.throwPossibleErrors();
Expand Down
27 changes: 20 additions & 7 deletions database/tabelas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,27 @@ create table email_attachments
attachments varchar(255)
);

create table enrollments
CREATE TABLE enrollments
(
created_at timestamp(6) not null,
id uuid NOT NULL PRIMARY KEY,
event_id uuid NOT NULL,
name varchar(255) NOT NULL,
email varchar(255) NOT NULL,
birth_date date NOT NULL,
document varchar(255) NOT NULL,
status varchar(255) NOT NULL,
created_at timestamp(6) NOT NULL,
updated_at timestamp(6),
event_id uuid not null,
id uuid not null
primary key,
user_id uuid not null,
status varchar(255) not null
user_id uuid
);

ALTER TABLE enrollments
ADD COLUMN name varchar(255),
ADD COLUMN email varchar(255),
ADD COLUMN birth_date date,
ADD COLUMN document varchar(255);


create table events_thumbnails
(
id uuid
Expand Down Expand Up @@ -129,6 +139,9 @@ create table tickets
status varchar(255) not null
);

ALTER TABLE tickets
ADD COLUMN document varchar(255);

create table users
(
active boolean not null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

@Getter
public class Enrollment extends Entity<EnrollmentID> {
private final Optional<UserID> userID;

private final UserID userID;
private final String name;
private final String email;
private final LocalDate birthDate;
Expand All @@ -22,7 +23,7 @@ public class Enrollment extends Entity<EnrollmentID> {
private EnrollmentStatus status;
private LocalDateTime updatedAt;

public Enrollment(EnrollmentID enrollmentID, String name, String email, LocalDate birthDate, String document, EventID eventID, EnrollmentStatus status, LocalDateTime createdAt, LocalDateTime updatedAt, Optional<UserID> userID) {
public Enrollment(EnrollmentID enrollmentID, String name, String email, LocalDate birthDate, String document, EventID eventID, EnrollmentStatus status, LocalDateTime createdAt, LocalDateTime updatedAt, UserID userID) {
super(enrollmentID);
this.userID = userID;
this.name = name;
Expand All @@ -36,11 +37,15 @@ public Enrollment(EnrollmentID enrollmentID, String name, String email, LocalDat
}

public static Enrollment with(EnrollmentID enrollmentID, String name, String email, LocalDate birthDate, String document, EventID eventID, EnrollmentStatus status, LocalDateTime createdAt, LocalDateTime updatedAt, UserID userID) {
return new Enrollment(enrollmentID, name, email, birthDate, document, eventID, status, createdAt, updatedAt, Optional.ofNullable(userID));
return new Enrollment(enrollmentID, name, email, birthDate, document, eventID, status, createdAt, updatedAt, userID);
}

public static Enrollment newEnrollment(String name, String email, String document, LocalDate birthDate, UserID userID, EventID eventID) {
return new Enrollment(EnrollmentID.unique(), name, email, birthDate, document, eventID, EnrollmentStatus.CONFIRMED, LocalDateTime.now(), null, Optional.ofNullable(userID));
return new Enrollment(EnrollmentID.unique(), name, email, birthDate, document, eventID, EnrollmentStatus.CONFIRMED, LocalDateTime.now(), null, userID);
}

public Optional<UserID> getUserID() {
return Optional.ofNullable(userID);
}

public void confirm() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Optional;

@Getter
public class Ticket extends Entity<TicketID> {

private final Optional<UserID> userID;
private final UserID userID;
private final String document;
private final EventID eventID;
private final String description;
Expand All @@ -31,7 +30,7 @@ public class Ticket extends Entity<TicketID> {
private TicketCode code;
private LocalDateTime lastTimeConsumed;

public Ticket(TicketID ticketID, String document, EventID eventID, String description, TicketStatus status, TicketCode code, LocalDate validIn, LocalDate expiredIn, LocalDateTime createdAt, LocalDateTime lastTimeConsumed, Optional<UserID> userID) {
public Ticket(TicketID ticketID, String document, EventID eventID, String description, TicketStatus status, TicketCode code, LocalDate validIn, LocalDate expiredIn, LocalDateTime createdAt, LocalDateTime lastTimeConsumed, UserID userID) {
super(ticketID);
this.userID = userID;
this.document = document;
Expand All @@ -45,12 +44,16 @@ public Ticket(TicketID ticketID, String document, EventID eventID, String descri
this.lastTimeConsumed = lastTimeConsumed;
}

public static Ticket with(TicketID ticketID, String document,EventID eventID, String description, TicketStatus status, TicketCode code, LocalDate validIn, LocalDate expiredIn, LocalDateTime createdAt, LocalDateTime lastTimeConsumed, UserID userID) {
return new Ticket(ticketID, document, eventID, description, status, code, validIn, expiredIn, createdAt, lastTimeConsumed, Optional.ofNullable(userID));
public static Ticket with(TicketID ticketID, String document, EventID eventID, String description, TicketStatus status, TicketCode code, LocalDate validIn, LocalDate expiredIn, LocalDateTime createdAt, LocalDateTime lastTimeConsumed, UserID userID) {
return new Ticket(ticketID, document, eventID, description, status, code, validIn, expiredIn, createdAt, lastTimeConsumed, userID);
}

public static Ticket newTicket(UserID userID, String document, Event event, String description, LocalDate validIn, LocalDate expiredIn) {
return new Ticket(TicketID.unique(), document, event.getId(), description, TicketStatus.AVAILABLE, TicketCode.generate(), validIn, expiredIn, LocalDateTime.now(ZoneId.of("GMT-3")), null, Optional.ofNullable(userID));
return new Ticket(TicketID.unique(), document, event.getId(), description, TicketStatus.AVAILABLE, TicketCode.generate(), validIn, expiredIn, LocalDateTime.now(ZoneId.of("GMT-3")), null, userID);
}

public Optional<UserID> getUserID() {
return Optional.ofNullable(this.userID);
}

public void generateNewCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class UserID extends Identifier<UUID> {

private final UUID value;

protected UserID(UUID value) {
public UserID(UUID value) {
this.value = value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import br.com.ifsp.tickets.domain.event.IEventGateway;
import br.com.ifsp.tickets.domain.shared.file.IFileStorage;
import br.com.ifsp.tickets.domain.ticket.ITicketGateway;
import br.com.ifsp.tickets.domain.user.IUserGateway;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
Expand All @@ -22,7 +21,6 @@
public class EnrollmentConfig {

private final IEmailGateway emailGateway;
private final IUserGateway userGateway;
private final IMessageGateway messageGateway;
private final IEventGateway eventGateway;
private final IEnrollmentGateway enrollmentGateway;
Expand All @@ -33,6 +31,6 @@ public class EnrollmentConfig {

@Bean
public EnrollmentService enrollmentService() {
return EnrollmentServiceFactory.create(emailGateway, userGateway, messageGateway, eventGateway, enrollmentGateway, ticketGateway, companyGateway, fileProvider, ticketQRGenerator);
return EnrollmentServiceFactory.create(emailGateway, messageGateway, eventGateway, enrollmentGateway, ticketGateway, companyGateway, fileProvider, ticketQRGenerator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import br.com.ifsp.tickets.domain.enrollment.EnrollmentStatus;
import br.com.ifsp.tickets.domain.event.EventID;
import br.com.ifsp.tickets.domain.user.UserID;
import br.com.ifsp.tickets.infra.shared.encryption.EncryptionService;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
Expand Down Expand Up @@ -49,7 +50,7 @@ public EnrollmentJpaEntity(UUID id, String name, String email, String document,
this.id = id;
this.name = name;
this.email = email;
this.document = document;
this.document = EncryptionService.encrypt(document);
this.birthDate = birthDate;
this.userID = userID;
this.eventID = eventID;
Expand All @@ -69,7 +70,7 @@ public static EnrollmentJpaEntity from(Enrollment enrollment) {
enrollment.getStatus().name(),
enrollment.getCreatedAt(),
enrollment.getUpdatedAt(),
enrollment.getUserID().orElse(null).getValue()
enrollment.getUserID().isPresent() ? enrollment.getUserID().get().getValue() : null
);
}

Expand All @@ -79,12 +80,16 @@ public Enrollment toAggregate() {
this.name,
this.email,
this.birthDate,
this.document,
this.getDecryptedDocument(),
EventID.with(this.eventID),
EnrollmentStatus.valueOf(this.status),
this.createdAt,
this.updatedAt,
UserID.with(this.userID)
);
}

public String getDecryptedDocument() {
return EncryptionService.decrypt(this.document);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import br.com.ifsp.tickets.domain.ticket.TicketStatus;
import br.com.ifsp.tickets.domain.ticket.vo.TicketCode;
import br.com.ifsp.tickets.domain.user.UserID;
import br.com.ifsp.tickets.infra.shared.encryption.EncryptionService;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class TicketJpaEntity implements Serializable {

public TicketJpaEntity(UUID id, String document, UUID userId, UUID eventId, String description, String status, String code, LocalDate validIn, LocalDate expiredIn, LocalDateTime createdAt, LocalDateTime lastTimeConsumed) {
this.id = id;
this.document = document;
this.document = EncryptionService.encrypt(document);
this.userId = userId;
this.eventId = eventId;
this.description = description;
Expand All @@ -66,7 +67,7 @@ public static TicketJpaEntity from(Ticket ticket) {
return new TicketJpaEntity(
ticket.getId().getValue(),
ticket.getDocument(),
ticket.getUserID().orElse(null).getValue(),
ticket.getUserID().isPresent() ? ticket.getUserID().get().getValue() : null,
ticket.getEventID().getValue(),
ticket.getDescription(),
ticket.getStatus().name(),
Expand All @@ -81,7 +82,7 @@ public static TicketJpaEntity from(Ticket ticket) {
public Ticket toAggregate() {
return Ticket.with(
TicketID.with(this.id),
this.getDocument(),
this.getDecryptedDocument(),
EventID.with(this.eventId),
this.description,
TicketStatus.valueOf(this.status),
Expand All @@ -93,4 +94,8 @@ public Ticket toAggregate() {
UserID.with(this.userId)
);
}

public String getDecryptedDocument(){
return EncryptionService.decrypt(this.document);
}
}

0 comments on commit 0739c9d

Please sign in to comment.