Skip to content

Instantly share code, notes, and snippets.

@harryWonder
Created June 10, 2021 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harryWonder/6acfb5fb04c93f7328c4adfc5d3a7207 to your computer and use it in GitHub Desktop.
Save harryWonder/6acfb5fb04c93f7328c4adfc5d3a7207 to your computer and use it in GitHub Desktop.
LD NodeJS & Nginx
const Controller = require('../Controller');
const Validators = require('../../validators/Validator');
const NewsletterModel = require('../../models/Newsletters');
const Mail = require('../../mail/Mail');
class Newsletter extends Controller {
constructor() { super(); }
async registerEmail(req, res) {
try {
const Body = req.body;
const _Validators = new Validators();
const Validator = await _Validators.validateEmail(Body, NewsletterModel);
if (Validator.status) {
return super.response(res,
400,
'There are some errors in your request. Please, try again.', {},
Validator.errors);
}
/* Send The User An Email & Signup The User For The Newsletter Program */
let newNewsletter = new NewsletterModel({ email: Body.email, status: 1, createdAt: global.Date(), updatedAt: global.Date() });
newNewsletter = await newNewsletter.save();
/* Send An Actual Email To The User */
Mail.send({
to: Body.email,
subject: 'Subscribed Successfully'
}, 'Newsletter')
.then((response) => console.log(response))
.catch((e) => console.log(e));
/* Send Back An HTTP Response */
return super.response(res,
201,
'Registration Completed', { email: Body.email }, {});
} catch (e) {
return super.response(res, 500, 'An unexpected error occurred. Please, try again.', {}, { server: 'Operation Failed' });
}
}
}
module.exports = new Newsletter();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment