Skip to content

Commit 5301deb

Browse files
committed
fix: cleanup and various updates
1 parent 118c97a commit 5301deb

21 files changed

+157
-107
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test/files/main.js
4141

4242
package-lock.json
4343

44-
.config*
44+
.config.js
4545

4646
# Compiled docs
4747
docs/*.html
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
module.exports = Object.freeze({
4+
mongodbUri: 'mongodb://localhost:27017/ecommerce',
5+
stripeSecretKey: 'YOUR STRIPE KEY HERE'
6+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
if (process.env.NODE_ENV) {
4+
try {
5+
module.exports = require('./' + process.env.NODE_ENV);
6+
console.log('Using ' + process.env.NODE_ENV);
7+
} catch (err) {
8+
module.exports = require('./development');
9+
}
10+
} else {
11+
console.log('using production');
12+
module.exports = require('./production');
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
module.exports = Object.freeze({
4+
mongodbUri: 'mongodb://localhost:27017/ecommerce_test',
5+
stripeSecretKey: 'test'
6+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# ecommerce-netlify-functions
2+
3+
This sample demonstrates using Mongoose to build an eCommerce shopping cart using [Netlify Functions](https://www.netlify.com/products/functions/), which runs on [AWS Lambda](https://mongoosejs.com/docs/lambda.html).
4+
5+
Other tools include:
6+
7+
1. Stripe for payment processing
8+
2. [Mocha](https://masteringjs.io/mocha) and [Sinon](https://masteringjs.io/sinon) for testing
9+
10+
## Running This Example
11+
12+
1. Make sure you have a MongoDB instance running on `localhost:27017`, or update `mongodbUri` in `.config/development.js` to your MongoDB server's address.
13+
2. Run `npm install`
14+
3. Run `npm run seed`
15+
4. Run `npm start`
16+
5. Visit `http://localhost:8888/.netlify/functions/getProducts` to list all available products
17+
6. Run other endpoints using curl or postman
18+
19+
## Testing
20+
21+
Make sure you have a MongoDB instance running on `localhost:27017`, or update `mongodbUri` in `.config/test.js` to your MongoDB server's address.
22+
Then run `npm test`.
23+
24+
```
25+
$ npm test
26+
27+
> test
28+
> env NODE_ENV=test mocha ./test/*.test.js
29+
30+
Using test
31+
32+
33+
Add to Cart
34+
✔ Should create a cart and add a product to the cart
35+
✔ Should find the cart and add to the cart
36+
✔ Should find the cart and increase the quantity of the item(s) in the cart
37+
38+
Checkout
39+
✔ Should do a successful checkout run
40+
41+
Get the cart given an id
42+
✔ Should create a cart and then find the cart.
43+
44+
Products
45+
✔ Should get all products.
46+
47+
Remove From Cart
48+
✔ Should create a cart and then it should remove the entire item from it.
49+
✔ Should create a cart and then it should reduce the quantity of an item from it.
50+
51+
52+
8 passing (112ms)
53+
54+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const config = require('./.config');
4+
const mongoose = require('mongoose');
5+
6+
let conn = null;
7+
8+
module.exports = async function connect() {
9+
if (conn != null) {
10+
return conn;
11+
}
12+
conn = mongoose.connection;
13+
await mongoose.connect(config.mongodbUri);
14+
return conn;
15+
}

examples/ecommerce-netlify-functions/dummyProducts.js

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

examples/ecommerce-netlify-functions/index.js

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

examples/ecommerce-netlify-functions/netlify/functions/addToCart.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const { Cart, Product } = require('../../models');
4-
const connect = require('../../index');
4+
const connect = require('../../connect');
55

66
const handler = async(event) => {
77
try {
@@ -11,6 +11,11 @@ const handler = async(event) => {
1111
if (event.body.cartId) {
1212
// get the document containing the specified cartId
1313
const cart = await Cart.findOne({ _id: event.body.cartId }).setOptions({ sanitizeFilter: true });
14+
15+
if (cart == null) {
16+
return { statusCode: 404, body: JSON.stringify({ message: 'Cart not found' }) };
17+
}
18+
1419
for (const product of event.body.items) {
1520
const exists = cart.items.find(item => item.productId.toString() === product.productId.toString());
1621
if (!exists && products.find(p => product.productId.toString() === p._id.toString())) {

examples/ecommerce-netlify-functions/netlify/functions/checkout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const stripe = require('../../integrations/stripe')
44

55
const { Cart, Order, Product } = require('../../models');
6-
const connect = require('../../index');
6+
const connect = require('../../connect');
77

88
const handler = async(event) => {
99
try {

examples/ecommerce-netlify-functions/netlify/functions/getCart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const { Cart } = require('../../models');
4-
const connect = require('../../index');
4+
const connect = require('../../connect');
55

66
const handler = async(event) => {
77
try {

examples/ecommerce-netlify-functions/netlify/functions/getProducts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const { Product } = require('../../models');
4-
const connect = require('../../index');
4+
const connect = require('../../connect');
55

66
const handler = async(event) => {
77
try {

examples/ecommerce-netlify-functions/netlify/functions/removeFromCart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const { Cart } = require('../../models');
4-
const connect = require('../../index');
4+
const connect = require('../../connect');
55

66
const handler = async(event) => {
77
try {
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
2-
"dependencies": {
3-
"mongoose": "^6.3.5",
4-
"sinon": "^14.0.0",
5-
"stripe": "^9.6.0"
6-
},
7-
"devDependencies": {
8-
"mocha": "10.0.0",
9-
"netlify-cli": "^10.7.1"
10-
},
11-
"scripts": {
12-
"test": "mocha --exit ./test/*.test.js"
13-
}
2+
"dependencies": {
3+
"mongoose": "6.3.5",
4+
"sinon": "14.0.0",
5+
"stripe": "9.6.0"
6+
},
7+
"devDependencies": {
8+
"mocha": "10.0.0",
9+
"netlify-cli": "10.7.1"
10+
},
11+
"scripts": {
12+
"seed": "env NODE_ENV=development node ./seed",
13+
"start": "env NODE_ENV=development netlify dev",
14+
"test": "env NODE_ENV=test mocha ./test/*.test.js"
15+
}
1416
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const { Product } = require('./models');
4+
const config = require('./.config');
5+
const mongoose = require('mongoose');
6+
7+
async function createProducts() {
8+
await mongoose.connect(config.mongodbUri);
9+
10+
await Product.create({
11+
productName: 'Dummy Product',
12+
productPrice: 500
13+
});
14+
15+
await Product.create({
16+
productName: 'Another Dummy Product',
17+
productPrice: 600
18+
});
19+
20+
console.log('done');
21+
process.exit(0);
22+
}
23+
24+
createProducts();

examples/ecommerce-netlify-functions/test/addToCart.test.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@ const { handler: addToCart } = require('../netlify/functions/addToCart');
66
const mongoose = require('mongoose');
77
const fixtures = require('../test/fixtures');
88

9-
10-
119
describe('Add to Cart', function() {
12-
before(async() => {
13-
await mongoose.connect('mongodb://localhost:27017/netlify');
14-
await mongoose.connection.dropDatabase();
15-
});
16-
17-
after(async() => {
18-
await mongoose.disconnect();
19-
});
2010
it('Should create a cart and add a product to the cart', async function() {
2111
const products = await fixtures.createProducts({product: [{ productName: 'A Test Products', productPrice: 500 }, {productName: 'Another Test Product', productPrice: 600 }]})
2212
.then((res) => res.products);

examples/ecommerce-netlify-functions/test/checkout.test.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ const sinon = require('sinon');
1010
const stripe = require('../integrations/stripe')
1111

1212
describe('Checkout', function() {
13-
before(async() => {
14-
await mongoose.connect('mongodb://localhost:27017/netlify');
15-
await mongoose.connection.dropDatabase();
16-
});
17-
18-
after(async() => {
19-
await mongoose.disconnect();
20-
});
2113
it('Should do a successful checkout run', async function() {
2214
const products = await fixtures.createProducts({product: [{ productName: 'A Test Products', productPrice: 500 }, {productName: 'Another Test Product', productPrice: 600 }]})
2315
.then((res) => res.products);

examples/ecommerce-netlify-functions/test/getCart.test.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@ const { handler: getCart } = require('../netlify/functions/getCart');
66
const fixtures = require('./fixtures');
77
const mongoose = require('mongoose');
88

9-
10-
119
describe('Get the cart given an id', function() {
12-
before(async() => {
13-
await mongoose.connect('mongodb://localhost:27017/netlify');
14-
await mongoose.connection.dropDatabase();
15-
});
16-
17-
after(async() => {
18-
await mongoose.disconnect();
19-
});
2010
it('Should create a cart and then find the cart.', async function() {
2111
const cart = await fixtures.createCart({ products: null });
2212
const params = {

examples/ecommerce-netlify-functions/test/getProducts.test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ const fixtures = require('./fixtures');
77
const mongoose = require('mongoose');
88

99
describe('Products', function() {
10-
before(async() => {
11-
await mongoose.connect('mongodb://localhost:27017/netlify');
12-
await mongoose.connection.dropDatabase();
13-
});
14-
15-
after(async() => {
16-
await mongoose.disconnect();
17-
});
18-
1910
it('Should get all products.', async function() {
2011
await fixtures.createProducts({product: [{ productName: 'A Test Products', productPrice: 500 }, {productName: 'Another Test Product', productPrice: 600 }]})
2112
.then((res) => res.products);

examples/ecommerce-netlify-functions/test/removeFromCart.test.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,7 @@ const { handler: removeFromCart } = require('../netlify/functions/removeFromCart
77
const mongoose = require('mongoose');
88
const fixtures = require('./fixtures');
99

10-
11-
1210
describe('Remove From Cart', function() {
13-
before(async() => {
14-
await mongoose.connect('mongodb://localhost:27017/netlify');
15-
await mongoose.connection.dropDatabase();
16-
});
17-
18-
after(async() => {
19-
await mongoose.disconnect();
20-
});
21-
2211
it('Should create a cart and then it should remove the entire item from it.', async function() {
2312
const products = await fixtures.createProducts({product: [{ productName: 'A Test Products', productPrice: 500 }, {productName: 'Another Test Product', productPrice: 600 }]})
2413
.then((res) => res.products);
@@ -81,5 +70,4 @@ describe('Remove From Cart', function() {
8170
remove.body = JSON.parse(remove.body);
8271
assert.equal(remove.body.items[0].quantity, 1);
8372
});
84-
8573
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const { after } = require('mocha');
4+
const config = require('../.config');
5+
const mongoose = require('mongoose');
6+
7+
before(async() => {
8+
await mongoose.connect(config.mongodbUri);
9+
await mongoose.connection.dropDatabase();
10+
});
11+
12+
after(async function() {
13+
await mongoose.disconnect();
14+
});

0 commit comments

Comments
 (0)