Skip to content

Commit

Permalink
feat: add cf checks to helpdesk
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-cialini committed Jan 22, 2024
1 parent fe3ab13 commit 00b377b
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import it.gov.pagopa.receipt.pdf.helpdesk.exception.PdfJsonMappingException;
import it.gov.pagopa.receipt.pdf.helpdesk.service.BizEventToReceiptService;
import it.gov.pagopa.receipt.pdf.helpdesk.service.PDVTokenizerServiceRetryWrapper;
import it.gov.pagopa.receipt.pdf.helpdesk.utils.BizEventToReceiptUtils;
import it.gov.pagopa.receipt.pdf.helpdesk.utils.ObjectMapperUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -45,6 +46,9 @@ public class BizEventToReceiptServiceImpl implements BizEventToReceiptService {

private final ReceiptCosmosClient receiptCosmosClient;

public static final String FISCAL_CODE_ANONYMOUS = "ANONIMO";


public BizEventToReceiptServiceImpl() {
this.pdvTokenizerService = new PDVTokenizerServiceRetryWrapperImpl();
this.queueClient = ReceiptQueueClientImpl.getInstance();
Expand Down Expand Up @@ -127,21 +131,21 @@ public String getTransactionCreationDate(BizEvent bizEvent) {
@Override
public void tokenizeFiscalCodes(BizEvent bizEvent, Receipt receipt, EventData eventData) throws JsonProcessingException, PDVTokenizerException {
try {
if (bizEvent.getDebtor() != null && bizEvent.getDebtor().getEntityUniqueIdentifierValue() != null) {
eventData.setDebtorFiscalCode("ANONIMO".equals(bizEvent.getDebtor().getEntityUniqueIdentifierValue()) ?
bizEvent.getDebtor().getEntityUniqueIdentifierValue() :
pdvTokenizerService.generateTokenForFiscalCodeWithRetry(
bizEvent.getDebtor().getEntityUniqueIdentifierValue())
);
}
if (bizEvent.getPayer() != null && bizEvent.getPayer().getEntityUniqueIdentifierValue() != null
eventData.setDebtorFiscalCode(
bizEvent.getDebtor() != null && BizEventToReceiptUtils.isValidFiscalCode(bizEvent.getDebtor().getEntityUniqueIdentifierValue()) ?
pdvTokenizerService.generateTokenForFiscalCodeWithRetry(bizEvent.getDebtor().getEntityUniqueIdentifierValue()) :
FISCAL_CODE_ANONYMOUS
);

if (bizEvent.getPayer() != null && BizEventToReceiptUtils.isValidFiscalCode(bizEvent.getPayer().getEntityUniqueIdentifierValue())
&& isFromAuthenticatedOrigin(bizEvent)) {
eventData.setPayerFiscalCode(
pdvTokenizerService.generateTokenForFiscalCodeWithRetry(
bizEvent.getPayer().getEntityUniqueIdentifierValue())
);
} else if (bizEvent.getTransactionDetails() != null && bizEvent.getTransactionDetails().getUser() != null
&& bizEvent.getTransactionDetails().getUser().getFiscalCode() != null
&& BizEventToReceiptUtils.isValidFiscalCode(bizEvent.getTransactionDetails().getUser().getFiscalCode())
&& isFromAuthenticatedOrigin(bizEvent)) {
eventData.setPayerFiscalCode(
pdvTokenizerService.generateTokenForFiscalCodeWithRetry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,12 @@ public static boolean isBizEventInvalid(BizEvent bizEvent, ExecutionContext cont
return true;
}

if (bizEvent.getDebtor().getEntityUniqueIdentifierValue() == null ||
(bizEvent.getDebtor().getEntityUniqueIdentifierValue().equals("ANONIMO") &&
(bizEvent.getPayer() == null || bizEvent.getPayer().getEntityUniqueIdentifierValue() == null))) {
logger.debug("[{}] event with id {} discarded because debtor identifier is missing or ANONIMO",
if (
(bizEvent.getDebtor() == null || !isValidFiscalCode(bizEvent.getDebtor().getEntityUniqueIdentifierValue()))
&&
(bizEvent.getPayer() == null || !isValidFiscalCode(bizEvent.getPayer().getEntityUniqueIdentifierValue()))
) {
logger.debug("[{}] event with id {} discarded because debtor's and payer's identifiers are missing or not valid",
context.getFunctionName(), bizEvent.getId());
return true;
}
Expand Down Expand Up @@ -434,5 +436,16 @@ public static boolean isFromAuthenticatedOrigin(BizEvent bizEvent) {
));
}

public static boolean isValidFiscalCode(String fiscalCode) {
if (fiscalCode != null && !fiscalCode.isEmpty()) {
Pattern patternCF = Pattern.compile("^[A-Z]{6}[0-9LMNPQRSTUV]{2}[ABCDEHLMPRST][0-9LMNPQRSTUV]{2}[A-Z][0-9LMNPQRSTUV]{3}[A-Z]$");
Pattern patternPIVA = Pattern.compile("/^[0-9]{11}$/");

return patternCF.matcher(fiscalCode).find() || patternPIVA.matcher(fiscalCode).find();
}

return false;
}

private BizEventToReceiptUtils() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
@MockitoSettings(strictness = Strictness.LENIENT)
class RecoverFailedCartTest {

private final String PAYER_FISCAL_CODE = "a valid payer CF";
private final String DEBTOR_FISCAL_CODE = "a valid debtor CF";
private final String PAYER_FISCAL_CODE = "AAAAAA00A00A000D";
private final String DEBTOR_FISCAL_CODE = "AAAAAA00A00A000P";
private final String TOKENIZED_DEBTOR_FISCAL_CODE = "tokenizedDebtorFiscalCode";
private final String TOKENIZED_PAYER_FISCAL_CODE = "tokenizedPayerFiscalCode";
private final String EVENT_ID = "a valid id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ private BizEvent generateValidBizEvent(String eventId){
BizEvent item = new BizEvent();

Payer payer = new Payer();
payer.setEntityUniqueIdentifierValue("a valid payer CF");
payer.setEntityUniqueIdentifierValue("AAAAAA00A00A000P");
Debtor debtor = new Debtor();
debtor.setEntityUniqueIdentifierValue("a valid debtor CF");
debtor.setEntityUniqueIdentifierValue("AAAAAA00A00A000D");

TransactionDetails transactionDetails = new TransactionDetails();
Transaction transaction = new Transaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ private BizEvent generateValidBizEvent(String eventId) {
BizEvent item = new BizEvent();

Payer payer = new Payer();
payer.setEntityUniqueIdentifierValue("a valid payer CF");
payer.setEntityUniqueIdentifierValue("AAAAAA00A00A000D");
Debtor debtor = new Debtor();
debtor.setEntityUniqueIdentifierValue("a valid debtor CF");
debtor.setEntityUniqueIdentifierValue("AAAAAA00A00A000D");

TransactionDetails transactionDetails = new TransactionDetails();
Transaction transaction = new Transaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
@ExtendWith(MockitoExtension.class)
class RecoverFailedReceiptTest {

private final String PAYER_FISCAL_CODE = "a valid payer CF";
private final String DEBTOR_FISCAL_CODE = "a valid debtor CF";
private final String PAYER_FISCAL_CODE = "AAAAAA00A00A000D";
private final String DEBTOR_FISCAL_CODE = "AAAAAA00A00A000P";
private final String TOKENIZED_DEBTOR_FISCAL_CODE = "tokenizedDebtorFiscalCode";
private final String TOKENIZED_PAYER_FISCAL_CODE = "tokenizedPayerFiscalCode";
private final String EVENT_ID = "a valid id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class BuildTemplateServiceImplTest {
public static final String DEBTOR_FULL_NAME_INVALID = "-- --";
public static final String DEBTOR_FULL_NAME_SPECIAL_CHAR = "John,Doe:Megacorp;SRL::Avenue;Street/";
public static final String DEBTOR_FULL_NAME_SPECIAL_CHAR_FORMATTED = "John Doe Megacorp SRL Avenue Street ";
public static final String DEBTOR_VALID_CF = "CF_DEBTOR";
public static final String DEBTOR_VALID_CF = "AAAAAA00A00A000D";
public static final String PAYER_FULL_NAME = "John Doe PAYER";
public static final String PAYER_VALID_CF = "CF_PAYER";
public static final String PAYER_VALID_CF = "AAAAAA00A00A000P";
public static final String HOLDER_FULL_NAME = "John Doe HOLDER";
public static final String PAYMENT_TOKEN = "9a9bad2caf604b86a339476373c659b0";
public static final String AMOUNT_WITHOUT_CENTS = "7000";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
@ExtendWith(MockitoExtension.class)
class BizEventToReceiptUtilsTest {
private final String EVENT_ID = "a valid id";
private final String PAYER_FISCAL_CODE = "a valid payer CF";
private final String DEBTOR_FISCAL_CODE = "a valid debtor CF";
private final String PAYER_FISCAL_CODE = "AAAAAA00A00A000P";
private final String DEBTOR_FISCAL_CODE = "AAAAAA00A00A000D";
private final String TOKENIZED_DEBTOR_FISCAL_CODE = "tokenizedDebtorFiscalCode";
private final String TOKENIZED_PAYER_FISCAL_CODE = "tokenizedPayerFiscalCode";
public static final String REMITTANCE_INFORMATION_PAYMENT_INFO = "TARI 2021";
Expand Down

0 comments on commit 00b377b

Please sign in to comment.