Skip to content

Commit

Permalink
Merge branch 'new-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jayair committed May 10, 2018
2 parents c41ad34 + 9088fd7 commit 0263a97
Show file tree
Hide file tree
Showing 20 changed files with 2,103 additions and 1,853 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ To support the different chapters and steps of the tutorial; we use branches to
- [Add a List All the Notes API](../../tree/add-a-list-all-the-notes-api)
- [Add an Update Note API](../../tree/add-an-update-note-api)
- [Add a Delete Note API](../../tree/add-a-delete-note-api)
- [Unit Tests in Serverless](../../tree/unit-tests-in-serverless)

#### Usage

Expand Down Expand Up @@ -59,6 +60,6 @@ $ serverless deploy

#### Maintainers

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.
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.

[Email]: mailto:[email protected]
24 changes: 24 additions & 0 deletions billing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import stripePackage from "stripe";
import { calculateCost } from "./libs/billing-lib";
import { success, failure } from "./libs/response-lib";

export async function main(event, context, callback) {
const { storage, source } = JSON.parse(event.body);
const amount = calculateCost(storage);
const description = "Scratch charge";

// Load our secret key from the environment variables
const stripe = stripePackage(process.env.stripeSecretKey);

try {
await stripe.charges.create({
source,
amount,
description,
currency: "usd"
});
callback(null, success({ status: true }));
} catch (e) {
callback(null, failure({ message: e.message }));
}
}
2 changes: 1 addition & 1 deletion create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { success, failure } from "./libs/response-lib";
export async function main(event, context, callback) {
const data = JSON.parse(event.body);
const params = {
TableName: "notes",
TableName: process.env.tableName,
Item: {
userId: event.requestContext.identity.cognitoIdentityId,
noteId: uuid.v1(),
Expand Down
2 changes: 1 addition & 1 deletion delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { success, failure } from "./libs/response-lib";

export async function main(event, context, callback) {
const params = {
TableName: "notes",
TableName: process.env.tableName,
// 'Key' defines the partition key and sort key of the item to be removed
// - 'userId': Identity Pool identity id of the authenticated user
// - 'noteId': path parameter
Expand Down
12 changes: 0 additions & 12 deletions env.example

This file was deleted.

2 changes: 1 addition & 1 deletion get.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { success, failure } from "./libs/response-lib";

export async function main(event, context, callback) {
const params = {
TableName: "notes",
TableName: process.env.tableName,
// 'Key' defines the partition key and sort key of the item to be retrieved
// - 'userId': Identity Pool identity id of the authenticated user
// - 'noteId': path parameter
Expand Down
16 changes: 0 additions & 16 deletions handler.js

This file was deleted.

9 changes: 9 additions & 0 deletions libs/billing-lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function calculateCost(storage) {
const rate = storage <= 10
? 4
: storage <= 100
? 2
: 1;

return rate * storage * 100;
}
2 changes: 1 addition & 1 deletion list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { success, failure } from "./libs/response-lib";

export async function main(event, context, callback) {
const params = {
TableName: "notes",
TableName: process.env.tableName,
// 'KeyConditionExpression' defines the condition for the query
// - 'userId = :userId': only return items with matching 'userId'
// partition key
Expand Down
8 changes: 8 additions & 0 deletions mocks/billing-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"body": "{\"source\":\"tok_visa\",\"storage\":21}",
"requestContext": {
"identity": {
"cognitoIdentityId": "USER-SUB-1234"
}
}
}
Loading

0 comments on commit 0263a97

Please sign in to comment.