-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2ca49d
commit 88d0f43
Showing
5 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |