diff --git a/app/livechat/server/api/rest.js b/app/livechat/server/api/rest.js index 3731e72f6b63..17610ba0e127 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 000000000000..6a6d18da574a --- /dev/null +++ b/app/livechat/server/api/v1/session.js @@ -0,0 +1,31 @@ +import { check, Match } 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.getVisitorLocation(this.urlParams.token); + }, +}); + +API.v1.addRoute('livechat/addLocationData', { + post() { + check(this.bodyParams, { + token: String, + location: Match.ObjectIncluding({ + city: String, + countryCode: String, + countryName: String, + latitude: Number, + longitude: Number, + }), + }); + + return Livechat.updateVisitorLocation(this.bodyParams); + }, +}); diff --git a/app/livechat/server/lib/Livechat.js b/app/livechat/server/lib/Livechat.js index 2119a53f9084..77e06643fdc8 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 = { }); }); }, + + getVisitorLocation(token) { + return LivechatSessions.findOneVisitorLocationByToken(token); + }, + + updateVisitorLocation(data = {}) { + return LivechatSessions.saveVisitorLocation(data); + }, }; Livechat.stream = new Meteor.Streamer('livechat-room'); diff --git a/app/models/server/index.js b/app/models/server/index.js index 9aa14dea719e..5f1dc108281b 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 000000000000..dc0960386b2f --- /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'); + } + + findOneVisitorLocationByToken(token) { + const query = { + token, + }; + + return this.findOne(query); + } + + saveVisitorLocation(data = {}) { + const { token, location } = data; + + return this.insert({ + token, + location, + ts: new Date(), + }); + } +} + +export default new LivechatSessions();