Skip to content

Commit

Permalink
Merge pull request #34 from pagopa/PRDP-283-sviluppo-script-rigeneraz…
Browse files Browse the repository at this point in the history
…ione-ricevute-utenti-b-simul

chore: Script rigenerazione ricevute utenti b-simul
  • Loading branch information
pasqualespica authored Dec 15, 2023
2 parents bb48e97 + 859c551 commit c8f8dea
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 2 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ local.settings.json
bin/
obj/
**/.identity
**/node_modules
**/report*.txt
**/*.lock
**/.env.local
**/copy.js
.azure/

package-lock.json
yarn.lock
.env
node_modules/
9 changes: 9 additions & 0 deletions rigen_rcv_bsimul/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# example: sh ./run_integration_test.sh <local|dev|uat|prod>
set -e

# run integration tests
cd ./src || exit
yarn install
yarn report:"$1"
3 changes: 3 additions & 0 deletions rigen_rcv_bsimul/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``` shell
sh run.sh local
```
7 changes: 7 additions & 0 deletions rigen_rcv_bsimul/src/config/.env copy.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
RECEIPTS_COSMOS_CONN_STRING=AccountEndpoint=
RECEIPT_COSMOS_DB_NAME=db
RECEIPT_COSMOS_DB_CONTAINER_NAME=receipts

BIZ_COSMOS_CONN_STRING=AccountEndpoint=
BIZ_COSMOS_DB_NAME=db
BIZ_COSMOS_DB_CONTAINER_NAME=biz-events
14 changes: 14 additions & 0 deletions rigen_rcv_bsimul/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "pagopa-receipt-pdf-rigenera",
"license": "MIT",
"version": "0.0.1",
"scripts": {
"report:local": "dotenv -e ./config/.env.local node ./utils/rigenera.js"
},
"devDependencies": {
"@azure/cosmos": "^3.17.3",
"dotenv": "^16.1.4",
"dotenv-cli": "^7.2.1",
"npx": "^10.2.2"
}
}
51 changes: 51 additions & 0 deletions rigen_rcv_bsimul/src/utils/io_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

const getPii = async function getPdvPii(url, token, customHeaders,data) {
return fetch(url+`tokens/${token}/pii`, {
method: "GET",
headers: customHeaders,
// body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
// console.log(data);
return data;
}).catch((error) => {
console.error('error in execution', error);
});
}

const postTokenPii = async function postTokenPii(url,customHeaders,data) {
return fetch(url+`tokens/search`, {
method: "POST",
headers: customHeaders,
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
// console.log(data);
return data;
}).catch((error) => {
console.error('error in execution', error);
});
}


const postPdfRigenerate = async function postPdfRigenerate(url, eventid, customHeaders,data) {
return fetch(url+`/receipts/${eventid}/regenerate-receipt-pdf`, {
method: "POST",
headers: customHeaders,
body: JSON.stringify(data),
})
.then((response) => response.status)
.then((data) => {
// console.log(data);
return data;
}).catch((error) => {
console.error('error in execution', error);
});
}


module.exports = {
getPii, postTokenPii, postPdfRigenerate
}
114 changes: 114 additions & 0 deletions rigen_rcv_bsimul/src/utils/rigenera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const fs = require('fs');

const { postTokenPii, postPdfRigenerate } = require("./io_client");
const { getReceiptsByCf, updateBizStatusToRETRY, getBiz } = require("./utils");


let currentDate = new Date()
let yesterday = new Date(currentDate)
yesterday.setDate(yesterday.getDate() - 1)

function padTo2Digits(num) {
return num.toString().padStart(2, '0');
}

function formatDate(date) {
return (
[
date.getFullYear(),
padTo2Digits(date.getMonth() + 1),
padTo2Digits(date.getDate()),
].join('-')
);
}

yesterday_ = formatDate(yesterday);

const cf_bsimul = [
'<CF_1>',
'<CF_2">',
];


// tokenizer
const PDV_TOKENIZER_BASE_PATH = "https://api.tokenizer.pdv.pagopa.it/tokenizer/v1/"
const customHeaders_ = {
"Content-Type": "application/json",
"x-api-key": "<x-api-key>"
}

// const PDV_TOKENIZER_BASE_PATH = "https://api.uat.tokenizer.pdv.pagopa.it/tokenizer/v1/"
// const customHeaders_ = {
// "Content-Type": "application/json",
// "x-api-key": "<x-api-key>"
// }



// pdf help
const PDF_HELP_BASE_PATH = "https://api.platform.pagopa.it/receipts/helpdesk/v1/"
const customHeadersPdf_ = {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": "<Ocp-Apim-Subscription-Key>"
}


// const PDF_HELP_BASE_PATH = "https://api.uat.platform.pagopa.it/receipts/helpdesk/v1/"
// const customHeadersPdf_ = {
// "Content-Type": "application/json",
// "Ocp-Apim-Subscription-Key": "<Ocp-Apim-Subscription-Key>"
// }


for (const cf of cf_bsimul) {
let data = {
"pii": cf
};

console.log(`CF>>>>>>>>>>>>>>>>>>>>>>>>>>> ${cf}`);

const rd = postTokenPii(PDV_TOKENIZER_BASE_PATH, customHeaders_, data);
rd.then(d => {
// console.log(d.token);
console.log(`CF> ${data.pii} token ${d.token}`)

// retrive Receip5 from CF
const res = getReceiptsByCf(d.token);
let p1 = res.then(async function (result) {


for (const e of result.resources) {
console.log(`CF>${cf} ${e.eventId} PDFd > ${e.mdAttach != undefined ? e.mdAttach.name : "NA"}`);
console.log(`CF>${cf} ${e.eventId} PDFp > ${e.mdAttachPayer != undefined ? e.mdAttachPayer.name : "NA"}`);

// getBiz
const res0 = getBiz(e.eventId);
let p0 = res0.then(async function (result0) {
console.log(result0.resources[0])
});


// update Biz to RETRY --- START
const res2 = updateBizStatusToRETRY(e.eventId);
let p2 = res2.then(async function (result2) {

await new Promise(r => setTimeout(r, 10000));
// console.log(result2);
// call PDF api ...
// console.log(`PdfRigenerate for bizId ${e.eventId} sent`);
const res3 = postPdfRigenerate(PDF_HELP_BASE_PATH, e.eventId, customHeadersPdf_);

let p3 = res3.then(async function (result3) {
console.log(`PdfRigenerate ${e.eventId} = ${result3}`);
});

});
// update Biz to RETRY --- STOP

}

});
});

}

70 changes: 70 additions & 0 deletions rigen_rcv_bsimul/src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { CosmosClient } = require("@azure/cosmos");
const { get } = require("http");

// receipt
const cosmos_db_conn_string = process.env.RECEIPTS_COSMOS_CONN_STRING || "";
const databaseId = process.env.RECEIPT_COSMOS_DB_NAME;
const receiptContainerId = process.env.RECEIPT_COSMOS_DB_CONTAINER_NAME;
const client = new CosmosClient(cosmos_db_conn_string);
const receiptContainer = client.database(databaseId).container(receiptContainerId);
// biz
const biz_cosmos_db_conn_string = process.env.BIZ_COSMOS_CONN_STRING || "";
const biz_databaseId = process.env.BIZ_COSMOS_DB_NAME;
const biz_ContainerId = process.env.BIZ_COSMOS_DB_CONTAINER_NAME;
const biz_client = new CosmosClient(biz_cosmos_db_conn_string);
const biz_Container = biz_client.database(biz_databaseId).container(biz_ContainerId);



async function getReceiptsByCf(cf) {
return await receiptContainer.items
.query({
query: `SELECT * FROM c WHERE
c.eventData.debtorFiscalCode = (@fiscalcode)
or c.eventData.payerFiscalCode = (@fiscalcode)`,
parameters: [
{ name: "@fiscalcode", value: cf },

]
})
.fetchAll();
}


async function updateBizStatusToRETRY(bizId) {

const operations =
[
{ op: 'replace', path: '/eventStatus', value: 'RETRY' },
{ op: 'remove', path: '/transactionDetails' }
];

try {
return await biz_Container.item(bizId, bizId).patch(operations);
} catch (error) {
if (error.code !== 404) {
console.log(error)
}
}
}

async function getBiz(bizId) {
return await biz_Container.items
.query({
query: `SELECT * FROM c WHERE
c.id = (@bizId)`,
parameters: [
{ name: "@bizId", value: bizId },

]
})
.fetchAll();
}



module.exports = {
getReceiptsByCf, updateBizStatusToRETRY, getBiz
}


0 comments on commit c8f8dea

Please sign in to comment.