From d73654692bcc43dcfb2ba89826b29a2ee25ccf39 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Sat, 8 Jun 2019 17:06:46 +0530 Subject: [PATCH 1/2] Added new API to get and set location of user to new collection --- app/livechat/server/api/rest.js | 1 + app/livechat/server/api/v1/session.js | 25 ++++++++++++++++ app/livechat/server/lib/Livechat.js | 10 ++++++- app/models/server/index.js | 2 ++ app/models/server/models/LivechatSessions.js | 31 ++++++++++++++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 app/livechat/server/api/v1/session.js create mode 100644 app/models/server/models/LivechatSessions.js diff --git a/app/livechat/server/api/rest.js b/app/livechat/server/api/rest.js index 3731e72f6b634..17610ba0e1275 100644 --- a/app/livechat/server/api/rest.js +++ b/app/livechat/server/api/rest.js @@ -8,3 +8,4 @@ import './v1/message.js'; import './v1/customField.js'; import './v1/room.js'; import './v1/videoCall.js'; +import './v1/session.js'; diff --git a/app/livechat/server/api/v1/session.js b/app/livechat/server/api/v1/session.js new file mode 100644 index 0000000000000..12dcc1b4d1f47 --- /dev/null +++ b/app/livechat/server/api/v1/session.js @@ -0,0 +1,25 @@ +import { check } from 'meteor/check'; + +import { API } from '../../../../api'; +import { Livechat } from '../../lib/Livechat'; + +API.v1.addRoute('livechat/userLocation/:token', { + get() { + check(this.urlParams, { + token: String, + }); + + return Livechat.checkUserLocation(this.urlParams.token); + }, +}); + +API.v1.addRoute('livechat/addLocationData', { + post() { + check(this.bodyParams, { + token: String, + location: Object, + }); + + return Livechat.addUserLocationData(this.bodyParams); + }, +}); diff --git a/app/livechat/server/lib/Livechat.js b/app/livechat/server/lib/Livechat.js index 2119a53f9084c..5cb39b85934ac 100644 --- a/app/livechat/server/lib/Livechat.js +++ b/app/livechat/server/lib/Livechat.js @@ -14,7 +14,7 @@ import { QueueMethods } from './QueueMethods'; import { Analytics } from './Analytics'; import { settings } from '../../../settings'; import { callbacks } from '../../../callbacks'; -import { Users, Rooms, Messages, Subscriptions, Settings, LivechatDepartmentAgents, LivechatDepartment, LivechatCustomField, LivechatVisitors } from '../../../models'; +import { Users, Rooms, Messages, Subscriptions, Settings, LivechatDepartmentAgents, LivechatDepartment, LivechatCustomField, LivechatVisitors, LivechatSessions } from '../../../models'; import { Logger } from '../../../logger'; import { sendMessage, deleteMessage, updateMessage } from '../../../lib'; import { addUserRoles, removeUserFromRoles } from '../../../authorization'; @@ -928,6 +928,14 @@ export const Livechat = { }); }); }, + + checkUserLocation(token) { + return LivechatSessions.getUserLocationByToken(token); + }, + + addUserLocationData(locationData) { + return LivechatSessions.saveLocationForUser(locationData); + }, }; Livechat.stream = new Meteor.Streamer('livechat-room'); diff --git a/app/models/server/index.js b/app/models/server/index.js index 9aa14dea719e3..5f1dc108281b2 100644 --- a/app/models/server/index.js +++ b/app/models/server/index.js @@ -29,6 +29,7 @@ import LivechatDepartment from './models/LivechatDepartment'; import LivechatDepartmentAgents from './models/LivechatDepartmentAgents'; import LivechatOfficeHour from './models/LivechatOfficeHour'; import LivechatPageVisited from './models/LivechatPageVisited'; +import LivechatSessions from './models/LivechatSessions'; import LivechatTrigger from './models/LivechatTrigger'; import LivechatVisitors from './models/LivechatVisitors'; import ReadReceipts from './models/ReadReceipts'; @@ -73,6 +74,7 @@ export { LivechatDepartmentAgents, LivechatOfficeHour, LivechatPageVisited, + LivechatSessions, LivechatTrigger, LivechatVisitors, ReadReceipts, diff --git a/app/models/server/models/LivechatSessions.js b/app/models/server/models/LivechatSessions.js new file mode 100644 index 0000000000000..7636438de8649 --- /dev/null +++ b/app/models/server/models/LivechatSessions.js @@ -0,0 +1,31 @@ +import { Base } from './_Base'; + +/** + * Livechat session model + */ + +export class LivechatSessions extends Base { + constructor() { + super('livechat_sessions'); + } + + getUserLocationByToken(token) { + const query = { + token, + }; + + return this.findOne(query); + } + + saveLocationForUser(locationData) { + const { token, location } = locationData; + + return this.insert({ + token, + location, + ts: new Date(), + }); + } +} + +export default new LivechatSessions(); From 3a662673669283864f5efbb7b12e3b3e3dc09212 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Sun, 23 Jun 2019 12:23:28 +0530 Subject: [PATCH 2/2] Variable fix --- app/livechat/server/api/v1/session.js | 14 ++++++++++---- app/livechat/server/lib/Livechat.js | 8 ++++---- app/models/server/models/LivechatSessions.js | 6 +++--- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/livechat/server/api/v1/session.js b/app/livechat/server/api/v1/session.js index 12dcc1b4d1f47..6a6d18da574ae 100644 --- a/app/livechat/server/api/v1/session.js +++ b/app/livechat/server/api/v1/session.js @@ -1,4 +1,4 @@ -import { check } from 'meteor/check'; +import { check, Match } from 'meteor/check'; import { API } from '../../../../api'; import { Livechat } from '../../lib/Livechat'; @@ -9,7 +9,7 @@ API.v1.addRoute('livechat/userLocation/:token', { token: String, }); - return Livechat.checkUserLocation(this.urlParams.token); + return Livechat.getVisitorLocation(this.urlParams.token); }, }); @@ -17,9 +17,15 @@ API.v1.addRoute('livechat/addLocationData', { post() { check(this.bodyParams, { token: String, - location: Object, + location: Match.ObjectIncluding({ + city: String, + countryCode: String, + countryName: String, + latitude: Number, + longitude: Number, + }), }); - return Livechat.addUserLocationData(this.bodyParams); + return Livechat.updateVisitorLocation(this.bodyParams); }, }); diff --git a/app/livechat/server/lib/Livechat.js b/app/livechat/server/lib/Livechat.js index 5cb39b85934ac..77e06643fdc88 100644 --- a/app/livechat/server/lib/Livechat.js +++ b/app/livechat/server/lib/Livechat.js @@ -929,12 +929,12 @@ export const Livechat = { }); }, - checkUserLocation(token) { - return LivechatSessions.getUserLocationByToken(token); + getVisitorLocation(token) { + return LivechatSessions.findOneVisitorLocationByToken(token); }, - addUserLocationData(locationData) { - return LivechatSessions.saveLocationForUser(locationData); + updateVisitorLocation(data = {}) { + return LivechatSessions.saveVisitorLocation(data); }, }; diff --git a/app/models/server/models/LivechatSessions.js b/app/models/server/models/LivechatSessions.js index 7636438de8649..dc0960386b2f5 100644 --- a/app/models/server/models/LivechatSessions.js +++ b/app/models/server/models/LivechatSessions.js @@ -9,7 +9,7 @@ export class LivechatSessions extends Base { super('livechat_sessions'); } - getUserLocationByToken(token) { + findOneVisitorLocationByToken(token) { const query = { token, }; @@ -17,8 +17,8 @@ export class LivechatSessions extends Base { return this.findOne(query); } - saveLocationForUser(locationData) { - const { token, location } = locationData; + saveVisitorLocation(data = {}) { + const { token, location } = data; return this.insert({ token,