Skip to content

Commit

Permalink
Merge pull request #7 from podium/ra/cortex-checks-quality
Browse files Browse the repository at this point in the history
Ra/cortex checks quality
  • Loading branch information
ruby-abutaleb authored Aug 29, 2024
2 parents 95ff291 + 8d9ddd5 commit c0d2cce
Show file tree
Hide file tree
Showing 9 changed files with 3,188 additions and 49 deletions.
6 changes: 3 additions & 3 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module.exports = {
node: true,
jest: true,
},
extends: "eslint:recommended",
extends: 'eslint:recommended',
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
ecmaVersion: 'latest',
sourceType: 'module',
},
};
35 changes: 25 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,43 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: "Checkout repository"
- name: 'Checkout repository'
uses: actions/checkout@v3
with:
persist-credentials: false
- name: "Use Node.js"
- name: 'Use Node.js'
uses: actions/setup-node@v3
with:
node-version: "16.x"
- run: npm install
- run: npm run lint
node-version: '21.x'
- run: yarn install
- run: yarn lint

format:
runs-on: ubuntu-latest

steps:
- name: 'Checkout repository'
uses: actions/checkout@v3
with:
persist-credentials: false
- name: 'Use Node.js'
uses: actions/setup-node@v3
with:
node-version: '21.x'
- run: yarn install
- run: yarn format
- run: yarn format:check
test:
runs-on: ubuntu-latest

steps:
- name: "Checkout repository"
- name: 'Checkout repository'
uses: actions/checkout@v3
with:
persist-credentials: false
- name: "Use Node.js"
- name: 'Use Node.js'
uses: actions/setup-node@v3
with:
node-version: "16.x"
- run: npm install
- run: npm test
node-version: '21.x'
- run: yarn install
- run: yarn test
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "es5"
}
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,61 @@
# Sample Integration: Contacts Sync


## About the Integration

This repository provides a quick solution that will allow contacts to be sync from a source of truth
to Podium. The integration uses the <a href="https://docs.podium.com/reference/contactcreate-1">create contact</a> and <a href="https://docs.podium.com/reference/contactindex">get list contacts</a> endpoint.
You can learn more about the contact object by accessing
our <a href="https://docs.podium.com/reference/the-contact-object">API reference docs</a>


## Get Started

If it is your first time using Podium API, checkout our <a href="https://docs.podium.com/docs/getting-started">Get Started Guide</a> to get your credentials and understand our scope.

## Running Locally
### 1. Get your developer account

### 1. Get your developer account

In order to make https requests make sure you have a developer account and the following keys:

<ul>
<li>ClientId</li>
<li>ClientSecret</li>
<li>RefreshToken</li>
</ul>

### 2. Setup local project

```html
<!--Clone this repository-->
git clone https://github.com/podium/podium-api-demo-contacts.git

<!--Go to the repository directory-->
cd ~/podium-api-demo-contacts

<!--Create https server with SSL certificate - Podium requires https requests
Create a folder name certs and reference key and certs to the files created in the folder-->
https://nodejs.org/en/knowledge/HTTP/servers/how-to-create-a-HTTPS-server/

<!--Install dependencies packages-->
npm install
```

### 3. Set environment variables

```html
<!--Create a .env file-->
<!--Set env variables-->
REFRESHTOKEN = '<your-refresh-token>'
CLIENTID = '<your-client-id>'
CLIENTSECRET = '<your-client-secret>'

REFRESHTOKEN = '<your-refresh-token
>' CLIENTID = '<your-client-id
>' CLIENTSECRET = '<your-client-secret
>'
</your-client-secret></your-client-id
></your-refresh-token
>
```

### 4. Run Code

```
node index.js
```
Expand Down
32 changes: 16 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import "dotenv/config";
import express from "express";
import fetch from "node-fetch";
import 'dotenv/config';
import express from 'express';
import fetch from 'node-fetch';
export const app = express();

const baseUrl = "https://api.podium.com/v4/";
const baseUrl = 'https://api.podium.com/v4/';
const refreshToken = process.env.REFRESHTOKEN;
const clientID = process.env.CLIENTID;
const clientSecret = process.env.CLIENTSECRET;
Expand All @@ -12,15 +12,15 @@ app.use(express.urlencoded());
app.use(express.json());

//Retrieve all contacts
app.get("/", async (_req, res) => {
app.get('/', async (_req, res) => {
try {
const token = await getTokenID();

if (token) {
const requestAPI = await fetch(`${baseUrl}/contacts`, {
Method: "GET",
Method: 'GET',
headers: {
"Content-Type": "application/json",
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});
Expand All @@ -29,22 +29,22 @@ app.get("/", async (_req, res) => {
return res.send(reqResponse);
}
} catch (error) {
console.error("Error", error);
console.error('Error', error);
return res.sendStatus(error.response.status);
}
});

//Create a contact for a specified location
app.post("/", async (req, res) => {
app.post('/', async (req, res) => {
try {
const token = await getTokenID();
let request;

if (token) {
request = await fetch(`${baseUrl}/contacts`, {
method: "POST",
method: 'POST',
headers: {
"Content-Type": "application/json",
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(req.body),
Expand All @@ -53,7 +53,7 @@ app.post("/", async (req, res) => {
const reqResponse = await request.json();
return res.send(reqResponse);
} else {
return res.send("No authorization token was found.");
return res.send('No authorization token was found.');
}
} catch (error) {
console.error(error);
Expand All @@ -65,17 +65,17 @@ export async function getTokenID() {
const bodyData = {
client_id: clientID,
client_secret: clientSecret,
grant_type: "refresh_token",
grant_type: 'refresh_token',
refresh_token: refreshToken,
};

try {
const tokenRequest = await fetch(
"https://accounts.podium.com/oauth/token",
'https://accounts.podium.com/oauth/token',
{
method: "POST",
method: 'POST',
headers: {
"Content-Type": "application/json",
'Content-Type': 'application/json',
},
body: JSON.stringify(bodyData),
}
Expand Down
6 changes: 3 additions & 3 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getTokenID } from "./index.js";
import { getTokenID } from './index.js';

it("validates existence of getTokenID", async () => {
expect(typeof getTokenID).toBe("function");
it('validates existence of getTokenID', async () => {
expect(typeof getTokenID).toBe('function');
});
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"description": "A Podium demo app for CRUD contacts",
"main": "index.js",
"scripts": {
"lint": "eslint . --ext .js",
"test": "node --experimental-vm-modules ./node_modules/.bin/jest"
"lint": "yarn eslint . --ext .js",
"test": "node --experimental-vm-modules ./node_modules/.bin/jest",
"format": "prettier --write .",
"format:check": "prettier --list-different '**/*.{js,md,json,yml}'"
},
"type": "module",
"repository": {
Expand All @@ -27,6 +29,7 @@
"node-fetch": "^3.2.4"
},
"devDependencies": {
"jest": "^29.6.1"
"jest": "^29.6.1",
"prettier": "^2.8.0"
}
}
12 changes: 6 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { app } from "./index.js";
import fs from "fs";
import { createServer } from "https";
import { app } from './index.js';
import fs from 'fs';
import { createServer } from 'https';

const key = fs.readFileSync("./certs/key.pem");
const cert = fs.readFileSync("./certs/cert.pem");
const key = fs.readFileSync('./certs/key.pem');
const cert = fs.readFileSync('./certs/cert.pem');
const server = createServer({ key: key, cert: cert }, app);

server.listen(3000, () => {
console.log("Server is listening on port 3000");
console.log('Server is listening on port 3000');
});
Loading

0 comments on commit c0d2cce

Please sign in to comment.