Skip to content

Commit

Permalink
feat: upsertEnrollments
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavofg1pontes committed Oct 7, 2024
1 parent 6acae06 commit 4199b90
Show file tree
Hide file tree
Showing 45 changed files with 777 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package br.com.ifsp.tickets.app.enrollment;

import br.com.ifsp.tickets.app.enrollment.create.CreateEnrollmentInput;
import br.com.ifsp.tickets.app.enrollment.create.CreateEnrollmentOutput;
import br.com.ifsp.tickets.app.enrollment.create.ICreateEnrollmentUseCase;
import br.com.ifsp.tickets.app.enrollment.retrieve.EnrollmentOutput;
import br.com.ifsp.tickets.app.enrollment.retrieve.list.IListEnrollmentsByUserUseCase;
import br.com.ifsp.tickets.app.enrollment.core.create.CreateEnrollmentInput;
import br.com.ifsp.tickets.app.enrollment.core.create.CreateEnrollmentOutput;
import br.com.ifsp.tickets.app.enrollment.core.create.ICreateEnrollmentUseCase;
import br.com.ifsp.tickets.app.enrollment.core.retrieve.EnrollmentOutput;
import br.com.ifsp.tickets.app.enrollment.core.retrieve.list.IListEnrollmentsByUserUseCase;
import br.com.ifsp.tickets.app.enrollment.upsert.create.CreateUpsertEnrollmentInput;
import br.com.ifsp.tickets.app.enrollment.upsert.create.ICreateUpsertEnrollmentUseCase;
import br.com.ifsp.tickets.app.enrollment.upsert.retrieve.GetUpsertEnrollmentInput;
import br.com.ifsp.tickets.app.enrollment.upsert.retrieve.GetUpsertEnrollmentOutput;
import br.com.ifsp.tickets.app.enrollment.upsert.retrieve.IGetUpsertEnrollmentUseCase;
import br.com.ifsp.tickets.domain.shared.search.Pagination;
import br.com.ifsp.tickets.domain.user.User;

public class EnrollmentService {

private final ICreateEnrollmentUseCase createEnrollmentUseCase;
private final IListEnrollmentsByUserUseCase listEnrollmentsByUserUseCase;
private final ICreateUpsertEnrollmentUseCase createUpsertEnrollmentUseCase;
private final IGetUpsertEnrollmentUseCase getUpsertEnrollmentUseCase;

public EnrollmentService(ICreateEnrollmentUseCase createEnrollmentUseCase, IListEnrollmentsByUserUseCase listEnrollmentsByUserUseCase) {
public EnrollmentService(ICreateEnrollmentUseCase createEnrollmentUseCase, IListEnrollmentsByUserUseCase listEnrollmentsByUserUseCase, ICreateUpsertEnrollmentUseCase createUpsertEnrollmentUseCase, IGetUpsertEnrollmentUseCase getUpsertEnrollmentUseCase) {
this.createEnrollmentUseCase = createEnrollmentUseCase;
this.listEnrollmentsByUserUseCase = listEnrollmentsByUserUseCase;
this.createUpsertEnrollmentUseCase = createUpsertEnrollmentUseCase;
this.getUpsertEnrollmentUseCase = getUpsertEnrollmentUseCase;
}

public CreateEnrollmentOutput create(CreateEnrollmentInput input) {
Expand All @@ -25,4 +34,12 @@ public CreateEnrollmentOutput create(CreateEnrollmentInput input) {
public Pagination<EnrollmentOutput> listEnrollmentsByUser(User authenticatedUser) {
return this.listEnrollmentsByUserUseCase.execute(authenticatedUser);
}

public String createUpsertEnrollment(CreateUpsertEnrollmentInput input) {
return this.createUpsertEnrollmentUseCase.execute(input);
}

public GetUpsertEnrollmentOutput getUpsertEnrollment(GetUpsertEnrollmentInput in) {
return this.getUpsertEnrollmentUseCase.execute(in);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package br.com.ifsp.tickets.app.enrollment;

import br.com.ifsp.tickets.app.enrollment.create.CreateEnrollmentUseCase;
import br.com.ifsp.tickets.app.enrollment.create.ICreateEnrollmentUseCase;
import br.com.ifsp.tickets.app.enrollment.retrieve.list.IListEnrollmentsByUserUseCase;
import br.com.ifsp.tickets.app.enrollment.retrieve.list.ListEnrollmentsByUserUseCase;
import br.com.ifsp.tickets.app.enrollment.core.create.CreateEnrollmentUseCase;
import br.com.ifsp.tickets.app.enrollment.core.create.ICreateEnrollmentUseCase;
import br.com.ifsp.tickets.app.enrollment.core.retrieve.list.IListEnrollmentsByUserUseCase;
import br.com.ifsp.tickets.app.enrollment.core.retrieve.list.ListEnrollmentsByUserUseCase;
import br.com.ifsp.tickets.app.enrollment.upsert.create.CreateUpsertUpsertEnrollmentUseCase;
import br.com.ifsp.tickets.app.enrollment.upsert.create.ICreateUpsertEnrollmentUseCase;
import br.com.ifsp.tickets.app.enrollment.upsert.retrieve.GetUpsertEnrollmentUseCase;
import br.com.ifsp.tickets.app.enrollment.upsert.retrieve.IGetUpsertEnrollmentUseCase;
import br.com.ifsp.tickets.domain.communication.email.IEmailGateway;
import br.com.ifsp.tickets.domain.communication.message.IMessageGateway;
import br.com.ifsp.tickets.domain.company.ICompanyGateway;
import br.com.ifsp.tickets.domain.enrollment.IEnrollmentGateway;
import br.com.ifsp.tickets.domain.enrollment.upsert.IUpsertEnrollmentGateway;
import br.com.ifsp.tickets.domain.event.IEventGateway;
import br.com.ifsp.tickets.domain.event.sale.ITicketSaleGateway;
import br.com.ifsp.tickets.domain.shared.file.IFileStorage;
Expand All @@ -25,7 +30,8 @@ public static EnrollmentService create(
ICompanyGateway companyGateway,
IFileStorage fileProvider,
ITicketQRGenerator ticketQRGenerator,
ITicketSaleGateway ticketSaleGateway
ITicketSaleGateway ticketSaleGateway,
IUpsertEnrollmentGateway upsertEnrollmentGateway
) {
if (enrollmentService == null) {
final ICreateEnrollmentUseCase createEnrollmentUseCase = new CreateEnrollmentUseCase(
Expand All @@ -41,7 +47,19 @@ public static EnrollmentService create(
final IListEnrollmentsByUserUseCase listEnrollmentsByUserUseCase = new ListEnrollmentsByUserUseCase(
enrollmentGateway
);
enrollmentService = new EnrollmentService(createEnrollmentUseCase, listEnrollmentsByUserUseCase);
final ICreateUpsertEnrollmentUseCase createUpsertEnrollmentUseCase = new CreateUpsertUpsertEnrollmentUseCase(
eventGateway,
enrollmentGateway,
upsertEnrollmentGateway,
ticketGateway,
ticketSaleGateway,
messageGateway,
companyGateway,
emailGateway,
fileProvider,
ticketQRGenerator);
final IGetUpsertEnrollmentUseCase getUpsertEnrollmentUseCase = new GetUpsertEnrollmentUseCase(upsertEnrollmentGateway);
enrollmentService = new EnrollmentService(createEnrollmentUseCase, listEnrollmentsByUserUseCase, createUpsertEnrollmentUseCase, getUpsertEnrollmentUseCase);
}
return enrollmentService;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.ifsp.tickets.app.enrollment.create;
package br.com.ifsp.tickets.app.enrollment.core.create;

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

Expand All @@ -11,9 +11,10 @@ public record CreateEnrollmentInput(
String document,
LocalDate birthDate,
String eventId,
String ticketSaleId
String ticketSaleId,
String ticketId
) {
public static CreateEnrollmentInput of(User aggregate, String name, String email, String document, LocalDate birthDate, String eventId, String ticketSaleId) {
return new CreateEnrollmentInput(aggregate, name, email, document, birthDate, eventId, ticketSaleId);
public static CreateEnrollmentInput of(User aggregate, String name, String email, String document, LocalDate birthDate, String eventId, String ticketSaleId, String ticketId) {
return new CreateEnrollmentInput(aggregate, name, email, document, birthDate, eventId, ticketSaleId, ticketId);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.ifsp.tickets.app.enrollment.create;
package br.com.ifsp.tickets.app.enrollment.core.create;

import br.com.ifsp.tickets.domain.communication.email.Email;
import br.com.ifsp.tickets.domain.enrollment.Enrollment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.ifsp.tickets.app.enrollment.create;
package br.com.ifsp.tickets.app.enrollment.core.create;

import br.com.ifsp.tickets.app.enrollment.ITicketQRGenerator;
import br.com.ifsp.tickets.domain.communication.email.Email;
Expand All @@ -23,6 +23,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.ticket.TicketID;
import br.com.ifsp.tickets.domain.user.User;
import br.com.ifsp.tickets.domain.user.UserID;

Expand Down Expand Up @@ -59,6 +60,7 @@ public CreateEnrollmentOutput execute(CreateEnrollmentInput anIn) {
String emailString = anIn.email();
LocalDate birthDate = anIn.birthDate();
String document = anIn.document();
TicketID ticketID = TicketID.with(anIn.ticketId());
final boolean alreadyExists;
final EventID eventID = EventID.with(anIn.eventId());
final TicketSaleID ticketSaleID = TicketSaleID.with(anIn.ticketSaleId());
Expand Down Expand Up @@ -87,7 +89,7 @@ public CreateEnrollmentOutput execute(CreateEnrollmentInput anIn) {
userID, event.getId());

final LocalDate expiredIn = event.getEndDate().plusDays(1);
final Ticket ticket = Ticket.newTicket(userID, document, event, ticketSale, "Ingresso sem limite de acompanhantes", event.getInitDate(), expiredIn);
final Ticket ticket = Ticket.newTicketWithId(ticketID, userID, document, event, ticketSale, "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 @@ -96,7 +98,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(emailString, message, name, 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.ifsp.tickets.app.enrollment.create;
package br.com.ifsp.tickets.app.enrollment.core.create;

import br.com.ifsp.tickets.app.IUseCase;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.ifsp.tickets.app.enrollment.retrieve;
package br.com.ifsp.tickets.app.enrollment.core.retrieve;

import br.com.ifsp.tickets.domain.enrollment.Enrollment;
import br.com.ifsp.tickets.domain.enrollment.EnrollmentStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package br.com.ifsp.tickets.app.enrollment.retrieve.list;
package br.com.ifsp.tickets.app.enrollment.core.retrieve.list;

import br.com.ifsp.tickets.app.IUseCase;
import br.com.ifsp.tickets.app.enrollment.retrieve.EnrollmentOutput;
import br.com.ifsp.tickets.app.enrollment.core.retrieve.EnrollmentOutput;
import br.com.ifsp.tickets.domain.shared.search.Pagination;
import br.com.ifsp.tickets.domain.user.User;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package br.com.ifsp.tickets.app.enrollment.retrieve.list;
package br.com.ifsp.tickets.app.enrollment.core.retrieve.list;

import br.com.ifsp.tickets.app.enrollment.retrieve.EnrollmentOutput;
import br.com.ifsp.tickets.app.enrollment.core.retrieve.EnrollmentOutput;
import br.com.ifsp.tickets.domain.enrollment.IEnrollmentGateway;
import br.com.ifsp.tickets.domain.shared.search.Pagination;
import br.com.ifsp.tickets.domain.shared.search.SearchQuery;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package br.com.ifsp.tickets.app.enrollment.upsert.create;

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

import java.time.LocalDate;

public record CreateUpsertEnrollmentInput(
User user,
String name,
String email,
String document,
LocalDate birthDate,
String eventId,
String ticketSaleId,
String ticketID,
String preferenceURL
) {
public static CreateUpsertEnrollmentInput of(User aggregate, String name, String email, String document, LocalDate birthDate, String eventId, String ticketSaleId, String preferenceURL, String ticketID) {
return new CreateUpsertEnrollmentInput(aggregate, name, email, document, birthDate, eventId, ticketSaleId, ticketID, preferenceURL);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package br.com.ifsp.tickets.app.enrollment.upsert.create;

import br.com.ifsp.tickets.app.enrollment.ITicketQRGenerator;
import br.com.ifsp.tickets.app.enrollment.core.create.CreateEnrollmentOutput;
import br.com.ifsp.tickets.domain.communication.email.Email;
import br.com.ifsp.tickets.domain.communication.email.IEmailGateway;
import br.com.ifsp.tickets.domain.communication.message.IMessageGateway;
import br.com.ifsp.tickets.domain.communication.message.Message;
import br.com.ifsp.tickets.domain.communication.message.type.MessageSubject;
import br.com.ifsp.tickets.domain.communication.message.type.MessageType;
import br.com.ifsp.tickets.domain.company.Company;
import br.com.ifsp.tickets.domain.company.ICompanyGateway;
import br.com.ifsp.tickets.domain.enrollment.Enrollment;
import br.com.ifsp.tickets.domain.enrollment.IEnrollmentGateway;
import br.com.ifsp.tickets.domain.enrollment.upsert.IUpsertEnrollmentGateway;
import br.com.ifsp.tickets.domain.enrollment.upsert.UpsertEnrollment;
import br.com.ifsp.tickets.domain.event.Event;
import br.com.ifsp.tickets.domain.event.EventID;
import br.com.ifsp.tickets.domain.event.EventStatus;
import br.com.ifsp.tickets.domain.event.IEventGateway;
import br.com.ifsp.tickets.domain.event.sale.ITicketSaleGateway;
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.file.IFileStorage;
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.ticket.TicketID;
import br.com.ifsp.tickets.domain.user.User;
import br.com.ifsp.tickets.domain.user.UserID;

import java.time.LocalDate;

public class CreateUpsertUpsertEnrollmentUseCase implements ICreateUpsertEnrollmentUseCase {
private final IEventGateway eventGateway;
private final IEnrollmentGateway enrollmentGateway;
private final IUpsertEnrollmentGateway upsertEnrollmentGateway;
private final ITicketGateway ticketGateway;
private final ITicketSaleGateway ticketSaleGateway;
private final IMessageGateway messageGateway;
private final ICompanyGateway companyGateway;
private final IEmailGateway emailGateway;
private final IFileStorage fileProvider;
private final ITicketQRGenerator ticketGenerator;

public CreateUpsertUpsertEnrollmentUseCase(IEventGateway eventGateway, IEnrollmentGateway enrollmentGateway, IUpsertEnrollmentGateway upsertEnrollmentGateway, ITicketGateway ticketGateway, ITicketSaleGateway ticketSaleGateway, IMessageGateway messageGateway, ICompanyGateway companyGateway, IEmailGateway emailGateway, IFileStorage fileProvider, ITicketQRGenerator ticketGenerator) {
this.eventGateway = eventGateway;
this.enrollmentGateway = enrollmentGateway;
this.upsertEnrollmentGateway = upsertEnrollmentGateway;
this.ticketGateway = ticketGateway;
this.ticketSaleGateway = ticketSaleGateway;
this.messageGateway = messageGateway;
this.companyGateway = companyGateway;
this.emailGateway = emailGateway;
this.fileProvider = fileProvider;
this.ticketGenerator = ticketGenerator;
}

@Override
public String execute(CreateUpsertEnrollmentInput anIn) {
final User user = anIn.user();
String preference = anIn.preferenceURL();
String name = anIn.name();
String emailString = anIn.email();
LocalDate birthDate = anIn.birthDate();
String document = anIn.document();
TicketID ticketID = TicketID.with(anIn.ticketID());
final boolean alreadyExists;
final EventID eventID = EventID.with(anIn.eventId());
final TicketSaleID ticketSaleID = TicketSaleID.with(anIn.ticketSaleId());
final TicketSale ticketSale = this.ticketSaleGateway.findById(ticketSaleID).orElseThrow(() -> NotFoundException.with(TicketSale.class, ticketSaleID));
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 (!event.getStatus().equals(EventStatus.OPENED))
Notification.create("Event is not opened").append("Event is not open for enrollment").throwPossibleErrors();

if (user != null) {
name = user.getName();
emailString = user.getEmail().getValue();
birthDate = user.getBirthDate();
document = user.getDocument().getValue();
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 UserID userID = user != null ? user.getId() : new UserID(null);


final UpsertEnrollment enrollment = UpsertEnrollment
.newUpsertEnrollment(name, emailString, document, birthDate, userID, eventID, ticketSaleID, ticketID);

final Message message = this.messageGateway.findBySubjectAndType(MessageSubject.PAYMENT_EMAIL, MessageType.HTML).orElseThrow(() -> NotFoundException.with("Email template not found"));
final Notification notification = Notification.create();
enrollment.validate(notification);
notification.throwPossibleErrors();
final UpsertEnrollment createdEnrollment = this.upsertEnrollmentGateway.create(enrollment);

final Email email = Email.createDynamic(emailString, message, name, company.getName(), preference);
email.validate(notification);
notification.throwPossibleErrors();
final Email createdEmail = this.emailGateway.create(email);

return createdEnrollment.getId().getValue().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package br.com.ifsp.tickets.app.enrollment.upsert.create;

import br.com.ifsp.tickets.app.IUseCase;

public interface ICreateUpsertEnrollmentUseCase extends IUseCase<CreateUpsertEnrollmentInput, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package br.com.ifsp.tickets.app.enrollment.upsert.retrieve;

public record GetUpsertEnrollmentInput (
String ticketId
){
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package br.com.ifsp.tickets.app.enrollment.upsert.retrieve;

import br.com.ifsp.tickets.domain.enrollment.EnrollmentStatus;
import br.com.ifsp.tickets.domain.enrollment.upsert.UpsertEnrollment;
import br.com.ifsp.tickets.domain.event.EventID;
import br.com.ifsp.tickets.domain.event.sale.TicketSaleID;
import br.com.ifsp.tickets.domain.ticket.TicketID;
import br.com.ifsp.tickets.domain.user.UserID;

import java.time.LocalDate;
import java.time.LocalDateTime;

public record GetUpsertEnrollmentOutput(
String userID,
String name,
String email,
String document,
LocalDate birthDate,
String eventId,
String ticketID,
String ticketSaleId
) {

public static GetUpsertEnrollmentOutput from(UpsertEnrollment output) {
return new GetUpsertEnrollmentOutput(
output.getUserID().getValue().toString(),
output.getName(),
output.getEmail(),
output.getDocument(),
output.getBirthDate(),
output.getEventId().getValue().toString(),
output.getTicketID().getValue().toString(),
output.getTicketSaleId().getValue().toString()
);
}
}
Loading

0 comments on commit 4199b90

Please sign in to comment.