Skip to content

Instantly share code, notes, and snippets.

@anmolgkv
Created August 5, 2020 08:21
Show Gist options
  • Save anmolgkv/db0d5619ed6b0bfb70c0cbe8f51a2828 to your computer and use it in GitHub Desktop.
Save anmolgkv/db0d5619ed6b0bfb70c0cbe8f51a2828 to your computer and use it in GitHub Desktop.
const express = require("express"),
dotenv = require("dotenv"),
jsforce = require("jsforce"),
bodyParser = require("body-parser"),
session = require("express-session"),
decode = require("salesforce-signed-request");
dotenv.config();
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static("public"));
app.use(session({ secret: "S3CRE7", resave: true, saveUninitialized: true }));
const oauth2 = new jsforce.OAuth2({
clientId: process.env.CLIENT_ID,
redirectUri: process.env.REDIRECT_URI,
clientSecret: process.env.CONSUMER_SECRET,
loginUrl: process.env.SALESFORCE_LOGIN_URL,
version: process.env.SALESFORCE_API_VERSION
});
const sfClient = new jsforce.Connection({ oauth2: oauth2 });
app.get("/", (req, res) => {
res.redirect("/oauth2/login");
});
app.get("/oauth2/login", (req, res) => {
const authUrl = oauth2.getAuthorizationUrl({ scope: "api id web refresh_token" });
res.redirect(authUrl);
});
app.get("/oauth2/callback", (req, res) => {
const tenMinutes = 1000 * 60 * 10;
req.connection.setTimeout(tenMinutes);
sfClient.authorize(req.query.code, (err, userInfo) => {
if (err) {
handleError(err, res);
} else {
req.session.accessToken = sfClient.accessToken;
req.session.instanceUrl = sfClient.instanceUrl;
req.session.refreshToken = sfClient.refreshToken;
sfClient.streaming.topic("/event/EncowayCPQ__e")
.subscribe((message) => {
console.log({ "RECEIVED EVENT": message });
});
res.redirect("/param1/param2/param3");
}
});
});
app.get("/:p1/:p2/:p3", (req, res) => {
if (!req.session.accessToken || !req.session.instanceUrl) {
return res.redirect("/");
}
let signedRequest = {
user: {
firstName: "Anmol",
fullName: "Anmol Kumar"
}
}
const query = "SELECT Id, Name, Account.Name, Opportunity.Name, BillingStreet, BillingCity FROM Quote";
sfClient.query(query, (err, result) => {
if (err) {
return handleError(err, res);
}
res.render("index", {
quoteId: result.records[0].Id,
context: JSON.stringify(signedRequest, null, 2),
quote: JSON.stringify(result, null, 2),
params: JSON.stringify(req.params, null, 2),
query: JSON.stringify(req.query, null, 2)
});
});
});
app.post("/:p1/:p2/:p3", (req, res) => {
if (!req.session.accessToken || !req.session.instanceUrl) {
return res.redirect("/oauth2/login");
}
const signedRequest = decode(req.body.signed_request, process.env.CONSUMER_SECRET);
const quoteId = signedRequest.context.environment.parameters.recordId;
const query = `SELECT Id, Name, Account.Name, Opportunity.Name, BillingStreet, BillingCity FROM Quote WHERE Id = '${quoteId}'`;
sfClient.query(query, (err, result) => {
if (err) {
return handleError(err, res);
}
res.render("index", {
quoteId: quoteId,
context: JSON.stringify(signedRequest, null, 2),
quote: JSON.stringify(result, null, 2),
params: JSON.stringify(req.params, null, 2),
query: JSON.stringify(req.query, null, 2)
});
});
});
app.get("/createQuote/:recordId", (req, response) => {
const recordId = req.params.recordId;
const payload = { Name: "Anmol", Price: 10000 };
setTimeout(() => {
publish(recordId, 'Quote created', payload);
}, 50);
response.send("We are working on it, will notify you through platform event in a while");
});
app.set("port", process.env.PORT || 5000);
app.listen(app.get("port"), function () {
console.log("Express server listening on port " + app.get("port"));
});
function publish(recordId, type, payload) {
sfClient.sobject("EncowayCPQ__e").create({
RecordId__c: recordId,
Type__c: type,
txt_PayLoad__c: JSON.stringify(payload)
}).then((result) => {
console.dir(result);
}).catch(function (err) {
console.log("Error: " + err);
});
}
function handleError(err, res) {
console.error(err);
res.status(403).send(err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment