Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working branch #56

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This repository is for the Integrating With HubSpot I: Foundations course. This

To read the full directions, please go to the [practicum instructions](https://app.hubspot.com/academy/l/tracks/1092124/1093824/5493?language=en).

**Put your HubSpot developer test account custom objects URL link here:** https://app.hubspot.com/contacts/l/objects/${custom-obj-number}/views/all/list
**Put your HubSpot developer test account custom objects URL link here:** https://app.hubspot.com/contacts/44772269/objects/2-22046266/views/all/list

___
## Tips:
Expand Down
99 changes: 70 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,106 @@ app.use(express.static(__dirname + '/public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

// * Please DO NOT INCLUDE the private app access token in your repo. Don't do this practicum in your normal account.
const PRIVATE_APP_ACCESS = '';
if(typeof process.env.PRIVATE_APP_ACCESS == 'undefined') {
console.error('This app requires an .env file containing "PRIVATE_APP_ACCESS=***MY-TOKEN***"' +
"\nRun with the following command\n\nnode --env-file=.env index.js"
);
return;
}

// TODO: ROUTE 1 - Create a new app.get route for the homepage to call your custom object data. Pass this data along to the front-end and create a new pug template in the views folder.

// * Code for Route 1 goes here

// TODO: ROUTE 2 - Create a new app.get route for the form to create or update new custom object data. Send this data along in the next route.

// * Code for Route 2 goes here

// TODO: ROUTE 3 - Create a new app.post route for the custom objects form to create or update your custom object data. Once executed, redirect the user to the homepage.
const PRIVATE_APP_ACCESS = process.env.PRIVATE_APP_ACCESS;
const CUSTOM_OBJ = '2-22046266';

// * Code for Route 3 goes here

/**
* * This is sample code to give you a reference for how you should structure your calls.
// TODO: ROUTE 1 - Create a new app.get route for the homepage to call your custom object data. Pass this data along to the front-end and create a new pug template in the views folder.

* * App.get sample
app.get('/contacts', async (req, res) => {
const contacts = 'https://api.hubspot.com/crm/v3/objects/contacts';
app.get('/', async (req, res) => {
const endpoint = `https://api.hubspot.com/crm/v3/objects/${CUSTOM_OBJ}?properties=character_name,character_age,job`;
const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
'Content-Type': 'application/json'
}
try {
const resp = await axios.get(contacts, { headers });
const resp = await axios.get(endpoint, { headers });
const data = resp.data.results;
res.render('contacts', { title: 'Contacts | HubSpot APIs', data });
res.render('homepage', { title: 'View Characters | Integrating With HubSpot I Practicum', data });
} catch (error) {
console.error(error);
}
});

* * App.post sample
// TODO: ROUTE 2 - Create a new app.get route for the form to create or update new custom object data. Send this data along in the next route.
app.get('/update-cobj', async (req, res) => {
let hs_id = '';
req.query.hs_id;
if(typeof req.query.hs_id != 'undefined') {
hs_id=req.query.hs_id;
}
let data = {
"properties": {
"character_name": "",
"character_age": "",
"job": ""
},
"id": hs_id
};
let title = 'Create New Character';
let submit_action = 'Create';

if(String(hs_id).length > 0) {
submit_action = 'Edit';
const endpoint = `https://api.hubspot.com/crm/v3/objects/${CUSTOM_OBJ}/${hs_id}?properties=character_name,character_age,job`;

const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
'Content-Type': 'application/json'
}
try {
const resp = await axios.get(endpoint, { headers });
data = resp.data;
} catch (error) {
console.error(error);
}
title = 'Update Character';
}

res.render('updates', { title: title + ' | Integrating With HubSpot I Practicum', submit_action: submit_action, data });
});

// TODO: ROUTE 3 - Create a new app.post route for the custom objects form to create or update your custom object data. Once executed, redirect the user to the homepage.

app.post('/update', async (req, res) => {
const update = {
properties: {
"favorite_book": req.body.newVal
"character_name": req.body.character_name,
"character_age": req.body.character_age,
"job": req.body.job
}
}
};
let hs_id = req.body.hs_id;

const email = req.query.email;
const updateContact = `https://api.hubapi.com/crm/v3/objects/contacts/${email}?idProperty=email`;
let updateEndpoint = `https://api.hubspot.com/crm/v3/objects/${CUSTOM_OBJ}`;
if(String(hs_id).length > 0) {
updateEndpoint += `/${hs_id}`;
}
const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
'Content-Type': 'application/json'
};

try {
await axios.patch(updateContact, update, { headers } );
res.redirect('back');
if(String(hs_id).length > 0) {
//update existing object
await axios.patch(updateEndpoint, update, { headers } );
} else {
//save new object
await axios.post(updateEndpoint, update, { headers } );
}
res.redirect('/');
} catch(err) {
console.error(err);
}

});
*/


// * Localhost
app.listen(3000, () => console.log('Listening on http://localhost:3000'));
9 changes: 9 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,13 @@ input[type="submit"] {
border: none;
padding: .375rem 1rem;
margin-top: 10px;
}

table th {
background-color: pink;
padding: 10px;
}

table td {
padding-left: 10px;
}
24 changes: 24 additions & 0 deletions views/homepage.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


doctype html
html
head
title= `${title}`
meta(name="viewport" content="width=device-width, initial-scale=1")
link(rel="stylesheet", href="/css/style.css")
body
h1 Characters
a(href=`/update-cobj`) Create New Character
table(border='1')
tr
th Character Name
th Age
th Job
th Action
each character in data
tr
td #{character.properties.character_name}
td #{character.properties.character_age}
td #{character.properties.job}
td
a(href=`/update-cobj?hs_id=${character.id}`) Edit
20 changes: 20 additions & 0 deletions views/updates.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
doctype html
html
head
title= `${title}`
meta(name="viewport" content="width=device-width, initial-scale=1")
link(rel="stylesheet", href="/css/style.css")
body
.form-wrapper
h1 #{submit_action} Character
form(action="/update" method="POST")
input(type="hidden" name="hs_id" value=`${data.id}`)
p Full Name:
input(type="text" name="character_name" placeholder="First / Last Name" value=`${data.properties.character_name}`)
p Age:
input(type="text" name="character_age" placeholder="Age" value=`${data.properties.character_age}`)
p Job:
input(type="text" name="job" placeholder="Job Title" value=`${data.properties.job}`)
input(type="submit" value=`${submit_action} Character`)
br
a(href='/') Return to the homepage