Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Livechat API to get and set location of user to database. #14765

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/livechat/server/api/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ import './v1/message.js';
import './v1/customField.js';
import './v1/room.js';
import './v1/videoCall.js';
import './v1/session.js';
31 changes: 31 additions & 0 deletions app/livechat/server/api/v1/session.js
Original file line number Diff line number Diff line change
@@ -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);
},
});
10 changes: 9 additions & 1 deletion app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions app/models/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -73,6 +74,7 @@ export {
LivechatDepartmentAgents,
LivechatOfficeHour,
LivechatPageVisited,
LivechatSessions,
LivechatTrigger,
LivechatVisitors,
ReadReceipts,
Expand Down
31 changes: 31 additions & 0 deletions app/models/server/models/LivechatSessions.js
Original file line number Diff line number Diff line change
@@ -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({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return this.insert({
return this.upsert({

Copy link
Contributor Author

@knrt10 knrt10 Jun 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this but it is not working, throwing an error. Insert is working fine.

token,
location,
ts: new Date(),
});
}
}

export default new LivechatSessions();