Send sales auto mails from same user per lead

This commit is contained in:
Matt Lott 2016-06-18 23:21:06 -07:00
parent b758b531d8
commit cd30e4d083
2 changed files with 61 additions and 27 deletions

View file

@ -29,7 +29,7 @@ getZPRepliedContacts((err, emailContactMap) => {
}
async.parallel(tasks, (err, results) => {
if (err) console.log(err);
console.log("Script runtime: " + (new Date() - scriptStartTime));
log("Script runtime: " + (new Date() - scriptStartTime));
});
});
@ -183,8 +183,12 @@ function getZPRepliedContacts(done) {
}
if (!emailContactMap[contact.email]) emailContactMap[contact.email] = contact;
}
console.log(`${total} total ZP contacts, ${Object.keys(emailContactMap).length} with replies`);
log(`${total} total ZP contacts, ${Object.keys(emailContactMap).length} with replies`);
return done(null, emailContactMap);
});
});
}
function log(str) {
console.log(new Date().toISOString() + " " + str);
}

View file

@ -27,7 +27,7 @@ const customFieldsToRemove = [
];
// Skip these problematic leads
const leadsToSkip = ['6 sınıflar', 'fdsafd', 'ashtasht', 'matt+20160404teacher3 school', 'sdfdsf', 'ddddd', 'dsfadsaf', "Nolan's School of Wonders"];
const leadsToSkip = ['6 sınıflar', 'fdsafd', 'ashtasht', 'matt+20160404teacher3 school', 'sdfdsf', 'ddddd', 'dsfadsaf', "Nolan's School of Wonders", 'asdfsadf'];
const createTeacherEmailTemplatesAuto1 = ['tmpl_i5bQ2dOlMdZTvZil21bhTx44JYoojPbFkciJ0F560mn', 'tmpl_CEZ9PuE1y4PRvlYiKB5kRbZAQcTIucxDvSeqvtQW57G'];
const demoRequestEmailTemplatesAuto1 = ['tmpl_s7BZiydyCHOMMeXAcqRZzqn0fOtk0yOFlXSZ412MSGm', 'tmpl_cGb6m4ssDvqjvYd8UaG6cacvtSXkZY3vj9b9lSmdQrf'];
@ -624,7 +624,7 @@ class CocoLead {
// ** Upsert Close.io methods
function updateExistingLead(lead, existingLead, done) {
function updateExistingLead(lead, existingLead, userApiKeyMap, done) {
// console.log('DEBUG: updateExistingLead', existingLead.id);
const putData = lead.getLeadPutData(existingLead);
const options = {
@ -646,7 +646,7 @@ function updateExistingLead(lead, existingLead, done) {
const tasks = []
for (const newContact of newContacts) {
newContact.lead_id = existingLead.id;
tasks.push(createAddContactFn(newContact, lead, existingLead));
tasks.push(createAddContactFn(newContact, lead, existingLead, userApiKeyMap));
}
async.parallel(tasks, (err, results) => {
if (err) return done(err);
@ -737,7 +737,7 @@ function createFindExistingLeadFn(email, name, existingLeads) {
};
}
function createUpdateLeadFn(lead, existingLeads) {
function createUpdateLeadFn(lead, existingLeads, userApiKeyMap) {
return (done) => {
// console.log('DEBUG: updateLead', lead.name);
const query = `name:"${lead.name}"`;
@ -750,7 +750,7 @@ function createUpdateLeadFn(lead, existingLeads) {
if (existingLeads[lead.name.toLowerCase()]) {
if (existingLeads[lead.name.toLowerCase()].length === 1) {
// console.log(`DEBUG: Using lead from email lookup: ${lead.name}`);
return updateExistingLead(lead, existingLeads[lead.name.toLowerCase()][0], done);
return updateExistingLead(lead, existingLeads[lead.name.toLowerCase()][0], userApiKeyMap, done);
}
console.error(`ERROR: ${existingLeads[lead.name.toLowerCase()].length} email leads found for ${lead.name}`);
return done();
@ -761,7 +761,7 @@ function createUpdateLeadFn(lead, existingLeads) {
console.error(`ERROR: ${data.total_results} leads found for ${lead.name}`);
return done();
}
return updateExistingLead(lead, data.data[0], done);
return updateExistingLead(lead, data.data[0], userApiKeyMap, done);
} catch (error) {
// console.log(url);
console.log(`ERROR: updateLead ${error}`);
@ -772,9 +772,11 @@ function createUpdateLeadFn(lead, existingLeads) {
};
}
function createAddContactFn(postData, internalLead, externalLead) {
function createAddContactFn(postData, internalLead, closeIoLead, userApiKeyMap) {
return (done) => {
// console.log('DEBUG: addContact', postData.lead_id);
// Create new contact
const options = {
uri: `https://${closeIoApiKey}:X@app.close.io/api/v1/contact/`,
body: JSON.stringify(postData)
@ -788,11 +790,20 @@ function createAddContactFn(postData, internalLead, externalLead) {
return done();
}
// Send emails to new contact
const email = postData.emails[0].email;
const countryCode = getCountryCode(internalLead.contacts[email].trial.properties.country, [email]);
const emailTemplate = getEmailTemplate(internalLead.contacts[email].trial.properties.siteOrigin, externalLead.status_label);
sendMail(email, externalLead.id, newContact.id, emailTemplate, getEmailApiKey(externalLead.status_label), emailDelayMinutes, done);
// Find previous internal user for new contact correspondence
const url = `https://${closeIoApiKey}:X@app.close.io/api/v1/activity/email/?lead_id=${closeIoLead.id}`;
request.get(url, (error, response, body) => {
if (error) return done(error);
const data = JSON.parse(body);
let emailApiKey = data.data && data.data.length > 0 ? userApiKeyMap[data.data[0].user_id] : getEmailApiKey(closeIoLead.status_label);
if (!emailApiKey) emailApiKey = getEmailApiKey(closeIoLead.status_label);
// Send email to new contact
const email = postData.emails[0].email;
const countryCode = getCountryCode(internalLead.contacts[email].trial.properties.country, [email]);
const emailTemplate = getEmailTemplate(internalLead.contacts[email].trial.properties.siteOrigin, closeIoLead.status_label);
sendMail(email, closeIoLead.id, newContact.id, emailTemplate, emailApiKey, emailDelayMinutes, done);
});
});
};
}
@ -883,25 +894,44 @@ function sendMail(toEmail, leadId, contactId, template, emailApiKey, delayMinute
}
function updateLeads(leads, done) {
// Lookup existing leads via email to protect against direct lead name querying later
// Querying via lead name is unreliable
const existingLeads = {};
const tasks = [];
for (const name in leads) {
if (leadsToSkip.indexOf(name) >= 0) continue;
for (const email in leads[name].contacts) {
tasks.push(createFindExistingLeadFn(email.toLowerCase(), name.toLowerCase(), existingLeads));
}
const userApiKeyMap = {};
let createGetUserFn = (apiKey) => {
return (done) => {
const url = `https://${apiKey}:X@app.close.io/api/v1/me/`;
request.get(url, (error, response, body) => {
if (error) return done();
const results = JSON.parse(body);
userApiKeyMap[results.id] = apiKey;
return done();
});
};
}
async.series(tasks, (err, results) => {
if (err) return done(err);
const tasks = [];
for (const closeIoMailApiKey of closeIoMailApiKeys) {
tasks.push(createGetUserFn(closeIoMailApiKey.apiKey));
}
async.parallel(tasks, (err, results) => {
if (err) console.log(err);
// Lookup existing leads via email to protect against direct lead name querying later
// Querying via lead name is unreliable
const existingLeads = {};
const tasks = [];
for (const name in leads) {
if (leadsToSkip.indexOf(name) >= 0) continue;
tasks.push(createUpdateLeadFn(leads[name], existingLeads));
for (const email in leads[name].contacts) {
tasks.push(createFindExistingLeadFn(email.toLowerCase(), name.toLowerCase(), existingLeads));
}
}
async.series(tasks, (err, results) => {
return done(err);
if (err) return done(err);
const tasks = [];
for (const name in leads) {
if (leadsToSkip.indexOf(name) >= 0) continue;
tasks.push(createUpdateLeadFn(leads[name], existingLeads, userApiKeyMap));
}
async.series(tasks, (err, results) => {
return done(err);
});
});
});
}