Skip to content

Commit f4d8694

Browse files
committed
Small tweaks
1 parent f64f667 commit f4d8694

File tree

10 files changed

+36
-55
lines changed

10 files changed

+36
-55
lines changed

services/notes/billing.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export const main = handler(async (event, context) => {
1414
source,
1515
amount,
1616
description,
17-
currency: "usd"
17+
currency: "usd",
1818
});
19+
1920
return { status: true };
2021
});

services/notes/create.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,14 @@ export const main = handler(async (event, context) => {
66
const data = JSON.parse(event.body);
77
const params = {
88
TableName: process.env.tableName,
9-
// 'Item' contains the attributes of the item to be created
10-
// - 'userId': user identities are federated through the
11-
// Cognito Identity Pool, we will use the identity id
12-
// as the user id of the authenticated user
13-
// - 'noteId': a unique uuid
14-
// - 'content': parsed from request body
15-
// - 'attachment': parsed from request body
16-
// - 'createdAt': current Unix timestamp
179
Item: {
18-
userId: event.requestContext.identity.cognitoIdentityId,
19-
noteId: uuid.v1(),
20-
content: data.content,
21-
attachment: data.attachment,
22-
createdAt: Date.now()
23-
}
10+
// The attributes of the item to be created
11+
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
12+
noteId: uuid.v1(), // A unique uuid
13+
content: data.content, // Parsed from request body
14+
attachment: data.attachment, // Parsed from request body
15+
createdAt: Date.now(), // Current Unix timestamp
16+
},
2417
};
2518

2619
await dynamoDb.put(params);

services/notes/delete.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ export const main = handler(async (event, context) => {
55
const params = {
66
TableName: process.env.tableName,
77
// 'Key' defines the partition key and sort key of the item to be removed
8-
// - 'userId': Identity Pool identity id of the authenticated user
9-
// - 'noteId': path parameter
108
Key: {
11-
userId: event.requestContext.identity.cognitoIdentityId,
12-
noteId: event.pathParameters.id
13-
}
9+
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
10+
noteId: event.pathParameters.id, // The id of the note from the path
11+
},
1412
};
1513

1614
await dynamoDb.delete(params);

services/notes/get.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ export const main = handler(async (event, context) => {
55
const params = {
66
TableName: process.env.tableName,
77
// 'Key' defines the partition key and sort key of the item to be retrieved
8-
// - 'userId': Identity Pool identity id of the authenticated user
9-
// - 'noteId': path parameter
108
Key: {
11-
userId: event.requestContext.identity.cognitoIdentityId,
12-
noteId: event.pathParameters.id
13-
}
9+
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
10+
noteId: event.pathParameters.id, // The id of the note from the path
11+
},
1412
};
1513

1614
const result = await dynamoDb.get(params);
17-
if ( ! result.Item) {
15+
if (!result.Item) {
1816
throw new Error("Item not found.");
1917
}
2018

2119
// Return the retrieved item
2220
return result.Item;
2321
});
24-

services/notes/libs/billing-lib.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
export function calculateCost(storage) {
2-
const rate = storage <= 10
3-
? 4
4-
: storage <= 100
5-
? 2
6-
: 1;
2+
const rate = storage <= 10 ? 4 : storage <= 100 ? 2 : 1;
73

84
return rate * storage * 100;
95
}

services/notes/libs/dynamodb-lib.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import AWS from "aws-sdk";
33
const client = new AWS.DynamoDB.DocumentClient();
44

55
export default {
6-
get : (params) => client.get(params).promise(),
7-
put : (params) => client.put(params).promise(),
8-
query : (params) => client.query(params).promise(),
6+
get: (params) => client.get(params).promise(),
7+
put: (params) => client.put(params).promise(),
8+
query: (params) => client.query(params).promise(),
99
update: (params) => client.update(params).promise(),
1010
delete: (params) => client.delete(params).promise(),
1111
};

services/notes/list.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ export const main = handler(async (event, context) => {
77
// 'KeyConditionExpression' defines the condition for the query
88
// - 'userId = :userId': only return items with matching 'userId'
99
// partition key
10-
// 'ExpressionAttributeValues' defines the value in the condition
11-
// - ':userId': defines 'userId' to be Identity Pool identity id
12-
// of the authenticated user
1310
KeyConditionExpression: "userId = :userId",
11+
// 'ExpressionAttributeValues' defines the value in the condition
12+
// - ':userId': defines 'userId' to be the id of the author
1413
ExpressionAttributeValues: {
15-
":userId": event.requestContext.identity.cognitoIdentityId
16-
}
14+
":userId": event.requestContext.identity.cognitoIdentityId,
15+
},
1716
};
1817

1918
const result = await dynamoDb.query(params);

services/notes/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
"url": "https://github.com/AnomalyInnovations/serverless-nodejs-starter.git"
1414
},
1515
"devDependencies": {
16-
"aws-sdk": "^2.768.0",
17-
"jest": "^26.5.2",
18-
"serverless-bundle": "2.0.0",
19-
"serverless-dotenv-plugin": "^2.1.1",
20-
"serverless-offline": "^5.3.3"
16+
"aws-sdk": "^2.773.0",
17+
"serverless-bundle": "3.2.0",
18+
"serverless-dotenv-plugin": "^2.3.2",
19+
"serverless-offline": "^6.1.4"
2120
},
2221
"dependencies": {
23-
"stripe": "^8.107.0",
22+
"stripe": "^8.119.0",
2423
"uuid": "^7.0.3"
2524
}
2625
}

services/notes/serverless.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
service: notes-api
1+
service: notes-app-api
22

33
# Create an optimized package for our functions
44
package:
@@ -28,6 +28,8 @@ provider:
2828
stripeSecretKey: ${env:STRIPE_SECRET_KEY}
2929
tableName: !ImportValue '${self:custom.sstApp}-TableName'
3030

31+
# 'iamRoleStatements' defines the permission policy for the Lambda function.
32+
# In this case Lambda functions are granted with permissions to access DynamoDB.
3133
iamRoleStatements:
3234
- Effect: Allow
3335
Action:
@@ -47,8 +49,6 @@ functions:
4749
# Defines an HTTP API endpoint that calls the main function in create.js
4850
# - path: url path is /notes
4951
# - method: POST request
50-
# - cors: enabled CORS (Cross-Origin Resource Sharing) for browser cross
51-
# domain api call
5252
# - authorizer: authenticate using the AWS IAM role
5353
create:
5454
handler: create.main

services/notes/update.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@ export const main = handler(async (event, context) => {
66
const params = {
77
TableName: process.env.tableName,
88
// 'Key' defines the partition key and sort key of the item to be updated
9-
// - 'userId': Identity Pool identity id of the authenticated user
10-
// - 'noteId': path parameter
119
Key: {
12-
userId: event.requestContext.identity.cognitoIdentityId,
13-
noteId: event.pathParameters.id
10+
userId: event.requestContext.identity.cognitoIdentityId, // The id of the author
11+
noteId: event.pathParameters.id, // The id of the note from the path
1412
},
1513
// 'UpdateExpression' defines the attributes to be updated
1614
// 'ExpressionAttributeValues' defines the value in the update expression
1715
UpdateExpression: "SET content = :content, attachment = :attachment",
1816
ExpressionAttributeValues: {
1917
":attachment": data.attachment || null,
20-
":content": data.content || null
18+
":content": data.content || null,
2119
},
2220
// 'ReturnValues' specifies if and how to return the item's attributes,
2321
// where ALL_NEW returns all attributes of the item after the update; you
2422
// can inspect 'result' below to see how it works with different settings
25-
ReturnValues: "ALL_NEW"
23+
ReturnValues: "ALL_NEW",
2624
};
2725

2826
await dynamoDb.update(params);

0 commit comments

Comments
 (0)