-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
82 lines (78 loc) · 2.23 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
require('dotenv').config()
const Intranet = require('intra-api')
const mongoose = require('mongoose')
const intraModels = require('intra-models');
const winston = require('winston');
winston.loggers.add('debug', {
console: {
level: 'info',
colorize: true,
label: 'debug'
}
});
winston.loggers.add('error', {
console: {
level: 'error',
colorize: true,
label: 'debug'
}
});
const infoLogger = winston.loggers.get('debug');
const errorLogger = winston.loggers.get('error');
// Load lib src
const Planning = require('./lib/planning')
const Module = require('./lib/module')
const Event = require('./lib/event');
const Rollbar = require("rollbar");
const rollbar = new Rollbar(process.env.ROLLBAR_KEY);
// Load models src
const { ModulesModel, ActivityModel, EventModel, InstanceModel } = intraModels;
// Connection configuration
const Intra = new Intranet(process.env.AUTOLOGIN_TOKEN)
// Because mongoose promises are deprecated
mongoose.Promise = global.Promise;
mongoose
.connect(`mongodb://${process.env.MONGO_HOST}/${process.env.MONGO_DB}`, {
user: process.env.MONGO_USER,
pass: process.env.MONGO_PASSWORD,
useMongoClient: false
})
.then(() => {
// Models
const conn = mongoose.connection,
models = {
event: new EventModel(conn),
instance: new InstanceModel(conn),
module: new ModulesModel(conn),
activity: new ActivityModel(conn),
}
infoLogger.info(`Connected to ${process.env.MONGO_DB}@${process.env.MONGO_HOST}`);
const planning = new Planning(Intra, models);
const module = new Module(Intra, models);
const event = new Event(Intra, models);
infoLogger.info('Refreshing modules');
return module.refresh()
.then((res) => {
infoLogger.info('Module refreshed');
infoLogger.info("Refreshing events");
return event
.refresh()
})
.then((res) => {
infoLogger.info("events refreshed");
conn.close();
})
.catch((err) => {
rollbar.error(err)
errorLogger.error('Fetching failed');
conn.close();
throw err
})
})
.then(() => {
console.log('All data refreshed')
})
.catch((err) => {
rollbar.error(err)
errorLogger.error(err)
})