Skip to content

Commit

Permalink
Subscribtion added in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
BHS-Harish committed Aug 7, 2024
1 parent c2ca49d commit 88d0f43
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 88d0f43

Please sign in to comment.