index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. constructor() {
  7. super("mail");
  8. }
  9. async initialize() {
  10. const importSchema = schemaName =>
  11. new Promise(resolve => {
  12. import(`./schemas/${schemaName}`).then(schema => resolve(schema.default));
  13. });
  14. this.schemas = {
  15. verifyEmail: await importSchema("verifyEmail"),
  16. resetPasswordRequest: await importSchema("resetPasswordRequest"),
  17. passwordRequest: await importSchema("passwordRequest")
  18. };
  19. this.enabled = config.get("apis.mailgun.enabled");
  20. if (this.enabled)
  21. this.mailgun = new Mailgun({
  22. apiKey: config.get("apis.mailgun.key"),
  23. domain: config.get("apis.mailgun.domain")
  24. });
  25. return new Promise(resolve => resolve());
  26. }
  27. SEND_MAIL(payload) {
  28. // data
  29. return new Promise(resolve => {
  30. if (this.enabled)
  31. this.mailgun.messages().send(payload.data, () => {
  32. resolve();
  33. });
  34. else resolve();
  35. });
  36. }
  37. GET_SCHEMA(payload) {
  38. return new Promise(resolve => {
  39. resolve(this.schemas[payload.schemaName]);
  40. });
  41. }
  42. }
  43. export default new MailModule();