-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (67 loc) · 1.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict'
const Trailpack = require('trailpack')
const sendGrid = require('sendgrid')
const lib = require('./lib')
const _ = require('lodash')
module.exports = class Sendgrid extends Trailpack {
/**
* validate spp configuration
*/
validate() {
if (!this.app.config.sendgrid) {
return Promise.reject(new Error('There not config.sendgrid !'))
}
if (_.isFunction(this.app.config.sendgrid)) {
return Promise.reject(new Error('config.sendgrid is not a function !'))
}
if (!this.app.config.sendgrid.apiKey) {
return Promise.reject(new Error('config.sendgrid apikey is not available !'))
}
return Promise.resolve()
}
/*/**
* configure sendgrid document method
*/
configure() {
console.log('My Trailpack is configured')
this.sendgrid = sendGrid(this.app.config.sendgrid.apiKey)
this.app.sendgrid = this
return Promise.resolve()
}
/**Send Mail
*
* @param message
* @returns {Promise.<*[]>}
*/
sendMail(message) {
if (_.isEmpty(message))
return Promise.reject(new Error('Invalid message, parameter missing!'))
// validate email
return lib.Mail
.validateMail(message)
.then(msg=>{
// send email
return lib.Mail
.sendMail(this.app, msg)
.then(email=>{
return Promise.resolve(email)
})
})
.catch(err=>{
return Promise.reject(err)
})
}
/**
* initialize sendgrid
*/
initialize() {
return Promise.resolve()
}
constructor(app) {
super(app, {
config: require('./config'),
api: require('./api'),
pkg: require('./package')
})
}
}