Skip to content

Working branch #185

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

Open
wants to merge 2 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-eu1.hubspot.com/contacts/145855241/objects/2-141566906/views/all/list

___
## Tips:
Expand Down
82 changes: 62 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,71 @@
const express = require('express');
const axios = require('axios');
const app = express();

app.set('view engine', 'pug');
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 = '';

const CUSTOM_OBJECT_ID = '2-141566906'; // Custom object ID for the Practicum custom object

// 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

app.get('/', async (req, res) => {
const url = `https://api.hubspot.com/crm/v3/objects/${CUSTOM_OBJECT_ID}?properties=name,team_location,conference`;
const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
'Content-Type': 'application/json'
}
try {
const resp = await axios.get(url, { headers });
const data = resp.data.results;
res.render('homepage', { title: 'Custom Object Table', data: data });
} catch (error) {
console.error(error);
}
});

// 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

app.get('/update-cobj', async (req, res) => {
res.render('updates', { title: 'Update Custom Object | HubSpot APIs' });
})

// 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.

// * Code for Route 3 goes here

/**
* * This is sample code to give you a reference for how you should structure your calls.

app.post('/update-cobj', async (req, res) => {
const update = {
properties: {
"name": req.body.name,
"team_location": req.body.team_location,
"team_name": req.body.team_name
}
}

const url = `https://api.hubapi.com/crm/v3/objects/${CUSTOM_OBJECT_ID}`;
const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
'Content-Type': 'application/json'
};

try {
await axios.post(url, update, { headers } );
res.redirect('/');
} catch(err) {
console.error(err);
}
});


/**
* * This is sample code to give you a reference for how you should structure your calls.

* * App.get sample
app.get('/contacts', async (req, res) => {
const contacts = 'https://api.hubspot.com/crm/v3/objects/contacts';
Expand All @@ -40,32 +81,33 @@ app.get('/contacts', async (req, res) => {
console.error(error);
}
});

* * App.post sample
app.post('/update', async (req, res) => {
const update = {
properties: {
"favorite_book": req.body.newVal
}
}

const email = req.query.email;
const updateContact = `https://api.hubapi.com/crm/v3/objects/contacts/${email}?idProperty=email`;
const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
'Content-Type': 'application/json'
};

try {
try {
await axios.patch(updateContact, update, { headers } );
res.redirect('back');
} catch(err) {
console.error(err);
}

});
*/


// * Localhost
app.listen(3000, () => console.log('Listening on http://localhost:3000'));
app.listen(3000, () => console.log('Listening on http://localhost:3000'));

27 changes: 27 additions & 0 deletions views/homepage.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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= title

if data && data.length
table
thead
tr
th Name
th Team Location
th Conference
tbody
each object in data
tr
td= object.properties.name
td= object.properties.team_location
td= object.properties.team_name
else
p No custom object records found.

br
a(href='/update-cobj') Add to this Table
27 changes: 27 additions & 0 deletions views/updates.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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= title

// Form to update the custom object data
form(action="/update-cobj" method="POST")
.form-group
label(for="name") Name
input(type="text" id="name" name="name" required)

.form-group
label(for="team_location") Team Location
input(type="text" id="team_location" name="team_location" required)

.form-group
label(for="team_name") Team Name
textarea(id="team_name" name="team_name" required)

button(type="submit") Submit

br
a(href='/') Return to the homepage