Add followup emails for CA/UK/AU/NZ

This commit is contained in:
Matt Lott 2016-07-22 16:59:55 -07:00
parent cac87559c6
commit 2722d501be

View file

@ -15,8 +15,12 @@ if (process.argv.length !== 8) {
const createTeacherEmailTemplatesAuto1 = ['tmpl_i5bQ2dOlMdZTvZil21bhTx44JYoojPbFkciJ0F560mn', 'tmpl_CEZ9PuE1y4PRvlYiKB5kRbZAQcTIucxDvSeqvtQW57G']; const createTeacherEmailTemplatesAuto1 = ['tmpl_i5bQ2dOlMdZTvZil21bhTx44JYoojPbFkciJ0F560mn', 'tmpl_CEZ9PuE1y4PRvlYiKB5kRbZAQcTIucxDvSeqvtQW57G'];
const demoRequestEmailTemplatesAuto1 = ['tmpl_s7BZiydyCHOMMeXAcqRZzqn0fOtk0yOFlXSZ412MSGm', 'tmpl_cGb6m4ssDvqjvYd8UaG6cacvtSXkZY3vj9b9lSmdQrf']; const demoRequestEmailTemplatesAuto1 = ['tmpl_s7BZiydyCHOMMeXAcqRZzqn0fOtk0yOFlXSZ412MSGm', 'tmpl_cGb6m4ssDvqjvYd8UaG6cacvtSXkZY3vj9b9lSmdQrf'];
const createTeacherInternationalEmailTemplateAuto1 = 'tmpl_8vsXwcr6dWefMnAEfPEcdHaxqSfUKUY8UKq6WfReGqG';
const demoRequestInternationalEmailTemplateAuto1 = 'tmpl_nnH1p3II7G7NJYiPOIHphuj4XUaDptrZk1mGQb2d9Xa';
const createTeacherEmailTemplatesAuto2 = ['tmpl_pGPtKa07ioISupdSc1MAzNC57K40XoA4k0PI1igi8Ec', 'tmpl_AYAcviU8NQGLbMGKSp3EmcBLha0gQw4cHSOR55Fmoha']; const createTeacherEmailTemplatesAuto2 = ['tmpl_pGPtKa07ioISupdSc1MAzNC57K40XoA4k0PI1igi8Ec', 'tmpl_AYAcviU8NQGLbMGKSp3EmcBLha0gQw4cHSOR55Fmoha'];
const demoRequestEmailTemplatesAuto2 = ['tmpl_HJ5zebh1SqC1QydDto05VPUMu4F7i5M35Llq7bzgfTw', 'tmpl_dmnK7IVpkyYfPYAl1rChhm9lClH5lJ9pQAZoPr7cvLt']; const demoRequestEmailTemplatesAuto2 = ['tmpl_HJ5zebh1SqC1QydDto05VPUMu4F7i5M35Llq7bzgfTw', 'tmpl_dmnK7IVpkyYfPYAl1rChhm9lClH5lJ9pQAZoPr7cvLt'];
const createTeacherInternationalEmailTemplatesAuto2 = ['tmpl_a6Syzzy6ri9MErfXQySM5UfaF5iNIv1VCArYowAEICT', 'tmpl_jOqWLgT0G19Eqs7qZaAeNwtiull7UrSX4ZuvkYRM2gC'];
const demoRequestInternationalEmailTemplatesAuto2 = ['tmpl_wz4SnDZMjNmAhp3MIuZaSMmjJTy5IW75Rcy3MYGb6Ti', 'tmpl_5oJ0YQMZFqNi3DgW7hplD6JS2zHqkB4Gt7Fj1u19Nks'];
const scriptStartTime = new Date(); const scriptStartTime = new Date();
const closeIoApiKey = process.argv[2]; const closeIoApiKey = process.argv[2];
@ -44,8 +48,24 @@ async.series([
// ** Utilities // ** Utilities
function getRandomEmailTemplateAuto2(template) {
if (createTeacherEmailTemplatesAuto1.indexOf(template) >= 0) {
return getRandomEmailTemplate(createTeacherEmailTemplatesAuto2);
}
if (demoRequestEmailTemplatesAuto1.indexOf(template) >= 0) {
return getRandomEmailTemplate(demoRequestEmailTemplatesAuto2);
}
if (createTeacherInternationalEmailTemplateAuto1 == template) {
return getRandomEmailTemplate(createTeacherInternationalEmailTemplatesAuto2);
}
if (demoRequestInternationalEmailTemplateAuto1 === template) {
return getRandomEmailTemplate(demoRequestInternationalEmailTemplatesAuto2);
}
return null;
}
function getRandomEmailTemplate(templates) { function getRandomEmailTemplate(templates) {
if (templates.length < 0) return ''; if (templates.length < 0) return null;
return templates[Math.floor(Math.random() * templates.length)]; return templates[Math.floor(Math.random() * templates.length)];
} }
@ -59,12 +79,12 @@ function isSameEmailTemplateType(template1, template2) {
return false; return false;
} }
function isDemoRequestTemplateAuto1(template) { function isTemplateAuto1(template) {
return demoRequestEmailTemplatesAuto1.indexOf(template) >= 0; if (createTeacherEmailTemplatesAuto1.indexOf(template) >= 0) return true;
} if (demoRequestEmailTemplatesAuto1.indexOf(template) >= 0) return true;
if (createTeacherInternationalEmailTemplateAuto1 == template) return true;
function isCreateTeacherTemplateAuto1(template) { if (demoRequestInternationalEmailTemplateAuto1 === template) return true;
return createTeacherEmailTemplatesAuto1.indexOf(template) >= 0; return false;
} }
function isDemoRequestTemplateAuto2(template) { function isDemoRequestTemplateAuto2(template) {
@ -137,7 +157,7 @@ function sendMail(toEmail, leadId, contactId, template, emailApiKey, delayMinute
} }
function updateLeadStatus(lead, status, done) { function updateLeadStatus(lead, status, done) {
// console.log("DEBUG: updateLeadStatus", lead.id, status); // console.log(`DEBUG: updateLeadStatus ${lead.id} ${status}`);
const putData = {status: status}; const putData = {status: status};
const options = { const options = {
uri: `https://${closeIoApiKey}:X@app.close.io/api/v1/lead/${lead.id}/`, uri: `https://${closeIoApiKey}:X@app.close.io/api/v1/lead/${lead.id}/`,
@ -202,25 +222,14 @@ function createSendFollowupMailFn(userApiKeyMap, latestDate, lead, email) {
} }
// Find first auto mail // Find first auto mail
let sentFirstCreateTeacherEmail = false;
let sentFirstDemoRequestEmail = false;
let firstMailActivity; let firstMailActivity;
for (const activity of results.data) { for (const activity of results.data) {
if (activity._type === 'Email' && activity.to[0] === email) { if (activity._type === 'Email' && activity.to[0] === email) {
if (isCreateTeacherTemplateAuto1(activity.template_id)) { if (isTemplateAuto1(activity.template_id)) {
if (sentFirstCreateTeacherEmail || sentFirstDemoRequestEmail) { if (firstMailActivity) {
console.log(`ERROR: ${lead.id} sent multiple auto1 emails!? ${sentFirstCreateTeacherEmail} ${sentFirstDemoRequestEmail}`); console.log(`ERROR: ${lead.id} sent multiple auto1 emails!?`);
return done(); return done();
} }
sentFirstCreateTeacherEmail = true;
firstMailActivity = activity;
}
else if (isDemoRequestTemplateAuto1(activity.template_id)) {
if (sentFirstCreateTeacherEmail || sentFirstDemoRequestEmail) {
console.log(`ERROR: ${lead.id} sent multiple auto1 emails!? ${sentFirstCreateTeacherEmail} ${sentFirstDemoRequestEmail}`);
return done();
}
sentFirstDemoRequestEmail = true;
firstMailActivity = activity; firstMailActivity = activity;
} }
} }
@ -235,11 +244,6 @@ function createSendFollowupMailFn(userApiKeyMap, latestDate, lead, email) {
return done(); return done();
} }
if (sentFirstCreateTeacherEmail && sentFirstDemoRequestEmail) {
console.log(`ERROR: ${lead.id} sent multiple auto1 emails!? ${sentFirstCreateTeacherEmail} ${sentFirstDemoRequestEmail}`);
return done();
}
// Find activity since first auto mail, that's not email to a different contact's email // Find activity since first auto mail, that's not email to a different contact's email
let recentActivity; let recentActivity;
for (const activity of results.data) { for (const activity of results.data) {
@ -251,17 +255,9 @@ function createSendFollowupMailFn(userApiKeyMap, latestDate, lead, email) {
} }
if (!recentActivity) { if (!recentActivity) {
let template; let template = getRandomEmailTemplateAuto2(firstMailActivity.template_id);
if (sentFirstCreateTeacherEmail) {
// console.log(`Create teacher auto 1 sent: ${lead.id} ${firstMailUserId} ${userApiKeyMap[firstMailUserId]}`);
template = getRandomEmailTemplate(createTeacherEmailTemplatesAuto2);
}
else if (sentFirstDemoRequestEmail) {
// console.log(`Demo request auto 1 sent: ${lead.id} ${firstMailUserId} ${userApiKeyMap[firstMailUserId]}`);
template = getRandomEmailTemplate(demoRequestEmailTemplatesAuto2);
}
if (!template) { if (!template) {
console.log(`ERROR: no template selected ${lead.id}`); console.log(`ERROR: no auto2 template selected for ${lead.id} ${firstMailActivity.template_id}`);
return done(); return done();
} }
// console.log(`TODO: ${firstMailActivity.to[0]} ${lead.id} ${firstMailActivity.contact_id} ${template} ${userApiKeyMap[firstMailActivity.user_id]}`); // console.log(`TODO: ${firstMailActivity.to[0]} ${lead.id} ${firstMailActivity.contact_id} ${template} ${userApiKeyMap[firstMailActivity.user_id]}`);
@ -271,12 +267,25 @@ function createSendFollowupMailFn(userApiKeyMap, latestDate, lead, email) {
// TODO: some sort of callback problem that stops the series here // TODO: some sort of callback problem that stops the series here
// TODO: manage this status mapping better
if (lead.status_label === "Auto Attempt 1") { if (lead.status_label === "Auto Attempt 1") {
return updateLeadStatus(lead, "Auto Attempt 2", done); return updateLeadStatus(lead, "Auto Attempt 2", done);
} }
else if (lead.status_label === "New US Schools Auto Attempt 1") { else if (lead.status_label === "New US Schools Auto Attempt 1") {
return updateLeadStatus(lead, "New US Schools Auto Attempt 2", done); return updateLeadStatus(lead, "New US Schools Auto Attempt 2", done);
} }
else if (lead.status_label === "Inbound AU Auto Attempt 1") {
return updateLeadStatus(lead, "Inbound AU Auto Attempt 2", done);
}
else if (lead.status_label === "Inbound Canada Auto Attempt 1") {
return updateLeadStatus(lead, "Inbound Canada Auto Attempt 2", done);
}
else if (lead.status_label === "Inbound NZ Auto Attempt 1") {
return updateLeadStatus(lead, "Inbound NZ Auto Attempt 2", done);
}
else if (lead.status_label === "Inbound UK Auto Attempt 1") {
return updateLeadStatus(lead, "Inbound UK Auto Attempt 2", done);
}
else { else {
console.log(`ERROR: unknown lead status ${lead.id} ${lead.status_label}`) console.log(`ERROR: unknown lead status ${lead.id} ${lead.status_label}`)
return done(); return done();
@ -322,7 +331,8 @@ function sendSecondFollowupMails(done) {
if (err) console.log(err); if (err) console.log(err);
const latestDate = new Date(); const latestDate = new Date();
latestDate.setUTCDate(latestDate.getUTCDate() - 3); latestDate.setUTCDate(latestDate.getUTCDate() - 3);
const query = `date_created > ${earliestDate.toISOString().substring(0, 19)} (lead_status:"Auto Attempt 1" or lead_status:"New US Schools Auto Attempt 1")"`; // TODO: manage this status list better
const query = `date_created > ${earliestDate.toISOString().substring(0, 19)} (lead_status:"Auto Attempt 1" or lead_status:"New US Schools Auto Attempt 1" or lead_status:"Inbound Canada Auto Attempt 1" or lead_status:"Inbound AU Auto Attempt 1" or lead_status:"Inbound NZ Auto Attempt 1" or lead_status:"Inbound UK Auto Attempt 1")`;
const limit = 100; const limit = 100;
const nextPage = (skip) => { const nextPage = (skip) => {
let has_more = false; let has_more = false;
@ -337,15 +347,15 @@ function sendSecondFollowupMails(done) {
has_more = results.has_more; has_more = results.has_more;
const tasks = []; const tasks = [];
for (const lead of results.data) { for (const lead of results.data) {
// console.log(`${lead.id}\t${lead.status_label}\t${lead.name}`); // console.log(`DEBUG: ${lead.id}\t${lead.status_label}\t${lead.name}`);
// if (lead.id !== 'lead_KYuI2HVOiUdJANvkOe1uLJBuuQVaaGSRveklhTWbHv2') continue; // if (lead.id !== 'lead_8YZlEVQ4w3lETSlF43RQHK7cJQaBQ4tpbbxUUNA2uGC') continue;
const existingContacts = lead.contacts || []; const existingContacts = lead.contacts || [];
for (const contact of existingContacts) { for (const contact of existingContacts) {
if (contact.emails && contact.emails.length > 0) { if (contact.emails && contact.emails.length > 0) {
tasks.push(createSendFollowupMailFn(userApiKeyMap, latestDate, lead, contact.emails[0].email.toLowerCase())); tasks.push(createSendFollowupMailFn(userApiKeyMap, latestDate, lead, contact.emails[0].email.toLowerCase()));
} }
else { else {
console.log(`ERROR: lead ${lead.id} contact has non-1 emails`); console.log(`ERROR: lead ${lead.id} contact ${contact.id} has no email`);
} }
} }
} }