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

Backend: Add Subscribers for Newsletter #1118

Merged
merged 7 commits into from
Aug 7, 2024
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
15 changes: 15 additions & 0 deletions backend/app/models/Subscriber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const moongoose = require('mongoose')

const { Schema } = moongoose

const SubscriberSchema = new Schema({
email: {
type: String,
trim: true,
unique:true,
required: true
}
}, { timestamps: { createdAt: 'createdAt' } }
)

module.exports=moongoose.model("subscriber",SubscriberSchema)
2 changes: 2 additions & 0 deletions backend/app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const answer = require('./Q&A/answers');
const teamMember = require('./teamMember');
const resource = require('./resources');
const testimonial = require('./testimonial');
const subscriber=require('./subscribers');

router.use('/admin', admin);
router.use('/auth', auth);
Expand All @@ -26,4 +27,5 @@ router.use('/teamMember', teamMember);
router.use('/', tinyURL);
router.use('/resources', resource);
router.use('/testimonials', testimonial);
router.use('/subscriber',subscriber)
module.exports = router;
7 changes: 7 additions & 0 deletions backend/app/routes/subscribers/@validationSchema/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Joi = require('joi');

const postSubscriberValidationSchema = Joi.object().keys({
email: Joi.string().required(),
});

module.exports = {postSubscriberValidationSchema};
9 changes: 9 additions & 0 deletions backend/app/routes/subscribers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const router = require('express').Router({ mergeParams: true });
const postSubscriber = require('./postSubscriber');
const validation = require('../../../helpers/middlewares/validation');
const {postSubscriberValidationSchema} = require('./@validationSchema');

// add new subscriber for news letter
router.post('/', validation(postSubscriberValidationSchema), postSubscriber);

module.exports = router;
29 changes: 29 additions & 0 deletions backend/app/routes/subscribers/postSubscriber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const to = require("await-to-js").default;
const Subscriber = require('../../models/Subscriber');
const { ErrorHandler } = require('../../../helpers/error')
const constants = require('../../../constants');

module.exports = async (req, res, next) => {
const [err, response] = await to(Subscriber.create({ ...req.body }));
if (err) {
if (err.code === 11000) {
const error = new ErrorHandler(constants.ERRORS.INPUT, {
statusCode: 400,
message: 'Bad request: Email already registered',
user: req.body.email,
});
return next(error);
}
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Mongo Error: Insertion Failed',
errStack: err,
});
return next(error);
}
res.status(200).send({
message: 'Subscribed Successfully',
response: response,
});
return next();
};
Loading