Skip to content

Commit

Permalink
fix: cleanup and various updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jul 17, 2022
1 parent 118c97a commit 5301deb
Show file tree
Hide file tree
Showing 21 changed files with 157 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -41,7 +41,7 @@ test/files/main.js

package-lock.json

.config*
.config.js

# Compiled docs
docs/*.html
Expand Down
6 changes: 6 additions & 0 deletions examples/ecommerce-netlify-functions/.config/development.js
@@ -0,0 +1,6 @@
'use strict';

module.exports = Object.freeze({
mongodbUri: 'mongodb://localhost:27017/ecommerce',
stripeSecretKey: 'YOUR STRIPE KEY HERE'
});
13 changes: 13 additions & 0 deletions examples/ecommerce-netlify-functions/.config/index.js
@@ -0,0 +1,13 @@
'use strict';

if (process.env.NODE_ENV) {
try {
module.exports = require('./' + process.env.NODE_ENV);
console.log('Using ' + process.env.NODE_ENV);
} catch (err) {
module.exports = require('./development');
}
} else {
console.log('using production');
module.exports = require('./production');
}
6 changes: 6 additions & 0 deletions examples/ecommerce-netlify-functions/.config/test.js
@@ -0,0 +1,6 @@
'use strict';

module.exports = Object.freeze({
mongodbUri: 'mongodb://localhost:27017/ecommerce_test',
stripeSecretKey: 'test'
});
54 changes: 54 additions & 0 deletions examples/ecommerce-netlify-functions/README.md
@@ -0,0 +1,54 @@
# ecommerce-netlify-functions

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

Other tools include:

1. Stripe for payment processing
2. [Mocha](https://masteringjs.io/mocha) and [Sinon](https://masteringjs.io/sinon) for testing

## Running This Example

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.
2. Run `npm install`
3. Run `npm run seed`
4. Run `npm start`
5. Visit `http://localhost:8888/.netlify/functions/getProducts` to list all available products
6. Run other endpoints using curl or postman

## Testing

Make sure you have a MongoDB instance running on `localhost:27017`, or update `mongodbUri` in `.config/test.js` to your MongoDB server's address.
Then run `npm test`.

```
$ npm test
> test
> env NODE_ENV=test mocha ./test/*.test.js
Using test
Add to Cart
✔ Should create a cart and add a product to the cart
✔ Should find the cart and add to the cart
✔ Should find the cart and increase the quantity of the item(s) in the cart
Checkout
✔ Should do a successful checkout run
Get the cart given an id
✔ Should create a cart and then find the cart.
Products
✔ Should get all products.
Remove From Cart
✔ Should create a cart and then it should remove the entire item from it.
✔ Should create a cart and then it should reduce the quantity of an item from it.
8 passing (112ms)
```
15 changes: 15 additions & 0 deletions examples/ecommerce-netlify-functions/connect.js
@@ -0,0 +1,15 @@
'use strict';

const config = require('./.config');
const mongoose = require('mongoose');

let conn = null;

module.exports = async function connect() {
if (conn != null) {
return conn;
}
conn = mongoose.connection;
await mongoose.connect(config.mongodbUri);
return conn;
}
20 changes: 0 additions & 20 deletions examples/ecommerce-netlify-functions/dummyProducts.js

This file was deleted.

20 changes: 0 additions & 20 deletions examples/ecommerce-netlify-functions/index.js

This file was deleted.

@@ -1,7 +1,7 @@
'use strict';

const { Cart, Product } = require('../../models');
const connect = require('../../index');
const connect = require('../../connect');

const handler = async(event) => {
try {
Expand All @@ -11,6 +11,11 @@ const handler = async(event) => {
if (event.body.cartId) {
// get the document containing the specified cartId
const cart = await Cart.findOne({ _id: event.body.cartId }).setOptions({ sanitizeFilter: true });

if (cart == null) {
return { statusCode: 404, body: JSON.stringify({ message: 'Cart not found' }) };
}

for (const product of event.body.items) {
const exists = cart.items.find(item => item.productId.toString() === product.productId.toString());
if (!exists && products.find(p => product.productId.toString() === p._id.toString())) {
Expand Down
Expand Up @@ -3,7 +3,7 @@
const stripe = require('../../integrations/stripe')

const { Cart, Order, Product } = require('../../models');
const connect = require('../../index');
const connect = require('../../connect');

const handler = async(event) => {
try {
Expand Down
@@ -1,7 +1,7 @@
'use strict';

const { Cart } = require('../../models');
const connect = require('../../index');
const connect = require('../../connect');

const handler = async(event) => {
try {
Expand Down
@@ -1,7 +1,7 @@
'use strict';

const { Product } = require('../../models');
const connect = require('../../index');
const connect = require('../../connect');

const handler = async(event) => {
try {
Expand Down
@@ -1,7 +1,7 @@
'use strict';

const { Cart } = require('../../models');
const connect = require('../../index');
const connect = require('../../connect');

const handler = async(event) => {
try {
Expand Down
26 changes: 14 additions & 12 deletions examples/ecommerce-netlify-functions/package.json
@@ -1,14 +1,16 @@
{
"dependencies": {
"mongoose": "^6.3.5",
"sinon": "^14.0.0",
"stripe": "^9.6.0"
},
"devDependencies": {
"mocha": "10.0.0",
"netlify-cli": "^10.7.1"
},
"scripts": {
"test": "mocha --exit ./test/*.test.js"
}
"dependencies": {
"mongoose": "6.3.5",
"sinon": "14.0.0",
"stripe": "9.6.0"
},
"devDependencies": {
"mocha": "10.0.0",
"netlify-cli": "10.7.1"
},
"scripts": {
"seed": "env NODE_ENV=development node ./seed",
"start": "env NODE_ENV=development netlify dev",
"test": "env NODE_ENV=test mocha ./test/*.test.js"
}
}
24 changes: 24 additions & 0 deletions examples/ecommerce-netlify-functions/seed.js
@@ -0,0 +1,24 @@
'use strict';

const { Product } = require('./models');
const config = require('./.config');
const mongoose = require('mongoose');

async function createProducts() {
await mongoose.connect(config.mongodbUri);

await Product.create({
productName: 'Dummy Product',
productPrice: 500
});

await Product.create({
productName: 'Another Dummy Product',
productPrice: 600
});

console.log('done');
process.exit(0);
}

createProducts();
10 changes: 0 additions & 10 deletions examples/ecommerce-netlify-functions/test/addToCart.test.js
Expand Up @@ -6,17 +6,7 @@ const { handler: addToCart } = require('../netlify/functions/addToCart');
const mongoose = require('mongoose');
const fixtures = require('../test/fixtures');



describe('Add to Cart', function() {
before(async() => {
await mongoose.connect('mongodb://localhost:27017/netlify');
await mongoose.connection.dropDatabase();
});

after(async() => {
await mongoose.disconnect();
});
it('Should create a cart and add a product to the cart', async function() {
const products = await fixtures.createProducts({product: [{ productName: 'A Test Products', productPrice: 500 }, {productName: 'Another Test Product', productPrice: 600 }]})
.then((res) => res.products);
Expand Down
8 changes: 0 additions & 8 deletions examples/ecommerce-netlify-functions/test/checkout.test.js
Expand Up @@ -10,14 +10,6 @@ const sinon = require('sinon');
const stripe = require('../integrations/stripe')

describe('Checkout', function() {
before(async() => {
await mongoose.connect('mongodb://localhost:27017/netlify');
await mongoose.connection.dropDatabase();
});

after(async() => {
await mongoose.disconnect();
});
it('Should do a successful checkout run', async function() {
const products = await fixtures.createProducts({product: [{ productName: 'A Test Products', productPrice: 500 }, {productName: 'Another Test Product', productPrice: 600 }]})
.then((res) => res.products);
Expand Down
10 changes: 0 additions & 10 deletions examples/ecommerce-netlify-functions/test/getCart.test.js
Expand Up @@ -6,17 +6,7 @@ const { handler: getCart } = require('../netlify/functions/getCart');
const fixtures = require('./fixtures');
const mongoose = require('mongoose');



describe('Get the cart given an id', function() {
before(async() => {
await mongoose.connect('mongodb://localhost:27017/netlify');
await mongoose.connection.dropDatabase();
});

after(async() => {
await mongoose.disconnect();
});
it('Should create a cart and then find the cart.', async function() {
const cart = await fixtures.createCart({ products: null });
const params = {
Expand Down
9 changes: 0 additions & 9 deletions examples/ecommerce-netlify-functions/test/getProducts.test.js
Expand Up @@ -7,15 +7,6 @@ const fixtures = require('./fixtures');
const mongoose = require('mongoose');

describe('Products', function() {
before(async() => {
await mongoose.connect('mongodb://localhost:27017/netlify');
await mongoose.connection.dropDatabase();
});

after(async() => {
await mongoose.disconnect();
});

it('Should get all products.', async function() {
await fixtures.createProducts({product: [{ productName: 'A Test Products', productPrice: 500 }, {productName: 'Another Test Product', productPrice: 600 }]})
.then((res) => res.products);
Expand Down
12 changes: 0 additions & 12 deletions examples/ecommerce-netlify-functions/test/removeFromCart.test.js
Expand Up @@ -7,18 +7,7 @@ const { handler: removeFromCart } = require('../netlify/functions/removeFromCart
const mongoose = require('mongoose');
const fixtures = require('./fixtures');



describe('Remove From Cart', function() {
before(async() => {
await mongoose.connect('mongodb://localhost:27017/netlify');
await mongoose.connection.dropDatabase();
});

after(async() => {
await mongoose.disconnect();
});

it('Should create a cart and then it should remove the entire item from it.', async function() {
const products = await fixtures.createProducts({product: [{ productName: 'A Test Products', productPrice: 500 }, {productName: 'Another Test Product', productPrice: 600 }]})
.then((res) => res.products);
Expand Down Expand Up @@ -81,5 +70,4 @@ describe('Remove From Cart', function() {
remove.body = JSON.parse(remove.body);
assert.equal(remove.body.items[0].quantity, 1);
});

});
14 changes: 14 additions & 0 deletions examples/ecommerce-netlify-functions/test/setup.test.js
@@ -0,0 +1,14 @@
'use strict';

const { after } = require('mocha');
const config = require('../.config');
const mongoose = require('mongoose');

before(async() => {
await mongoose.connect(config.mongodbUri);
await mongoose.connection.dropDatabase();
});

after(async function() {
await mongoose.disconnect();
});

0 comments on commit 5301deb

Please sign in to comment.