-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (62 loc) · 2.14 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
const axios = require('axios')
// set your api key
axios.defaults.headers.common['api-key'] = ''
// test environment, production -> https://api.echo.lu/v1
const baseUrl = 'https://test-api.echo.lu/v1'
async function main() {
try {
console.log('creating experience');
const exp = await createExperience()
console.log('experience created');
setTimeout(async () => {
console.log('updating experience');
await publishExperience(exp)
console.log('experience updated');
}, 3000)
} catch (error) {
console.log('main', error);
}
}
/**
* @description function will create a new experience from a template located at /templates/experience.js
* @returns {object} created experience
*/
function createExperience() {
return new Promise(async (resolve, reject) => {
try {
const experience = require('./templates/experience')()
const req = await axios.post(`${baseUrl}/experiences`, experience)
return resolve(req.data.record)
} catch (error) {
console.log('createExperience', error.response?.data || error);
return reject('nope')
}
})
}
/**
* @description Put an existing experience into the status pending.
* This will send the experience to the echo.lu backoffice for the validation process.
* @param {object} experience
* @returns {string} message
*/
function publishExperience(experience = null) {
return new Promise(async (resolve, reject) => {
try {
if (!experience) {
return reject('experience not provided')
}
if (typeof experience !== 'object') {
return reject('object expected')
}
if (!experience.hasOwnProperty('id')) {
return reject('experience id is missing')
}
experience.status = 'pending'
await axios.put(`${baseUrl}/experiences/${experience.id}`, experience)
return resolve('Updated')
} catch (error) {
console.log('publishExperience', error.response?.data || error);
}
})
}
main()