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

Added new JS ver where I can, did some removals #368

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 17 additions & 19 deletions api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const i18nextMiddleware = require('i18next-http-middleware');
const i18nextBackend = require('i18next-fs-backend');

i18next.use(i18nextBackend).init({
backend: {
loadPath: __dirname + '/locales/{{lng}}/{{ns}}.json',
},
fallbackLng: 'en',
preload: ['en', 'es'],
backend: {
loadPath: __dirname + '/locales/{{lng}}/{{ns}}.json',
},
fallbackLng: 'en',
preload: ['en', 'es'],
});

const app = express();
Expand All @@ -27,13 +27,11 @@ connectDb(process.env.DB_URL);
require('./config/passport');

const corsOptions = {
origin: (origin, callback) => {
if (origin === process.env.BASE_CLIENT_URL) {
callback(null, true);
} else {
callback(createError(403));
}
},
origin: (origin, callback) => {
origin === process.env.BASE_CLIENT_URL
? callback(null, true)
: callback(createError(403));
},
};

app.use(cors(corsOptions));
Expand All @@ -45,16 +43,16 @@ app.use(cookieParser());
app.use('/', require('./routes/index'));

app.use(function (req, res, next) {
next(createError(404));
next(createError(404));
});

app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: err,
});
return;
res.status(err.status || 500);
res.json({
message: err.message,
error: err,
});
return;
});

global.CronJob = require('./utils/cron.js');
Expand Down
36 changes: 18 additions & 18 deletions api/config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ const LocalStrategy = require('passport-local');
const User = require('../models/User');

passport.use(
new LocalStrategy(
{
usernameField: 'user[email]',
passwordField: 'user[password]',
},
(email, password, done) => {
User.findOne({ email })
.then((user) => {
if (!user || !user.validatePassword(password)) {
return done(null, false, {
errors: { 'email or password': 'is invalid' },
});
}
new LocalStrategy(
{
usernameField: 'user[email]',
passwordField: 'user[password]',
},
(email, password, done) => {
User.findOne({ email })
.then((user) => {
if (!user || !user.validatePassword(password)) {
return done(null, false, {
errors: { 'email or password': 'is invalid' },
});
}

return done(null, user);
})
.catch(done);
}
)
return done(null, user);
})
.catch(done);
}
)
);
94 changes: 47 additions & 47 deletions api/controllers/attendantsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,57 @@ const Attendant = require('../models/Attendant');
const Notification = require('../models/Notification');

exports.get = (req, res, next) => {
return Attendant.find({ event: req.params.eventId })
.populate('user', 'name avatar')
.then((attendants) => res.json({ attendants }));
return Attendant.find({ event: req.params.eventId })
.populate('user', 'name avatar')
.then((attendants) => res.json({ attendants }));
};

exports.post = (req, res, next) => {
const {
body: { attendant },
} = req;

Attendant.find({ event: attendant.event._id }).then((attendants) => {
if (attendants.length >= attendant.event.attendantsLimit) {
return res.status(400).json({
errors: [
{
param: 'Attendants limit',
value: attendant.event.attendantsLimit,
msg: 'has been met',
},
],
});
} else {
if (attendants.some((a) => a.user == attendant.user._id)) {
return res.sendStatus(400);
}

const finalAttendant = new Attendant(attendant);

return finalAttendant.save().then(() => {
const notification = new Notification({
user: attendant.event.createdBy,
type: 'newAttendant',
referencedUser: attendant.user._id,
referencedEvent: attendant.event._id,
});
notification.save();

res.json({ attendant: finalAttendant });
});
}
});
const {
body: { attendant },
} = req;

Attendant.find({ event: attendant.event._id }).then((attendants) => {
if (attendants.length >= attendant.event.attendantsLimit) {
return res.status(400).json({
errors: [
{
param: 'Attendants limit',
value: attendant.event.attendantsLimit,
msg: 'has been met',
},
],
});
} else {
if (attendants.some((a) => a.user == attendant.user._id)) {
return res.sendStatus(400);
}

const finalAttendant = new Attendant(attendant);

return finalAttendant.save().then(() => {
const notification = new Notification({
user: attendant.event.createdBy,
type: 'newAttendant',
referencedUser: attendant.user._id,
referencedEvent: attendant.event._id,
});
notification.save();

res.json({ attendant: finalAttendant });
});
}
});
};

exports.deleteId = (req, res, next) => {
return Attendant.findByIdAndRemove(req.params.attendantId).then(
(attendant) => {
if (!attendant) {
return res.sendStatus(404);
}

res.json({ attendant });
}
);
return Attendant.findByIdAndRemove(req.params.attendantId).then(
(attendant) => {
if (!attendant) {
return res.sendStatus(404);
}

res.json({ attendant });
}
);
};
46 changes: 23 additions & 23 deletions api/controllers/commentsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ const Attendant = require('../models/Attendant');
const Notification = require('../models/Notification');

exports.get = (req, res, next) => {
return Comment.find({ event: req.params.eventId })
.populate('user', 'name avatar')
.then((comments) => res.json({ comments }));
return Comment.find({ event: req.params.eventId })
.populate('user', 'name avatar')
.then((comments) => res.json({ comments }));
};

exports.post = (req, res, next) => {
const {
body: { comment },
} = req;
const {
body: { comment },
} = req;

Attendant.find({ event: comment.event._id }).then((attendants) => {
const finalComment = new Comment(comment);
Attendant.find({ event: comment.event._id }).then((attendants) => {
const finalComment = new Comment(comment);

return finalComment.save().then(() => {
attendants.forEach((attendant) => {
if (attendant.user != comment.user._id) {
const notification = new Notification({
user: attendant.user,
type: 'newComment',
referencedUser: comment.user._id,
referencedEvent: comment.event._id,
});
notification.save();
}
});
return finalComment.save().then(() => {
attendants.forEach((attendant) => {
if (attendant.user != comment.user._id) {
const notification = new Notification({
user: attendant.user,
type: 'newComment',
referencedUser: comment.user._id,
referencedEvent: comment.event._id,
});
notification.save();
}
});

res.json({ comment: finalComment });
});
});
res.json({ comment: finalComment });
});
});
};
Loading