-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
57 lines (51 loc) · 1.5 KB
/
common.js
File metadata and controls
57 lines (51 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const AWS = require('aws-sdk');
AWS.config.apiVersions = {
ssm: '2014-11-06',
dynamodb: '2012-08-10',
sns: '2010-03-31',
sesv2: '2019-09-27'
}
const getTableName = async () => await getSsmParameter(process.env.DB_NAME);
async function getSsmParameter(name, withDecryption = false) {
try {
const ssmClient = new AWS.SSM();
const result = await ssmClient.getParameter({
Name: name,
WithDecryption: withDecryption
}).promise();
return result.Parameter.Value;
} catch(e) {
console.error(e)
throw new Error(`error on retrieving ssm parameter ${name} ${e.message}`)
}
}
async function getCustomersCount() {
const dynamoClient = new AWS.DynamoDB();
const TableName = await getTableName();
const scanResult = await dynamoClient.scan({
TableName,
Select: 'COUNT'
}).promise();
return scanResult.Count || 0;
}
async function getCustomerByEmail(email) {
console.info(`getting customer by email: ${email}`);
const dynamoClient = new AWS.DynamoDB();
const TableName = await getTableName();
const scanResult = await dynamoClient.scan({
TableName,
FilterExpression: 'email = :e',
ExpressionAttributeValues: {
':e': { S: email }
}
}).promise();
return scanResult.Count ? scanResult.Items[0] : undefined;
}
module.exports = {
getCustomersCount,
getSsmParameter,
getCustomerByEmail,
getTableName,
AWS
// configureAws
}