-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathproject.js
232 lines (202 loc) · 6.62 KB
/
project.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var winston = require('../config/winston');
var Profile = require('../models/profile');
var Channel = require('../models/channel');
var pjson = require('../package.json');
const uuidv4 = require('uuid/v4');
var trialEnabled = process.env.TRIAL_MODE_ENABLED || false;
winston.debug("trial mode enabled: "+trialEnabled );
if (trialEnabled) {
winston.info("Trial mode enabled");
}
var ChatLimitOn = process.env.SMART_ASSIGNMENT_CHAT_LIMIT_ON_DEFAULT_PROJECT || false;
winston.debug("ChatLimitOn: "+ChatLimitOn );
if (ChatLimitOn) {
winston.info("Smart Assignment Chat Limit On as default value for projects");
}
var ProjectSchema = new Schema({
name: {
type: String,
required: true
},
activeOperatingHours: {
type: Boolean,
},
operatingHours: {
type: Object,
},
timeSlots: {
type: Object,
},
settings: {
type: Object,
default: function () {
var defaultValue = {};
if (ChatLimitOn) {
defaultValue.chat_limit_on = true;
}
return defaultValue;
}
},
widget: {
type: Object,
},
publicKey: {
type: String,
select: false
},
privateKey: {
type: String,
select: false
},
status: {
type: Number,
required: true,
default: 100,
index: true,
// select: false
},
jwtSecret: {
type: String,
select: false
},
attributes: {
type: Object,
},
// apiKey: { //You do want to block anonymous traffic. https://cloud.google.com/endpoints/docs/openapi/when-why-api-key#:~:text=API%20keys%20identify%20an%20application's,patterns%20in%20your%20API's%20traffic
// type: String,
// select: false,
// default: function () {
// return uuidv4();
// //aggiungere env.GLOBAL_API_KEY che deve essere usato da code e dialogflow
// }
// },
profile: {
type: Profile.schema,
default: function () {
return new Profile();
}
},
versions: {
type: Number,
default: function() {
// try {
// var version = pjson.version;
// var versionNumber = parseInt(version.split(".").join(""));
// //console.log("versionNumber",versionNumber);
// return versionNumber;
// } catch(e) {
return 20115; //2.01.15
// }
}
},
channels: {
type: [Channel.schema],
default: function () {
return [new Channel({ name: 'chat21' })];
}
},
organization: {
type: String,
},
ipFilterEnabled:{
type: Boolean,
default: false
},
ipFilter: [{
type: String
}],
ipFilterDenyEnabled:{
type: Boolean,
default: false
},
ipFilterDeny: [{
type: String
}],
bannedUsers: [{
id: String, ip: String
}],
// defaultLanguage: {
// type: String,
// required: true,
// default: "EN",
// index: true
// },
createdBy: {
type: String,
required: true
}
}, {
timestamps: true,
toJSON: { virtuals: true } //used to polulate messages in toJSON// https://mongoosejs.com/docs/populate.html
}
);
ProjectSchema.virtual('trialExpired').get(function () {
if (trialEnabled === false) {
return false;
}
// https://stackoverflow.com/questions/6963311/add-days-to-a-date-object
let now = new Date();
// winston.debug("now.getTime() " + now.getTime());
// winston.debug("this.createdAt.getTime() " + this.createdAt.getTime());
// winston.debug("this ", this.toObject());
// winston.debug("trial " + this.profile.trialDays * 86400000);
if (this.createdAt.getTime() + this.profile.trialDays * 86400000 >= now.getTime()) {
// winston.debug("not expired");
return false;
} else {
// winston.debug("expired");
return true;
}
});
ProjectSchema.virtual('trialDaysLeft').get(function () {
// https://stackoverflow.com/questions/6963311/add-days-to-a-date-object
let now = new Date();
// winston.debug("trialDaysLeft now.getTime() " + now.getTime());
// winston.debug("trialDaysLeft this.createdAt.getTime() " + this.createdAt.getTime());
// winston.debug("trialDaysLeft this ", this.toObject());
// winston.debug("trialDaysLeft trial " + this.profile.trialDays * 86400000);
const millisTrialDaysLeft = now.getTime() - (this.createdAt.getTime() + this.profile.trialDays * 86400000);
const trialDaysLeft = Math.floor(millisTrialDaysLeft / (60 * 60 * 24 * 1000));
// winston.debug("trialDaysLeft now.getTime() " + now.getTime());
// winston.debug("trialDaysLeft this.createdAt.getTime() " + this.createdAt.getTime());
// winston.debug("trialDaysLeft ", millisTrialDaysLeft);
// winston.debug("trialDaysLeft - PROJECT NAME " + this.name + '; CREATED at ' + this.createdAt + ' -- trialDaysLeft: ', trialDaysLeft);
return trialDaysLeft
// return -8
});
ProjectSchema.virtual('isActiveSubscription').get(function () {
let now = new Date();
// winston.debug("isActiveSubscription - now.getTime() " + now.getTime());
// winston.debug("isActiveSubscription - PROJECT NAME: " + this.name);
// winston.debug("isActiveSubscription - PROJECT profile " + this.profile);
// winston.debug("isActiveSubscription - PROJECT profile trialDays: " + this.profile.trialDays);
// winston.debug("isActiveSubscription - PROJECT profile name: " + this.profile.name);
// winston.debug("isActiveSubscription - PROJECT profile type: " + this.profile.type);
// winston.debug("isActiveSubscription - PROJECT profile subscription end date: " + this.profile.subEnd);
// winston.debug("isActiveSubscription - this.activeOperatingHours: " + this.activeOperatingHours);
var isActiveSubscription = '';
if (this.profile && this.profile.type === 'payment') {
if (this.profile.subEnd) {
// winston.debug("isActiveSubscription - PROJECT profile subscription end date getTime(): " + this.profile.subEnd.getTime());
var subEndPlus3gg = this.profile.subEnd.getTime() + 259200000
// winston.debug("isActiveSubscription - PROJECT profile subscription end date getTime() + 3gg: " + subEndPlus3gg);
// FOR DEBUG
var subEndMinus3gg = this.profile.subEnd.getTime() - 259200000
// winston.debug("isActiveSubscription - PROJECT profile subscription end date getTime() - 3gg: " + subEndMinus3gg);
// + 259200000
if (now.getTime() > (this.profile.subEnd.getTime() + 259200000)) {
isActiveSubscription = false;
} else {
isActiveSubscription = true;
}
}
} else {
isActiveSubscription = false;
}
// winston.debug("isActiveSubscription - isActiveSubscription " + isActiveSubscription);
return isActiveSubscription
});
ProjectSchema.index({ _id: 1, status: 1 }); // schema level
module.exports = mongoose.model('project', ProjectSchema);