Last active
October 2, 2018 07:41
-
-
Save TDodgeCo/9f5ece53e3794ffefada72997ecd92e7 to your computer and use it in GitHub Desktop.
Mail Provider Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const chalk = use('chalk') | |
const moment = use('moment') | |
const Env = use('Env') | |
const Mail = use('Mail') | |
const REP = use('App/Models/Representative') | |
const LEAD = use('App/Models/Lead') | |
const CONTACT = use('App/Models/Contact') | |
const DateService = make('App/Services/DateService') | |
class ErrorReportController { | |
async index({ view }) { | |
// const today = await this.today() | |
const thisWeek = await this.thisWeek() | |
// const thisMonth = await this.thisMonth() | |
const data = [] | |
// data.push(today) | |
data.push(thisWeek) | |
// data.push(thisMonth) | |
return this.sendMail({ data, view }) | |
} | |
async thisWeek() { | |
const thisWeek = await DateService.thisWeek() | |
let reps = await REP.all() | |
reps = reps.toJSON() | |
try { | |
let data = [] | |
for (let i = 0; i < reps.length; i++) { | |
let leads = await LEAD | |
.query() | |
.where('RESPONSIBLE_USER_ID', reps[i].USER_ID) | |
.where('created_at', '>=', thisWeek) | |
.fetch() | |
let contacts = await CONTACT | |
.query() | |
.where('OWNER_USER_ID', reps[i].USER_ID) | |
.where('created_at', '>=', thisWeek) | |
.fetch() | |
leads = leads.toJSON() | |
contacts = contacts.toJSON() | |
let userData = {} | |
userData.rep = reps[i].FIRST_NAME // + ' ' + reps[i].LAST_NAME | |
userData.leads = leads.length | |
userData.contacts = contacts.length | |
userData.conversion = Math.round(contacts.length / leads.length * 100) | |
data.push(userData) | |
} | |
return data | |
} catch (err) { | |
console.log(chalk.bgRed('Week Error')) | |
console.log(err.message) | |
} | |
} | |
async sendMail({ data, view }) { | |
try { | |
// tried data.toJSON() and data[0]. Neither worked. | |
await Mail.send('emails.error-report', data, (message) => { | |
message | |
.to('timldodge@gmail.com') | |
.from(Env.get('MAIL_USERNAME')) | |
.subject('Lead/Opportunity Conversion Report') | |
}) | |
return view.render('emails.error-report', { | |
data | |
}) | |
} catch (err) { | |
console.log(chalk.red(err.message)) | |
} | |
} | |
} | |
module.exports = ErrorReportController |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment