index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* eslint-disable global-require */
  2. import config from "config";
  3. import Mailgun from "mailgun-js";
  4. import CoreClass from "../../core";
  5. class MailModule extends CoreClass {
  6. // eslint-disable-next-line require-jsdoc
  7. constructor() {
  8. super("mail");
  9. }
  10. /**
  11. * Initialises the mail module
  12. *
  13. * @returns {Promise} - returns promise (reject, resolve)
  14. */
  15. async initialize() {
  16. const importSchema = schemaName =>
  17. new Promise(resolve => {
  18. import(`./schemas/${schemaName}`).then(schema => resolve(schema.default));
  19. });
  20. this.schemas = {
  21. verifyEmail: await importSchema("verifyEmail"),
  22. resetPasswordRequest: await importSchema("resetPasswordRequest"),
  23. passwordRequest: await importSchema("passwordRequest")
  24. };
  25. this.enabled = config.get("apis.mailgun.enabled");
  26. if (this.enabled)
  27. this.mailgun = new Mailgun({
  28. apiKey: config.get("apis.mailgun.key"),
  29. domain: config.get("apis.mailgun.domain")
  30. });
  31. return new Promise(resolve => resolve());
  32. }
  33. /**
  34. * Sends an email
  35. *
  36. * @param {object} payload - object that contains the payload
  37. * @param {object} payload.data - information such as to, from in order to send the email
  38. * @returns {Promise} - returns promise (reject, resolve)
  39. */
  40. SEND_MAIL(payload) {
  41. return new Promise(resolve => {
  42. if (this.enabled)
  43. this.mailgun.messages().send(payload.data, () => {
  44. resolve();
  45. });
  46. else resolve();
  47. });
  48. }
  49. /**
  50. * Returns an email schema
  51. *
  52. * @param {object} payload - object that contains the payload
  53. * @param {string} payload.schemaName - name of the schema to get
  54. * @returns {Promise} - returns promise (reject, resolve)
  55. */
  56. GET_SCHEMA(payload) {
  57. return new Promise(resolve => {
  58. resolve(this.schemas[payload.schemaName]);
  59. });
  60. }
  61. }
  62. export default new MailModule();