UNPKG

@speakr/speakr-module-services

Version:
380 lines (369 loc) • 12.9 kB
"use strict"; const db = require("../../db/"); const _ = require("lodash"); const mailer = require("../../utils/mailer"); module.exports = { byIdHash, byAccountId, createInvites, email, reminderEmail }; //todo move to read, there is no create here function byIdHash(options) { let where = { id_hash: options.req.params.id_hash }; return db.initiatives .findOne({ where: where, include: [db.campaigns, db.networks] }) .then(initiatives => { options.initiatives = initiatives; return Promise.resolve(options); }) .catch(error => { return Promise.reject({ message: "Could not find an initiative with the id: " + options.req.params.id_hash, code: 401, purpose: "Create Invite Failed to find the initiative specified. " + error }); }); } //todo move to read, there is no create here function byAccountId(options) { let accountsObj = options.req.body.accounts; //Find accounts that do not have invites for the given initiative return db.accounts .findAll({ where: { id: { $in: accountsObj.map(account => account.account_id) } } }) .then(accounts => { options.accounts = accounts; return Promise.resolve(options); }) .catch(error => { return Promise.reject({ message: "Could not find accounts with the ids: " + accountsObj.map(account => account.account_id).toString(), code: 401, purpose: "Create Invite Failed to find the accounts specified. " + error }); }); } function createInvites(options) { let accountsObj = options.req.body.accounts; let price = 0.0; let invitePromises = options.accounts.map(account => { return db.invites .findOne({ where: { account_id: account.id, initiative_id: options.initiatives.id } }) .then(invite => { let update = { account_id: account.id, initiative_id: options.initiatives.id, promo_link: accountsObj.find( accountObj => parseInt(accountObj.account_id) === account.id ).promo_link, offer_price: accountsObj.find( accountObj => parseInt(accountObj.account_id) === account.id ).offer_price || price }; if (!!invite) { return invite.updateAttributes(update); } else { return db.invites.create(update); } }) .then(invite => { return db.notifications.create({ type: "Notifications::Campaign", invite_id: invite.id, influencer_id: account.influencer_id }).then(notification => { return Promise.resolve(invite); }); }) .catch(error => { console.log(error); return Promise.reject({ message: "Could not create Invite for account_id: " + account.id, code: 401, purpose: "Create Invite Failed to find the invite specified. " + error }); }); }); return Promise.all(invitePromises) .then((invites)=> { options.invites = invites; return Promise.resolve(options) }); } function reminderEmail(options) { let ids = options.invites.map(i => i.id); return db.invites.findAll({ where: {id : {$in: ids}}, include: [db.accounts]}) .then(invites => { options.invites = invites; if(options.req.body.accounts){ return db.point_of_contacts.findOne({ where: { campaign_id: options.req.body.accounts[0].campaign_id } }) .then((point_of_contact) => { options.point_of_contact = point_of_contact; return Promise.resolve(options); }) } else { return Promise.resolve(options); } }) .then((options) => { options.invites.map((invite) => { let send_at = new Date(); send_at.setDate(send_at.getDate() + 2); console.log(send_at) console.log(options.initiatives.end_date) console.log(send_at <= options.initiatives.end_date) if (send_at <= options.initiatives.end_date) { let email = { 'template_name':'offer-reminder-letter', 'template_content': [], 'html': '<h1>Don\'t forget to submit for your offer!</h1>', 'text': options.initiatives.name, 'subject': 'DON\'T FORGET TO SUBMIT FOR YOUR OFFER', 'from_email': 'influencers@speakr.com', 'from_name': 'Speakr Admin', 'to': { 'email': invite.account.account_email, 'name': invite.account.current_username, 'type': 'to' }, 'headers': { 'replyTo': options.point_of_contact || 'idteam@speakr.com' }, 'send_at': send_at, 'merge': true, 'merge_language': 'handlebars', 'global_merge_vars': [ {'name': 'username', 'content': invite.account.current_username }, {'name': 'campaignName', 'content': options.initiatives.campaign.title }, {'name': 'campaignUrl', 'content': `https://influencers.speakr.com/campaigns/${invite.id_hash}`}, {'name': 'payment', 'content': (options.initiatives.payment_type=='Special' ? 'Special' : `$${parseFloat(invite.offer_price).toFixed(2)}`) }, {'name': 'network_name', 'content': options.initiatives.network ? options.initiatives.network.name : null }, {'name': 'network_id', 'content': options.initiatives.network ? options.initiatives.network.id : null }, {'name': 'platform_name', 'content': options.req.body.accounts ? options.req.body.accounts[0].platform_name : null} ] }; mailer.send(email, invite); } }); return Promise.resolve(options); }) .catch((error) => { return Promise.reject({ message: 'Could not email Invite Reminder for account_email.', code: 401, purpose: 'Invite Reminder Failed to find the invite specified. ' + error }); }) } function email(options) { return db.invites .findAll({ where: { account_id: { $in: options.accounts.map(account => account.id) }, initiative_id: options.initiatives.id }, include: [{ model: db.accounts, required: true }] }) .then(invites => { options.invites = invites; if (options.req.body.accounts) { return db.point_of_contacts .findOne({ where: { campaign_id: options.req.body.accounts[0].campaign_id } }) .then(point_of_contact => { options.point_of_contact = point_of_contact; return Promise.resolve(options); }); } else { return Promise.resolve(options); } }) .then(options => { if (options.req.body.accounts) { return db.networks .findOne({ where: { id: options.req.body.accounts[0].initiative_network_id } }) .then(network => { options.current_network_name = network.name; return Promise.resolve(options); }); } else { return Promise.resolve(options); } }) .then(options => { // Shoot out the emails options.invites.map(invite => { let email = { template_name: "Template_GeneralOffer_for_Influencers", template_content: [], html: "<h1>You have been invited to a Speakr Initiative for like a bagillion dollars</h1>", text: options.initiatives.name, subject: options.current_network_name ? `New campaign offer from ${options.current_network_name}!` : `You have a new campaign offer!`, from_email: "influencers@speakr.com", from_name: "Speakr Admin", to: { email: invite.account.account_email, name: invite.account.current_username, type: "to" }, headers: { replyTo: options.point_of_contact || "idteam@speakr.com" }, merge: true, merge_language: "handlebars", global_merge_vars: [ { name: "username", content: invite.account.current_username }, { name: "campaignName", content: options.initiatives.campaign.title }, { name: "campaignUrl", content: `https://influencers.speakr.com/campaigns/${invite.id_hash}` }, { name: "payment", content: (options.initiatives.payment_type=='Special' ? 'Special' : `$${parseFloat(invite.offer_price).toFixed(2)}`) }, { name: "network_id", content: options.req.body.accounts ? options.req.body.accounts[0].initiative_network_id : null }, { name: "platform_name", content: options.req.body.accounts ? options.req.body.accounts[0].platform_name : null }, { name: "network_name", content: options.current_network_name ? options.current_network_name : null } ] }; //sends out first email mailer.send(email, invite); //This is really fuckin ugly, and im sorry(not sorry), but product kept changing how they wanted to send emails, talk to Khoa //SENDING OUT SECONDARY EMAIL db.influencer_profiles .findOne({ where: { influencer_id: invite.account.influencer_id } }) .then(profile => { if ( profile.second_email && profile.second_email !== profile.email ) { let email = { template_name: "Template_GeneralOffer_for_Influencers", template_content: [], html: "<h1>You have been invited to a Speakr Initiative for like a bagillion dollars</h1>", text: options.initiatives.name, subject: options.current_network_name ? `New campaign offer from ${options.current_network_name}!` : `You have a new campaign offer!`, from_email: "influencers@speakr.com", from_name: "Speakr Admin", to: { email: profile.second_email, name: invite.account.current_username, type: "to" }, headers: { replyTo: options.point_of_contact || "idteam@speakr.com" }, merge: true, merge_language: "handlebars", global_merge_vars: [ { name: "username", content: invite.account.current_username }, { name: "campaignName", content: options.initiatives.campaign.title }, { name: "campaignUrl", content: `https://influencers.speakr.com/campaigns/${invite.id_hash}` }, { name: "payment", content: (options.initiatives.payment_type=='Special' ? 'Special' : `$${parseFloat(invite.offer_price).toFixed(2)}`) }, { name: "network_id", content: options.req.body.accounts ? options.req.body.accounts[0].initiative_network_id : null }, { name: "platform_name", content: options.req.body.accounts ? options.req.body.accounts[0].platform_name : null }, { name: "network_name", content: options.current_network_name ? options.current_network_name : null } ] }; mailer.send(email, invite); } }); }); return Promise.resolve(options); }) .catch(error => { return Promise.reject({ message: "Could not email Invite for account_email.", code: 401, purpose: "Create Invite Failed to find the invite specified. " + error }); }); }