-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
executable file
·88 lines (73 loc) · 2.14 KB
/
install.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
87
88
'use strict';
const aws = require('aws-sdk');
const qs = require('querystring');
const request = require('request');
const db = new aws.DynamoDB.DocumentClient();
const extractCode = function(event) {
return new Promise((resolve, reject) => {
if (event.queryStringParameters && event.queryStringParameters.code) {
return resolve(event.queryStringParameters.code);
}
reject('Code not provided');
});
};
const getOAuthToken = function(code) {
return new Promise((resolve, reject) => {
if (code === null) { return reject('Could not provided'); }
const params = {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
code
}
console.log(params)
const url = process.env.SLACK_OAUTH + qs.stringify(params);
console.log(url)
console.log(res.statusCode)
request.get(url, (err, res, body) => {
console.log(err)
if (err || res.statusCode !== 200) {
console.log(res)
reject(err);
} else {
console.log(body)
resolve(body);
}
})
});
};
const saveToDynamo = function(response) {
console.log('saving to dynamo')
return new Promise((resolve, reject) => {
const params = {
TableName: process.env.TABLE_NAME,
Item: JSON.parse(response)
}
console.log(params)
db.put(params, (err, data) =>{
console.log(data)
console.log(err)
if (err) {
reject(err);
} else {
resolve();
}
})
});
}
const successResponse = function() {
return {
statusCode: 200
}
}
const errorResponse = function() {
return {
statusCode: 302
}
}
module.exports.endpoint = (event, context, callback) => {
extractCode(event)
.then((code) => getOAuthToken(code))
.then((response) => saveToDynamo(response))
.then(() => callback(null, successResponse()))
.catch((err) => callback(null, errorResponse()))
};