generated from pagopa/pagopa-functions-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from pagopa/PRDP-283-sviluppo-script-rigeneraz…
…ione-ricevute-utenti-b-simul chore: Script rigenerazione ricevute utenti b-simul
- Loading branch information
Showing
8 changed files
with
273 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
``` shell | ||
sh run.sh local | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
|
||
}); | ||
}); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|