Skip to content

Instantly share code, notes, and snippets.

@tonyspiro tonyspiro/bids.js
Last active Jun 11, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.