Skip to content

Commit

Permalink
feat: start report lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
marnixdessing committed Oct 27, 2022
1 parent 858f7af commit 8eb5db0
Show file tree
Hide file tree
Showing 17 changed files with 710 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .projen/files.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const project = new awscdk.AwsCdkTypeScriptApp({
'@aws-sdk/client-dynamodb',
'@aws-sdk/client-secrets-manager',
'@aws-sdk/client-ssm',
'@aws-sdk/client-ses',
'@gemeentenijmegen/apiclient',
'@gemeentenijmegen/session',
'@gemeentenijmegen/utils',
Expand Down
5 changes: 4 additions & 1 deletion package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/ParameterStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,10 @@ export class ssmParamsConstruct extends Construct {
secretName: Statics.secretIrmaApiKey,
description: 'IRMA API key',
});

new SSM.StringParameter(this, 'ssm_irma_statistics_1', {
stringValue: '-',
parameterName: Statics.ssmIrmaStatisticsRecipients,
});
}
}
15 changes: 15 additions & 0 deletions src/app/code/AwsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
SecretsManagerClient,
GetSecretValueCommand,
} from '@aws-sdk/client-secrets-manager';
import { GetParameterCommand, SSMClient } from '@aws-sdk/client-ssm';

export class AwsUtil {

Expand All @@ -23,4 +24,18 @@ export class AwsUtil {
throw new Error('No secret value found');
}

/**
* Get a parameter from parameter store. This is used
* as a workaround for the 4kb limit for environment variables.
*
* @param {string} parameter Name of the ssm param
* @returns param value
*/
async getParameter(parameter: string){
const client = new SSMClient({});
const command = new GetParameterCommand({ Name: parameter });
const response = await client.send(command);
return response.Parameter?.Value;
}

}
2 changes: 1 addition & 1 deletion src/app/static-resources/static/scripts/issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ irmaClient.start()
.catch((err) => { // Hide QR show error message
document.getElementById('irma-form').classList.add("hidden");
document.getElementById('failed-irma').classList.remove("hidden");
fetch(encodeURI('/callback?result=failure&error=' + err.message))
fetch(encodeURI('/callback?result=failure&error=' + err))
.then(() => console.log('Callback succesfull'))
.catch(err => console.log('Callback failed', err));
});
26 changes: 26 additions & 0 deletions src/report/report-function.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/report/report.lambda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { SESClient } from '@aws-sdk/client-ses'
import { AwsUtil } from '../app/code/AwsUtil';
import { handleReporting } from './reportRequestHandler';

const dynamoDBClient = new DynamoDBClient({region: 'eu-west-1'});
const sesClient = new SESClient({region: 'eu-west-1'});
let recipients: string[] = [];

async function init() {
if(!process.env.RECIPIENTS_LIST){
throw Error("process.env.RECIPIENTS_LIST not set!");
}
const util = new AwsUtil();
const recipientsList = await util.getParameter(process.env.RECIPIENTS_LIST);
recipients = recipientsList ? recipientsList.split('\n') : [];
}

const initialization = init();

exports.handler = async () => {
try {
await initialization;
await handleReporting(sesClient, dynamoDBClient, recipients);
} catch (err) {
console.error(err);
}
};
17 changes: 17 additions & 0 deletions src/report/report.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{>header}}

<main>
<!-- START: template component(s) -->
<div class="container" id="section-1">
<h1>{{name}} Opladen</h1>
<p class="lead">Login met DigiD en laad uw gegevens in de {{name}} app.</p>
<a href="{{{authUrl}}}" class="btn btn-primary waves-effect waves-light display-table">
<span class="title"> Inloggen </span><span class="assistive">via DigiD</span>
</a>
</div>
</div>

<!-- END: template component(s) -->
</main>

{{>footer}}
49 changes: 49 additions & 0 deletions src/report/reportRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses'
import Mustache from 'mustache';
import template from './report.mustache';

export async function handleReporting(sesClient: SESClient, dynamoDBClient: DynamoDBClient, recipients: string[]){

console.log(dynamoDBClient.config.apiVersion);
// Do dynamodb query (yesterday)
// Do dynamodb query (this month)
// Do dynamodb query (this year)
// calculate statistics
// Calculate nr of issuances with same bsn

const data = {}; // TODO fill with statistics from above.

const report = renderReport(data);
await sendReportViaMail(sesClient, report, recipients);

}

function renderReport(data: any) {
return Mustache.render(template, data);
}

async function sendReportViaMail(client: SESClient, body: string, recipients: string[]) {

const sendEmail = new SendEmailCommand({
Destination: {
ToAddresses: recipients,
},
Message: {
Subject: {
Charset: 'UTF-8',
Data: 'IRMA Issue Statistics',
},
Body: {
Html: {
Charset: "UTF-8",
Data: body
},
},
},
Source: 'gemeente@accp.csp-nijmegen.nl'
});

await client.send(sendEmail)

}
2 changes: 2 additions & 0 deletions src/statics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export abstract class Statics {
static readonly secretIrmaApiSecretKey: string = '/cdk/irma-issue-app/irma-api-secret-key';
static readonly secretIrmaApiKey: string = '/cdk/irma-issue-app/irma-api-key';

static readonly ssmIrmaStatisticsRecipients: string = '/cdk/irma-issue-app/irma-statistics/recipients';

static readonly codeStarConnectionArn: string = 'arn:aws:codestar-connections:eu-west-1:418648875085:connection/4f647929-c982-4f30-94f4-24ff7dbf9766';


Expand Down
Loading

0 comments on commit 8eb5db0

Please sign in to comment.