-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
86 lines (75 loc) · 2.34 KB
/
main.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
78
79
80
81
82
83
84
85
86
const express = require('express')
const log = require('./lib/logging')
const client = require('./lib/client')
function getState (info) {
return '[' + Math.floor(Math.random() * 1000) + '] ' + info + ' | '
}
function processError (res, state, error) {
const result = { status: 'error', type: 'unknown', error: error }
res.status(400).json(result)
log.debug(state + 'response sent: ' + JSON.stringify(result))
}
const app = express()
app.get('/sign_in', (req, res) => {
client.signIn(req.query.code)
.then(() => res.status(200).json({status: 'ok'}))
.catch(() => res.status(400).json({status: 'error', type: 'unknown'}))
})
app.get('/get_contacts', (req, res) => {
client.getContacts()
.then(contacts => res.status(200).json(contacts))
.catch(error => res.status(400).json(error))
})
app.get('/get_full_user', (req, res) => {
const state = getState(req.url)
log.debug(state + 'start processing')
try {
client.getFullUser(req.query.user_id)
.then(user => {
const result = {
status: 'ok',
data: {
user_id: user.user.id,
first_name: user.user.first_name,
last_name: user.user.last_name
}
}
res.status(200).json(result)
log.debug(state + 'response sent: ' + JSON.stringify(result))
})
.catch(error => processError(res, state, error))
} catch (error) {
processError(res, state, error)
}
})
app.get('/check_phone', (req, res) => {
client.checkPhone(req.query.phone)
.then(result => res.status(200).json(result))
.catch(error => res.status(400).json(error))
})
app.get('/import_contact', (req, res) => {
const state = getState(req.url)
log.debug(state + 'start processing')
try {
client.importContacts([{
client_id: 0,
phone: req.query.phone,
first_name: req.query.first_name || 'UNKNOWN',
last_name: req.query.last_name || 'UNKNOWN'
}], false)
.then(imported => {
const result = {
status: 'ok',
data: { user_id: imported.users.list[0].id }
}
res.status(200).json(result)
log.debug(state + 'response sent: ' + JSON.stringify(result))
})
.catch(error => processError(res, state, error))
} catch (error) {
processError(res, state, error)
}
})
app.listen(3000, () => {
log.debug('listening on *:3000')
})