Skip to content

Commit

Permalink
Merge pull request #64 from panoratech/feat/re-org
Browse files Browse the repository at this point in the history
feat: reorg done
  • Loading branch information
naelob authored Nov 10, 2023
2 parents df02752 + 3ba6ae8 commit 04493c3
Show file tree
Hide file tree
Showing 17 changed files with 522 additions and 507 deletions.
20 changes: 0 additions & 20 deletions packages/api/src/@core/connections/connections.controller.spec.ts

This file was deleted.

8 changes: 2 additions & 6 deletions packages/api/src/@core/connections/connections.module.ts
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 {}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Controller, Get, Query, Res } from '@nestjs/common';
import { ConnectionsService } from './services/connections.service';
import { Response } from 'express'; // Importing the Express Response type for type checking
import { CrmConnectionsService } from './services/crm-connection.service';

@Controller('connections')
export class ConnectionsController {
constructor(private readonly connectionsService: ConnectionsService) {}
@Controller('connections/crm')
export class CrmConnectionsController {
constructor(private readonly crmConnectionsService: CrmConnectionsService) {}

@Get('oauth/crm/callback')
@Get('oauth/callback')
handleCRMCallback(
@Res() res: Response,
@Query('projectId') projectId: string,
Expand All @@ -18,7 +18,7 @@ export class ConnectionsController {
) {
//TODO; ADD VERIFICATION OF PARAMS

this.connectionsService.handleCRMCallBack(
this.crmConnectionsService.handleCRMCallBack(
projectId,
linkedUserId,
providerName,
Expand Down
24 changes: 24 additions & 0 deletions packages/api/src/@core/connections/crm/crm-connection.module.ts
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 {}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { Injectable } from '@nestjs/common';
import { CrmConnectionsService } from './crm/crm-connection.service';
import { PrismaService } from 'src/@core/prisma/prisma.service';

Check warning on line 2 in packages/api/src/@core/connections/crm/services/crm-connection.service.ts

View workflow job for this annotation

GitHub Actions / build-api (16.x)

'PrismaService' is defined but never used
import { ZohoConnectionService } from './zoho/zoho.service';
import { NotFoundError } from 'src/@core/utils/errors';
import { HubspotConnectionService } from './hubspot/hubspot.service';
import { PipedriveConnectionService } from './pipedrive/pipedrive.service';
import { ZendeskConnectionService } from './zendesk/zendesk.service';
import { FreshsalesConnectionService } from './freshsales/freshsales.service';

@Injectable()
export class ConnectionsService {
export class CrmConnectionsService {
constructor(
private zohoConnectionService: ZohoConnectionService,
private hubspotConnectionService: HubspotConnectionService,
private pipedriveConnectionService: PipedriveConnectionService,
private zendeskConnectionService: ZendeskConnectionService,
private freshsalesConnectionService: FreshsalesConnectionService,
) {}
//STEP 1:[FRONTEND STEP]
//create a frontend SDK snippet in which an authorization embedded link is set up so when users click
// on it to grant access => they grant US the access and then when confirmed
Expand All @@ -18,8 +30,6 @@ export class ConnectionsService {
// we catch the tmp token and swap it against oauth2 server for access/refresh tokens
// to perform actions on his behalf
// this call pass 1. integrationID 2. CustomerId 3. Panora Api Key
constructor(private crmConnectionService: CrmConnectionsService) {}

async handleCRMCallBack(
projectId: string,
linkedUserId: string,
Expand All @@ -33,7 +43,7 @@ export class ConnectionsService {
if (!code) {
throw new NotFoundError('no hubspot code found');
}
return this.crmConnectionService.handleHubspotCallback(
return this.hubspotConnectionService.handleHubspotCallback(
linkedUserId,
projectId,
code,
Expand All @@ -42,7 +52,7 @@ export class ConnectionsService {
if (!code || !zohoAccountURL) {
throw new NotFoundError('no zoho code/ zoho AccountURL found');
}
return this.crmConnectionService.handleZohoCallback(
return this.zohoConnectionService.handleZohoCallback(
linkedUserId,
projectId,
code,
Expand All @@ -52,7 +62,7 @@ export class ConnectionsService {
if (!code) {
throw new NotFoundError('no pipedrive code found');
}
return this.crmConnectionService.handlePipedriveCallback(
return this.pipedriveConnectionService.handlePipedriveCallback(
linkedUserId,
projectId,
code,
Expand All @@ -64,7 +74,7 @@ export class ConnectionsService {
if (!code) {
throw new NotFoundError('no zendesk code found');
}
return this.crmConnectionService.handleZendeskCallback(
return this.zendeskConnectionService.handleZendeskCallback(
linkedUserId,
projectId,
code,
Expand All @@ -89,26 +99,26 @@ export class ConnectionsService {
try {
switch (providerId) {
case 'hubspot':
return this.crmConnectionService.handleHubspotTokenRefresh(
return this.hubspotConnectionService.handleHubspotTokenRefresh(
connectionId,
refresh_token,
);
case 'zoho':
return this.crmConnectionService.handleZohoTokenRefresh(
return this.zohoConnectionService.handleZohoTokenRefresh(
connectionId,
refresh_token,
account_url,
);
case 'pipedrive':
return this.crmConnectionService.handlePipedriveTokenRefresh(
return this.pipedriveConnectionService.handlePipedriveTokenRefresh(
connectionId,
refresh_token,
);
case 'freshsales':
//todo: LATER
break;
case 'zendesk':
return this.crmConnectionService.handleZendeskTokenRefresh(
return this.zendeskConnectionService.handleZendeskTokenRefresh(
connectionId,
refresh_token,
);
Expand Down
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;
}
}
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({

Check warning on line 37 in packages/api/src/@core/connections/crm/services/hubspot/hubspot.service.ts

View workflow job for this annotation

GitHub Actions / build-api (16.x)

'db_res' is assigned a value but never used
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);
}
}
}
Loading

0 comments on commit 04493c3

Please sign in to comment.