-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (164 loc) · 5.18 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// application imports
import Slack from '@slack/bolt';
import cors from 'cors';
import express from 'express';
import mongoose from 'mongoose';
import { googleDriveAuth } from './imports/utils/googleAuth.js';
// routes
import { peopleRouter } from './routes/people.routes.js';
import { socialStructureRouter } from './routes/socialStructures.routes.js';
import { venueRouter } from './routes/venues.routes.js';
import { projectRouter } from './routes/projects.routes.js';
import { sprintRouter } from './routes/sprints.routes.js';
import { slackRouter } from './routes/slack.routes.js';
import { dataRouter } from './routes/data.routes.js';
// fixtures for development
import {
allDatabasesAreEmpty,
populateData,
} from './controllers/databaseManagement/refreshData.js';
/*
Get environment variables.
*/
const PORT = process.env.PORT || 3000;
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost/studio-api';
const NODE_ENV = process.env.NODE_ENV || 'development';
const SHOULD_REFRESH_DATA =
process.env.SHOULD_REFRESH_DATA.trim().toLowerCase() === 'true' || false; // TODO: fix
const APP_URL = process.env.APP_URL || `http://localhost:${PORT}`;
/*
Setup application.
*/
const receiver = new Slack.ExpressReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
});
export const app = new Slack.App({
token: process.env.SLACK_BOT_TOKEN,
receiver: receiver,
});
// TODO: have message update the same text so multiple messages aren't coming in
// add handlers for different selection types
app.action('single-select', async ({ body, client, ack, say, logger }) => {
await ack();
try {
// console.log(JSON.stringify(body, null, 2))
await say(
'Ok! I will orchestrate the following strategies: \n' +
`${body.actions[0].selected_option.text.text}`
);
} catch (error) {
logger.error(error);
}
});
app.action('multi-select', async ({ body, client, ack, say, logger }) => {
await ack();
try {
// console.log(JSON.stringify(body, null, 2))
await say(
'Ok! I will orchestrate the following strategies: \n' +
`${body.actions[0].selected_options
.map((option) => {
return `${option.text.text} \n`;
})
.join('')}`
);
} catch (error) {
logger.error(error);
}
});
app.action('checkbox', async ({ body, client, ack, say, logger }) => {
await ack();
try {
// console.log(JSON.stringify(body, null, 2))
await say(
'Ok! I will orchestrate the following strategies: \n' +
`${body.actions[0].selected_options
.map((option) => {
return `${option.text.text} \n`;
})
.join('')}`
);
} catch (error) {
logger.error(error);
}
});
/*
Setup routes
*/
// TODO: see second answer about how to split up routes: https://stackoverflow.com/questions/25260818/rest-with-express-js-nested-router
app.receiver.app.use(express.json());
app.receiver.app.use(cors());
app.receiver.app.use(
express.urlencoded({
extended: true,
})
);
app.receiver.app.use('/people', peopleRouter);
app.receiver.app.use('/socialStructures', socialStructureRouter);
app.receiver.app.use('/venues', venueRouter);
app.receiver.app.use('/projects', projectRouter);
app.receiver.app.use('/sprints', sprintRouter);
app.receiver.app.use('/slack', slackRouter);
app.receiver.app.use('/data', dataRouter);
app.receiver.app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
app.receiver.app.all('*', (request, response) => {
console.error(
`External request: ${request.url} does not exist. Returning 404 error.`
);
return response.status(404).json({ error: `${request.url} not found` });
});
/*
Start application.
*/
await app.start(PORT);
console.log(`App running: ${APP_URL}`);
/*
Setup options for mongodb connection
*/
const mongooseOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
};
// attempt to connect to mongodb, and detect any connection errors
try {
await mongoose.connect(MONGODB_URI, mongooseOptions);
console.log(
`Connected to MongoDB: ${MONGODB_URI} with options ${mongooseOptions}`
);
} catch (error) {
console.error(`Error with connecting to MongoDB: ${error}`);
} finally {
if (NODE_ENV === 'development') {
if (SHOULD_REFRESH_DATA) {
console.log('Development -- Local databases are empty. Populating.');
// TODO: populate database fixtures here
// populate all data synchronously
await populateData();
} else {
console.log(
'Development -- Local databases are populated. Not re-populating.'
);
}
}
if (NODE_ENV === 'production') {
// check if collections are empty first so that data isn't overwritten
if (await allDatabasesAreEmpty()) {
console.log('Production -- Databases are empty. Populating.');
// populate all data synchronously
await populateData();
} else {
console.log('Production -- Databases are populated. Not re-populating.');
}
}
}
// listen for any errors after initial connection
mongoose.connection.on('error', (err) => {
console.error(`MongoDB connection error: ${err}`);
});