@things-factory/integration-notification 8.0.0-beta.9 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@things-factory/integration-notification",
|
|
3
|
-
"version": "8.0.0
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"main": "dist-server/index.js",
|
|
5
5
|
"browser": "client/index.js",
|
|
6
6
|
"things-factory": true,
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
"clean": "npm run clean:server"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@things-factory/integration-base": "^8.0.0
|
|
27
|
-
"@things-factory/notification": "^8.0.0
|
|
26
|
+
"@things-factory/integration-base": "^8.0.0",
|
|
27
|
+
"@things-factory/notification": "^8.0.0",
|
|
28
28
|
"ses": "^1.5.0"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "07ef27d272dd9a067a9648ac7013748510556a18"
|
|
31
31
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './task'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './notification'
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { ILike } from 'typeorm'
|
|
2
|
+
import 'ses'
|
|
3
|
+
import { User } from '@things-factory/auth-base'
|
|
4
|
+
import { TaskRegistry } from '@things-factory/integration-base'
|
|
5
|
+
import { notify } from '@things-factory/notification'
|
|
6
|
+
import { getRepository, pubsub } from '@things-factory/shell'
|
|
7
|
+
|
|
8
|
+
async function Notification(step, { logger, domain, user, data, variables }) {
|
|
9
|
+
var {
|
|
10
|
+
params: { level: type, receivers, title, image, url, body, push = false }
|
|
11
|
+
} = step
|
|
12
|
+
|
|
13
|
+
if (!receivers || !title || !body) {
|
|
14
|
+
throw Error(
|
|
15
|
+
`receivers, title and body should be defined: receivers - '${receivers}', title - '${title}', body - '${body}'`
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const repository = getRepository(User)
|
|
20
|
+
|
|
21
|
+
// TODO filter only users having current domain privilege
|
|
22
|
+
receivers = (
|
|
23
|
+
await Promise.all(
|
|
24
|
+
receivers
|
|
25
|
+
.split(',')
|
|
26
|
+
.map(email => email.trim())
|
|
27
|
+
.map(async email => {
|
|
28
|
+
var receiver: User = await repository.findOneBy({ email: ILike(email) })
|
|
29
|
+
return receiver && receiver.id
|
|
30
|
+
})
|
|
31
|
+
)
|
|
32
|
+
).filter(receiver => !!receiver)
|
|
33
|
+
|
|
34
|
+
const compartment = new Compartment({
|
|
35
|
+
domain,
|
|
36
|
+
user,
|
|
37
|
+
data,
|
|
38
|
+
variables,
|
|
39
|
+
console
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
let evalBody
|
|
43
|
+
try {
|
|
44
|
+
evalBody = compartment.evaluate('`' + body + '`')
|
|
45
|
+
} catch (err) {
|
|
46
|
+
throw new Error(`Failed to evaluate body: ${err.message}`)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
body = evalBody
|
|
50
|
+
const timestamp = String(Date.now())
|
|
51
|
+
|
|
52
|
+
if (push) {
|
|
53
|
+
await notify({
|
|
54
|
+
receivers,
|
|
55
|
+
privileges: undefined,
|
|
56
|
+
tokens: undefined,
|
|
57
|
+
topic: undefined,
|
|
58
|
+
title,
|
|
59
|
+
body,
|
|
60
|
+
data: {
|
|
61
|
+
url,
|
|
62
|
+
timestamp
|
|
63
|
+
},
|
|
64
|
+
image,
|
|
65
|
+
actions: undefined
|
|
66
|
+
})
|
|
67
|
+
} else {
|
|
68
|
+
await pubsub.publish('notification', {
|
|
69
|
+
notification: {
|
|
70
|
+
domain,
|
|
71
|
+
type,
|
|
72
|
+
subject: type || 'info',
|
|
73
|
+
title,
|
|
74
|
+
body,
|
|
75
|
+
image,
|
|
76
|
+
url,
|
|
77
|
+
timestamp
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
data: true
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
Notification.parameterSpec = [
|
|
88
|
+
{
|
|
89
|
+
type: 'select',
|
|
90
|
+
name: 'level',
|
|
91
|
+
label: 'level',
|
|
92
|
+
property: {
|
|
93
|
+
options: ['', 'info', 'warn', 'error']
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
type: 'string',
|
|
98
|
+
name: 'receivers',
|
|
99
|
+
label: 'receivers'
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
type: 'string',
|
|
103
|
+
name: 'title',
|
|
104
|
+
label: 'title'
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
type: 'string',
|
|
108
|
+
name: 'image',
|
|
109
|
+
label: 'image'
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
type: 'string',
|
|
113
|
+
name: 'url',
|
|
114
|
+
label: 'url'
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
type: 'textarea',
|
|
118
|
+
name: 'body',
|
|
119
|
+
label: 'message-body',
|
|
120
|
+
property: {
|
|
121
|
+
language: 'text',
|
|
122
|
+
showLineNumbers: true
|
|
123
|
+
},
|
|
124
|
+
styles: {
|
|
125
|
+
flex: '1'
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
type: 'checkbox',
|
|
130
|
+
name: 'push',
|
|
131
|
+
label: 'push-notification'
|
|
132
|
+
}
|
|
133
|
+
]
|
|
134
|
+
|
|
135
|
+
Notification.connectorFree = true
|
|
136
|
+
Notification.help = 'integration/task/notification'
|
|
137
|
+
|
|
138
|
+
TaskRegistry.registerTaskHandler('notification', Notification)
|
package/server/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './engine'
|