Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
oproprioleonardo committed Sep 24, 2024
2 parents a88c9a7 + 88f9ea4 commit 5c9199e
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
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 @@ -28,6 +30,7 @@ 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 @@ -2,11 +2,17 @@

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

import java.time.LocalDate;

public record CreateEnrollmentInput(
User user,
String name,
String email,
String document,
LocalDate birthDate,
String eventId
) {
public static CreateEnrollmentInput of(User aggregate, String s) {
return new CreateEnrollmentInput(aggregate, s);
public static CreateEnrollmentInput of(User aggregate, String name, String email, String document, LocalDate birthDate, String eventId) {
return new CreateEnrollmentInput(aggregate, name, email, document, birthDate, eventId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
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 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 @@ -34,8 +36,9 @@ public class CreateEnrollmentUseCase implements ICreateEnrollmentUseCase {
private final IFileStorage fileProvider;
private final ITicketQRGenerator ticketGenerator;

public CreateEnrollmentUseCase(IEventGateway eventGateway, IEnrollmentGateway enrollmentGateway, ITicketGateway ticketGateway, IMessageGateway messageGateway, ICompanyGateway companyGateway, IEmailGateway emailGateway, IFileStorage fileProvider, ITicketQRGenerator ticketGenerator) {
public CreateEnrollmentUseCase(IEventGateway eventGateway, IUserGateway userGateway, 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 @@ -48,16 +51,32 @@ public CreateEnrollmentUseCase(IEventGateway eventGateway, IEnrollmentGateway en
@Override
public CreateEnrollmentOutput execute(CreateEnrollmentInput anIn) {
final User user = anIn.user();
String name = anIn.name();
String emailString = anIn.email();
LocalDate birthDate = anIn.birthDate();
String document = anIn.document();
final boolean alreadyExists;
final EventID eventID = EventID.with(anIn.eventId());
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 (this.enrollmentGateway.existsByUserIDAndEventID(user.getId(), eventID)) {

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

if (alreadyExists) {
Notification.create("Validation Error").append("User already enrolled in this event").throwPossibleErrors();
}
final Enrollment enrollment = Enrollment.newEnrollment(user.getId(), event.getId());
final Enrollment enrollment = Enrollment
.newEnrollment(name, emailString, document, birthDate,
user != null ? user.getId() : null, event.getId());

final LocalDate expiredIn = event.getEndDate().plusDays(1);
final Ticket ticket = Ticket.newTicket(user.getId(), event, "Ingresso sem limite de acompanhantes", event.getInitDate(), expiredIn);
final Ticket ticket = Ticket.newTicket(user.getId(), 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public record TicketOutput(
String id,
String userId,
String document,
String eventId,
String description,
LocalDate validIn,
Expand All @@ -22,7 +23,8 @@ public record TicketOutput(
public static TicketOutput from(Ticket ticket) {
return new TicketOutput(
ticket.getId().getValue().toString(),
ticket.getUserID().getValue().toString(),
ticket.getUserID().isEmpty() ? null : ticket.getUserID().orElse(null).getValue().toString(),
ticket.getDocument(),
ticket.getEventID().getValue().toString(),
ticket.getDescription(),
ticket.getValidIn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,41 @@
import br.com.ifsp.tickets.domain.user.UserID;
import lombok.Getter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Optional;

@Getter
public class Enrollment extends Entity<EnrollmentID> {

private final UserID userID;
private final Optional<UserID> userID;
private final String name;
private final String email;
private final LocalDate birthDate;
private final String document;
private final EventID eventID;
private final LocalDateTime createdAt;
private EnrollmentStatus status;
private LocalDateTime updatedAt;

public Enrollment(EnrollmentID enrollmentID, UserID userID, EventID eventID, EnrollmentStatus status, LocalDateTime createdAt, 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) {
super(enrollmentID);
this.userID = userID;
this.name = name;
this.email = email;
this.birthDate = birthDate;
this.document = document;
this.eventID = eventID;
this.status = status;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

public static Enrollment with(EnrollmentID enrollmentID, UserID userID, EventID eventID, EnrollmentStatus status, LocalDateTime createdAt, LocalDateTime updatedAt) {
return new Enrollment(enrollmentID, userID, eventID, status, createdAt, updatedAt);
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));
}

public static Enrollment newEnrollment(UserID userID, EventID eventID) {
return new Enrollment(EnrollmentID.unique(), userID, eventID, EnrollmentStatus.CONFIRMED, LocalDateTime.now(), null);
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));
}

public void confirm() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public EnrollmentValidator(IValidationHandler aHandler, Enrollment enrollment) {
public void validate() {
if (this.enrollment.getEventID() == null || this.enrollment.getEventID().getValue() == null)
error("EventID is required");
if (this.enrollment.getUserID() == null || this.enrollment.getUserID().getValue() == null)
error("UserID is required");
if (this.enrollment.getDocument() == null)
error("Document is required");
if (this.enrollment.getStatus() == null) error("Status is required");
if (this.enrollment.getCreatedAt() == null) error("CreatedAt is required");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public interface IEnrollmentGateway {
Enrollment findByUserIDAndEventID(UserID userID, EventID eventID);

boolean existsByUserIDAndEventID(UserID userID, EventID eventID);
boolean existsByDocumentAndEventID(String document, EventID eventID);

Enrollment update(Enrollment enrollment);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
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 UserID userID;
private final Optional<UserID> userID;
private final String document;
private final EventID eventID;
private final String description;
private final LocalDate validIn;
Expand All @@ -29,9 +31,10 @@ public class Ticket extends Entity<TicketID> {
private TicketCode code;
private LocalDateTime lastTimeConsumed;

public Ticket(TicketID ticketID, UserID userID, EventID eventID, String description, TicketStatus status, TicketCode code, LocalDate validIn, LocalDate expiredIn, LocalDateTime createdAt, 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) {
super(ticketID);
this.userID = userID;
this.document = document;
this.eventID = eventID;
this.description = description;
this.status = status;
Expand All @@ -42,12 +45,12 @@ public Ticket(TicketID ticketID, UserID userID, EventID eventID, String descript
this.lastTimeConsumed = lastTimeConsumed;
}

public static Ticket with(TicketID ticketID, UserID userID, EventID eventID, String description, TicketStatus status, TicketCode code, LocalDate validIn, LocalDate expiredIn, LocalDateTime createdAt, LocalDateTime lastTimeConsumed) {
return new Ticket(ticketID, userID, eventID, description, status, code, validIn, expiredIn, createdAt, 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 newTicket(UserID userID, Event event, String description, LocalDate validIn, LocalDate expiredIn) {
return new Ticket(TicketID.unique(), userID, event.getId(), description, TicketStatus.AVAILABLE, TicketCode.generate(), validIn, expiredIn, LocalDateTime.now(ZoneId.of("GMT-3")), null);
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));
}

public void generateNewCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public ResponseEntity<Void> create(CreateEnrollmentRequest request) {
final UserJpaEntity authenticatedUser = (UserJpaEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
final CreateEnrollmentInput input = CreateEnrollmentInput.of(
authenticatedUser.toAggregate(),
request.name(),
request.email(),
request.document(),
request.birthDate(),
request.eventId()
);
final CreateEnrollmentOutput out = this.enrollmentService.create(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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 @@ -21,6 +22,7 @@
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 @@ -31,6 +33,6 @@ public class EnrollmentConfig {

@Bean
public EnrollmentService enrollmentService() {
return EnrollmentServiceFactory.create(emailGateway, messageGateway, eventGateway, enrollmentGateway, ticketGateway, companyGateway, fileProvider, ticketQRGenerator);
return EnrollmentServiceFactory.create(emailGateway, userGateway, messageGateway, eventGateway, enrollmentGateway, ticketGateway, companyGateway, fileProvider, ticketQRGenerator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public boolean existsByUserIDAndEventID(UserID userID, EventID eventID) {
return this.enrollmentRepository.existsByUserIDAndEventID(userID.getValue(), eventID.getValue());
}

@Override
public boolean existsByDocumentAndEventID(String document, EventID eventID) {
return this.enrollmentRepository.existsByDocumentAndEventID(document, eventID.getValue());
}

@Override
public Enrollment update(Enrollment enrollment) {
return this.enrollmentRepository.save(EnrollmentJpaEntity.from(enrollment)).toAggregate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.LocalDate;

public record CreateEnrollmentRequest(
@JsonProperty("event_id") String eventId
@JsonProperty("event_id") String eventId,
@JsonProperty("name") String name,
@JsonProperty("email") String email,
@JsonProperty("document") String document,
@JsonProperty("birth_date") LocalDate birthDate
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;

Expand All @@ -25,19 +26,31 @@ public class EnrollmentJpaEntity implements Serializable {
@Id
@Column(name = "id", nullable = false, unique = true, updatable = false)
private UUID id;
@Column(name = "user_id", nullable = false)
private UUID userID;
@Column(name = "event_id", nullable = false)
private UUID eventID;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "email", nullable = false)
private String email;
@Column(name = "birth_date", nullable = false)
private LocalDate birthDate;
@Column(name = "document", nullable = false)
private String document;
@Column(name = "status", nullable = false)
private String status;
@Column(name = "created_at", nullable = false)
private LocalDateTime createdAt;
@Column(name = "updated_at")
private LocalDateTime updatedAt;
@Column(name = "user_id")
private UUID userID;

public EnrollmentJpaEntity(UUID id, UUID userID, UUID eventID, String status, LocalDateTime createdAt, LocalDateTime updatedAt) {
public EnrollmentJpaEntity(UUID id, String name, String email, String document, LocalDate birthDate, UUID eventID, String status, LocalDateTime createdAt, LocalDateTime updatedAt, UUID userID) {
this.id = id;
this.name = name;
this.email = email;
this.document = document;
this.birthDate = birthDate;
this.userID = userID;
this.eventID = eventID;
this.status = status;
Expand All @@ -48,22 +61,30 @@ public EnrollmentJpaEntity(UUID id, UUID userID, UUID eventID, String status, Lo
public static EnrollmentJpaEntity from(Enrollment enrollment) {
return new EnrollmentJpaEntity(
enrollment.getId().getValue(),
enrollment.getUserID().getValue(),
enrollment.getName(),
enrollment.getEmail(),
enrollment.getDocument(),
enrollment.getBirthDate(),
enrollment.getEventID().getValue(),
enrollment.getStatus().name(),
enrollment.getCreatedAt(),
enrollment.getUpdatedAt()
enrollment.getUpdatedAt(),
enrollment.getUserID().orElse(null).getValue()
);
}

public Enrollment toAggregate() {
return Enrollment.with(
EnrollmentID.with(this.id),
UserID.with(this.userID),
this.name,
this.email,
this.birthDate,
this.document,
EventID.with(this.eventID),
EnrollmentStatus.valueOf(this.status),
this.createdAt,
this.updatedAt
this.updatedAt,
UserID.with(this.userID)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public interface EnrollmentRepository extends JpaRepository<EnrollmentJpaEntity,
Page<EnrollmentJpaEntity> findAllByEventID(UUID id, Pageable pageable);
Optional<EnrollmentJpaEntity> findByUserIDAndEventID(UUID userID, UUID eventID);
boolean existsByUserIDAndEventID(UUID userID, UUID eventID);
boolean existsByDocumentAndEventID(String document, UUID eventID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public record TicketResponse(
String id,
@JsonProperty("user_id")
String userId,
@JsonProperty("document")
String document,
@JsonProperty("event_id")
String eventId,
@JsonProperty("description")
Expand Down
Loading

0 comments on commit 5c9199e

Please sign in to comment.