diff --git a/README.md b/README.md index 76d6d581..dc91d2c3 100644 --- a/README.md +++ b/README.md @@ -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/50132132/objects/2-45665973/views/all/list ___ ## Tips: diff --git a/index.js b/index.js index f337a32d..884ccc03 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +require('dotenv').config(); const express = require('express'); const axios = require('axios'); const app = express(); @@ -7,65 +8,55 @@ 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 = ''; +// * Reads the token from .env file +const PRIVATE_APP_ACCESS = process.env.PRIVATE_APP_TOKEN; // 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'; +app.get('/', async (req, res) => { + const endpoint = 'https://api.hubapi.com/crm/v3/objects/pets'; const headers = { Authorization: `Bearer ${PRIVATE_APP_ACCESS}`, 'Content-Type': 'application/json' - } + }; try { - const resp = await axios.get(contacts, { headers }); - const data = resp.data.results; - res.render('contacts', { title: 'Contacts | HubSpot APIs', data }); + const resp = await axios.get(endpoint, { + headers, + params: { properties: 'pet_name,pet_type,pet_bio' } + }); + const records = resp.data.results; + res.render('homepage', { title: 'Pet List', records }); } catch (error) { console.error(error); + res.send('Error loading records'); } }); -* * App.post sample -app.post('/update', async (req, res) => { - const update = { - properties: { - "favorite_book": req.body.newVal - } - } +// 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', (req, res) => { + res.render('updates', { title: 'Add a Pet | 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. - const email = req.query.email; - const updateContact = `https://api.hubapi.com/crm/v3/objects/contacts/${email}?idProperty=email`; +app.post('/update-cobj', async (req, res) => { + const { pet_name, pet_type, pet_bio } = req.body; + const endpoint = 'https://api.hubapi.com/crm/v3/objects/pets'; const headers = { Authorization: `Bearer ${PRIVATE_APP_ACCESS}`, 'Content-Type': 'application/json' }; - - try { - await axios.patch(updateContact, update, { headers } ); - res.redirect('back'); - } catch(err) { + const data = { + properties: { pet_name, pet_type, pet_bio } + }; + try { + await axios.post(endpoint, data, { headers }); + res.redirect('/'); + } catch (err) { console.error(err); + res.send('Error creating record'); } - }); -*/ - -// * Localhost app.listen(3000, () => console.log('Listening on http://localhost:3000')); \ No newline at end of file diff --git a/package.json b/package.json index 62db37aa..4eda7957 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "axios": "^1.3.5", + "dotenv": "^16.5.0", "express": "^4.18.2", "pug": "^3.0.2" } diff --git a/views/homepage.pug b/views/homepage.pug new file mode 100644 index 00000000..e69de29b diff --git a/views/updates.pug b/views/updates.pug new file mode 100644 index 00000000..a828b1c0 --- /dev/null +++ b/views/updates.pug @@ -0,0 +1,20 @@ +doctype html +html + head + title= title + body + h1= title + form(action="/update-cobj" method="POST") + label(for="pet_name") Name: + input(type="text" name="pet_name" required) + br + label(for="pet_type") Type: + input(type="text" name="pet_type") + br + label(for="pet_bio") Bio: + input(type="text" name="pet_bio") + br + button(type="submit") Add Pet + br + a(href="/") Return to the homepage +