Skip to content

Commit

Permalink
add nodoInviaCarrelloRPT Resp crawling and test
Browse files Browse the repository at this point in the history
  • Loading branch information
esposimo committed Mar 27, 2024
1 parent ca4066e commit 9559dbf
Show file tree
Hide file tree
Showing 12 changed files with 1,524 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require_once './vendor/autoload.php';



use Illuminate\Database\Capsule\Manager as Capsule;


Expand Down
4 changes: 4 additions & 0 deletions src/mock_crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@
$a = new \pagopa\crawler\paymentlist\req\nodoInviaCarrelloRPT(new \DateTime('2024-03-10'),'nodoInviaCarrelloRPT', 'REQ', $redis_cache);
$a->run();

$a = new \pagopa\crawler\paymentlist\resp\nodoInviaCarrelloRPT(new \DateTime('2024-03-10'),'nodoInviaCarrelloRPT', 'RESP', $redis_cache);
$a->run();


$a = new \pagopa\crawler\paymentlist\resp\nodoInviaCarrelloRPT(new \DateTime('2024-03-11'),'nodoInviaCarrelloRPT', 'RESP', $redis_cache);
$a->run();
//$a = new \pagopa\crawler\paymentlist\resp\activatePaymentNotice(new \DateTime('2024-03-10'),'activatePaymentNotice', 'RESP', $memcache);
//$a->run();
312 changes: 311 additions & 1 deletion src/mock_insert.php

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/src/crawler/events/resp/nodoInviaCarrelloRPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,15 @@ public function workflowEvent(int $index = 0): Workflow|null
$workflow->setEventId($this->getUniqueId());
$workflow->setEventTimestamp($this->getInsertedTimestamp());
$workflow->setFkTipoEvento(4);
$stazione = $this->getStazione();
if (!is_null($this->getMethodInterface()->getFaultCode()))
{
$workflow->setFaultCode($this->getMethodInterface()->getFaultCode());
}

if (!is_null($stazione))
{
$workflow->setStazione($stazione);
}
return $workflow;
}
}
29 changes: 27 additions & 2 deletions src/src/crawler/paymentlist/req/nodoInviaCarrelloRPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,51 @@ public function runCreateAttempt(int $index = 0): array

$session_key = base64_encode(sprintf('session_original_%s', $this->getEvent()->getSessionIdOriginal()));
$cache_key = base64_encode(sprintf('attempt_%s_%s_%s_%s_%s', $date_x_cache, $id_carrello, $iuv, $pa_emittente, $ccp));
$transfer_add = array();
$cache_value = [
'date_event' => $date_event,
'id' => $last_inserted_id,
'iuv' => $iuv,
'pa_emittente' => $pa_emittente,
'token_ccp' => $ccp,
'id_carrello' => $id_carrello,
'transfer_add' => true,
'amount_update' => true
];
$this->addValueCache($cache_key, $cache_value);
$this->addValueCache($session_key, $cache_value);

for($i=0;$i<$this->getEvent()->getTransferCount($index);$i++)
{
$details = $this->getEvent()->transactionDetails($i, $index);
$details->setFkPayment($last_inserted_id);
$details->insert();
DB::statement($details->getQuery(), $details->getBindParams());

if ($this->getEvent()->getMethodInterface()->isBollo($i, $index))
{
$transfer_add = [
'pa_transfer' => $this->getEvent()->getMethodInterface()->getTransferPa($i, $index),
'bollo' => true,
'amount_transfer' => $this->getEvent()->getMethodInterface()->getTransferAmount($i, $index),
'iban_transfer' => ''
];
}
else
{
$transfer_add = [
'pa_transfer' => $this->getEvent()->getMethodInterface()->getTransferPa($i, $index),
'bollo' => false,
'amount_transfer' => $this->getEvent()->getMethodInterface()->getTransferAmount($i, $index),
'iban_transfer' => $this->getEvent()->getMethodInterface()->getTransferIban($i, $index)
];
}
$cache_value['transfer_list'][] = $transfer_add;
}


$this->addValueCache($cache_key, $cache_value);
$this->addValueCache($session_key, $cache_value);


$workflow = $this->getEvent()->workflowEvent($index);
$workflow->setFkPayment($last_inserted_id);
$workflow->insert();
Expand Down
197 changes: 188 additions & 9 deletions src/src/crawler/paymentlist/resp/nodoInviaCarrelloRPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

namespace pagopa\crawler\paymentlist\resp;

use Illuminate\Database\Capsule\Manager as DB;
use pagopa\crawler\paymentlist\AbstractPaymentList;
use pagopa\database\sherlock\Transaction;
use pagopa\database\sherlock\TransactionDetails;
use pagopa\database\sherlock\TransactionRe;
use pagopa\database\sherlock\Workflow;

/**
* la nodoInviaCarrello Response ha sempre un carrello associato, se l'id session original della
* request è stato già analizzato
*/
class nodoInviaCarrelloRPT extends AbstractPaymentList
{

Expand All @@ -13,62 +21,85 @@ class nodoInviaCarrelloRPT extends AbstractPaymentList
*/
public function createEventInstance(array $eventData): void
{
// TODO: Implement createEventInstance() method.
$event = new \pagopa\crawler\events\resp\nodoInviaCarrelloRPT($eventData);
$this->setEvent($event);
}

/**
* @inheritDoc
*/
public function isValidPayment(int $index = 0): bool
{
// TODO: Implement isValidPayment() method.
$session_key = base64_encode(sprintf('session_original_%s', $this->getEvent()->getSessionIdOriginal()));
$cache_data = $this->getFromCache($session_key);
if (!is_array($cache_data))
{
return false;
}
return array_key_exists($index, $cache_data);
}

/**
* @inheritDoc
*/
public function isAttempt(int $index = 0): bool
{
// TODO: Implement isAttempt() method.
return $this->isValidPayment($index);
}

/**
* @inheritDoc
*/
public function isAttemptInCache(int $index = 0): bool
{
// TODO: Implement isAttemptInCache() method.
$session_key = base64_encode(sprintf('session_original_%s', $this->getEvent()->getSessionIdOriginal()));
$cache_value = $this->getFromCache($session_key);

$attempt = $cache_value[$index];


$date = $attempt['date_event'];
$iuv = $attempt['iuv'];
$pa = $attempt['pa_emittente'];
$ccp = $attempt['token_ccp'];
$id_carrello = $attempt['id_carrello'];
$date_x_cache = $this->getEvent()->getInsertedTimestamp()->format('Ymd');
$key = base64_encode(sprintf('attempt_%s_%s_%s_%s_%s', $date_x_cache, $id_carrello, $iuv, $pa, $ccp));

return $this->hasInCache($key);

}

/**
* @inheritDoc
*/
public function isPaymentInCache(int $index = 0): bool
{
// TODO: Implement isPaymentInCache() method.
return $this->isAttemptInCache($index);
}

/**
* @inheritDoc
*/
public function isFoundOnDb(int $index = 0): bool
{
// TODO: Implement isFoundOnDb() method.
return false;
}

/**
* @inheritDoc
*/
public function runAttemptAlreadyEvaluated(int $index = 0): void
{
// TODO: Implement runAttemptAlreadyEvaluated() method.

}

/**
* @inheritDoc
*/
public function runCreateAttempt(int $index = 0): array
{

// TODO: Implement runCreateAttempt() method.
}

Expand Down Expand Up @@ -101,14 +132,162 @@ public function runCopyPaymentToday(int $index = 0): void
*/
public function runRejectedEvent(string $message = null): TransactionRe
{
// TODO: Implement runRejectedEvent() method.
return $this->getEvent()->getEventRowInstance()->reject($message)->update();
}

/**
* @inheritDoc
*/
public function runCompleteEvent(string $message = null): TransactionRe
{
// TODO: Implement runCompleteEvent() method.
return $this->getEvent()->getEventRowInstance()->loaded($message)->update();
}



public function runAnalysisSingleEvent() : void
{
try {
// la nodo invia carrello può essere OK o KO
// ok carrello accettato, ho sicuramente in cache i pagamenti
// ko carrello non accettato, ma registro comunque l'evento
// non ho l'id sessione, rigetto l'evento

$session_key = base64_encode(sprintf('session_original_%s', $this->getEvent()->getSessionIdOriginal()));
$cached_data = $this->getFromCache($session_key);
if (is_array($cached_data))
{
$n = count($cached_data);
for($i=0;$i<$n;$i++)
{
if ($this->isAttemptInCache($i))
{
// se esiste in cache, aggiungo solo l'evento di workflow
$workflow = $this->getEvent()->workflowEvent($i);
$id = $cached_data[$i]['id'];
$workflow->setFkPayment($id);
$workflow->insert();
DB::statement($workflow->getQuery(), $workflow->getBindParams());
}
else
{
// non esiste, mi ricopio i dettagli del pagamento nella nuova giornata e aggiorno la cache

$attempt = $cached_data[$i];
$iuv = $attempt['iuv'];
$pa_emittente = $attempt['pa_emittente'];
$ccp = $attempt['token_ccp'];
$id_carrello = $attempt['id_carrello'];
$stazione = $this->getEvent()->getStazione();


$transaction = new Transaction($this->getEvent()->getInsertedTimestamp());
$transaction->setIuv($iuv);
$transaction->setPaEmittente($pa_emittente);
$transaction->setTokenCcp($ccp);
$transaction->setInsertedTimestamp($this->getEvent()->getInsertedTimestamp());
$transaction->setNewColumnValue('date_event', $this->getEvent()->getInsertedTimestamp()->format('Y-m-d'));

if (!is_null($id_carrello))
{
$transaction->setIdCarrello($id_carrello);
}
if (!is_null($stazione))
{
$transaction->setStazione($stazione);
}

$transaction->insert();
DB::statement($transaction->getQuery(), $transaction->getBindParams());
$last_inserted_id = DB::connection()->getPdo()->lastInsertId();

/*$transfer_add = [];
$new_cache_data = [
'date_event' => $this->getEvent()->getInsertedTimestamp()->format('Y-m-d'),
'id' => $last_inserted_id,
'iuv' => $iuv,
'pa_emittente' => $pa_emittente,
'token_ccp' => $ccp,
'id_carrello' => $id_carrello,
'transfer_add' => true,
'amount_update' => false
];
foreach($attempt['transfer_list'] as $transfer_details)
{
$details = new TransactionDetails($this->getEvent()->getInsertedTimestamp());
$details->setNewColumnValue('date_event', $this->getEvent()->getInsertedTimestamp()->format('Y-m-d'));
$details->setAmountTransfer($transfer_details['amount_transfer']);
$details->setBollo($transfer_details['bollo']);
$details->setTransferIban($transfer_details['iban_transfer']);
$details->setPaTransfer($transfer_details['pa_transfer']);
$details->setFkPayment($last_inserted_id);
$details->insert();
DB::statement($details->getQuery(), $details->getBindParams());
if ($transfer_details['bollo'] === true)
{
$transfer_add = [
'pa_transfer' => $transfer_details['pa_transfer'],
'bollo' => true,
'amount_transfer' => $transfer_details['amount_transfer'],
'iban_transfer' => ''
];
}
else
{
$transfer_add = [
'pa_transfer' => $transfer_details['pa_transfer'],
'bollo' => false,
'amount_transfer' => $transfer_details['amount_transfer'],
'iban_transfer' => $transfer_details['iban_transfer']
];
}
$new_cache_data['transfer_details'] = $transfer_add;
$date_x_cache = $this->getEvent()->getInsertedTimestamp()->format('Ymd');
$cache_key = base64_encode(sprintf('attempt_%s_%s_%s_%s_%s', $date_x_cache, $id_carrello, $iuv, $pa_emittente, $ccp));
if (!$this->hasInCache($cache_key))
{
$this->setCache($cache_key, $new_cache_data);
}
}*/
$date_x_cache = $this->getEvent()->getInsertedTimestamp()->format('Ymd');
$cache_key = base64_encode(sprintf('attempt_%s_%s_%s_%s_%s', $date_x_cache, $id_carrello, $iuv, $pa_emittente, $ccp));

$new_cache_data = [
'date_event' => $this->getEvent()->getInsertedTimestamp()->format('Y-m-d'),
'id' => $last_inserted_id,
'iuv' => $iuv,
'pa_emittente' => $pa_emittente,
'token_ccp' => $ccp,
'id_carrello' => $id_carrello,
'transfer_add' => true,
'amount_update' => false
];
if (!$this->hasInCache($cache_key))
{
$this->setCache($cache_key, $new_cache_data);
}
$workflow = $this->getEvent()->workflowEvent(0);
$workflow->setFkPayment($last_inserted_id);
$workflow->insert();
DB::statement($workflow->getQuery(), $workflow->getBindParams());
}
}

$rowid = $this->runCompleteEvent();
DB::statement($rowid->getQuery(), $rowid->getBindParams());
}
else
{
$error = $this->runRejectedEvent('Id Sessione Originale non trovato in cache');
DB::statement($error->getQuery(), $error->getBindParams());
}

}
catch (\Exception $e)
{
$rowid = $this->getEvent()->getEventRowInstance()->reject(substr($e->getMessage(), 0, 190))->update();
DB::statement($rowid->getQuery(), $rowid->getBindParams());
}
}
}
2 changes: 1 addition & 1 deletion src/tests/.phpunit.result.cache

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/tests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</php>
<testsuites>
<testsuite name="temp_suite">
<file>process/crawler/nodoInviaCarrelloRPT/SingleEvent/T00023_CreateAttemptTwoRPTFirstRpt2TransferSecondRpt2Transfer1Bollo.php</file>
<file>process/crawler/nodoInviaCarrelloRPT/BothEvents/T00027_CreateAttempTwoRPTTwoTransferForRPTWithResponseDifferentData.php</file>
</testsuite>
<testsuite name="crawler">
<file>process/crawler/activatePaymentNotice/SingleEvent/T00001_CreateAttemptAllInfoInEvent.php</file>
Expand All @@ -44,6 +44,10 @@
<file>process/crawler/nodoInviaCarrelloRPT/SingleEvent/T00021_CreateAttemptOneRPTTwoTransfer.php</file>
<file>process/crawler/nodoInviaCarrelloRPT/SingleEvent/T00022_CreateAttemptTwoRPTTwoTransfer.php</file>
<file>process/crawler/nodoInviaCarrelloRPT/SingleEvent/T00023_CreateAttemptTwoRPTFirstRpt2TransferSecondRpt2Transfer1Bollo.php</file>
<file>process/crawler/nodoInviaCarrelloRPT/BothEvents/T00024_CreateAttemptOneRPT1TransferSameData.php</file>
<file>process/crawler/nodoInviaCarrelloRPT/BothEvents/T00025_CreateAttemptOneRPT2TransferSameData.php</file>
<file>process/crawler/nodoInviaCarrelloRPT/BothEvents/T00026_CreateAttempTwoRPTTwoTransferForRPTSameData.php</file>
<file>process/crawler/nodoInviaCarrelloRPT/BothEvents/T00027_CreateAttempTwoRPTTwoTransferForRPTWithResponseDifferentData.php</file>
</testsuite>
<testsuite name="unit_tests">
<file>pagopa/database/SingleRowTest.php</file>
Expand Down
Loading

0 comments on commit 9559dbf

Please sign in to comment.