Skip to content

Commit

Permalink
feat: company_created, company_deleted, event_created logs
Browse files Browse the repository at this point in the history
  • Loading branch information
oproprioleonardo committed Nov 27, 2024
1 parent 2f9823d commit 91880dc
Show file tree
Hide file tree
Showing 25 changed files with 525 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void execute(RecoveryRequestInput aCommand) {
}
final PasswordRecovery passwordRecovery = PasswordRecovery.create(user, ipAddress, userAgent);

final Notification notification = Notification.create();
final Notification notification = Notification.create("An error occurred while validating the password recovery request");
passwordRecovery.validate(notification);
notification.throwPossibleErrors();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
import br.com.ifsp.tickets.app.company.update.IUpdateCompanyUseCase;
import br.com.ifsp.tickets.app.company.update.UpdateCompanyUseCase;
import br.com.ifsp.tickets.domain.company.ICompanyGateway;
import br.com.ifsp.tickets.domain.shared.IDomainEventPublisher;
import br.com.ifsp.tickets.domain.user.IUserGateway;

public class CompanyServiceFactory {

private static CompanyService companyService;

public static CompanyService create(ICompanyGateway companyGateway, IUserGateway userGateway) {
public static CompanyService create(ICompanyGateway companyGateway, IUserGateway userGateway, IDomainEventPublisher eventPublisher) {
if (companyService == null) {
final ICreateCompanyUseCase companyUseCase = new CreateCompanyUseCase(userGateway, companyGateway);
final ICreateCompanyUseCase companyUseCase = new CreateCompanyUseCase(userGateway, companyGateway, eventPublisher);
final IGetCompanyByIdUseCase getCompanyByIdUseCase = new GetCompanyByIdUseCase(companyGateway);
final IDeleteCompanyUseCase deleteCompanyUseCase = new DeleteCompanyUseCase(companyGateway);
final IDeleteCompanyUseCase deleteCompanyUseCase = new DeleteCompanyUseCase(companyGateway, eventPublisher);
final IUpdateCompanyUseCase updateCompanyUseCase = new UpdateCompanyUseCase(companyGateway);
final ISearchCompanyUseCase searchCompanyUseCase = new SearchCompanyUseCase(companyGateway);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import br.com.ifsp.tickets.domain.company.Company;
import br.com.ifsp.tickets.domain.company.ICompanyGateway;
import br.com.ifsp.tickets.domain.company.vo.CNPJ;
import br.com.ifsp.tickets.domain.shared.IDomainEventPublisher;
import br.com.ifsp.tickets.domain.shared.event.CompanyCreated;
import br.com.ifsp.tickets.domain.shared.exceptions.AlreadyJoinedACompany;
import br.com.ifsp.tickets.domain.shared.exceptions.IllegalResourceAccessException;
import br.com.ifsp.tickets.domain.shared.exceptions.NotFoundException;
Expand All @@ -16,10 +18,12 @@ public class CreateCompanyUseCase implements ICreateCompanyUseCase {

private final IUserGateway userGateway;
private final ICompanyGateway companyGateway;
private final IDomainEventPublisher eventPublisher;

public CreateCompanyUseCase(IUserGateway userGateway, ICompanyGateway companyGateway) {
public CreateCompanyUseCase(IUserGateway userGateway, ICompanyGateway companyGateway, IDomainEventPublisher eventPublisher) {
this.userGateway = userGateway;
this.companyGateway = companyGateway;
this.eventPublisher = eventPublisher;
}

@Override
Expand Down Expand Up @@ -56,12 +60,16 @@ public CreateCompanyOutput execute(CreateCompanyInput anIn) {
address
);

final Notification notification = Notification.create();
final Notification notification = Notification.create("An error occurred while validating the company");
company.validate(notification);
notification.throwPossibleErrors();
company = this.companyGateway.create(company);
owner.joinCompany(company.getId());
this.userGateway.update(owner);

company.registerEvent(new CompanyCreated(company, creator));
company.publishDomainEvents(this.eventPublisher);

return CreateCompanyOutput.from(company);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import br.com.ifsp.tickets.domain.company.Company;
import br.com.ifsp.tickets.domain.company.CompanyID;
import br.com.ifsp.tickets.domain.company.ICompanyGateway;
import br.com.ifsp.tickets.domain.shared.IDomainEventPublisher;
import br.com.ifsp.tickets.domain.shared.event.CompanyDeleted;
import br.com.ifsp.tickets.domain.shared.exceptions.IllegalResourceAccessException;
import br.com.ifsp.tickets.domain.shared.exceptions.NotFoundException;
import br.com.ifsp.tickets.domain.user.User;

public class DeleteCompanyUseCase implements IDeleteCompanyUseCase {

private final ICompanyGateway companyGateway;
private final IDomainEventPublisher eventPublisher;

public DeleteCompanyUseCase(ICompanyGateway companyGateway) {
public DeleteCompanyUseCase(ICompanyGateway companyGateway, IDomainEventPublisher eventPublisher) {
this.companyGateway = companyGateway;
this.eventPublisher = eventPublisher;
}

@Override
Expand All @@ -25,5 +29,8 @@ public void execute(DeleteCompanyInput anIn) {

// TODO - lidar com os tickets, eventos e usuários associados a essa empresa
this.companyGateway.delete(company);

company.registerEvent(new CompanyDeleted(company, authenticatedUser));
company.publishDomainEvents(this.eventPublisher);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public UpdateCompanyOutput execute(UpdateCompanyInput anIn) {

final Company company = this.companyGateway.findById(companyID).orElseThrow(() -> NotFoundException.with(Company.class, companyID));
company.updateCompanyInfo(name, description, cnpj);
final Notification notification = Notification.create();
final Notification notification = Notification.create("An error occurred while validating the company");
company.validate(notification);
notification.throwPossibleErrors();
return UpdateCompanyOutput.from(this.companyGateway.update(company));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public CreateEnrollmentOutput execute(CreateEnrollmentInput anIn) {
final LocalDate expiredIn = event.getEndDate().plusDays(1);
final Ticket ticket = Ticket.newTicketWithId(ticketID, userID, document, event, ticketSale, ticketSale.getDescription(), 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();
final Notification notification = Notification.create("An error occurred while validating the enrollment");
enrollment.validate(notification);
ticket.validate(notification);
notification.throwPossibleErrors();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
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;
Expand All @@ -25,7 +19,6 @@
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;
Expand Down Expand Up @@ -93,7 +86,7 @@ public String execute(CreateUpsertEnrollmentInput anIn) {
final UpsertEnrollment enrollment = UpsertEnrollment
.newUpsertEnrollment(name, emailString, document, birthDate, userID, eventID, ticketSaleID, ticketID);

final Notification notification = Notification.create();
final Notification notification = Notification.create("An error occurred while validating the enrollment");
enrollment.validate(notification);
notification.throwPossibleErrors();
final UpsertEnrollment createdEnrollment = this.upsertEnrollmentGateway.create(enrollment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
import br.com.ifsp.tickets.domain.company.ICompanyGateway;
import br.com.ifsp.tickets.domain.event.IEventGateway;
import br.com.ifsp.tickets.domain.event.sale.ITicketSaleGateway;
import br.com.ifsp.tickets.domain.shared.IDomainEventPublisher;
import br.com.ifsp.tickets.domain.shared.file.IFileStorage;

public class EventServiceFactory {

private static EventService eventService;

public static EventService create(ICompanyGateway companyGateway, IEventGateway eventGateway, ITicketSaleGateway ticketSaleGateway, IFileStorage fileStorage) {
public static EventService create(ICompanyGateway companyGateway, IEventGateway eventGateway, ITicketSaleGateway ticketSaleGateway, IFileStorage fileStorage, IDomainEventPublisher eventPublisher) {
if (eventService == null) {
final ICreateEventUseCase createEventUseCase = new CreateEventUseCase(companyGateway, eventGateway);
final ICreateEventUseCase createEventUseCase = new CreateEventUseCase(companyGateway, eventGateway, eventPublisher);
final IGetEventUseCase getEventUseCase = new GetEventUseCase(eventGateway);
final ISearchEventUseCase searchEventUseCase = new SearchEventUseCase(eventGateway);
final IDownloadThumbnailUseCase downloadThumbnailUseCase = new DownloadThumbnailUseCase(fileStorage, eventGateway);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import br.com.ifsp.tickets.domain.event.EventConfig;
import br.com.ifsp.tickets.domain.event.EventConfigKey;
import br.com.ifsp.tickets.domain.event.IEventGateway;
import br.com.ifsp.tickets.domain.shared.IDomainEventPublisher;
import br.com.ifsp.tickets.domain.shared.event.EventCreated;
import br.com.ifsp.tickets.domain.shared.exceptions.IllegalResourceAccessException;
import br.com.ifsp.tickets.domain.shared.exceptions.NoCompanyException;
import br.com.ifsp.tickets.domain.shared.exceptions.NotFoundException;
Expand All @@ -21,22 +23,29 @@ public class CreateEventUseCase implements ICreateEventUseCase {

private final ICompanyGateway companyGateway;
private final IEventGateway eventGateway;
private final IDomainEventPublisher eventPublisher;

public CreateEventUseCase(ICompanyGateway companyGateway, IEventGateway eventGateway) {
public CreateEventUseCase(ICompanyGateway companyGateway, IEventGateway eventGateway, IDomainEventPublisher eventPublisher) {
this.companyGateway = companyGateway;
this.eventGateway = eventGateway;
this.eventPublisher = eventPublisher;
}

@Override
public CreateEventOutput execute(CreateEventInput anIn) {
final User user = anIn.user();
final CompanyID companyID = CompanyID.with(anIn.companyId());

if (!user.canManageEvents() && !user.canManageAnyEvent())
throw new IllegalResourceAccessException("User does not have permission to create events");

if (!user.hasCompany() && !user.canManageAnyEvent()) throw new NoCompanyException();

final CompanyID userCompanyID = user.getCompanyID();

if (!user.canManageAnyEvent() && !userCompanyID.equals(companyID))
throw new IllegalResourceAccessException("User does not have permission to create events for this company");

final Company company = this.companyGateway.findById(companyID).orElseThrow(() -> NotFoundException.with(Company.class, companyID));
final String name = anIn.name();
final String description = anIn.description();
Expand All @@ -55,11 +64,14 @@ public CreateEventOutput execute(CreateEventInput anIn) {
config
);

final Notification notification = Notification.create();
final Notification notification = Notification.create("An error occurred while validating the event");
event.validate(notification);
notification.throwPossibleErrors();

final Event eventCreated = this.eventGateway.create(event);
eventCreated.registerEvent(new EventCreated(eventCreated, user));
eventCreated.publishDomainEvents(this.eventPublisher);

return CreateEventOutput.from(eventCreated);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public CreateTicket2SellOutput execute(CreateTicket2SellInput anIn) {
final BigDecimal price = anIn.price();
final int entries = anIn.entries();
final TicketSale ticketSale = TicketSale.newTicketSale(event, name, description, price, entries);
final Notification notification = Notification.create();
final Notification notification = Notification.create("An error occurred while validating a ticket sale");
ticketSale.validate(notification);
notification.throwPossibleErrors();
return CreateTicket2SellOutput.from(this.ticketSaleGateway.create(ticketSale));
Expand Down
2 changes: 2 additions & 0 deletions database/inserir_padroes.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE OR REPLACE PROCEDURE CriarEntidadesPadroes()
AS
$$
Expand Down
Loading

0 comments on commit 91880dc

Please sign in to comment.