-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventManager.js
77 lines (69 loc) · 1.83 KB
/
eventManager.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
const fetch = require('node-fetch')
const {
getRacksProjectManagerContract
} = require('./app/middleware/external/contractCalls')
require('dotenv-safe').config()
const startEventManager = async () => {
try {
const racksPM = await getRacksProjectManagerContract()
const loginData = {
email: process.env.EMAIL_FROM_ADDRESS,
password: process.env.ADMIN_PASSWORD
}
const getToken = async () => {
const res = await fetch(process.env.API_URL + 'login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(loginData)
})
if (res?.ok) {
const data = await res.json()
return data.token
} else {
console.log('Login error')
return null
}
}
racksPM.on(
'NewProjectCreated',
async (newProjectNameBytes, newProjectName, newProjectAddress) => {
const token = await getToken()
if (token) {
const projectData = {
newProjectName,
newProjectAddress
}
await fetch(process.env.API_URL + 'projects/webhook', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: token
},
body: JSON.stringify(projectData)
})
}
}
)
racksPM.on('NewContributorRegistered', async (newContributorAddress) => {
const token = await getToken()
if (token) {
await fetch(
process.env.API_URL +
'users/contributor/webhook/' +
newContributorAddress,
{
method: 'PATCH',
headers: {
Authorization: token
}
}
)
}
})
} catch (error) {
console.log(error)
}
}
startEventManager()