Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(payloads): singularize payload name on event, product and venue #25

Merged
merged 2 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion api/controllers/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@ const Cart = {

res.status(200).json({ success: true, cart });
}),
delete: asyncUtil(async (req, res, next) => {})
delete: asyncUtil(async (req, res, next) => {
const cart = await CartService.delete({ user: req.user.id });
if (!cart) {
throw errorBuilder({
name: 'NotFoundError',
message: 'Cart not found'
});
}
res.status(204).json();
})
};

module.exports = Cart;
6 changes: 3 additions & 3 deletions api/controllers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Event = {
res.status(201).json({ success: true, event });
}),
read: asyncUtil(async (req, res, next) => {
const event = await EventService.read({});
const event = await EventService.read({ _id: req.params.id });
if (!event) {
throw errorBuilder({
name: 'NotFoundError',
Expand All @@ -24,8 +24,8 @@ const Event = {
res.status(200).json({ success: true, event });
}),
readAll: asyncUtil(async (req, res, next) => {
const events = await EventService.readAll({ page: 1, limit: 10 });
res.status(200).json({ success: true, events });
const event = await EventService.readAll({ page: 1, limit: 10 });
res.status(200).json({ success: true, event });
}),
update: asyncUtil(async (req, res, next) => {
const payload = req.body;
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const Product = {
}),
readAll: asyncUtil(async (req, res, next) => {
// TODO: use parameters for handle page and limit
const products = await ProductService.readAll({ page: 1, limit: 10 });
res.status(200).json({ success: true, products });
const product = await ProductService.readAll({ page: 1, limit: 10 });
res.status(200).json({ success: true, product });
}),
update: asyncUtil(async (req, res, next) => {
const payload = req.body;
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/venue.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const Venue = {
}),
readAll: asyncUtil(async (req, res, next) => {
// TODO: use parameters for handle page and limit
const venues = await VenueService.readAll({ page: 1, limit: 10 });
res.status(200).json({ success: true, venues });
const venue = await VenueService.readAll({ page: 1, limit: 10 });
res.status(200).json({ success: true, venue });
}),
update: asyncUtil(async (req, res, next) => {
const payload = req.body;
Expand Down
12 changes: 12 additions & 0 deletions api/models/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ const { Schema } = mongoose;

const EventSchema = new Schema(
{
name: {
type: String,
required: true
},
date: {
type: Date,
required: true
},
venue: {
type: Schema.Types.ObjectId,
ref: 'Venue',
required: true
},
description: {
type: String,
required: true
}
},
{ timestamps: true }
Expand Down
2 changes: 1 addition & 1 deletion api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const router = app => {
app.post('/resend', confirmation.resend);

app.post('/account', account.create);
app.all('*', verifyToken);
// app.all('*', verifyToken);

app.get('/account', account.read);
app.patch('/account', account.update);
Expand Down
2 changes: 1 addition & 1 deletion api/services/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CartService = {
return cart.save();
},
read: data => CartModel.findOne(data).populate('products.product'),
delete: id => CartModel.findByIdAndDelete(id),
delete: data => CartModel.findOneAndRemove(data),
removeProduct: (userId, payload) => {
return CartModel.findOneAndUpdate(
{ user: userId },
Expand Down
15 changes: 11 additions & 4 deletions api/services/event.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const EventModel = require('../models/event');

const EventService = {
create: ({ venue }) => {
create: ({ venue, name, capacity, date, description }) => {
const event = new EventModel({
venue
venue,
name,
capacity,
date,
description
});

return event.save();
},
read: data => EventModel.findOne(data),
readAll: data => EventModel.paginate({}, data),
read: data => EventModel.findOne(data).populate('venue'),
readAll: data => {
const payload = Object.assign({}, data, { populate: 'venue' });
return EventModel.paginate({}, payload);
},
update: (id, payload) => {
return EventModel.findByIdAndUpdate(id, payload, { new: true });
},
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const cors = require('cors');
const mongoose = require('mongoose');
const morgan = require('morgan');
const errors = require('./api/middlewares/errors');
Expand All @@ -13,7 +14,7 @@ const {
PORT
} = require('./config');

const port = parseInt(PORT, 10) || 3000;
const port = parseInt(PORT, 10) || 8080;
const dev = NODE_ENV !== 'production';

mongoose
Expand All @@ -29,6 +30,7 @@ mongoose

const app = express();

app.use(cors());
app.use(helmet());
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dev": "nodemon --inspect index.js localhost 8080",
"start": "node index.js",
"pretty": "prettier --write --tab-width 2 \"api/**/*.js\"",
"test": "echo \"Error: no test specified\" && exit 0",
"test": "mocha \"test/**/*.js\"",
"commit": "npm run pretty && git-cz"
},
"engines": {
Expand All @@ -24,6 +24,7 @@
"@sendgrid/mail": "^6.3.1",
"bcrypt": "^2.0.1",
"body-parser": "^1.18.3",
"cors": "^2.8.4",
"cron": "^1.4.0",
"crypto": "^1.0.1",
"date-fns": "^1.29.0",
Expand Down
73 changes: 73 additions & 0 deletions test/api/controllers/account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const sinon = require('sinon');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const chaiHttp = require('chai-http');
const app = require('../../../index');
const Account = require('../../../api/controllers/account');
const JWT = require('../../../api/lib/auth');

chai.use(chaiAsPromised);
chai.use(chaiHttp);
const expect = chai.expect;
const request = chai.request;

describe('Account Controller', () => {
describe('/GET account', () => {
it('should get the account with valid token', async () => {
const token = `Bearer ${JWT.create({
data: '5b6f49230619fc36cde7424b'
})}`;
const res = await request(app)
.get('/account/')
.set('Authorization', token);
expect(res.status).to.be.equal(200);
});

it('should get unauthorized response without token', async () => {
const res = await request(app).get('/account/');
expect(res.status).to.be.equal(403);
});
});

describe('POST account', () => {
it('should avoid create a existent user', async () => {
const user = {
firstName: 'felipe',
lastName: 'janer',
password: '123',
email: 'f@cymbals.com'
};
const res = await request(app)
.post('/account')
.send(user);
expect(res.status).to.be.equal(400);
expect(res.body).to.be.a('object');
expect(res.body).to.have.property('error');
});
});

describe('PUT account', () => {
it('should update a existent user', async () => {
const login = await request(app)
.post('/auth')
.send({ email: 'f@cymbals.com', password: '123' });

console.log(login);

const token = `Bearer ${login.res}`;

const user = {
firstName: 'ronaldinho',
lastName: 'janer',
email: 'f@cymbals.com'
};
const res = await request(app)
.put('/account')
.send(user)
.set('Authorization', token);
expect(res.status).to.be.equal(200);
expect(res.body).to.be.a('object');
expect(res.body).to.have.property('error');
});
});
});
35 changes: 0 additions & 35 deletions test/api/models/user.js

This file was deleted.