Skip to content

Commit

Permalink
🍂Backend: Add Subscribers for Newsletter (#1118)
Browse files Browse the repository at this point in the history
* Upvote and Downvotes fixed

* Subscribtion added in backend
  • Loading branch information
BHS-Harish authored Aug 7, 2024
1 parent 8e217e5 commit 2b63e3d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
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();
};

0 comments on commit 2b63e3d

Please sign in to comment.