-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (92 loc) · 3.17 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
var express = require('express');
var orm = require('orm');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.use(orm.express(process.env.DATABASE_URL, {
define: function (db, models, next) {
models.organization = db.define("organizations", {
id: Number,
name: String
});
models.client = db.define("clients", {
id: Number,
name: String
});
models.client.hasOne('organization', models.organization, { reverse: "clients" });
models.user = db.define("users", {
id: Number,
name: String,
email: String
});
models.user.hasOne('organization', models.organization, { reverse: "users" });
models.project = db.define("projects", {
id: Number,
name: String
});
models.project.hasOne('client', models.client, { reverse: "projects" });
models.task = db.define("tasks", {
id: {type: 'number', key: true},
name: String,
description: String,
timeAllocated: {type: 'number', mapsTo: 'time_allocated'},
timeSpent: {type: 'number', mapsTo: 'time_spent'},
startTime: {type: 'date', mapsTo: 'start_time'}
}, {
methods: {
setStarted: function() {
this.started = this.startTime !== null;
var spent = this.timeSpent;
if(this.started) {
var diff = new Date() - Date.parse(this.startTime);
spent += diff / 1000 / 60 / 60;
}
this.timeLeft = Math.floor((this.timeAllocated - spent) * 60);
},
getJson: function() {
this.setStarted();
return this;
},
stop: function() {
var diff = new Date() - Date.parse(this.startTime);
this.timeSpent += diff / 1000 / 60 / 60;
this.startTime = null;
this.started = false;
},
start: function() {
this.startTime = new Date();
this.started = true;
}
},
hooks: {
afterLoad: function (next) {
if(!this.timeSpent) this.timeSpent = 0;
this.setStarted();
return next();
}
}
});
models.task.hasOne('project', models.project, { reverse: "tasks" });
models.task.hasOne('user', models.user, { reverse: "tasks" });
next();
}
}));
app.use('/', require('./routes/index'));
app.use('/clients', require('./routes/clients'));
app.use('/organizations', require('./routes/organizations'));
app.use('/users', require('./routes/users'));
app.use('/tasks', require('./routes/tasks'));
app.use('/projects', require('./routes/projects'));
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'));
});