Skip to content

Commit 0263a97

Browse files
committed
Merge branch 'new-master'
2 parents c41ad34 + 9088fd7 commit 0263a97

20 files changed

Lines changed: 2103 additions & 1853 deletions

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ To support the different chapters and steps of the tutorial; we use branches to
1515
- [Add a List All the Notes API](../../tree/add-a-list-all-the-notes-api)
1616
- [Add an Update Note API](../../tree/add-an-update-note-api)
1717
- [Add a Delete Note API](../../tree/add-a-delete-note-api)
18+
- [Unit Tests in Serverless](../../tree/unit-tests-in-serverless)
1819

1920
#### Usage
2021

@@ -59,6 +60,6 @@ $ serverless deploy
5960

6061
#### Maintainers
6162

62-
Serverless Stack is authored and maintained by Frank Wang ([@fanjiewang](https://twitter.com/fanjiewang)) & Jay V ([@jayair](https://twitter.com/jayair)). [**Subscribe to our newsletter**](http://eepurl.com/cEaBlf) for updates on Serverless Stack. Send us an [email][Email] if you have any questions.
63+
Serverless Stack is authored and maintained by Frank Wang ([@fanjiewang](https://twitter.com/fanjiewang)) & Jay V ([@jayair](https://twitter.com/jayair)). [**Subscribe to our newsletter**](https://emailoctopus.com/lists/1c11b9a8-1500-11e8-a3c9-06b79b628af2/forms/subscribe) for updates on Serverless Stack. Send us an [email][Email] if you have any questions.
6364

6465
[Email]: mailto:contact@anoma.ly

billing.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import stripePackage from "stripe";
2+
import { calculateCost } from "./libs/billing-lib";
3+
import { success, failure } from "./libs/response-lib";
4+
5+
export async function main(event, context, callback) {
6+
const { storage, source } = JSON.parse(event.body);
7+
const amount = calculateCost(storage);
8+
const description = "Scratch charge";
9+
10+
// Load our secret key from the environment variables
11+
const stripe = stripePackage(process.env.stripeSecretKey);
12+
13+
try {
14+
await stripe.charges.create({
15+
source,
16+
amount,
17+
description,
18+
currency: "usd"
19+
});
20+
callback(null, success({ status: true }));
21+
} catch (e) {
22+
callback(null, failure({ message: e.message }));
23+
}
24+
}

create.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { success, failure } from "./libs/response-lib";
55
export async function main(event, context, callback) {
66
const data = JSON.parse(event.body);
77
const params = {
8-
TableName: "notes",
8+
TableName: process.env.tableName,
99
Item: {
1010
userId: event.requestContext.identity.cognitoIdentityId,
1111
noteId: uuid.v1(),

delete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { success, failure } from "./libs/response-lib";
33

44
export async function main(event, context, callback) {
55
const params = {
6-
TableName: "notes",
6+
TableName: process.env.tableName,
77
// 'Key' defines the partition key and sort key of the item to be removed
88
// - 'userId': Identity Pool identity id of the authenticated user
99
// - 'noteId': path parameter

env.example

Lines changed: 0 additions & 12 deletions
This file was deleted.

get.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { success, failure } from "./libs/response-lib";
33

44
export async function main(event, context, callback) {
55
const params = {
6-
TableName: "notes",
6+
TableName: process.env.tableName,
77
// 'Key' defines the partition key and sort key of the item to be retrieved
88
// - 'userId': Identity Pool identity id of the authenticated user
99
// - 'noteId': path parameter

handler.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

libs/billing-lib.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function calculateCost(storage) {
2+
const rate = storage <= 10
3+
? 4
4+
: storage <= 100
5+
? 2
6+
: 1;
7+
8+
return rate * storage * 100;
9+
}

list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { success, failure } from "./libs/response-lib";
33

44
export async function main(event, context, callback) {
55
const params = {
6-
TableName: "notes",
6+
TableName: process.env.tableName,
77
// 'KeyConditionExpression' defines the condition for the query
88
// - 'userId = :userId': only return items with matching 'userId'
99
// partition key

mocks/billing-event.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"body": "{\"source\":\"tok_visa\",\"storage\":21}",
3+
"requestContext": {
4+
"identity": {
5+
"cognitoIdentityId": "USER-SUB-1234"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)