diff --git a/backend/app/models/Subscriber.js b/backend/app/models/Subscriber.js new file mode 100644 index 00000000..dd621298 --- /dev/null +++ b/backend/app/models/Subscriber.js @@ -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) \ No newline at end of file diff --git a/backend/app/routes/index.js b/backend/app/routes/index.js index 0919698f..d0ccf9ca 100644 --- a/backend/app/routes/index.js +++ b/backend/app/routes/index.js @@ -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); @@ -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; diff --git a/backend/app/routes/subscribers/@validationSchema/index.js b/backend/app/routes/subscribers/@validationSchema/index.js new file mode 100644 index 00000000..7e142263 --- /dev/null +++ b/backend/app/routes/subscribers/@validationSchema/index.js @@ -0,0 +1,7 @@ +const Joi = require('joi'); + +const postSubscriberValidationSchema = Joi.object().keys({ + email: Joi.string().required(), +}); + +module.exports = {postSubscriberValidationSchema}; \ No newline at end of file diff --git a/backend/app/routes/subscribers/index.js b/backend/app/routes/subscribers/index.js new file mode 100644 index 00000000..1808db71 --- /dev/null +++ b/backend/app/routes/subscribers/index.js @@ -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; diff --git a/backend/app/routes/subscribers/postSubscriber.js b/backend/app/routes/subscribers/postSubscriber.js new file mode 100644 index 00000000..4860dae1 --- /dev/null +++ b/backend/app/routes/subscribers/postSubscriber.js @@ -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(); +}; \ No newline at end of file