-
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.
- Loading branch information
Showing
78 changed files
with
4,827 additions
and
451 deletions.
There are no files selected for viewing
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
2 changes: 0 additions & 2 deletions
2
packages/api/src/@core/connections-strategies/connections-strategies.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
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/ecommerce/ecommerce.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 { EnvironmentService } from '@@core/@core-services/environment/environment.service'; | ||
import { BullQueueModule } from '@@core/@core-services/queues/queue.module'; | ||
import { WebhookModule } from '@@core/@core-services/webhooks/panora-webhooks/webhook.module'; | ||
import { WebhookService } from '@@core/@core-services/webhooks/panora-webhooks/webhook.service'; | ||
import { ConnectionsStrategiesService } from '@@core/connections-strategies/connections-strategies.service'; | ||
import { Module } from '@nestjs/common'; | ||
import { EcommerceConnectionsService } from './services/ecommerce.connection.service'; | ||
import { ServiceRegistry } from './services/registry.service'; | ||
import { ShopifyConnectionService } from './services/shopify/shopify.service'; | ||
|
||
@Module({ | ||
imports: [WebhookModule, BullQueueModule], | ||
providers: [ | ||
EcommerceConnectionsService, | ||
WebhookService, | ||
EnvironmentService, | ||
ServiceRegistry, | ||
ConnectionsStrategiesService, | ||
//PROVIDERS SERVICES, | ||
ShopifyConnectionService, | ||
], | ||
exports: [EcommerceConnectionsService], | ||
}) | ||
export class EcommerceConnectionModule {} |
104 changes: 104 additions & 0 deletions
104
packages/api/src/@core/connections/ecommerce/services/ecommerce.connection.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,104 @@ | ||
import { LoggerService } from '@@core/@core-services/logger/logger.service'; | ||
import { PrismaService } from '@@core/@core-services/prisma/prisma.service'; | ||
import { WebhookService } from '@@core/@core-services/webhooks/panora-webhooks/webhook.service'; | ||
import { | ||
CallbackParams, | ||
IConnectionCategory, | ||
RefreshParams, | ||
} from '@@core/connections/@utils/types'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { connections as Connection } from '@prisma/client'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { ServiceRegistry } from './registry.service'; | ||
import { CategoryConnectionRegistry } from '@@core/@core-services/registries/connections-categories.registry'; | ||
|
||
@Injectable() | ||
export class EcommerceConnectionsService implements IConnectionCategory { | ||
constructor( | ||
private serviceRegistry: ServiceRegistry, | ||
private connectionCategoryRegistry: CategoryConnectionRegistry, | ||
private webhook: WebhookService, | ||
private logger: LoggerService, | ||
private prisma: PrismaService, | ||
) { | ||
this.logger.setContext(EcommerceConnectionsService.name); | ||
this.connectionCategoryRegistry.registerService('ecommerce', this); | ||
} | ||
//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 | ||
/*const authUrl = | ||
'https://app.hubspot.com/oauth/authorize' + | ||
`?client_id=${encodeURIComponent(CLIENT_ID)}` + | ||
`&scope=${encodeURIComponent(SCOPES)}` + | ||
`&redirect_uri=${encodeURIComponent(REDIRECT_URI)}`;*/ //oauth/callback | ||
|
||
// oauth server calls this redirect callback | ||
// WE WOULD HAVE CREATED A DEV ACCOUNT IN THE 5 CRMs (Panora dev account) | ||
// 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 | ||
async handleCallBack( | ||
providerName: string, | ||
callbackOpts: CallbackParams, | ||
type_strategy: 'oauth' | 'apikey' | 'basic', | ||
) { | ||
try { | ||
const serviceName = providerName.toLowerCase(); | ||
|
||
const service = this.serviceRegistry.getService(serviceName); | ||
|
||
if (!service) { | ||
throw new ReferenceError(`Unknown provider, found ${providerName}`); | ||
} | ||
const data: Connection = await service.handleCallback(callbackOpts); | ||
const event = await this.prisma.events.create({ | ||
data: { | ||
id_event: uuidv4(), | ||
status: 'success', | ||
type: 'connection.created', | ||
method: 'GET', | ||
url: `/${type_strategy}/callback`, | ||
provider: providerName.toLowerCase(), | ||
direction: '0', | ||
timestamp: new Date(), | ||
id_linked_user: callbackOpts.linkedUserId, | ||
}, | ||
}); | ||
//directly send the webhook | ||
await this.webhook.deliverWebhook( | ||
data, | ||
'connection.created', | ||
callbackOpts.projectId, | ||
event.id_event, | ||
); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
async handleTokensRefresh( | ||
connectionId: string, | ||
providerName: string, | ||
refresh_token: string, | ||
id_project: string, | ||
account_url?: string, | ||
) { | ||
try { | ||
const serviceName = providerName.toLowerCase(); | ||
const service = this.serviceRegistry.getService(serviceName); | ||
if (!service) { | ||
throw new ReferenceError(`Unknown provider, found ${providerName}`); | ||
} | ||
const refreshOpts: RefreshParams = { | ||
connectionId: connectionId, | ||
refreshToken: refresh_token, | ||
account_url: account_url, | ||
projectId: id_project, | ||
}; | ||
await service.handleTokenRefresh(refreshOpts); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/api/src/@core/connections/ecommerce/services/registry.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,25 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { IEcommerceConnectionService } from '../types'; | ||
|
||
@Injectable() | ||
export class ServiceRegistry { | ||
private serviceMap: Map<string, IEcommerceConnectionService>; | ||
|
||
constructor() { | ||
this.serviceMap = new Map<string, IEcommerceConnectionService>(); | ||
} | ||
|
||
registerService(serviceKey: string, service: IEcommerceConnectionService) { | ||
this.serviceMap.set(serviceKey, service); | ||
} | ||
|
||
getService(integrationId: string): IEcommerceConnectionService { | ||
const service = this.serviceMap.get(integrationId); | ||
if (!service) { | ||
throw new ReferenceError( | ||
`Service not found for integration ID: ${integrationId}`, | ||
); | ||
} | ||
return service; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
packages/api/src/@core/connections/ecommerce/services/shopify/shopify.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,84 @@ | ||
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service'; | ||
import { LoggerService } from '@@core/@core-services/logger/logger.service'; | ||
import { PrismaService } from '@@core/@core-services/prisma/prisma.service'; | ||
import { ConnectionUtils } from '@@core/connections/@utils'; | ||
import { APIKeyCallbackParams } from '@@core/connections/@utils/types'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { CONNECTORS_METADATA } from '@panora/shared'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { IEcommerceConnectionService } from '../../types'; | ||
import { ServiceRegistry } from '../registry.service'; | ||
|
||
@Injectable() | ||
export class ShopifyConnectionService implements IEcommerceConnectionService { | ||
constructor( | ||
private prisma: PrismaService, | ||
private logger: LoggerService, | ||
private cryptoService: EncryptionService, | ||
private registry: ServiceRegistry, | ||
private connectionUtils: ConnectionUtils, | ||
) { | ||
this.logger.setContext(ShopifyConnectionService.name); | ||
this.registry.registerService('ashby', this); | ||
} | ||
|
||
async handleCallback(opts: APIKeyCallbackParams) { | ||
try { | ||
const { linkedUserId, projectId } = opts; | ||
const isNotUnique = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: linkedUserId, | ||
provider_slug: 'ashby', | ||
vertical: 'ecommerce', | ||
}, | ||
}); | ||
|
||
let db_res; | ||
const connection_token = uuidv4(); | ||
|
||
if (isNotUnique) { | ||
db_res = await this.prisma.connections.update({ | ||
where: { | ||
id_connection: isNotUnique.id_connection, | ||
}, | ||
data: { | ||
access_token: this.cryptoService.encrypt(opts.apikey), | ||
account_url: CONNECTORS_METADATA['ecommerce']['ashby'].urls | ||
.apiUrl as string, | ||
status: 'valid', | ||
created_at: new Date(), | ||
}, | ||
}); | ||
} else { | ||
db_res = await this.prisma.connections.create({ | ||
data: { | ||
id_connection: uuidv4(), | ||
connection_token: connection_token, | ||
provider_slug: 'ashby', | ||
vertical: 'ecommerce', | ||
token_type: 'api_key', | ||
account_url: CONNECTORS_METADATA['ecommerce']['ashby'].urls | ||
.apiUrl as string, | ||
access_token: this.cryptoService.encrypt(opts.apikey), | ||
status: 'valid', | ||
created_at: new Date(), | ||
projects: { | ||
connect: { id_project: projectId }, | ||
}, | ||
linked_users: { | ||
connect: { | ||
id_linked_user: await this.connectionUtils.getLinkedUserId( | ||
projectId, | ||
linkedUserId, | ||
), | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
return db_res; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
} |
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,7 @@ | ||
import { CallbackParams, RefreshParams } from '@@core/connections/@utils/types'; | ||
import { connections as Connection } from '@prisma/client'; | ||
|
||
export interface IEcommerceConnectionService { | ||
handleCallback(opts: CallbackParams): Promise<Connection>; | ||
handleTokenRefresh?(opts: RefreshParams): Promise<any>; | ||
} |
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
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
Oops, something went wrong.