Skip to content

Commit

Permalink
fix template message for whatsapp
Browse files Browse the repository at this point in the history
  • Loading branch information
steliomo committed Nov 20, 2023
1 parent fd24fc5 commit 8f3907d
Show file tree
Hide file tree
Showing 17 changed files with 303 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package mz.co.grocery.core.application.pos.in;

import mz.co.grocery.core.application.pos.out.SaleNotifier;
import mz.co.grocery.core.domain.sale.Sale;
import mz.co.msaude.boot.frameworks.exception.BusinessException;
import mz.co.msaude.boot.frameworks.model.UserContext;
Expand All @@ -15,4 +16,6 @@ public interface OpenTableUseCase {

Sale openTable(UserContext context, Sale sale) throws BusinessException;

void setSaleNotifier(SaleNotifier saleNotifier);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
*
*/
package mz.co.grocery.core.application.pos.out;

import mz.co.grocery.core.domain.sale.Sale;
import mz.co.msaude.boot.frameworks.exception.BusinessException;
import mz.co.msaude.boot.frameworks.model.UserContext;

/**
* @author Stélio Moiane
*
*/
public interface SaleListner {

Sale send(UserContext context, Sale sale) throws BusinessException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
*
*/
package mz.co.grocery.core.application.pos.out;

import mz.co.grocery.core.domain.sale.Sale;
import mz.co.msaude.boot.frameworks.exception.BusinessException;
import mz.co.msaude.boot.frameworks.model.UserContext;

/**
* @author Stélio Moiane
*
*/
public interface SaleNotifier {

void registListner(SaleListner listner);

void removeListner(SaleListner listner);

Sale notify(UserContext context, Sale sale) throws BusinessException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import mz.co.grocery.core.application.customer.out.CustomerPort;
import mz.co.grocery.core.application.pos.in.OpenTableUseCase;
import mz.co.grocery.core.application.sale.out.SalePort;
import mz.co.grocery.core.application.pos.out.SaleNotifier;
import mz.co.grocery.core.common.Clock;
import mz.co.grocery.core.common.UseCase;
import mz.co.grocery.core.domain.customer.Customer;
Expand All @@ -32,12 +32,11 @@ public class OpenTableService extends AbstractService implements OpenTableUseCas

private CustomerPort customerPort;

private SalePort salePort;
private SaleNotifier saleNotifier;

public OpenTableService(final Clock clock, final CustomerPort customerPort, final SalePort salePort) {
public OpenTableService(final Clock clock, final CustomerPort customerPort) {
this.clock = clock;
this.customerPort = customerPort;
this.salePort = salePort;
}

@Override
Expand Down Expand Up @@ -74,9 +73,13 @@ public Sale openTable(final UserContext context, Sale sale) throws BusinessExcep
sale.setSaleStatus(SaleStatus.OPENED);
sale.setSaleType(SaleType.INSTALLMENT);

sale = this.salePort.createSale(context, sale);
sale = this.saleNotifier.notify(context, sale);

return sale;
}

@Override
public void setSaleNotifier(final SaleNotifier saleNotifier) {
this.saleNotifier = saleNotifier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public Sale sendBill(final Sale sale) throws BusinessException {

sale.setFilename(filename);

final Message textMessage = new Message(sale.getCustomer().get().getName(), sale.getCustomer().get().getContact(), MessageType.TEXT,
final Message textMessage = new Message(sale.getCustomer().get().getName(),
sale.getCustomer().get().getContact(), MessageType.TEXT,
Boolean.FALSE);

final Message documentMessage = new Message(sale.getCustomer().get().getContact(), MessageType.DOCUMENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

public class Message {

private String unit;

private String customer;

private String contact;
Expand All @@ -33,6 +35,17 @@ public Message(final String customer, final String contact, final MessageType ty
this.hasUrlText = hasUrlText;
}

public Message(final String unit, final String customer, final String contact, final MessageType type) {
this.unit = unit;
this.customer = customer;
this.contact = contact;
this.type = type;
}

public String getUnit() {
return this.unit;
}

public String getContact() {
return this.contact;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Optional;

import org.junit.Assert;
import org.junit.Before;
Expand All @@ -17,6 +16,7 @@

import mz.co.grocery.core.application.customer.out.CustomerPort;
import mz.co.grocery.core.application.pos.in.OpenTableUseCase;
import mz.co.grocery.core.application.pos.out.SaleNotifier;
import mz.co.grocery.core.application.pos.service.OpenTableService;
import mz.co.grocery.core.application.sale.out.SalePort;
import mz.co.grocery.core.common.Clock;
Expand Down Expand Up @@ -48,8 +48,11 @@ public class OpenTableUseCaseTest extends AbstractUnitServiceTest {
@Mock
private Clock clock;

@Mock
private SaleNotifier saleNotifier;

@InjectMocks
private OpenTableUseCase openTableUseCase = new OpenTableService(this.clock, this.customerPort, this.salePort);
private OpenTableUseCase openTableUseCase = new OpenTableService(this.clock, this.customerPort);

private UserContext context;

Expand All @@ -70,11 +73,16 @@ public void setup() {
@Test
public void shouldOpenTable() throws BusinessException {

Mockito.when(this.saleNotifier.notify(this.context, this.sale)).thenReturn(this.sale);
Mockito.when(this.customerPort.createCustomer(ArgumentMatchers.any(UserContext.class),
ArgumentMatchers.any(Customer.class))).thenReturn(this.sale.getCustomer().get());

this.openTableUseCase.openTable(this.context, this.sale);

Mockito.verify(this.customerPort, Mockito.times(1)).createCustomer(ArgumentMatchers.any(UserContext.class),
ArgumentMatchers.any(Customer.class));
Mockito.verify(this.salePort, Mockito.times(1)).createSale(this.context, this.sale);

Mockito.verify(this.saleNotifier, Mockito.times(1)).notify(this.context, this.sale);

Assert.assertEquals(this.sale.getSaleDate(), LocalDate.now());
Assert.assertEquals(this.sale.getSaleStatus(), SaleStatus.OPENED);
Expand All @@ -84,14 +92,16 @@ public void shouldOpenTable() throws BusinessException {

@Test
public void shouldOpenTableWithAnExistingCustomer() throws BusinessException {
Mockito.when(this.customerPort.findCustomerByContact(ArgumentMatchers.anyString()))
.thenReturn(Optional.of(EntityFactory.gimme(Customer.class, CustomerTemplate.VALID)));
Mockito.when(this.saleNotifier.notify(this.context, this.sale)).thenReturn(this.sale);
Mockito.when(this.customerPort.findCustomerByContact(this.sale.getCustomer().get().getContact()))
.thenReturn(this.sale.getCustomer());

this.openTableUseCase.openTable(this.context, this.sale);

Mockito.verify(this.customerPort, Mockito.times(0)).createCustomer(ArgumentMatchers.any(UserContext.class),
ArgumentMatchers.any(Customer.class));
Mockito.verify(this.salePort, Mockito.times(1)).createSale(this.context, this.sale);

Mockito.verify(this.saleNotifier, Mockito.times(1)).notify(this.context, this.sale);

Assert.assertEquals(this.sale.getSaleDate(), LocalDate.now());
Assert.assertEquals(this.sale.getSaleStatus(), SaleStatus.OPENED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import mz.co.grocery.core.application.pos.in.OpenTableUseCase;
import mz.co.grocery.core.application.pos.in.RegistTableItemsUseCase;
import mz.co.grocery.core.application.pos.in.SendTableBillUseCase;
import mz.co.grocery.core.application.pos.out.SaleListner;
import mz.co.grocery.core.application.pos.out.SaleNotifier;
import mz.co.grocery.core.application.rent.out.RentPaymentPort;
import mz.co.grocery.core.application.sale.in.SalePaymentUseCase;
import mz.co.grocery.core.application.sale.in.SaleUseCase;
Expand All @@ -44,6 +46,8 @@
import mz.co.grocery.integ.resources.sale.dto.SaleDTO;
import mz.co.grocery.integ.resources.sale.dto.SalePaymentDTO;
import mz.co.grocery.integ.resources.sale.dto.SalesDTO;
import mz.co.grocery.persistence.pos.adapter.SendSaleTemplateMessageToWhastAppListner;
import mz.co.grocery.persistence.pos.adapter.SendSaleToDBListner;
import mz.co.msaude.boot.frameworks.exception.BusinessException;
import mz.co.msaude.boot.frameworks.mapper.DTOMapper;
import mz.co.msaude.boot.frameworks.util.LocalDateAdapter;
Expand Down Expand Up @@ -97,6 +101,17 @@ public class SaleResource extends AbstractResource {
@Inject
private SendTableBillUseCase sendTableBillUseCase;

@Inject
private SaleNotifier saleNotifier;

@Autowired
@BeanQualifier(SendSaleToDBListner.NAME)
private SaleListner sendToDBListner;

@Autowired
@BeanQualifier(SendSaleTemplateMessageToWhastAppListner.NAME)
private SaleListner sendTemplateMessageListner;

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -237,6 +252,10 @@ public Response fetchSalesWithDeliveryGuidesByCustomer(@PathParam("customerUuid"
@Produces(MediaType.APPLICATION_JSON)
public Response openTable(final SaleDTO tableDTO) throws BusinessException {

this.saleNotifier.registListner(this.sendToDBListner);
this.saleNotifier.registListner(this.sendTemplateMessageListner);
this.openTableUseCase.setSaleNotifier(this.saleNotifier);

final Sale table = this.openTableUseCase.openTable(this.getContext(), this.saleMapper.toDomain(tableDTO));

return Response.ok(this.saleMapper.toDTO(table)).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class WhatsApp {
@Value("${whatsapp.message.bill}")
private String bill;

@Value("${whatsapp.message.template.bill}")
private String templateBill;

@Value("${whatsapp.message.receipt}")
private String receipt;

Expand All @@ -50,11 +53,14 @@ public class WhatsApp {
@Value("${document.url}")
private String documentUrl;

@Value("${whatsapp.component.type}")
private String componentType;
@Value("${whatsapp.component.body.type}")
private String bodyType;

@Value("${whatsapp.component.header.type}")
private String headerType;

@Value("${whatsapp.component.parameter.type}")
private String parameterType;
@Value("${whatsapp.component.text.parameter.type}")
private String textParameterType;

public String getUrl() {
return this.url;
Expand All @@ -80,6 +86,10 @@ public String getBill() {
return this.bill;
}

public String getTemplateBill() {
return this.templateBill;
}

public String getReceipt() {
return this.receipt;
}
Expand All @@ -104,11 +114,15 @@ public String getDocumentUrl() {
return this.documentUrl;
}

public String getComponentType() {
return this.componentType;
public String getHeaderType() {
return this.headerType;
}

public String getBodyType() {
return this.bodyType;
}

public String getParameterType() {
return this.parameterType;
public String getTextParameterType() {
return this.textParameterType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
*
*/
package mz.co.grocery.persistence.pos.adapter;

import java.util.HashSet;
import java.util.Set;

import mz.co.grocery.core.application.pos.out.SaleListner;
import mz.co.grocery.core.application.pos.out.SaleNotifier;
import mz.co.grocery.core.common.PersistenceAdapter;
import mz.co.grocery.core.domain.sale.Sale;
import mz.co.msaude.boot.frameworks.exception.BusinessException;
import mz.co.msaude.boot.frameworks.model.UserContext;

/**
* @author Stélio Moiane
*
*/

@PersistenceAdapter
public class SaleNotifierAdapter implements SaleNotifier {

private Set<SaleListner> listners;

public SaleNotifierAdapter() {
this.listners = new HashSet<>();
}

@Override
public void registListner(final SaleListner listner) {
this.listners.add(listner);
}

@Override
public void removeListner(final SaleListner listner) {
this.listners.remove(listner);
}

@Override
public Sale notify(final UserContext context, Sale sale) throws BusinessException {
if (this.listners.isEmpty()) {
throw new BusinessException("regist.listners");
}

for (final SaleListner listner : this.listners) {
sale = listner.send(context, sale);
}

return sale;
}
}
Loading

0 comments on commit 8f3907d

Please sign in to comment.