Skip to content

Commit

Permalink
feat: big update
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Nov 10, 2023
1 parent 2a84e71 commit cd6ac4f
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 108 deletions.
3 changes: 3 additions & 0 deletions packages/api/.env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
ENV=dev
PROD_DISTRIBUTION=managed # could be self-host if you want to run it locally
DATABASE_URL=
JWT_SECRET="SECRET"
POSTGRES_HOST=

# Sentry for logging errors
SENTRY_DSN=your_sentry_dsn_here

# INTEGRATIONS PROVIDER CREDENTIALS FOR PANORA
# CRM
Expand Down
3 changes: 3 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
"@nestjs/mapped-types": "*",
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^4.0.0",
"@nestjs/swagger": "^7.1.14",
"@prisma/client": "^5.4.2",
"@sentry/node": "^7.80.0",
"@sentry/tracing": "^7.80.0",
"axios": "^1.5.1",
"bcrypt": "^5.1.1",
"crypto": "^1.0.1",
Expand Down
30 changes: 24 additions & 6 deletions packages/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ model organizations {
}

model projects {
id_project BigInt @id(map: "pk_projects") @default(autoincrement())
id_project BigInt @id(map: "pk_projects") @default(autoincrement())
name String
id_organization BigInt
api_keys api_keys[]
connections connections[]
organizations organizations @relation(fields: [id_organization], references: [id_organization], onDelete: NoAction, onUpdate: NoAction, map: "fk_6")
linked_users linked_users[]
organizations organizations @relation(fields: [id_organization], references: [id_organization], onDelete: NoAction, onUpdate: NoAction, map: "fk_6")
@@index([id_organization], map: "fk_1_projects")
}
Expand All @@ -109,16 +110,33 @@ model users {

/// This model or at least one of its fields has comments in the database, and requires an additional setup for migrations: Read more: https://pris.ly/d/database-comments
model connections {
id_connection BigInt @id(map: "pk_connections") @default(autoincrement())
id_connection BigInt @id(map: "pk_connections") @default(autoincrement())
provider_slug String
account_url String?
token_type String
access_token String?
refresh_token String?
expiration_timestamp DateTime? @db.Timestamp(6)
created_at DateTime @db.Timestamp(6)
expiration_timestamp DateTime? @db.Timestamp(6)
created_at DateTime @db.Timestamp(6)
id_project BigInt
projects projects @relation(fields: [id_project], references: [id_project], onDelete: NoAction, onUpdate: NoAction, map: "fk_9")
id_linked_user BigInt
linked_users linked_users @relation(fields: [id_linked_user], references: [id_linked_user], onDelete: NoAction, onUpdate: NoAction, map: "fk_11")
projects projects @relation(fields: [id_project], references: [id_project], onDelete: NoAction, onUpdate: NoAction, map: "fk_9")
@@unique([access_token, refresh_token], map: "index_3")
@@index([id_project], map: "fk_1")
@@index([id_linked_user], map: "fk_connections_to_linkedusersid")
}

/// This model or at least one of its fields has comments in the database, and requires an additional setup for migrations: Read more: https://pris.ly/d/database-comments
model linked_users {
id_linked_user BigInt @id(map: "key_id_linked_users") @default(autoincrement())
linked_user_origin_id String
alias String
status String
id_project BigInt
connections connections[]
projects projects @relation(fields: [id_project], references: [id_project], onDelete: NoAction, onUpdate: NoAction, map: "fk_10")
@@index([id_project], map: "fk_proectid_linked_users")
}
14 changes: 2 additions & 12 deletions packages/api/src/@core/connections/connections.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ConnectionsController {
handleCRMCallback(
@Res() res: Response,
@Query('projectId') projectId: string,
@Query('customerId') customerId: string,
@Query('linkedUserId') linkedUserId: string,
@Query('providerName') providerName: string,
@Query('returnUrl') returnUrl: string,
@Query('code') code: string,
Expand All @@ -20,21 +20,11 @@ export class ConnectionsController {

this.connectionsService.handleCRMCallBack(
projectId,
customerId,
linkedUserId,
providerName,
code,
zohoAccountURL,
);
res.redirect(returnUrl);
}

@Get('oauth/crm/refresh')
handleCRMTokensRefresh(
@Query('customerId') customerId: string,
@Query('providerName') providerName: string,
) {
//TODO; ADD VERIFICATION OF PARAMS

this.connectionsService.handleCRMTokensRefresh(customerId, providerName);
}
}
4 changes: 3 additions & 1 deletion packages/api/src/@core/connections/connections.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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';

@Module({
controllers: [ConnectionsController],
providers: [ConnectionsService],
providers: [ConnectionsService, CrmConnectionsService, PrismaService],
})
export class ConnectionsModule {}
40 changes: 24 additions & 16 deletions packages/api/src/@core/connections/services/connections.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable } from '@nestjs/common';
import { CrmConnectionsService } from './crm';
import { CrmConnectionsService } from './crm/crm-connection.service';
import { NotFoundError } from 'src/@core/utils/errors';
import { PrismaService } from 'src/@core/prisma/prisma.service';

@Injectable()
export class ConnectionsService {
Expand All @@ -19,14 +18,11 @@ 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,
private prismaService: PrismaService,
) {}
constructor(private crmConnectionService: CrmConnectionsService) {}

async handleCRMCallBack(
projectId: string,
customerId: string,
linkedUserId: string,
providerName: string,
code: string,
zohoAccountURL?: string,
Expand All @@ -38,7 +34,7 @@ export class ConnectionsService {
throw new NotFoundError('no hubspot code found');
}
return this.crmConnectionService.handleHubspotCallback(
customerId,
linkedUserId,
projectId,
code,
);
Expand All @@ -47,7 +43,7 @@ export class ConnectionsService {
throw new NotFoundError('no zoho code/ zoho AccountURL found');
}
return this.crmConnectionService.handleZohoCallback(
customerId,
linkedUserId,
projectId,
code,
zohoAccountURL,
Expand All @@ -57,7 +53,7 @@ export class ConnectionsService {
throw new NotFoundError('no pipedrive code found');
}
return this.crmConnectionService.handlePipedriveCallback(
customerId,
linkedUserId,
projectId,
code,
);
Expand All @@ -69,7 +65,7 @@ export class ConnectionsService {
throw new NotFoundError('no zendesk code found');
}
return this.crmConnectionService.handleZendeskCallback(
customerId,
linkedUserId,
projectId,
code,
);
Expand All @@ -84,25 +80,37 @@ export class ConnectionsService {
}
}

async handleCRMTokensRefresh(customerId: string, providerId: string) {
async handleCRMTokensRefresh(
connectionId: bigint,
providerId: string,
refresh_token: string,
account_url?: string,
) {
try {
switch (providerId) {
case 'hubspot':
return this.crmConnectionService.handleHubspotTokenRefresh(
customerId,
connectionId,
refresh_token,
);
case 'zoho':
return this.crmConnectionService.handleZohoTokenRefresh(customerId);
return this.crmConnectionService.handleZohoTokenRefresh(
connectionId,
refresh_token,
account_url,
);
case 'pipedrive':
return this.crmConnectionService.handlePipedriveTokenRefresh(
customerId,
connectionId,
refresh_token,
);
case 'freshsales':
//todo: LATER
break;
case 'zendesk':
return this.crmConnectionService.handleZendeskTokenRefresh(
customerId,
connectionId,
refresh_token,
);
default:
return;
Expand Down
Loading

0 comments on commit cd6ac4f

Please sign in to comment.