-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
82 lines (77 loc) · 2.24 KB
/
Copy pathserver.js
File metadata and controls
82 lines (77 loc) · 2.24 KB
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
81
82
import { Roles } from 'meteor/alanning:roles'
import { Meteor } from 'meteor/meteor'
import SystemMailsCollection from './collection'
import MarkdownIt from 'markdown-it'
import { forEach, get } from 'lodash'
const RegisterEmail = doc => {
const mail = SystemMailsCollection.findOne(doc._id)
if (!mail) {
doc.subject = {}
doc.body = {}
doc.rendered = { body: {} }
SystemMailsCollection.insert(doc)
} else {
SystemMailsCollection.update(doc._id, { $set: doc })
}
}
class GenerateEmail {
constructor ({ _id, language }) {
if (!language) {
throw new Meteor.Error(
'Cannot send mail without language for mail id: ' + _id
)
}
this.mail = SystemMailsCollection.findOne(_id)
this.language = language
}
from () {
return Meteor.settings.systemMailsFrom
}
subject (params) {
return this.renderParams(params, this.mail.subject[this.language] || '')
}
html (params) {
let input = ''
if (this.mail.rendered && this.mail.rendered.body) {
input = this.mail.rendered.body[this.language]
}
return this.renderParams(params, input)
}
renderParams (params, input) {
if (!input) return `System email ${this.mail._id} not configured`
forEach(this.mail.params, (path, key) => {
const pattern = new RegExp(`{{${key}}}`, 'g')
input = input.replace(pattern, get(params, path, ''))
})
return input
}
}
Meteor.publish('systemmails', (q, params) => {
if (Roles.userIsInRole(Meteor.userId(), 'admin')) {
return SystemMailsCollection.find(q, params)
}
})
Meteor.methods({
totalSystemMails: query => {
return SystemMailsCollection.find(query).count()
},
getSystemMailsIds: (query, params) => {
return SystemMailsCollection.find(query, params).map(({ _id }) => _id)
},
updateSystemMail: doc => {
if (Roles.userIsInRole(Meteor.userId(), 'admin')) {
const { _id } = doc
delete doc._id
doc.rendered = { body: {} }
forEach(doc.body, (md, language) => {
doc.rendered.body[language] = MarkdownIt({
html: true,
linkify: true,
typography: true
}).render(md)
})
return SystemMailsCollection.update({ _id }, { $set: doc })
}
}
})
export { RegisterEmail, GenerateEmail }