Skip to content

Develop #136

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 3 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/48165264/objects/2-40059598/views/all/list

___
## Tips:
Expand Down
81 changes: 40 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config();
const express = require('express');
const axios = require('axios');
const app = express();
Expand All @@ -8,64 +9,62 @@ 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 PRIVATE_APP_ACCESS = process.env.PRIVATE_APP_ACCESS;
console.log('PRIVATE_APP_ACCESS:', PRIVATE_APP_ACCESS ? 'Loaded successfully' : 'Not loaded');

// 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) => {
try {
const response = await axios.get('https://api.hubapi.com/crm/v3/objects/pets', {
headers: { 'Authorization': `Bearer ${PRIVATE_APP_ACCESS}` },
params: {
properties: ['name', 'species', 'legs'].join(',')
}
});

console.log('API Response:', response.data);
const pets = response.data.results || [];
console.log('Pets with Properties:', pets);
res.render('homepage', { title: 'Pet List', pets });
} catch (error) {
console.error('Error fetching pet data:', error);
res.render('homepage', { title: 'Pet List', pets: [] });
}
});


// 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', (req, res) => {
res.render('updates', { title: 'Update Custom Object Form | Integrating With HubSpot I Practicum' });
});

// 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.get sample
app.get('/contacts', async (req, res) => {
const contacts = 'https://api.hubspot.com/crm/v3/objects/contacts';
const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
'Content-Type': 'application/json'
}
app.post('/update-cobj', async (req, res) => {
const { name, species, legs } = req.body;
try {
const resp = await axios.get(contacts, { headers });
const data = resp.data.results;
res.render('contacts', { title: 'Contacts | HubSpot APIs', data });
await axios.post('https://api.hubapi.com/crm/v3/objects/pets', {
properties: {
name,
species,
legs: parseInt(legs, 10)
}
}, {
headers: { 'Authorization': `Bearer ${PRIVATE_APP_ACCESS}`, 'Content-Type': 'application/json' }
});
res.redirect('/');
} catch (error) {
console.error(error);
console.error('Error creating/updating pet:', error);
res.status(500).send('Error submitting data');
}
});

* * 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 {
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'));
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"axios": "^1.3.5",
"dotenv": "^16.4.7",
"express": "^4.18.2",
"pug": "^3.0.2"
}
Expand Down
21 changes: 21 additions & 0 deletions views/homepage.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
doctype html
html
head
title= title
body
h1 Pet List
a(href="/update-cobj") Add to this table
table(border="1")
tr
th Name
th Species
th Legs
if pets && pets.length > 0
each pet in pets
tr
td= pet.properties.name
td= pet.properties.species
td= pet.properties.legs
else
tr
td(colspan="3") No pets found.
18 changes: 18 additions & 0 deletions views/updates.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
doctype html
html
head
title= title
body
h1 Update Custom Object Form
a(href="/") Return to the homepage
form(action="/update-cobj" method="POST")
label(for="name") Name:
input(type="text" id="name" name="name" required)
br
label(for="species") Species:
input(type="text" id="species" name="species" required)
br
label(for="legs") Legs:
input(type="number" id="legs" name="legs" required)
br
button(type="submit") Submit