Skip to content
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/146392072/objects/2-143983893/views/all/list

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


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 = '';

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

// * 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 accessToken = process.env.TOKEN;
//route 1: homepage
app.get('/', async (req, res) => {
const airDefSystems = 'https://api.hubspot.com/crm/v3/objects/2-143983893?limit=10&properties=name,range,description&archived=false';
const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
try {
const resp = await axios.get(contacts, { headers });
const resp = await axios.get(airDefSystems, { headers });
const data = resp.data.results;
res.render('contacts', { title: 'Contacts | HubSpot APIs', data });
res.render('homepage', { title: 'Air Defense Systems | HubSpot APIs', data });
} catch (error) {
console.error(error);
}
});

* * App.post sample
app.post('/update', async (req, res) => {
const update = {
//route 2: update form
app.get('/update-cobj', async (req, res) => {
const headers = {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json'
};
try {
res.render('updates', {title: "Update Custom Object Form | Integrating With HubSpot I Practicum"});
} catch(err) {
console.error(err);
}
});

//route 3: post
app.post('/update-cobj', async (req, res) => {
const { newName, newRange, newDescription } = req.body;

const data = {
properties: {
"favorite_book": req.body.newVal
name: newName,
range: newRange,
description: newDescription
}
}
};

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

try {
await axios.patch(updateContact, update, { headers } );
res.redirect('back');
} catch(err) {
console.error(err);
try {
await axios.post(url, data, { headers });
res.redirect('/');
} catch (error) {
console.error('Error creating object:', error.response?.data || error.message);
res.status(500).send("Failed to create custom object.");
}

});
*/


// * Localhost
app.listen(3000, () => console.log('Listening on http://localhost:3000'));
app.listen(3000, () => console.log('Listening on http://localhost:3000'));
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"license": "ISC",
"dependencies": {
"axios": "^1.3.5",
"dotenv": "^16.5.0",
"express": "^4.18.2",
"pug": "^3.0.2"
"pug": "^3.0.3"
}
}
15 changes: 0 additions & 15 deletions views/contacts.pug

This file was deleted.

22 changes: 22 additions & 0 deletions views/homepage.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//- ** This is a sample of a pug template and how it uses the data passed to it from index.js.

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 Custom Object Table
a(href="/update-cobj") Add to this table
table(style='width:100%', border='1')
tr
th Name
th Range
th Description
each air_defense_systems in data
tr
td #{air_defense_systems.properties.name}
td #{air_defense_systems.properties.range}
td #{air_defense_systems.properties.description}

21 changes: 21 additions & 0 deletions views/updates.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
doctype html
html(lang="en")
head
title= title
link(rel="stylesheet", href="/css/style.css")
body
.form-wrapper
p Air Defense Systems:
form(method="POST")
label(for="newName") New name
input(type="text", name="newName", id="newName", required)

label(for="newRange") New range
input(type="number", name="newRange", id="newRange", required)

label(for="newDescription") New description
input(type="text", name="newDescription", id="newDescription", required)

input(type="submit", value="Submit")

a(href="/") Return to the homepage