Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c4cf843
Update index.js
ggazzo Jan 8, 2021
97b69f7
Fix Livechat.registerGuest method.
renatobecker Jan 8, 2021
a0f2498
Merge branch 'develop' into omnichannel/fix-register-guest-method
renatobecker Jan 8, 2021
90def2b
Merge branch 'develop' into omnichannel/fix-register-guest-method
renatobecker Jan 8, 2021
efad87f
pass only fields that need to be inserted
rafaelblink Jan 8, 2021
b411319
Merge remote-tracking branch 'origin/omnichannel/fix-register-guest-m…
rafaelblink Jan 8, 2021
d53a299
undo changes on Livechat, registerGuest method.
rafaelblink Jan 11, 2021
b4c18ea
refactor contacts using your own lib file.
rafaelblink Jan 11, 2021
3cd9e9d
remove unnecessary http header method
rafaelblink Jan 11, 2021
8741c80
refactor null value on server side.
rafaelblink Jan 11, 2021
5317135
check if customFields are valid and adding only valid fields.
rafaelblink Jan 11, 2021
7544559
check if customField is visitor and it has some value.
rafaelblink Jan 11, 2021
278930d
Improve code style.
renatobecker Jan 11, 2021
d68d5dc
Merge branch 'develop' into omnichannel/fix-register-guest-method
renatobecker Jan 11, 2021
af2354e
Implements search for fields check if either email or phone already e…
rafaelblink Jan 11, 2021
d2b55a5
validate if both email and phone already exist
rafaelblink Jan 12, 2021
b5a88ef
Merge branch 'develop' into omnichannel/fix-register-guest-method
renatobecker Jan 12, 2021
4d33bc3
code enhancements on api and field validations.
rafaelblink Jan 12, 2021
fbf27e8
Merge remote-tracking branch 'origin/omnichannel/fix-register-guest-m…
rafaelblink Jan 12, 2021
d972a01
undo unecessary field clean validation.
rafaelblink Jan 12, 2021
934dee4
remove email field icon to avoid weird error validation and clear err…
rafaelblink Jan 12, 2021
aa29aa1
Merge branch 'develop' into omnichannel/fix-register-guest-method
renatobecker Jan 12, 2021
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
5 changes: 3 additions & 2 deletions app/livechat/server/api/v1/contact.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Match, check } from 'meteor/check';

import { API } from '../../../../api/server';
import { Livechat } from '../../lib/Livechat';
import { Contacts } from '../../lib/Contacts';
import {
LivechatVisitors,
} from '../../../../models';
Expand All @@ -23,7 +23,8 @@ API.v1.addRoute('omnichannel/contact', { authRequired: true }, {
if (this.bodyParams.phone) {
Comment thread
rafaelblink marked this conversation as resolved.
Outdated
contactParams.phone = { number: this.bodyParams.phone };
}
const contact = Livechat.registerGuest(contactParams);

const contact = Contacts.registerContact(contactParams);

return API.v1.success({ contact });
} catch (e) {
Expand Down
62 changes: 62 additions & 0 deletions app/livechat/server/lib/Contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { check } from 'meteor/check';
import s from 'underscore.string';
import _ from 'underscore';

import {
LivechatVisitors,
} from '../../../models';

export const Contacts = {

registerContact({ token, name, email, phone, username, livechatData, contactManager } = {}) {
Comment thread
rafaelblink marked this conversation as resolved.
Outdated
check(token, String);

let userId;
const updateUser = {
$set: {
token,
},
};

const user = LivechatVisitors.getVisitorByToken(token, { fields: { _id: 1 } });

if (user) {
userId = user._id;
} else {
if (!username) {
username = LivechatVisitors.getNextVisitorUsername();
}

let existingUser = null;

if (s.trim(email) !== '' && (existingUser = LivechatVisitors.findOneGuestByEmailAddress(email))) {
userId = existingUser._id;
} else {
const userData = {
username,
ts: new Date(),
};

userId = LivechatVisitors.insert(userData);
}
}

updateUser.$set.name = name;

updateUser.$set.phone = [
{ phoneNumber: phone?.number },
];

updateUser.$set.visitorEmails = [
{ address: email },
];

updateUser.$set.livechatData = livechatData;
Comment thread
rafaelblink marked this conversation as resolved.

updateUser.$set.contactManager = !_.isEmpty(contactManager) ? contactManager : '';
Comment thread
rafaelblink marked this conversation as resolved.
Outdated

LivechatVisitors.updateById(userId, updateUser);

return userId;
},
};
27 changes: 5 additions & 22 deletions app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,14 @@ export const Livechat = {
return true;
},

registerGuest({ token, name, email, department, phone, username, livechatData, contactManager, connectionData } = {}) {
registerGuest({ token, name, email, department, phone, username, connectionData } = {}) {
check(token, String);

let userId;
const updateUser = {
$set: {
token,
},
$unset: { },
};

const user = LivechatVisitors.getVisitorByToken(token, { fields: { _id: 1 } });
Expand Down Expand Up @@ -235,45 +234,29 @@ export const Livechat = {
}
}

if (name) {
updateUser.$set.name = name;
}

if (phone) {
updateUser.$set.phone = [
{ phoneNumber: phone.number },
];
} else {
updateUser.$unset.phone = 1;
}

if (email && email.trim() !== '') {
updateUser.$set.visitorEmails = [
{ address: email },
];
} else {
updateUser.$unset.visitorEmails = 1;
}

if (livechatData) {
updateUser.$set.livechatData = livechatData;
} else {
updateUser.$unset.livechatData = 1;
}

if (contactManager) {
updateUser.$set.contactManager = contactManager;
} else {
updateUser.$unset.contactManager = 1;
if (name) {
updateUser.$set.name = name;
}

if (!department) {
updateUser.$unset.department = 1;
Object.assign(updateUser, { $unset: { department: 1 } });
} else {
const dep = LivechatDepartment.findOneByIdOrName(department);
updateUser.$set.department = dep && dep._id;
}
if (_.isEmpty(updateUser.$unset)) { delete updateUser.$unset; }

LivechatVisitors.updateById(userId, updateUser);

return userId;
Expand Down
8 changes: 4 additions & 4 deletions client/omnichannel/directory/ContactForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ export function ContactNewEdit({ id, data, reload, close }) {

const payload = {
name,
email,
phone,
};
payload.phone = phone;
payload.email = email;
payload.livechatData = livechatData;
Comment thread
rafaelblink marked this conversation as resolved.
Outdated
payload.contactManager = username ? { username } : {};
Comment thread
rafaelblink marked this conversation as resolved.

if (id) {
payload._id = id;
payload.token = token;
} else {
payload.token = createToken();
}
if (livechatData) { payload.livechatData = livechatData; }
if (username) { payload.contactManager = { username }; }

try {
await saveContact(payload);
Expand Down