diff --git a/application/src/main/java/br/com/ifsp/tickets/app/enrollment/create/CreateEnrollmentInput.java b/application/src/main/java/br/com/ifsp/tickets/app/enrollment/create/CreateEnrollmentInput.java index f5754fe..ffc63c6 100644 --- a/application/src/main/java/br/com/ifsp/tickets/app/enrollment/create/CreateEnrollmentInput.java +++ b/application/src/main/java/br/com/ifsp/tickets/app/enrollment/create/CreateEnrollmentInput.java @@ -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); } } diff --git a/application/src/main/java/br/com/ifsp/tickets/app/enrollment/create/CreateEnrollmentUseCase.java b/application/src/main/java/br/com/ifsp/tickets/app/enrollment/create/CreateEnrollmentUseCase.java index 50afe19..81b134e 100644 --- a/application/src/main/java/br/com/ifsp/tickets/app/enrollment/create/CreateEnrollmentUseCase.java +++ b/application/src/main/java/br/com/ifsp/tickets/app/enrollment/create/CreateEnrollmentUseCase.java @@ -19,6 +19,7 @@ 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; @@ -26,6 +27,7 @@ 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; @@ -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; @@ -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); diff --git a/application/src/main/java/br/com/ifsp/tickets/app/ticket/retrieve/get/TicketOutput.java b/application/src/main/java/br/com/ifsp/tickets/app/ticket/retrieve/get/TicketOutput.java index 6d3f2a7..a1afedb 100644 --- a/application/src/main/java/br/com/ifsp/tickets/app/ticket/retrieve/get/TicketOutput.java +++ b/application/src/main/java/br/com/ifsp/tickets/app/ticket/retrieve/get/TicketOutput.java @@ -22,7 +22,7 @@ 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.getEventID().getValue().toString(), ticket.getDescription(), ticket.getValidIn(), diff --git a/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/Enrollment.java b/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/Enrollment.java index 2069dfb..8429845 100644 --- a/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/Enrollment.java +++ b/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/Enrollment.java @@ -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 { - - private final UserID userID; + private final Optional 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) { 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() { diff --git a/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/EnrollmentValidator.java b/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/EnrollmentValidator.java index af8a071..aa2413b 100644 --- a/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/EnrollmentValidator.java +++ b/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/EnrollmentValidator.java @@ -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"); } diff --git a/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/IEnrollmentGateway.java b/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/IEnrollmentGateway.java index 1463812..6523656 100644 --- a/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/IEnrollmentGateway.java +++ b/domain/src/main/java/br/com/ifsp/tickets/domain/enrollment/IEnrollmentGateway.java @@ -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); diff --git a/domain/src/main/java/br/com/ifsp/tickets/domain/ticket/Ticket.java b/domain/src/main/java/br/com/ifsp/tickets/domain/ticket/Ticket.java index 17b109c..7d91dae 100644 --- a/domain/src/main/java/br/com/ifsp/tickets/domain/ticket/Ticket.java +++ b/domain/src/main/java/br/com/ifsp/tickets/domain/ticket/Ticket.java @@ -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 { - private final UserID userID; + private final Optional userID; + private final String document; private final EventID eventID; private final String description; private final LocalDate validIn; @@ -29,9 +31,10 @@ public class Ticket extends Entity { 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) { super(ticketID); this.userID = userID; + this.document = document; this.eventID = eventID; this.description = description; this.status = status; @@ -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() { diff --git a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/api/controllers/EnrollmentController.java b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/api/controllers/EnrollmentController.java index 67a2ab1..36d1bd7 100644 --- a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/api/controllers/EnrollmentController.java +++ b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/api/controllers/EnrollmentController.java @@ -28,6 +28,10 @@ public ResponseEntity 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); diff --git a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/EnrollmentGateway.java b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/EnrollmentGateway.java index b7a6386..7f8bbe8 100644 --- a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/EnrollmentGateway.java +++ b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/EnrollmentGateway.java @@ -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(); diff --git a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/models/CreateEnrollmentRequest.java b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/models/CreateEnrollmentRequest.java index 6a09532..9f01f98 100644 --- a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/models/CreateEnrollmentRequest.java +++ b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/models/CreateEnrollmentRequest.java @@ -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 ) { } diff --git a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/persistence/EnrollmentJpaEntity.java b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/persistence/EnrollmentJpaEntity.java index a7f305a..cc9fadf 100644 --- a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/persistence/EnrollmentJpaEntity.java +++ b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/persistence/EnrollmentJpaEntity.java @@ -13,6 +13,7 @@ import lombok.NoArgsConstructor; import java.io.Serializable; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.UUID; @@ -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; @@ -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) ); } } diff --git a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/persistence/EnrollmentRepository.java b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/persistence/EnrollmentRepository.java index ebf02a1..99a39c2 100644 --- a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/persistence/EnrollmentRepository.java +++ b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/enrollment/persistence/EnrollmentRepository.java @@ -14,4 +14,5 @@ public interface EnrollmentRepository extends JpaRepository findAllByEventID(UUID id, Pageable pageable); Optional findByUserIDAndEventID(UUID userID, UUID eventID); boolean existsByUserIDAndEventID(UUID userID, UUID eventID); + boolean existsByDocumentAndEventID(String document, UUID eventID); } diff --git a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/ticket/models/TicketResponse.java b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/ticket/models/TicketResponse.java index 6a426bf..d191fd3 100644 --- a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/ticket/models/TicketResponse.java +++ b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/ticket/models/TicketResponse.java @@ -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") diff --git a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/ticket/persistence/TicketJpaEntity.java b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/ticket/persistence/TicketJpaEntity.java index ebae0ba..7cb71b7 100644 --- a/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/ticket/persistence/TicketJpaEntity.java +++ b/infrastructure/src/main/java/br/com/ifsp/tickets/infra/contexts/ticket/persistence/TicketJpaEntity.java @@ -27,7 +27,9 @@ public class TicketJpaEntity implements Serializable { @Id @Column(name = "id", nullable = false, unique = true, updatable = false) private UUID id; - @Column(name = "user_id", nullable = false) + @Column(name = "document", nullable = false) + private String document; + @Column(name = "user_id") private UUID userId; @Column(name = "event_id", nullable = false) private UUID eventId; @@ -46,8 +48,9 @@ public class TicketJpaEntity implements Serializable { @Column(name = "last_time_consumed") private LocalDateTime lastTimeConsumed; - public TicketJpaEntity(UUID id, UUID userId, UUID eventId, String description, String status, String code, LocalDate validIn, LocalDate expiredIn, LocalDateTime createdAt, LocalDateTime lastTimeConsumed) { + 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.userId = userId; this.eventId = eventId; this.description = description; @@ -62,7 +65,8 @@ public TicketJpaEntity(UUID id, UUID userId, UUID eventId, String description, S public static TicketJpaEntity from(Ticket ticket) { return new TicketJpaEntity( ticket.getId().getValue(), - ticket.getUserID().getValue(), + ticket.getDocument(), + ticket.getUserID().orElse(null).getValue(), ticket.getEventID().getValue(), ticket.getDescription(), ticket.getStatus().name(), @@ -77,7 +81,7 @@ public static TicketJpaEntity from(Ticket ticket) { public Ticket toAggregate() { return Ticket.with( TicketID.with(this.id), - UserID.with(this.userId), + this.getDocument(), EventID.with(this.eventId), this.description, TicketStatus.valueOf(this.status), @@ -85,7 +89,8 @@ public Ticket toAggregate() { this.validIn, this.expiredIn, this.createdAt, - this.lastTimeConsumed + this.lastTimeConsumed, + UserID.with(this.userId) ); } }