|
|
@ -1,5 +1,7 @@
|
|
|
|
const { omit, compact, map } = require('lodash');
|
|
|
|
const { omit, compact, map } = require('lodash');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { toLogFormat } = require('./errors');
|
|
|
|
|
|
|
|
|
|
|
|
exports.parseAndWriteContactAvatar = upgradeAttachment => async (
|
|
|
|
exports.parseAndWriteContactAvatar = upgradeAttachment => async (
|
|
|
|
contact,
|
|
|
|
contact,
|
|
|
|
context = {}
|
|
|
|
context = {}
|
|
|
@ -18,10 +20,15 @@ exports.parseAndWriteContactAvatar = upgradeAttachment => async (
|
|
|
|
// eliminates empty numbers, emails, and addresses; adds type if not provided
|
|
|
|
// eliminates empty numbers, emails, and addresses; adds type if not provided
|
|
|
|
const contactWithCleanedElements = parseContact(contactWithUpdatedAvatar);
|
|
|
|
const contactWithCleanedElements = parseContact(contactWithUpdatedAvatar);
|
|
|
|
|
|
|
|
|
|
|
|
// We'll log if the contact is invalid, leave everything as-is
|
|
|
|
const error = exports._validateContact(contactWithCleanedElements, {
|
|
|
|
validateContact(contactWithCleanedElements, {
|
|
|
|
|
|
|
|
messageId: idForLogging(message),
|
|
|
|
messageId: idForLogging(message),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
|
|
|
|
'Contact.parseAndWriteContactAvatar: contact was malformed.',
|
|
|
|
|
|
|
|
toLogFormat(error)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return contactWithCleanedElements;
|
|
|
|
return contactWithCleanedElements;
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -41,15 +48,14 @@ function idForLogging(message) {
|
|
|
|
return `${message.source}.${message.sourceDevice} ${message.sent_at}`;
|
|
|
|
return `${message.source}.${message.sourceDevice} ${message.sent_at}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function validateContact(contact, options = {}) {
|
|
|
|
exports._validateContact = (contact, options = {}) => {
|
|
|
|
const { messageId } = options;
|
|
|
|
const { messageId } = options;
|
|
|
|
const { name, number, email, address, organization } = contact;
|
|
|
|
const { name, number, email, address, organization } = contact;
|
|
|
|
|
|
|
|
|
|
|
|
if ((!name || !name.displayName) && !organization) {
|
|
|
|
if ((!name || !name.displayName) && !organization) {
|
|
|
|
console.log(
|
|
|
|
return new Error(
|
|
|
|
`Message ${messageId}: Contact had neither 'displayName' nor 'organization'`
|
|
|
|
`Message ${messageId}: Contact had neither 'displayName' nor 'organization'`
|
|
|
|
);
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
if (
|
|
|
@ -57,14 +63,13 @@ function validateContact(contact, options = {}) {
|
|
|
|
(!email || !email.length) &&
|
|
|
|
(!email || !email.length) &&
|
|
|
|
(!address || !address.length)
|
|
|
|
(!address || !address.length)
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
console.log(
|
|
|
|
return new Error(
|
|
|
|
`Message ${messageId}: Contact had no included numbers, email or addresses`
|
|
|
|
`Message ${messageId}: Contact had no included numbers, email or addresses`
|
|
|
|
);
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function cleanBasicItem(item) {
|
|
|
|
function cleanBasicItem(item) {
|
|
|
|
if (!item.value) {
|
|
|
|
if (!item.value) {
|
|
|
|