diff --git a/packages/api/src/@core/connections/crm/crm.connection.module.ts b/packages/api/src/@core/connections/crm/crm.connection.module.ts index 0ff7ef8c2..b594557a2 100644 --- a/packages/api/src/@core/connections/crm/crm.connection.module.ts +++ b/packages/api/src/@core/connections/crm/crm.connection.module.ts @@ -1,6 +1,5 @@ import { Module } from '@nestjs/common'; import { CrmConnectionsService } from './services/crm.connection.service'; -import { PrismaService } from '@@core/prisma/prisma.service'; import { LoggerService } from '@@core/logger/logger.service'; import { WebhookService } from '@@core/webhook/webhook.service'; import { WebhookModule } from '@@core/webhook/webhook.module'; @@ -17,6 +16,10 @@ import { CloseConnectionService } from './services/close/close.service'; import { CapsuleConnectionService } from './services/capsule/capsule.service'; import { TeamleaderConnectionService } from './services/teamleader/teamleader.service'; import { ConnectionUtils } from '../@utils'; +import { AffinityConnectionService } from './services/affinity/affinity.service'; +import { KeapConnectionService } from './services/keap/keap.service'; +import { CopperConnectionService } from './services/copper/copper.service'; +import { TeamworkConnectionService } from './services/teamwork/teamwork.service'; @Module({ imports: [WebhookModule], @@ -38,6 +41,10 @@ import { ConnectionUtils } from '../@utils'; CloseConnectionService, CapsuleConnectionService, TeamleaderConnectionService, + AffinityConnectionService, + KeapConnectionService, + CopperConnectionService, + TeamworkConnectionService, ], exports: [CrmConnectionsService], }) diff --git a/packages/api/src/@core/connections/crm/services/affinity/affinity.service.ts b/packages/api/src/@core/connections/crm/services/affinity/affinity.service.ts new file mode 100644 index 000000000..557292a1d --- /dev/null +++ b/packages/api/src/@core/connections/crm/services/affinity/affinity.service.ts @@ -0,0 +1,82 @@ +import { Injectable } from '@nestjs/common'; +import { PrismaService } from '@@core/prisma/prisma.service'; +import { LoggerService } from '@@core/logger/logger.service'; +import { v4 as uuidv4 } from 'uuid'; +import { EncryptionService } from '@@core/encryption/encryption.service'; +import { ICrmConnectionService } from '../../types'; +import { ServiceRegistry } from '../registry.service'; +import { CONNECTORS_METADATA } from '@panora/shared'; +import { ConnectionUtils } from '@@core/connections/@utils'; +import { APIKeyCallbackParams } from '@@core/connections/@utils/types'; + +@Injectable() +export class AffinityConnectionService implements ICrmConnectionService { + constructor( + private prisma: PrismaService, + private logger: LoggerService, + private cryptoService: EncryptionService, + private registry: ServiceRegistry, + private connectionUtils: ConnectionUtils, + ) { + this.logger.setContext(AffinityConnectionService.name); + this.registry.registerService('affinity', this); + } + + async handleCallback(opts: APIKeyCallbackParams) { + try { + const { linkedUserId, projectId } = opts; + const isNotUnique = await this.prisma.connections.findFirst({ + where: { + id_linked_user: linkedUserId, + provider_slug: 'affinity', + vertical: 'crm', + }, + }); + + 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['crm']['affinity'].urls.apiUrl, + status: 'valid', + created_at: new Date(), + }, + }); + } else { + db_res = await this.prisma.connections.create({ + data: { + id_connection: uuidv4(), + connection_token: connection_token, + provider_slug: 'affinity', + vertical: 'crm', + token_type: 'api_key', + account_url: CONNECTORS_METADATA['crm']['affinity'].urls.apiUrl, + 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; + } + } +} diff --git a/packages/api/src/main.ts b/packages/api/src/main.ts index e977301f5..09045c4f4 100644 --- a/packages/api/src/main.ts +++ b/packages/api/src/main.ts @@ -1,5 +1,5 @@ -import * as Sentry from '@sentry/node'; import '@@core/sentry/instrument'; +import * as Sentry from '@sentry/node'; import { BaseExceptionFilter, HttpAdapterHost, diff --git a/packages/shared/src/connectors/metadata.ts b/packages/shared/src/connectors/metadata.ts index 2682cabd5..c3115a891 100644 --- a/packages/shared/src/connectors/metadata.ts +++ b/packages/shared/src/connectors/metadata.ts @@ -100,12 +100,11 @@ export const CONNECTORS_METADATA: ProvidersConfig = { authStrategy: AuthStrategy.api_key }, 'affinity': { - scopes: '', urls: { docsUrl: 'https://api-docs.affinity.co/#getting-started', apiUrl: 'https://api.affinity.co', }, - logoPath: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTMRfcwBA9Jn9z9dJQgY3f_H-bBeUzl-jRHNOm8xrmwtA&s', + logoPath: 'https://media.licdn.com/dms/image/C4D0BAQFOaK6KXEYj_w/company-logo_200_200/0/1630489791871/project_affinity_logo?e=2147483647&v=beta&t=u8j-1u3nO2m6vqgT170WJMCJyFSDiLYS_VguYOllNMI', description: 'Sync & Create contacts, deals, companies, notes, engagements, stages, tasks and users', active: false, authStrategy: AuthStrategy.api_key