2018-06-21 08:29:35 +02:00
|
|
|
const express = require('express')
|
|
|
|
const Webtask = require('webtask-tools')
|
|
|
|
const cors = require('cors')
|
2018-06-21 16:52:16 +02:00
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const request = require('request')
|
2018-06-21 08:29:35 +02:00
|
|
|
|
|
|
|
const server = express()
|
|
|
|
|
2018-06-21 16:52:16 +02:00
|
|
|
server.listen(4430)
|
|
|
|
server.use(bodyParser.json())
|
2018-06-21 15:28:57 +02:00
|
|
|
|
2018-06-22 13:05:58 +02:00
|
|
|
//
|
|
|
|
// Allow requests from these domains only
|
|
|
|
//
|
|
|
|
const corsOptions = {
|
|
|
|
origin: ['https://oceanprotocol.com', /\.oceanprotocol\.com$/],
|
|
|
|
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
|
|
|
|
}
|
|
|
|
server.use(cors(corsOptions))
|
|
|
|
|
2018-06-21 17:54:41 +02:00
|
|
|
//
|
2018-06-21 18:09:22 +02:00
|
|
|
// Zoho APIs
|
2018-06-21 17:54:41 +02:00
|
|
|
//
|
2018-06-21 16:52:16 +02:00
|
|
|
const apiUrlZohoCampaigns = 'https://campaigns.zoho.com/api/'
|
|
|
|
const apiUrlZohoCRM = 'https://www.zohoapis.com/crm/v2/'
|
2018-06-21 15:28:57 +02:00
|
|
|
|
2018-06-21 18:09:22 +02:00
|
|
|
const sendRequest = (options, res) => {
|
|
|
|
request(options, (error, response, body) => {
|
|
|
|
if (error) res.send(error)
|
|
|
|
|
2018-06-22 13:05:58 +02:00
|
|
|
// just pass through whatever we get from the APIs
|
|
|
|
// as the response
|
2018-06-21 18:09:22 +02:00
|
|
|
res.send(body)
|
2018-06-21 19:46:27 +02:00
|
|
|
res.sendStatus(response.statusCode)
|
2018-06-21 18:09:22 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-21 16:52:16 +02:00
|
|
|
//
|
|
|
|
// Subscribe to newsletter via Zoho Campaigns API
|
|
|
|
// https://www.zoho.com/campaigns/newhelp/api/contact-subscribe.html
|
|
|
|
//
|
|
|
|
server.get('/newsletter/:data', (req, res) => {
|
2018-06-21 15:28:57 +02:00
|
|
|
const { ZOHO_CAMPAIGNS_TOKEN, ZOHO_CAMPAIGNS_LIST_KEY } = req.webtaskContext.secrets
|
2018-06-21 16:52:16 +02:00
|
|
|
const { data } = req.params
|
|
|
|
|
|
|
|
const options = {
|
2018-06-21 18:42:52 +02:00
|
|
|
url: `${apiUrlZohoCampaigns}json/listsubscribe?authtoken=${ZOHO_CAMPAIGNS_TOKEN}&scope=CampaignsAPI&resfmt=JSON&listkey=${ZOHO_CAMPAIGNS_LIST_KEY}&contactinfo=${decodeURIComponent(data)}` // eslint-disable-line max-len
|
2018-06-21 16:52:16 +02:00
|
|
|
}
|
2018-06-21 18:09:22 +02:00
|
|
|
sendRequest(options, res)
|
2018-06-21 16:52:16 +02:00
|
|
|
})
|
|
|
|
|
2018-06-21 17:54:41 +02:00
|
|
|
//
|
|
|
|
// Create a new lead via Zoho CRM API
|
|
|
|
// https://www.zoho.com/crm/help/api/v2/#create-specify-records
|
|
|
|
//
|
|
|
|
server.get('/crm/:data', (req, res) => {
|
2018-06-21 16:52:16 +02:00
|
|
|
const { ZOHO_CRM_TOKEN } = req.webtaskContext.secrets
|
2018-06-21 17:54:41 +02:00
|
|
|
const { data } = req.params
|
2018-06-21 16:52:16 +02:00
|
|
|
|
2018-06-21 17:54:41 +02:00
|
|
|
const options = {
|
|
|
|
url: `${apiUrlZohoCRM}Leads`, // eslint-disable-line max-len
|
|
|
|
headers: { 'Authorization': `Zoho-oauthtoken ${ZOHO_CRM_TOKEN}` },
|
|
|
|
method: 'POST',
|
|
|
|
formData: data
|
|
|
|
}
|
2018-06-21 18:09:22 +02:00
|
|
|
sendRequest(options, res)
|
2018-06-21 17:54:41 +02:00
|
|
|
})
|
2018-06-21 08:29:35 +02:00
|
|
|
|
|
|
|
module.exports = Webtask.fromExpress(server)
|