Skip to content

Commit

Permalink
feat: frontend_snippet > hubspot oauth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Nov 12, 2023
1 parent 69a70b2 commit 3455ce9
Show file tree
Hide file tree
Showing 23 changed files with 709 additions and 35 deletions.
48 changes: 48 additions & 0 deletions packages/api/src/@core/connections/connections.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Controller, Get, Query, Res } from '@nestjs/common';
import { Response } from 'express'; // Importing the Express Response type for type checking
import { CrmConnectionsService } from './crm/services/crm-connection.service';
import { ProviderVertical, getProviderVertical } from '../utils/providers';

@Controller('connections')
export class ConnectionsController {
constructor(private readonly crmConnectionsService: CrmConnectionsService) {}

@Get('oauth/callback')
handleCallback(
@Res() res: Response,
@Query('projectId') projectId: string,
@Query('linkedUserId') linkedUserId: string,
@Query('providerName') providerName: string,
@Query('code') code: string,
@Query('returnUrl') returnUrl: string,
@Query('accountURL') zohoAccountURL?: string,
) {
//TODO; ADD VERIFICATION OF PARAMS
switch (getProviderVertical(providerName)) {
case ProviderVertical.CRM:
this.crmConnectionsService.handleCRMCallBack(
projectId,
linkedUserId,
providerName,
code,
zohoAccountURL,
);
case ProviderVertical.ATS:
break;
case ProviderVertical.Accounting:
break;
case ProviderVertical.FileStorage:
break;
case ProviderVertical.HRIS:
break;
case ProviderVertical.MarketingAutomation:
break;
case ProviderVertical.Ticketing:
break;
case ProviderVertical.Unknown:
break;
}

res.redirect(returnUrl);
}
}
2 changes: 2 additions & 0 deletions packages/api/src/@core/connections/connections.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Module } from '@nestjs/common';
import { CrmConnectionModule } from './crm/crm-connection.module';
import { ConnectionsController } from './connections.controller';

@Module({
controllers: [ConnectionsController],
imports: [CrmConnectionModule],
})
export class ConnectionsModule {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Module } from '@nestjs/common';
import { CrmConnectionsController } from './crm-connection.controller';
import { CrmConnectionsService } from './services/crm-connection.service';
import { PrismaService } from 'src/@core/prisma/prisma.service';
import { FreshsalesConnectionService } from './services/freshsales/freshsales.service';
Expand All @@ -10,7 +9,6 @@ import { ZohoConnectionService } from './services/zoho/zoho.service';
import { LoggerService } from 'src/@core/logger/logger.service';

@Module({
controllers: [CrmConnectionsController],
providers: [
CrmConnectionsService,
PrismaService,
Expand Down
48 changes: 48 additions & 0 deletions packages/api/src/@core/utils/providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export const CRM_PROVIDERS = [
'zoho',
'zendesk',
'hubspot',
'pipedrive',
'freshsales',
];
export const HRIS_PROVIDERS = [];
export const ATS_PROVIDERS = [];
export const ACCOUNTING_PROVIDERS = [];
export const TICKETING_PROVIDERS = [];
export const MARKETING_AUTOMATION_PROVIDERS = [];
export const FILE_STORAGE_PROVIDERS = [];

export enum ProviderVertical {
CRM = 'CRM',
HRIS = 'HRIS',
ATS = 'ATS',
Accounting = 'Accounting',
Ticketing = 'Ticketing',
MarketingAutomation = 'Marketing Automation',
FileStorage = 'File Storage',
Unknown = 'Unknown', // Used if the provider does not match any category
}
export function getProviderVertical(providerName: string): ProviderVertical {
if (CRM_PROVIDERS.includes(providerName)) {
return ProviderVertical.CRM;
}
if (HRIS_PROVIDERS.includes(providerName)) {
return ProviderVertical.HRIS;
}
if (ATS_PROVIDERS.includes(providerName)) {
return ProviderVertical.ATS;
}
if (ACCOUNTING_PROVIDERS.includes(providerName)) {
return ProviderVertical.Accounting;
}
if (TICKETING_PROVIDERS.includes(providerName)) {
return ProviderVertical.Ticketing;
}
if (MARKETING_AUTOMATION_PROVIDERS.includes(providerName)) {
return ProviderVertical.MarketingAutomation;
}
if (FILE_STORAGE_PROVIDERS.includes(providerName)) {
return ProviderVertical.FileStorage;
}
return ProviderVertical.Unknown;
}
1 change: 1 addition & 0 deletions packages/frontend-snippet/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CLIENT_ID= #hubspot client id
18 changes: 18 additions & 0 deletions packages/frontend-snippet/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}
26 changes: 26 additions & 0 deletions packages/frontend-snippet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

.env
27 changes: 27 additions & 0 deletions packages/frontend-snippet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: ['./tsconfig.json', './tsconfig.node.json'],
tsconfigRootDir: __dirname,
},
```

- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list
13 changes: 13 additions & 0 deletions packages/frontend-snippet/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
28 changes: 28 additions & 0 deletions packages/frontend-snippet/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "frontend-snippet",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"@vitejs/plugin-react": "^4.0.3",
"eslint": "^8.45.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.3",
"typescript": "^5.0.2",
"vite": "^4.4.5"
}
}
1 change: 1 addition & 0 deletions packages/frontend-snippet/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions packages/frontend-snippet/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
28 changes: 28 additions & 0 deletions packages/frontend-snippet/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import './App.css'
import useOAuth from './hooks/useOAuth';

const CLIENT_ID = import.meta.env.VITE_CLIENT_ID; // Replace with your OAuth client ID
const SCOPES = 'crm.lists.read%20crm.objects.contacts.read%20crm.objects.contacts.write%20crm.objects.custom.read%20crm.objects.custom.write%20crm.objects.companies.write%20crm.schemas.contacts.read%20crm.objects.feedback_submissions.read%20crm.lists.write%20crm.objects.companies.read%20crm.objects.deals.read%20crm.objects.deals.write%20crm.schemas.contacts.write'; // Replace with your requested scopes
const REDIRECT_URI = 'http://localhost:3000/oauth/callback'; // Replace with your redirect URI

function App() {
const { open, isReady } = useOAuth({
linkToken: 'ADD_GENERATED_LINK_TOKEN', // Replace with your link token
clientId: CLIENT_ID!,
scopes: SCOPES,
redirectUri: REDIRECT_URI,
onSuccess: () => console.log('OAuth successful')
});
return (
<>
<h1>Integrations Flow</h1>
<div className="card">
<button disabled={!isReady} onClick={open}>
Start OAuth Flow
</button>
</div>
</>
)
}

export default App
1 change: 1 addition & 0 deletions packages/frontend-snippet/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions packages/frontend-snippet/src/hooks/useOAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState, useEffect } from 'react';

type UseOAuthProps = {
linkToken: string;
clientId: string; // Your OAuth client ID
scopes: string; // The scopes you are requesting
redirectUri: string; // The redirect URI registered with the OAuth provider
onSuccess: () => void;
};

const useOAuth = ({ linkToken, clientId, scopes, redirectUri, onSuccess }: UseOAuthProps) => {
const [isReady, setIsReady] = useState(false);

useEffect(() => {
// Perform any setup logic here
setTimeout(() => setIsReady(true), 1000); // Simulating async operation
}, []);

const openModal = () => {
//const authUrl = `https://app-eu1.hubspot.com/oauth/authorize?client_id=${encodeURIComponent(clientId)}&scope=${encodeURIComponent(scopes)}&redirect_uri=${encodeURIComponent(redirectUri)}`;
const authUrl = `https://app-eu1.hubspot.com/oauth/authorize?client_id=${clientId}&redirect_uri=http://localhost:3000/oauth/callback&scope=crm.lists.read%20crm.lists.write`
const width = 600, height = 600;
const left = (window.innerWidth - width) / 2;
const top = (window.innerHeight - height) / 2;
window.open(authUrl, 'OAuth', `width=${width},height=${height},top=${top},left=${left}`);

// Call the onSuccess function here if needed, or after the OAuth flow is completed
};

return { open: openModal, isReady };
};

export default useOAuth;
Loading

0 comments on commit 3455ce9

Please sign in to comment.