55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
const asyncHandler = require("express-async-handler");
|
|
const News = require("../models/newsModels");
|
|
const { email } = require("../config/emailConnection");
|
|
const { checkValidations } = require("../plugins/HelperFunctions")
|
|
const { body, validationResult } = require('express-validator')
|
|
const { _sr } = require("../plugins/resServer")
|
|
const { rateLimit } = require('express-rate-limit')
|
|
|
|
const limiter = rateLimit({
|
|
windowMs: 24 * 60 * 60 * 1000, // 1 minutes
|
|
limit: 1, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
|
|
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
|
|
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
|
|
message: { msg: "Too many requests, please try again later." }
|
|
})
|
|
|
|
/**
|
|
*@desc Add new email
|
|
*@route POST /api/news/
|
|
*@access private
|
|
*/
|
|
const addEmail = [
|
|
[
|
|
body('email').notEmpty().isEmail().withMessage(_sr.fa.required.email),
|
|
],
|
|
checkValidations(validationResult),
|
|
limiter,
|
|
asyncHandler(async (req, res) => {
|
|
const { email } = req.body
|
|
const alreadyEmail = await News.findOne({ email })
|
|
if (alreadyEmail) {
|
|
res.status(400);
|
|
throw new Error(_sr.fa.duplicated.email);
|
|
}
|
|
const emailAdded = await News.create({
|
|
email
|
|
})
|
|
res.status(200).json(emailAdded)
|
|
})
|
|
]
|
|
|
|
|
|
module.exports = {
|
|
baseRoute: "/news",
|
|
items: [
|
|
{
|
|
off: false,
|
|
route: "/",
|
|
method: "post",
|
|
middleware: [],
|
|
use: addEmail
|
|
},
|
|
|
|
]
|
|
}; |