From d9b589a77ed0feef148f6be6619a05c2f1d80809 Mon Sep 17 00:00:00 2001 From: pasqualespica <36746022+pasqualespica@users.noreply.github.com> Date: Fri, 15 Dec 2023 14:23:59 +0100 Subject: [PATCH] script rigenerazione ricevute utenti b-simul --- .gitignore | 7 +- rigen_rcv_bsimul/run.sh | 9 ++ rigen_rcv_bsimul/src/README.md | 3 + rigen_rcv_bsimul/src/config/.env copy.local | 7 ++ rigen_rcv_bsimul/src/package.json | 14 +++ rigen_rcv_bsimul/src/utils/io_client.js | 51 +++++++++ rigen_rcv_bsimul/src/utils/rigenera.js | 114 ++++++++++++++++++++ rigen_rcv_bsimul/src/utils/utils.js | 70 ++++++++++++ 8 files changed, 274 insertions(+), 1 deletion(-) create mode 100644 rigen_rcv_bsimul/run.sh create mode 100644 rigen_rcv_bsimul/src/README.md create mode 100644 rigen_rcv_bsimul/src/config/.env copy.local create mode 100644 rigen_rcv_bsimul/src/package.json create mode 100644 rigen_rcv_bsimul/src/utils/io_client.js create mode 100644 rigen_rcv_bsimul/src/utils/rigenera.js create mode 100644 rigen_rcv_bsimul/src/utils/utils.js diff --git a/.gitignore b/.gitignore index d6fe466..11add1f 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,9 @@ hs_err_pid* local.settings.json bin/ obj/ -**/.identity \ No newline at end of file +**/.identity +**/node_modules +**/report*.txt +**/*.lock +**/.env.local +**/copy.js \ No newline at end of file diff --git a/rigen_rcv_bsimul/run.sh b/rigen_rcv_bsimul/run.sh new file mode 100644 index 0000000..f1417f0 --- /dev/null +++ b/rigen_rcv_bsimul/run.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# example: sh ./run_integration_test.sh +set -e + +# run integration tests +cd ./src || exit +yarn install +yarn report:"$1" \ No newline at end of file diff --git a/rigen_rcv_bsimul/src/README.md b/rigen_rcv_bsimul/src/README.md new file mode 100644 index 0000000..e960450 --- /dev/null +++ b/rigen_rcv_bsimul/src/README.md @@ -0,0 +1,3 @@ +``` shell +sh run.sh local +``` \ No newline at end of file diff --git a/rigen_rcv_bsimul/src/config/.env copy.local b/rigen_rcv_bsimul/src/config/.env copy.local new file mode 100644 index 0000000..f1ceea7 --- /dev/null +++ b/rigen_rcv_bsimul/src/config/.env copy.local @@ -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 \ No newline at end of file diff --git a/rigen_rcv_bsimul/src/package.json b/rigen_rcv_bsimul/src/package.json new file mode 100644 index 0000000..35ff749 --- /dev/null +++ b/rigen_rcv_bsimul/src/package.json @@ -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" + } +} diff --git a/rigen_rcv_bsimul/src/utils/io_client.js b/rigen_rcv_bsimul/src/utils/io_client.js new file mode 100644 index 0000000..1dbf747 --- /dev/null +++ b/rigen_rcv_bsimul/src/utils/io_client.js @@ -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 +} diff --git a/rigen_rcv_bsimul/src/utils/rigenera.js b/rigen_rcv_bsimul/src/utils/rigenera.js new file mode 100644 index 0000000..fc55d34 --- /dev/null +++ b/rigen_rcv_bsimul/src/utils/rigenera.js @@ -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 = [ + '', + '', +]; + + +// tokenizer +const PDV_TOKENIZER_BASE_PATH = "https://api.tokenizer.pdv.pagopa.it/tokenizer/v1/" +const customHeaders_ = { + "Content-Type": "application/json", + "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": "" +// } + + + +// 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": "" +} + + +// const PDF_HELP_BASE_PATH = "https://api.uat.platform.pagopa.it/receipts/helpdesk/v1/" +// const customHeadersPdf_ = { +// "Content-Type": "application/json", +// "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 + + } + + }); + }); + +} + diff --git a/rigen_rcv_bsimul/src/utils/utils.js b/rigen_rcv_bsimul/src/utils/utils.js new file mode 100644 index 0000000..06c5bb3 --- /dev/null +++ b/rigen_rcv_bsimul/src/utils/utils.js @@ -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 +} + +