-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#172621882] send a message to the user that requested her own data #50
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2a013e5
draft: send user data download message
a5d2449
Merge branch 'master' of https://github.com/pagopa/io-functions-admin…
1ae023f
add message sending to cli
ab3eb64
fix tests
4c83b92
fix cli and debug
821adf3
add public url
94d04b6
fix cli
2366bdd
removed docker files
fb7167b
removed todo
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,10 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"name": "name", | ||
"type": "activityTrigger", | ||
"direction": "in" | ||
} | ||
], | ||
"scriptFile": "../dist/SendUserDataDownloadMessageActivity/index.js" | ||
} |
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,148 @@ | ||
import { Context } from "@azure/functions"; | ||
import { NewMessage } from "io-functions-commons/dist/generated/definitions/NewMessage"; | ||
import { readableReport } from "italia-ts-commons/lib/reporters"; | ||
|
||
import * as t from "io-ts"; | ||
import { FiscalCode, NonEmptyString } from "italia-ts-commons/lib/strings"; | ||
|
||
// TODO: switch text based on user's preferred_language | ||
const userDataDownloadMessage = ( | ||
blobName: string, | ||
password: string, | ||
publicDownloadBaseUrl: string | ||
) => | ||
NewMessage.decode({ | ||
content: { | ||
markdown: `Caro/a Utente, | ||
Abbiamo completato la gestione della tua richiesta di accesso. | ||
Puoi scaricare al link che segue i tuoi dati personali che trattiamo tramite l’App IO utilizzando la relativa password. | ||
|
||
Se hai necessità di maggiori dettagli o informazioni su questi dati o vuoi riceverne dettaglio, | ||
ti invitiamo a scrivere all’indirizzo email dpo@pagopa.it. | ||
|
||
Nel caso in cui tu non sia soddisfatto/a dalla modalità con cui abbiamo gestito la tua richiesta, | ||
siamo a disposizione per risolvere domande o dubbi aggiuntivi, che puoi indicare scrivendo all’indirizzo email indicato sopra. | ||
|
||
[Link all'archivio ZIP](${publicDownloadBaseUrl}/${blobName}) | ||
|
||
Password dell'archivio ZIP: | ||
|
||
${password} | ||
|
||
Grazie ancora per aver utilizzato IO, | ||
il Team Privacy di PagoPA | ||
`, | ||
subject: `IO App - richiesta di accesso ai dati` | ||
} | ||
}).getOrElseL(errs => { | ||
throw new Error("Invalid MessageContent: " + readableReport(errs)); | ||
}); | ||
|
||
/** | ||
* Send a single user data download message | ||
* using the IO Notification API (REST). | ||
*/ | ||
async function sendMessage( | ||
fiscalCode: FiscalCode, | ||
apiUrl: string, | ||
apiKey: string, | ||
newMessage: NewMessage, | ||
timeoutFetch: typeof fetch | ||
): Promise<number> { | ||
const response = await timeoutFetch( | ||
`${apiUrl}/api/v1/messages/${fiscalCode}`, | ||
{ | ||
body: JSON.stringify(newMessage), | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Ocp-Apim-Subscription-Key": apiKey | ||
}, | ||
method: "POST" | ||
} | ||
); | ||
return response.status; | ||
} | ||
|
||
// Activity result | ||
const ActivityResultSuccess = t.interface({ | ||
kind: t.literal("SUCCESS") | ||
}); | ||
|
||
type ActivityResultSuccess = t.TypeOf<typeof ActivityResultSuccess>; | ||
|
||
const ActivityResultFailure = t.interface({ | ||
kind: t.literal("FAILURE"), | ||
reason: t.string | ||
}); | ||
|
||
type ActivityResultFailure = t.TypeOf<typeof ActivityResultFailure>; | ||
|
||
export const ActivityResult = t.taggedUnion("kind", [ | ||
ActivityResultSuccess, | ||
ActivityResultFailure | ||
]); | ||
export type ActivityResult = t.TypeOf<typeof ActivityResult>; | ||
|
||
export const ActivityInput = t.interface({ | ||
blobName: t.string, | ||
fiscalCode: FiscalCode, | ||
password: t.string | ||
}); | ||
export type ActivityInput = t.TypeOf<typeof ActivityInput>; | ||
|
||
export const getActivityFunction = ( | ||
publicApiUrl: NonEmptyString, | ||
publicApiKey: NonEmptyString, | ||
publicDownloadBaseUrl: NonEmptyString, | ||
timeoutFetch: typeof fetch | ||
) => (context: Context, input: unknown): Promise<ActivityResult> => { | ||
const failure = (reason: string) => { | ||
context.log.error(reason); | ||
return ActivityResultFailure.encode({ | ||
kind: "FAILURE", | ||
reason | ||
}); | ||
}; | ||
|
||
const success = () => | ||
ActivityResultSuccess.encode({ | ||
kind: "SUCCESS" | ||
}); | ||
|
||
return ActivityInput.decode(input).fold<Promise<ActivityResult>>( | ||
async errs => | ||
failure( | ||
`SendUserDataDownloadMessageActivity|Cannot decode input|ERROR=${readableReport( | ||
errs | ||
)}|INPUT=${JSON.stringify(input)}` | ||
), | ||
async ({ blobName, fiscalCode, password }) => { | ||
const logPrefix = `SendUserDataDownloadMessageActivity|PROFILE=${fiscalCode}`; | ||
context.log.verbose(`${logPrefix}|Sending user data download message`); | ||
|
||
// throws in case of timeout so | ||
// the orchestrator can schedule a retry | ||
const status = await sendMessage( | ||
fiscalCode, | ||
publicApiUrl, | ||
publicApiKey, | ||
userDataDownloadMessage(blobName, password, publicDownloadBaseUrl), | ||
timeoutFetch | ||
); | ||
|
||
if (status !== 201) { | ||
const msg = `${logPrefix}|ERROR=${status}`; | ||
if (status >= 500) { | ||
throw new Error(msg); | ||
} else { | ||
return failure(msg); | ||
} | ||
} | ||
|
||
context.log.verbose(`${logPrefix}|RESPONSE=${status}`); | ||
return success(); | ||
} | ||
); | ||
}; | ||
|
||
export default getActivityFunction; |
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,36 @@ | ||
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env"; | ||
import { agent } from "italia-ts-commons"; | ||
import { | ||
AbortableFetch, | ||
setFetchTimeout, | ||
toFetch | ||
} from "italia-ts-commons/lib/fetch"; | ||
import { Millisecond } from "italia-ts-commons/lib/units"; | ||
import { getActivityFunction } from "./handler"; | ||
|
||
// HTTP external requests timeout in milliseconds | ||
const DEFAULT_REQUEST_TIMEOUT_MS = 10000; | ||
|
||
// Needed to call notifications API | ||
const publicApiUrl = getRequiredStringEnv("PUBLIC_API_URL"); | ||
const publicApiKey = getRequiredStringEnv("PUBLIC_API_KEY"); | ||
const publicDownloadBaseUrl = getRequiredStringEnv("PUBLIC_DOWNLOAD_BASE_URL"); | ||
|
||
// HTTP-only fetch with optional keepalive agent | ||
// @see https://github.com/pagopa/io-ts-commons/blob/master/src/agent.ts#L10 | ||
const httpApiFetch = agent.getHttpFetch(process.env); | ||
|
||
// a fetch that can be aborted and that gets cancelled after fetchTimeoutMs | ||
const abortableFetch = AbortableFetch(httpApiFetch); | ||
const timeoutFetch = toFetch( | ||
setFetchTimeout(DEFAULT_REQUEST_TIMEOUT_MS as Millisecond, abortableFetch) | ||
); | ||
|
||
const index = getActivityFunction( | ||
publicApiUrl, | ||
publicApiKey, | ||
publicDownloadBaseUrl, | ||
timeoutFetch | ||
); | ||
|
||
export default index; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you cannot return an Either from an activity since the return value must be serializable (that's the reason we have ActivityResultSuccess / Failure)