@tiledesk/tiledesk-server 2.9.26 → 2.9.28

Sign up to get free protection for your applications and to get access to all the features.
package/routes/users.js CHANGED
@@ -193,17 +193,14 @@ router.post('/loginemail', function (req, res) {
193
193
 
194
194
  let project_id = req.body.id_project;
195
195
  let chatbot_id = req.body.bot_id;
196
+ let namespace_id = req.body.namespace_id;
196
197
 
197
198
  if (!project_id) {
198
199
  res.status(500).send({ success: false, error: "missing 'id_project' field" });
199
200
  }
200
201
 
201
- if (!chatbot_id) {
202
- res.status(500).send({ success: false, error: "missing 'bot_id' field" });
203
- }
204
-
205
- if (!chatbot_id) {
206
- res.status(500).send({ success: false, error: "missing 'bot_id' field" });
202
+ if (!chatbot_id && !namespace_id) {
203
+ res.status(500).send({ success: false, error: "missing 'bot_id' or 'namespace_id' field" });
207
204
  }
208
205
 
209
206
  User.findById(user_id, (err, user) => {
@@ -212,7 +209,7 @@ router.post('/loginemail', function (req, res) {
212
209
  }
213
210
  winston.debug("user found: ", user);
214
211
 
215
- emailService.sendEmailRedirectOnDesktop(user.email, token, project_id, chatbot_id)
212
+ emailService.sendEmailRedirectOnDesktop(user.email, token, project_id, chatbot_id, namespace_id)
216
213
  return res.status(200).send({ success: true, message: "Sending email..."})
217
214
  })
218
215
 
@@ -1,4 +1,5 @@
1
1
  const { ceil, floor } = require('lodash');
2
+ const moment = require('moment');
2
3
  let winston = require('../config/winston');
3
4
  const requestEvent = require('../event/requestEvent');
4
5
  const messageEvent = require('../event/messageEvent');
@@ -99,40 +100,82 @@ class QuoteManager {
99
100
 
100
101
  async generateKey(object, type) {
101
102
 
102
- winston.debug("generateKey object ", object)
103
- winston.debug("generateKey type " + type)
103
+ let objectDate = moment(object.createdAt);
104
104
  let subscriptionDate;
105
105
 
106
106
  if (this.project.isActiveSubscription === true) {
107
107
  if (this.project.profile.subStart) {
108
- subscriptionDate = this.project.profile.subStart;
108
+ subscriptionDate = moment(this.project.profile.subStart);
109
+ winston.debug("Subscription date from subStart: " + subscriptionDate.toISOString());
109
110
  } else {
110
111
  // it should never happen
111
112
  winston.error("Error: quote manager - isActiveSubscription is true but subStart does not exists.")
112
113
  }
113
114
  } else {
114
115
  if (this.project.profile.subEnd) {
115
- subscriptionDate = this.project.profile.subEnd;
116
+ subscriptionDate = moment(this.project.profile.subEnd);
117
+ winston.debug("Subscription date from subEnd: " + subscriptionDate.toISOString());
116
118
  } else {
117
- subscriptionDate = this.project.createdAt;
119
+ subscriptionDate = moment(this.project.createdAt);
120
+ winston.debug("Subscription date from project createdAt: " + subscriptionDate.toISOString());
118
121
  }
119
122
  }
120
123
 
121
- let objectDate = object.createdAt;
122
- winston.debug("objectDate " + objectDate);
124
+ // Calculate the difference in months between the object date and the subscription date
125
+ let diffInMonths = objectDate.diff(subscriptionDate, 'months');
126
+ winston.debug("diffInMonths: ", diffInMonths)
127
+
128
+ // Make a clone of the subscription date --> this operation could be avoided
129
+ // Get the renewal date adding diffInMonths. Moment.js manage automatically the less longer month.
130
+ // E.g. if subscription date is 31 jan the renewals will be, 28/29 feb, 31 mar, 30 apr, etc.
131
+ let renewalDate = subscriptionDate.clone().add(diffInMonths, 'months');
132
+ // Force the renewal date equal to the last day of the month --> this operation could be avoided
133
+ if (renewalDate.date() !== subscriptionDate.date()) {
134
+ renewalDate = renewalDate.endOf('month');
135
+ }
136
+ winston.debug("renewalDate: ", renewalDate)
123
137
 
124
- // converts date in timestamps and transform from ms to s
125
- const objectDateTimestamp = ceil(objectDate.getTime() / 1000);
126
- const subscriptionDateTimestamp = ceil(subscriptionDate.getTime() / 1000);
138
+ return "quotes:" + type + ":" + this.project._id + ":" + renewalDate.format('M/D/YYYY');
139
+ // return "quotes:" + type + ":" + this.project._id + ":" + renewalDate.format('MM/DD/YYYY');
140
+ // return "quotes:" + type + ":" + this.project._id + ":" + renewalDate.toLocaleString();
141
+ }
127
142
 
128
- let ndays = (objectDateTimestamp - subscriptionDateTimestamp) / 86400; // 86400 is the number of seconds in 1 day
129
- let nmonths = floor(ndays / 30); // number of month to add to the initial subscription date;
143
+ // async _generateKey(object, type) {
130
144
 
131
- let date = new Date(subscriptionDate);
132
- date.setMonth(date.getMonth() + nmonths);
145
+ // winston.debug("generateKey object ", object)
146
+ // winston.debug("generateKey type " + type)
147
+ // let subscriptionDate;
133
148
 
134
- return "quotes:" + type + ":" + this.project._id + ":" + date.toLocaleDateString();
135
- }
149
+ // if (this.project.isActiveSubscription === true) {
150
+ // if (this.project.profile.subStart) {
151
+ // subscriptionDate = this.project.profile.subStart;
152
+ // } else {
153
+ // // it should never happen
154
+ // winston.error("Error: quote manager - isActiveSubscription is true but subStart does not exists.")
155
+ // }
156
+ // } else {
157
+ // if (this.project.profile.subEnd) {
158
+ // subscriptionDate = this.project.profile.subEnd;
159
+ // } else {
160
+ // subscriptionDate = this.project.createdAt;
161
+ // }
162
+ // }
163
+
164
+ // let objectDate = object.createdAt;
165
+ // winston.debug("objectDate " + objectDate);
166
+
167
+ // // converts date in timestamps and transform from ms to s
168
+ // const objectDateTimestamp = ceil(objectDate.getTime() / 1000);
169
+ // const subscriptionDateTimestamp = ceil(subscriptionDate.getTime() / 1000);
170
+
171
+ // let ndays = (objectDateTimestamp - subscriptionDateTimestamp) / 86400; // 86400 is the number of seconds in 1 day
172
+ // let nmonths = floor(ndays / 30); // number of month to add to the initial subscription date;
173
+
174
+ // let date = new Date(subscriptionDate);
175
+ // date.setMonth(date.getMonth() + nmonths);
176
+
177
+ // return "quotes:" + type + ":" + this.project._id + ":" + date.toLocaleDateString();
178
+ // }
136
179
 
137
180
  /**
138
181
  * Get current quote for a single type (tokens or request or ...)
@@ -373,6 +416,51 @@ class QuoteManager {
373
416
  }
374
417
  }
375
418
 
419
+ async getCurrentSlot(project) {
420
+ let subscriptionDate;
421
+ if (project.isActiveSubscription === true) {
422
+ if (project.profile.subStart) {
423
+ subscriptionDate = moment(project.profile.subStart);
424
+ winston.debug("Subscription date from subStart: " + subscriptionDate.toISOString());
425
+ } else {
426
+ // it should never happen
427
+ winston.error("Error: quote manager - isActiveSubscription is true but subStart does not exists.")
428
+ }
429
+ } else {
430
+ if (project.profile.subEnd) {
431
+ subscriptionDate = moment(project.profile.subEnd);
432
+ winston.debug("Subscription date from subEnd: " + subscriptionDate.toISOString());
433
+ } else {
434
+ subscriptionDate = moment(project.createdAt);
435
+ winston.debug("Subscription date from project createdAt: " + subscriptionDate.toISOString());
436
+ }
437
+ }
438
+
439
+ let now = moment();
440
+ winston.debug("now: ", now);
441
+
442
+ let diffInMonths = now.diff(subscriptionDate, 'months');
443
+ winston.debug("diffInMonths: ", diffInMonths)
444
+
445
+ let renewalDate = subscriptionDate.clone().add(diffInMonths, 'months').startOf('day');
446
+ winston.debug("renewalDate: ", renewalDate)
447
+
448
+ let slotEnd = subscriptionDate.clone().add(diffInMonths + 1, 'month');
449
+ slotEnd.subtract(1, 'day').endOf('day')
450
+ winston.debug("slotEnd: ", slotEnd)
451
+
452
+ // let slot = {
453
+ // startDate: renewalDate.format('DD/MM/YYYY'),
454
+ // endDate: slotEnd.format('DD/MM/YYYY')
455
+ // }
456
+ let slot = {
457
+ startDate: renewalDate,
458
+ endDate: slotEnd
459
+ }
460
+
461
+ return slot;
462
+ }
463
+
376
464
 
377
465
 
378
466
  start() {
@@ -324,7 +324,8 @@ class EmailService {
324
324
  winston.debug(' mail.config', mail.config);
325
325
 
326
326
  if (!mail.to) {
327
- return winston.warn("EmailService send method. to field is not defined", mailOptions);
327
+ // return winston.warn("EmailService send method. to field is not defined", mailOptions);
328
+ return winston.warn("EmailService send method. to field is not defined");
328
329
  }
329
330
 
330
331
  // send mail with defined transport object
@@ -1802,7 +1803,7 @@ class EmailService {
1802
1803
 
1803
1804
  }
1804
1805
 
1805
- async sendEmailRedirectOnDesktop(to, token, project_id, chatbot_id) {
1806
+ async sendEmailRedirectOnDesktop(to, token, project_id, chatbot_id, namespace_id) {
1806
1807
  winston.debug("sendEmailRedirectOnDesktop: " + to);
1807
1808
 
1808
1809
  var that = this;
@@ -1817,8 +1818,16 @@ class EmailService {
1817
1818
  let baseScope = JSON.parse(JSON.stringify(that));
1818
1819
  delete baseScope.pass;
1819
1820
 
1821
+ let redirect_url;
1822
+ if (chatbot_id) {
1823
+ redirect_url = `https://panel.tiledesk.com/v3/cds/#/project/${project_id}/chatbot/${chatbot_id}/intent/0?jwt=${token}`;
1824
+ } else {
1825
+ redirect_url = `${baseScope.baseUrl}/#/project/${project_id}/knowledge-bases/${namespace_id}?token=${token}`;
1826
+ }
1827
+
1820
1828
  let replacements = {
1821
1829
  baseScope: baseScope,
1830
+ redirect_url: redirect_url,
1822
1831
  token: token,
1823
1832
  project_id: project_id,
1824
1833
  chatbot_id: chatbot_id
@@ -355,6 +355,7 @@ function checkDay(operatingHoursPars, dayNowAtPrjctTz) {
355
355
  }
356
356
 
357
357
  function checkTimes(operatingHoursPars, dayNowAtPrjctTz, timeNowAtPrjctTz) {
358
+
358
359
  for (var operatingHoursweekDay in operatingHoursPars) {
359
360
  if (operatingHoursweekDay != 'tzname') {
360
361
  if (dayNowAtPrjctTz == operatingHoursweekDay) {
@@ -6,6 +6,8 @@ var mongoose = require('mongoose');
6
6
  var departmentService = require('../services/departmentService');
7
7
  var projectEvent = require("../event/projectEvent");
8
8
  var winston = require('../config/winston');
9
+ const cacheEnabler = require("./cacheEnabler");
10
+ const cacheUtil = require("../utils/cacheUtil");
9
11
 
10
12
  class ProjectService {
11
13
 
@@ -71,6 +73,25 @@ class ProjectService {
71
73
  });
72
74
  }
73
75
 
76
+ async getCachedProject(id_project) {
77
+ return new Promise((resolve, reject) => {
78
+ let q = Project.findOne({ _id: id_project, status: 100 });
79
+ if (cacheEnabler.project) {
80
+ q.cache(cacheUtil.longTTL, "projects:id:" + id_project) //project_cache
81
+ winston.debug('project cache enabled for /project detail');
82
+ }
83
+ q.exec( async (err, p) => {
84
+ if (err) {
85
+ reject(err);
86
+ }
87
+ if (!p) {
88
+ reject('Project not found')
89
+ }
90
+
91
+ resolve(p);
92
+ })
93
+ })
94
+ }
74
95
 
75
96
 
76
97
  }