Created
March 16, 2021 00:39
-
-
Save supertopoz/3550bab9c98fc49cd2ec4c2e3065eb71 to your computer and use it in GitHub Desktop.
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
const express = require('express'); | |
const axios = require("axios"); | |
const sf = require('node-salesforce'); | |
const app = express(); | |
app.use(express.json()) | |
const port = process.env.PORT || 3000; | |
app.use(express.json()) | |
app.use(function(req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
next(); | |
}); | |
app.get('/', (req, res) => res.send('Server up and running!')); | |
app.post("/createticket", (async (req,res) => { | |
const userId = req.body.user_id; | |
if(!userId) return res.status(400).send("No user_id"); | |
const auth = await getSalesforceAccessToken(); | |
if(auth.error) return res.status(400).send("Salesforce no auth!"); | |
const salesforceCase = await createCase(auth.token); | |
if (!salesforceCase.success) return res.status(400).send("Case not created!"); | |
const channelUrl = salesforceCase.id; | |
const channel = await createChannel(channelUrl, userId); | |
if(channel.error) return res.status(400).send("Channel not created!"); | |
res.status(200).send(channel.channel); | |
})); | |
const getSalesforceAccessToken = async function() { | |
const url = ""https://YOUR_DOMAIN-dev-ed.my.salesforce.com"" | |
const conn = new sf.Connection({ loginUrl: url}); | |
try { | |
await conn.login("SF_EMAIL","SF_PASSWORDSF_TOKEN"); | |
return { error: undefined, token: conn.accessToken }; | |
} catch (e){ | |
return { error: e, token: undefined } | |
} | |
}; | |
const createCase = async function (token) { | |
const body = { | |
"Subject": "Problem with dog", | |
"Description": "I'm writing to complain about a problem I'm having with my dog. ", | |
"SuppliedName": "Bob Smith", | |
"SuppliedEmail": "bob@smith.com", | |
}; | |
const headers = { | |
"headers": { | |
'Content-Type': 'application/json', | |
'Authorization': 'Bearer ' + token | |
} | |
}; | |
const url = "https://YOUR_DOMAIN-dev-ed.my.salesforce.com/services/data/v39.0/sobjects/Case"; | |
try { | |
const result = await axios.post(url, body, headers); | |
return result.data; | |
} catch (e) { | |
return { id: undefined, success: false, errors: e }; | |
} | |
}; | |
const createChannel = async function(channel_url, userId) { | |
const data = { channel_url, user_ids: [userId]}; | |
const headers = {"headers": { | |
"Api-Token": 'YOUR_SENDBIRD_API_TOKEN', | |
"Content-Type": "text/plain" | |
} | |
} | |
const url = `https://api-YOUR_SENDBIRD_APP_ID.sendbird.com/v3/group_channels`; | |
try { | |
const channel = await axios.post(url, data, headers); | |
return { success: true, channel: channel.data, error: undefined } | |
} catch (e){ | |
return { success: false, channel: undefined, error: e } | |
} | |
}; | |
app.get("*", (req, res) =>{ | |
res.sendStatus(200); | |
}); | |
app.listen(process.env.PORT || port, () => console.log(`Example app listening on port ${port}!`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment