Skip to content

Instantly share code, notes, and snippets.

@tonyspiro
Last active June 11, 2018 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyspiro/a11e50cd33a6606b422e3960f75281b2 to your computer and use it in GitHub Desktop.
Save tonyspiro/a11e50cd33a6606b422e3960f75281b2 to your computer and use it in GitHub Desktop.
module.exports = async function(req, res) {
const Cosmic = require('cosmicjs')
const sgMail = require('@sendgrid/mail')
const async = require('async')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
const api = Cosmic()
const bucket = api.bucket({
slug: 'story-licensing',
write_key: process.env.COSMIC_WRITE_KEY
})
const bid = await bucket.addObject({
title: 'Bid - ' + (new Date()),
type_slug: 'bids',
metafields: [
{
title: 'Email',
key: 'email',
type: 'text',
value: req.body.email
},
{
title: 'Bid',
key: 'bid',
type: 'text',
value: req.body.bid
},
{
title: 'Post Title',
key: 'post_title',
type: 'text',
value: req.body.post_title
},
{
title: 'Post Link',
key: 'post_link',
type: 'text',
value: req.body.post_link
}
],
options: {
content_editor: false,
slug_input: false
}
})
async.series([
callback => {
// Send to Admin
const subject = 'A Bid has been received'
const html_body = '<div>A bid has been received for <strong>' + req.body.post_title + '</strong> for <strong>$' + req.body.bid + '</strong></div>'
const message = {
// sender info
from: {
email: req.body.email
},
// Comma separated list of recipients
to: process.env.BID_EMAIL,
// Subject of the message
subject: subject, //
// plaintext body
text: subject,
// HTML body
html: html_body,
}
sgMail.send(message)
.then(() => {
//Celebrate
callback()
})
.catch(error => {
// Log friendly error
console.error(error.toString())
res.json({ success: false })
})
},
() => {
// Send to Bidder
const subject = 'Your bid has been received'
const html_body = '<div>A bid has been received for <strong>' + req.body.post_title + '</strong> for <strong>$' + req.body.bid + '</strong>. Thank you.</div>'
const message = {
// sender info
from: {
email: process.env.BID_EMAIL,
name: 'Story Licensing'
},
// Comma separated list of recipients
to: req.body.email,
// Subject of the message
subject: subject, //
// plaintext body
text: subject,
// HTML body
html: html_body,
}
sgMail.send(message)
.then(() => {
// Celebrate
res.json({ success: true })
})
.catch(error => {
// Log friendly error
console.error(error.toString())
res.json({ success: false })
})
}
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment