-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from panoratech/feat/re-org
feat: reorg done
- Loading branch information
Showing
17 changed files
with
522 additions
and
507 deletions.
There are no files selected for viewing
20 changes: 0 additions & 20 deletions
20
packages/api/src/@core/connections/connections.controller.spec.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConnectionsService } from './services/connections.service'; | ||
import { ConnectionsController } from './connections.controller'; | ||
import { CrmConnectionsService } from './services/crm/crm-connection.service'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { CrmConnectionModule } from './crm/crm-connection.module'; | ||
|
||
@Module({ | ||
controllers: [ConnectionsController], | ||
providers: [ConnectionsService, CrmConnectionsService, PrismaService], | ||
imports: [CrmConnectionModule], | ||
}) | ||
export class ConnectionsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/api/src/@core/connections/crm/crm-connection.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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'; | ||
import { HubspotConnectionService } from './services/hubspot/hubspot.service'; | ||
import { PipedriveConnectionService } from './services/pipedrive/pipedrive.service'; | ||
import { ZendeskConnectionService } from './services/zendesk/zendesk.service'; | ||
import { ZohoConnectionService } from './services/zoho/zoho.service'; | ||
|
||
@Module({ | ||
controllers: [CrmConnectionsController], | ||
providers: [ | ||
CrmConnectionsService, | ||
PrismaService, | ||
FreshsalesConnectionService, | ||
HubspotConnectionService, | ||
PipedriveConnectionService, | ||
ZendeskConnectionService, | ||
ZohoConnectionService, | ||
], | ||
exports: [CrmConnectionsService], | ||
}) | ||
export class CrmConnectionModule {} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/api/src/@core/connections/crm/services/freshsales/freshsales.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class FreshsalesConnectionService { | ||
//TODO: later | ||
async handleFreshsalesCallback() { | ||
return; | ||
} | ||
async handleFreshsalesTokenRefresh() { | ||
return; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
packages/api/src/@core/connections/crm/services/hubspot/hubspot.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from 'src/@core/prisma/prisma.service'; | ||
import axios from 'axios'; | ||
import config from 'src/@core/utils/config'; | ||
import { Prisma } from '@prisma/client'; | ||
import { HubspotOAuthResponse } from '../../types'; | ||
|
||
@Injectable() | ||
export class HubspotConnectionService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async handleHubspotCallback( | ||
linkedUserId: string, | ||
projectId: string, | ||
code: string, | ||
) { | ||
try { | ||
//first create a linked_user | ||
//reconstruct the redirect URI that was passed in the frontend it must be the same | ||
const REDIRECT_URI = `${config.OAUTH_REDIRECT_BASE}/oauth/crm/callback`; //tocheck | ||
|
||
const formData = { | ||
grant_type: 'authorization_code', | ||
client_id: config.HUBSPOT_CLIENT_ID, | ||
client_secret: config.HUBSPOT_CLIENT_SECRET, | ||
redirect_uri: REDIRECT_URI, | ||
code: code, | ||
}; | ||
const res = await axios.post( | ||
'https://api.hubapi.com/oauth/v1/token', | ||
formData, | ||
); | ||
const data: HubspotOAuthResponse = res.data; | ||
console.log('OAuth credentials : hubspot ', data); | ||
// save tokens for this customer inside our db | ||
//TODO: encrypt the access token and refresh tokens | ||
const db_res = await this.prisma.connections.create({ | ||
data: { | ||
provider_slug: 'hubspot', | ||
token_type: 'oauth', | ||
access_token: data.access_token, | ||
refresh_token: data.refresh_token, | ||
expiration_timestamp: new Date( | ||
new Date().getTime() + data.expires_in * 1000, | ||
), | ||
created_at: new Date(), | ||
projects: { | ||
connect: { id_project: BigInt(projectId) }, | ||
}, | ||
linked_users: { | ||
connect: { id_linked_user: BigInt(linkedUserId) }, | ||
}, | ||
//id of the end-customer defined in the company application, this is how requests could be made on behlaf of the user | ||
// without it, we cant retrieve the right row in our db | ||
}, | ||
}); | ||
} catch (error) { | ||
if (axios.isAxiosError(error)) { | ||
// Handle Axios-specific errors | ||
console.error('Error with Axios request:', error.response?.data); | ||
} | ||
if (error instanceof Prisma.PrismaClientKnownRequestError) { | ||
console.error('Error with Prisma request:', error); | ||
} | ||
console.log(error); | ||
} | ||
} | ||
async handleHubspotTokenRefresh(connectionId: bigint, refresh_token: string) { | ||
try { | ||
const REDIRECT_URI = `${config.OAUTH_REDIRECT_BASE}/oauth/crm/callback`; | ||
|
||
const formData = { | ||
grant_type: 'refresh_token', | ||
client_id: config.HUBSPOT_CLIENT_ID, | ||
client_secret: config.HUBSPOT_CLIENT_SECRET, | ||
redirect_uri: REDIRECT_URI, | ||
refresh_token: refresh_token, | ||
}; | ||
const res = await axios.post( | ||
'https://api.hubapi.com/oauth/v1/token', | ||
formData, | ||
); | ||
const data: HubspotOAuthResponse = res.data; | ||
await this.prisma.connections.update({ | ||
where: { | ||
id_connection: connectionId, | ||
}, | ||
data: { | ||
access_token: data.access_token, | ||
refresh_token: data.refresh_token, | ||
expiration_timestamp: new Date( | ||
new Date().getTime() + data.expires_in * 1000, | ||
), | ||
}, | ||
}); | ||
console.log('OAuth credentials updated : hubspot ', data); | ||
} catch (error) { | ||
if (axios.isAxiosError(error)) { | ||
// Handle Axios-specific errors | ||
console.error('Error with Axios request:', error.response?.data); | ||
} | ||
if (error instanceof Prisma.PrismaClientKnownRequestError) { | ||
console.error('Error with Prisma request:', error); | ||
} | ||
console.log(error); | ||
} | ||
} | ||
} |
Oops, something went wrong.