Skip to content

Commit

Permalink
Merge pull request #10 from diegosneves/feat/notification
Browse files Browse the repository at this point in the history
refactor(Envio de email) 🎨 Remover o código da estratégia de pagamento e melhorar a lógica de negócios
  • Loading branch information
diegosneves authored Dec 26, 2023
2 parents 4324677 + e259445 commit 557d075
Show file tree
Hide file tree
Showing 23 changed files with 337 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ public ResponseEntity<ExceptionDTO> generalError(Exception exception) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(dto);
}

@ExceptionHandler(CloseOrderException.class)
public ResponseEntity<ExceptionDTO> orderRelatedFaileures(CloseOrderException exception) {
ExceptionDTO dto = new ExceptionDTO(exception.getMessage(), CloseOrderException.ERROR.getStatusCodeValue());
return ResponseEntity.status(CloseOrderException.ERROR.getHttpStatusCode()).body(dto);
}

@ExceptionHandler(CalculateInvoiceException.class)
public ResponseEntity<ExceptionDTO> orderRelatedFaileures(CalculateInvoiceException exception) {
ExceptionDTO dto = new ExceptionDTO(exception.getMessage(), CalculateInvoiceException.ERROR.getStatusCodeValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public interface SplitInvoiceControllerContract {

@PostMapping(value = "/invoice", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Receber os dados para realizar a divisão da fatura", tags = "Racha Pedido", parameters = {
@Parameter(name = "referencia", description = "Tipos de descontos que devem ser aplicado no `discountType` do body",
schema = @Schema(enumAsRef = true, defaultValue = "cash", allowableValues = {"cash", "percentage", "no discount"}))
@Parameter(name = "descontos", description = "Tipos de descontos a serem aplicados no campo `discountType` do corpo da requisição",
schema = @Schema(enumAsRef = true, defaultValue = "cash", allowableValues = {"cash", "percentage", "no discount"})),
@Parameter(name = "bancos", description = "Tipos de bancos aceitos que devem ser preenchidos no campo `selectedBank` do corpo da requisição",
schema = @Schema(enumAsRef = true, defaultValue = "nubank", allowableValues = {"nubank", "picpay"}))
})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Divisão da Fatura realizada com sucesso", content = @Content)
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/diegosneves/github/rachapedido/core/BankAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,32 @@
public enum BankAccount {

@JsonProperty(value = "nubank")
NUBANK("https://nubank.com.br/cobrar/5h2au/658235a8-f38a-4cf5-881c-1de7114d66c7"),
NUBANK("NuBank", "https://nubank.com.br/cobrar/5h2au/658235a8-f38a-4cf5-881c-1de7114d66c7") {
@Override
public String paymentLink(Double amount) {
return NUBANK.billingLink;
}
},
@JsonProperty(value = "picpay")
PICPAY("https://app.picpay.com/user/diego.neves215");
PICPAY("PicPay", "https://app.picpay.com/user/diego.neves215") {
@Override
public String paymentLink(Double amount) {
return String.format("%s/%.2f", PICPAY.billingLink, amount);
}
};

private final String bankName;
private final String billingLink;

BankAccount(String billingLink) {
BankAccount(String bankName, String billingLink) {
this.bankName = bankName;
this.billingLink = billingLink;
}

public String paymentLink() {
return this.billingLink;
}
public abstract String paymentLink(Double amount);

@Override
public String toString() {
return this.bankName;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package diegosneves.github.rachapedido.core;

import diegosneves.github.rachapedido.dto.InvoiceDTO;
import diegosneves.github.rachapedido.enums.DiscountType;
import diegosneves.github.rachapedido.mapper.BuilderMapper;
import diegosneves.github.rachapedido.mapper.InvoiceFromPersonMapper;
import diegosneves.github.rachapedido.model.Invoice;
import diegosneves.github.rachapedido.model.Item;
import diegosneves.github.rachapedido.model.Person;
import diegosneves.github.rachapedido.utils.RoundUtil;

public class CashDiscountStrategy extends DiscountStrategy {

@Override
public Invoice calculateDiscount(InvoiceDTO dto, Double discountAmount, DiscountType type, Double total, Double deliveryFee) {
public Invoice calculateDiscount(Person person, Double discountAmount, DiscountType type, Double total, Double deliveryFee) {
if(DiscountType.CASH.name().equals(type.name())) {
dto.setPercentageConsumedTotalBill(dto.getValueConsumed() / total);
dto.setTotalPayable(RoundUtil.round(((total - type.discountAmount(discountAmount) + deliveryFee) * dto.getPercentageConsumedTotalBill())));
return BuilderMapper.builderMapper(Invoice.class, dto);
InvoiceFromPersonMapper mapper = new InvoiceFromPersonMapper();
Double consumption = person.getItems().stream().mapToDouble(Item::getPrice).sum();
Double percentageConsumedTotalBill = consumption / total;
Invoice newInvoice = BuilderMapper.builderMapper(Invoice.class, person, mapper);
newInvoice.setPercentageConsumedTotalBill(percentageConsumedTotalBill);
newInvoice.setTotalPayable(RoundUtil.round(((total - type.discountAmount(discountAmount) + deliveryFee) * percentageConsumedTotalBill)));
return newInvoice;
}
return this.checkNext(dto, discountAmount, type, total, deliveryFee);
return this.checkNext(person, discountAmount, type, total, deliveryFee);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package diegosneves.github.rachapedido.core;

import diegosneves.github.rachapedido.dto.InvoiceDTO;
import diegosneves.github.rachapedido.enums.DiscountType;
import diegosneves.github.rachapedido.model.Invoice;
import diegosneves.github.rachapedido.model.Person;

public abstract class DiscountStrategy {

Expand All @@ -16,13 +16,13 @@ public static DiscountStrategy link(DiscountStrategy first, DiscountStrategy...
}
return first;
}
public abstract Invoice calculateDiscount(InvoiceDTO dto, Double discountAmount, DiscountType type, Double total, Double deliveryFee);
public abstract Invoice calculateDiscount(Person person, Double discountAmount, DiscountType type, Double total, Double deliveryFee);

protected Invoice checkNext(InvoiceDTO dto, Double discountAmount, DiscountType type, Double total, Double deliveryFee) {
protected Invoice checkNext(Person person, Double discountAmount, DiscountType type, Double total, Double deliveryFee) {
if (this.next == null) {
return new Invoice();
}
return next.calculateDiscount(dto, discountAmount, type, total, deliveryFee);
return next.calculateDiscount(person, discountAmount, type, total, deliveryFee);
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package diegosneves.github.rachapedido.core;

import diegosneves.github.rachapedido.dto.InvoiceDTO;
import diegosneves.github.rachapedido.enums.DiscountType;
import diegosneves.github.rachapedido.mapper.BuilderMapper;
import diegosneves.github.rachapedido.mapper.InvoiceFromPersonMapper;
import diegosneves.github.rachapedido.model.Invoice;
import diegosneves.github.rachapedido.model.Item;
import diegosneves.github.rachapedido.model.Person;
import diegosneves.github.rachapedido.utils.RoundUtil;

public class NoDiscountStrategy extends DiscountStrategy {

@Override
public Invoice calculateDiscount(InvoiceDTO dto, Double discountAmount, DiscountType type, Double total, Double deliveryFee) {
public Invoice calculateDiscount(Person person, Double discountAmount, DiscountType type, Double total, Double deliveryFee) {
if (DiscountType.NO_DISCOUNT.name().equals(type.name())) {
dto.setPercentageConsumedTotalBill(dto.getValueConsumed() / total);
dto.setTotalPayable(RoundUtil.round((total - type.discountAmount(discountAmount) + deliveryFee) * dto.getPercentageConsumedTotalBill()));
return BuilderMapper.builderMapper(Invoice.class, dto);
Double consumption = person.getItems().stream().mapToDouble(Item::getPrice).sum();
Double percentageConsumedTotalBill = consumption / total;
InvoiceFromPersonMapper mapper = new InvoiceFromPersonMapper();
Invoice newInvoice = BuilderMapper.builderMapper(Invoice.class, person, mapper);
newInvoice.setPercentageConsumedTotalBill(percentageConsumedTotalBill);
newInvoice.setTotalPayable(RoundUtil.round((total - type.discountAmount(discountAmount) + deliveryFee) * percentageConsumedTotalBill));
return newInvoice;
}
return this.checkNext(dto, discountAmount, type, total, deliveryFee);
return this.checkNext(person, discountAmount, type, total, deliveryFee);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package diegosneves.github.rachapedido.core;

import diegosneves.github.rachapedido.dto.InvoiceDTO;
import diegosneves.github.rachapedido.enums.DiscountType;
import diegosneves.github.rachapedido.mapper.BuilderMapper;
import diegosneves.github.rachapedido.mapper.InvoiceFromPersonMapper;
import diegosneves.github.rachapedido.model.Invoice;
import diegosneves.github.rachapedido.model.Item;
import diegosneves.github.rachapedido.model.Person;
import diegosneves.github.rachapedido.utils.RoundUtil;

public class PercentageDiscountStrategy extends DiscountStrategy {

@Override
public Invoice calculateDiscount(InvoiceDTO dto, Double discountAmount, DiscountType type, Double total, Double deliveryFee) {
public Invoice calculateDiscount(Person person, Double discountAmount, DiscountType type, Double total, Double deliveryFee) {
if (DiscountType.PERCENTAGE.name().equals(type.name())) {
dto.setPercentageConsumedTotalBill(dto.getValueConsumed() / total);
dto.setTotalPayable(RoundUtil.round((total - (total * type.discountAmount(discountAmount)) + deliveryFee) * dto.getPercentageConsumedTotalBill()));
return BuilderMapper.builderMapper(Invoice.class, dto);
Double consumption = person.getItems().stream().mapToDouble(Item::getPrice).sum();
InvoiceFromPersonMapper mapper = new InvoiceFromPersonMapper();
Double percentageConsumedTotalBill = consumption / total;
Invoice newInvoice = BuilderMapper.builderMapper(Invoice.class, person, mapper);
newInvoice.setPercentageConsumedTotalBill(percentageConsumedTotalBill);
newInvoice.setTotalPayable(RoundUtil.round((total - (total * type.discountAmount(discountAmount)) + deliveryFee) * percentageConsumedTotalBill));
return newInvoice;
}
return this.checkNext(dto, discountAmount, type, total, deliveryFee);
return this.checkNext(person, discountAmount, type, total, deliveryFee);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package diegosneves.github.rachapedido.dto;

import diegosneves.github.rachapedido.model.Item;
import lombok.*;

import java.util.ArrayList;
import java.util.List;

/**
* A classe {@link InvoiceDTO} representa um objeto de transferência de dados de fatura (DTO).
* Ela contém informações sobre o nome do consumidor, valor consumido, valor total a pagar e a porcentagem do total da fatura consumida.
Expand Down Expand Up @@ -33,8 +37,8 @@
public class InvoiceDTO {

private String consumerName;
private Double valueConsumed;
private List<Item> items = new ArrayList<>();
private Double totalPayable;
private Double percentageConsumedTotalBill;
private String paymentLink;

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package diegosneves.github.rachapedido.mapper;

import diegosneves.github.rachapedido.model.Invoice;
import diegosneves.github.rachapedido.model.Person;

public class InvoiceFromPersonMapper implements BuildingStrategy<Invoice, Person> {

@Override
public Invoice run(Person origem) {
return Invoice.builder()
.consumerName(origem.getPersonName())
.email(origem.getEmail())
.isBuyer(origem.getIsBuyer())
.items(origem.getItems())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package diegosneves.github.rachapedido.mapper;

import diegosneves.github.rachapedido.model.Invoice;
import diegosneves.github.rachapedido.model.Item;
import diegosneves.github.rachapedido.model.NotificationEmail;
import diegosneves.github.rachapedido.model.Person;

public class NotificationEmailMapper implements BuildingStrategy <NotificationEmail, Person> {
public class NotificationEmailMapper implements BuildingStrategy <NotificationEmail, Invoice> {

@Override
public NotificationEmail run(Person origem) {
public NotificationEmail run(Invoice origem) {
return NotificationEmail.builder()
.consumerName(origem.getPersonName())
.consumerName(origem.getConsumerName())
.email(origem.getEmail())
.itens(origem.getItems())
.total(origem.getItems().stream().mapToDouble(Item::getPrice).sum())
.total(origem.getTotalPayable())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package diegosneves.github.rachapedido.model;

import diegosneves.github.rachapedido.dto.InvoiceDTO;
import lombok.*;

import java.util.ArrayList;
Expand All @@ -12,7 +13,7 @@
@Builder
public class BillSplit {

private List<Invoice> invoices = new ArrayList<>();
private List<InvoiceDTO> invoices = new ArrayList<>();
private Double totalPayable;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import lombok.*;

import java.util.ArrayList;
import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@Getter
Expand All @@ -10,7 +13,9 @@
public class Invoice {

private String consumerName;
private Double valueConsumed;
private String email;
private Boolean isBuyer = Boolean.FALSE;
private List<Item> items = new ArrayList<>();
private Double totalPayable;
private Double percentageConsumedTotalBill;
private String paymentLink;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class NotificationEmail {
private String consumerName;
private Double total;
private List<Item> itens;
private String bank;
private String link;

}
6 changes: 6 additions & 0 deletions src/main/java/diegosneves/github/rachapedido/model/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import lombok.*;

import java.util.ArrayList;
import java.util.List;

/**
* A classe {@link Order} representa um pedido feito por um {@link Person consumidor}.
* Cada pedido contém o {@link String nome do consumidor} e o {@link Double valor total consumido}.
Expand All @@ -16,6 +19,9 @@
public class Order {

private String consumerName;
private String email;
private Boolean isBuyer = Boolean.FALSE;
private Double valueConsumed;
private List<Item> items = new ArrayList<>();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package diegosneves.github.rachapedido.response;

import diegosneves.github.rachapedido.dto.InvoiceDTO;
import diegosneves.github.rachapedido.model.Invoice;
import lombok.*;

Expand All @@ -12,6 +13,6 @@
@Builder
public class SplitInvoiceResponse {

private List<Invoice> invoices;
private List<InvoiceDTO> invoices;
private Double totalPayable;
}
Loading

0 comments on commit 557d075

Please sign in to comment.