-
Notifications
You must be signed in to change notification settings - Fork 0
/
util-remote-sync.js
60 lines (45 loc) · 1.2 KB
/
util-remote-sync.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
const twilio = require('twilio')
const client = new twilio.Twilio(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN)
const retrieveDocument = function (name) {
console.log('Retrieve Remote Document: ' + name)
return new Promise((resolve, reject) => {
client.sync.services(process.env.TWILIO_SYNC_SERVICE_SID).documents(name).fetch()
.then(doc => {
/* we could retrieve the document */
resolve(doc)
}).catch(error => {
/* create a new document */
const payload = {
uniqueName: name,
data: null
}
return client.sync.services(process.env.TWILIO_SYNC_SERVICE_SID).documents.create(payload)
.then(doc => {
resolve(doc)
}).catch(error => {
reject(error)
})
})
})
}
module.exports.update = function (name, data) {
const payload = {
name: name,
data: JSON.stringify(data)
}
return new Promise ((resolve, reject) => {
retrieveDocument(name)
.then(doc => {
console.log('Remote Update: ' + name)
client.sync.services(process.env.TWILIO_SYNC_SERVICE_SID).documents(doc.sid).update(payload)
.then(doc => {
resolve(doc)
})
}).catch(error => {
console.log(error)
reject(error)
})
})
}