diff --git a/packages/api/.env.example b/packages/api/.env.example index 01b4c4863..7b4b3f2b4 100644 --- a/packages/api/.env.example +++ b/packages/api/.env.example @@ -21,4 +21,6 @@ ZENDESK_CLIENT_ID= ZENDESK_CLIENT_SECRET= -OAUTH_REDIRECT_BASE='https://api-staging.panora.dev/' \ No newline at end of file +OAUTH_REDIRECT_BASE='https://api-staging.panora.dev/' + +ENCRYPT_CRYPTO_SECRET_KEY= # key for encrypting tokens ! NEVER SHARE ! \ No newline at end of file diff --git a/packages/api/src/@core/auth/auth.service.ts b/packages/api/src/@core/auth/auth.service.ts index 6ac70f987..2e24f09dc 100644 --- a/packages/api/src/@core/auth/auth.service.ts +++ b/packages/api/src/@core/auth/auth.service.ts @@ -83,7 +83,6 @@ export class AuthService { } hashApiKey(apiKey: string): string { - console.log('hey hashing...'); return crypto.createHash('sha256').update(apiKey).digest('hex'); } @@ -119,7 +118,6 @@ export class AuthService { projectId: number | string, userId: number | string, ): Promise<{ access_token: string }> { - console.log("'ddddd"); const jwtPayload = { sub: userId, projectId: projectId, @@ -164,10 +162,8 @@ export class AuthService { // Generate a new API key (use a secure method for generation) const { access_token } = await this.generateApiKey(projectId, userId); - console.log('hey'); // Store the API key in the database associated with the user const hashed_token = this.hashApiKey(access_token); - console.log('hey2'); const new_api_key = await this.prisma.api_keys.create({ data: { id_api_key: uuidv4(), diff --git a/packages/api/src/@core/auth/strategies/auth-header-api-key.strategy.ts b/packages/api/src/@core/auth/strategies/auth-header-api-key.strategy.ts index 9ed51c99c..4e9bbff05 100644 --- a/packages/api/src/@core/auth/strategies/auth-header-api-key.strategy.ts +++ b/packages/api/src/@core/auth/strategies/auth-header-api-key.strategy.ts @@ -18,7 +18,7 @@ export class ApiKeyStrategy extends PassportStrategy( if (!isValid) { return done(new UnauthorizedException('Invalid API Key'), null); } - console.log('validating api request... : ' + req.user); + //console.log('validating api request... : ' + req.user); // If the API key is valid, attach the user to the request object req.user = { ...req.user, apiKeyValidated: true }; diff --git a/packages/api/src/@core/connections/crm/services/hubspot/hubspot.service.ts b/packages/api/src/@core/connections/crm/services/hubspot/hubspot.service.ts index f8223eaf1..86be1820d 100644 --- a/packages/api/src/@core/connections/crm/services/hubspot/hubspot.service.ts +++ b/packages/api/src/@core/connections/crm/services/hubspot/hubspot.service.ts @@ -103,10 +103,7 @@ export class HubspotConnectionService { }, ); const data: HubspotOAuthResponse = res.data; - //console.log('OAuth credentials : hubspot ', data); - this.logger.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: { id_connection: uuidv4(), @@ -167,7 +164,7 @@ export class HubspotConnectionService { ), }, }); - console.log('OAuth credentials updated : hubspot ', data); + this.logger.log('OAuth credentials updated : hubspot '); } catch (error) { handleServiceError(error, this.logger, 'hubspot', Action.oauthRefresh); } diff --git a/packages/api/src/@core/connections/crm/services/pipedrive/pipedrive.service.ts b/packages/api/src/@core/connections/crm/services/pipedrive/pipedrive.service.ts index 0340e4bca..5aa69aded 100644 --- a/packages/api/src/@core/connections/crm/services/pipedrive/pipedrive.service.ts +++ b/packages/api/src/@core/connections/crm/services/pipedrive/pipedrive.service.ts @@ -55,7 +55,7 @@ export class PipedriveConnectionService { ); //TODO: handle if res throws an error const data: PipeDriveOAuthResponse = res.data; - console.log('OAuth credentials : pipedrive ', data); + this.logger.log('OAuth credentials : pipedrive '); const db_res = await this.prisma.connections.create({ data: { id_connection: uuidv4(), @@ -118,7 +118,7 @@ export class PipedriveConnectionService { ), }, }); - console.log('OAuth credentials updated : pipedrive ', data); + this.logger.log('OAuth credentials updated : pipedrive '); } catch (error) { handleServiceError(error, this.logger, 'pipedrive', Action.oauthRefresh); } diff --git a/packages/api/src/@core/connections/crm/services/zendesk/zendesk.service.ts b/packages/api/src/@core/connections/crm/services/zendesk/zendesk.service.ts index 0cfc35a03..1653aca6b 100644 --- a/packages/api/src/@core/connections/crm/services/zendesk/zendesk.service.ts +++ b/packages/api/src/@core/connections/crm/services/zendesk/zendesk.service.ts @@ -54,7 +54,7 @@ export class ZendeskConnectionService { ); //TODO: handle if res throws an error const data: ZendeskOAuthResponse = res.data; - console.log('OAuth credentials : zendesk ', data); + this.logger.log('OAuth credentials : zendesk '); // save tokens for this customer inside our db const db_res = await this.prisma.connections.create({ data: { @@ -111,7 +111,7 @@ export class ZendeskConnectionService { ), }, }); - console.log('OAuth credentials updated : zendesk ', data); + this.logger.log('OAuth credentials updated : zendesk '); } catch (error) { handleServiceError(error, this.logger, 'zendesk', Action.oauthRefresh); } diff --git a/packages/api/src/@core/connections/crm/services/zoho/zoho.service.ts b/packages/api/src/@core/connections/crm/services/zoho/zoho.service.ts index 675e95e08..2adba4f4b 100644 --- a/packages/api/src/@core/connections/crm/services/zoho/zoho.service.ts +++ b/packages/api/src/@core/connections/crm/services/zoho/zoho.service.ts @@ -61,7 +61,7 @@ export class ZohoConnectionService { }, ); const data: ZohoOAuthResponse = res.data; - console.log('OAuth credentials : zoho ', data); + this.logger.log('OAuth credentials : zoho '); const db_res = await this.prisma.connections.create({ data: { id_connection: uuidv4(), @@ -124,7 +124,7 @@ export class ZohoConnectionService { ), }, }); - console.log('OAuth credentials updated : zoho ', data); + this.logger.log('OAuth credentials updated : zoho '); } catch (error) { handleServiceError(error, this.logger, 'zoho', Action.oauthRefresh); } diff --git a/packages/api/src/@core/utils/config.ts b/packages/api/src/@core/utils/config.ts index 427922672..fe36813bd 100644 --- a/packages/api/src/@core/utils/config.ts +++ b/packages/api/src/@core/utils/config.ts @@ -13,6 +13,7 @@ const config = { SENTRY_DSN: process.env.SENTRY_DSN, NODE_ENV: process.env.ENV, DISTRIBUTION: process.env.DISTRIBUTION, + ENCRYPT_CRYPTO_SECRET_KEY: process.env.ENCRYPT_CRYPTO_SECRET_KEY, }; export default config; diff --git a/packages/api/src/@core/utils/crypto.ts b/packages/api/src/@core/utils/crypto.ts index 175c9b044..1d28ff759 100644 --- a/packages/api/src/@core/utils/crypto.ts +++ b/packages/api/src/@core/utils/crypto.ts @@ -1,8 +1,7 @@ import * as crypto from 'crypto'; +import config from './config'; -// Secret key must be 32 bytes for aes-256-cbc -const secretKey = '12345678901234567890123456789012'; // Replace with your actual secret key -// IV should be 16 bytes +const secretKey = config.ENCRYPT_CRYPTO_SECRET_KEY; const iv = crypto.randomBytes(16); export function encrypt(data: string): string { diff --git a/packages/api/src/@core/utils/unification/crm/hubspot/mappers/contact.ts b/packages/api/src/@core/utils/unification/crm/hubspot/mappers/contact.ts index 0eb57d12e..efa8779f0 100644 --- a/packages/api/src/@core/utils/unification/crm/hubspot/mappers/contact.ts +++ b/packages/api/src/@core/utils/unification/crm/hubspot/mappers/contact.ts @@ -47,8 +47,6 @@ export function mapToUnifiedContact( if (!Array.isArray(source)) { return _mapSingleContact(source); } - console.log('hhddhdhdhdh'); - console.log(JSON.stringify(customFieldMappings)); // Handling array of HubspotContactOutput return source.map((contact) => _mapSingleContact(contact, customFieldMappings), diff --git a/packages/api/src/crm/contact/contact.controller.ts b/packages/api/src/crm/contact/contact.controller.ts index 8fe20907e..08642545d 100644 --- a/packages/api/src/crm/contact/contact.controller.ts +++ b/packages/api/src/crm/contact/contact.controller.ts @@ -44,4 +44,8 @@ export class ContactController { remote_data, ); } + + //TODO: batch add Contact + // TODO: update contact + // TODO: delete a contact } diff --git a/packages/api/src/crm/contact/services/contact.service.ts b/packages/api/src/crm/contact/services/contact.service.ts index cd80349b5..f90bd67af 100644 --- a/packages/api/src/crm/contact/services/contact.service.ts +++ b/packages/api/src/crm/contact/services/contact.service.ts @@ -72,7 +72,13 @@ export class ContactService { }; } - async addContactToDb(data: UnifiedContactInput, job_id: string) { + async addContactToDb( + data: UnifiedContactInput, + job_id: string, + integrationId: string, + linkedUserId: string, + field_mappings?: Record[], + ) { const { first_name, last_name, email_addresses, phone_numbers } = data; const { normalizedEmails, normalizedPhones } = this.normalizeEmailsAndNumbers(email_addresses, phone_numbers); @@ -93,22 +99,34 @@ export class ContactService { id_job: job_id, }, }); - } - // Helper method to apply custom field mappings to the contact data - applyCustomFieldMappings(contactData, customFieldMappings) { - // Logic to transform the contactData by applying the customFieldMappings - // For each custom field mapping, replace or add the field in contactData - customFieldMappings.forEach((mapping) => { - // Assuming mapping has `unifiedFieldName` and `providerFieldName` - if (contactData.hasOwnProperty(mapping.unifiedFieldName)) { - contactData[mapping.providerFieldName] = - contactData[mapping.unifiedFieldName]; - // Optionally remove the unified field if it should not be sent to the provider - // delete contactData[mapping.unifiedFieldName]; + if (field_mappings && field_mappings.length > 0) { + const entity = await this.prisma.entity.findFirst({ + where: { ressource_owner_id: 'contact' }, + }); + + for (const mapping of field_mappings) { + const attribute = await this.prisma.attribute.findFirst({ + where: { + slug: Object.keys(mapping)[0], + source: integrationId, + id_consumer: linkedUserId, + id_entity: entity.id_entity, + }, + }); + + if (attribute) { + await this.prisma.value.create({ + data: { + id_value: uuidv4(), + data: Object.values(mapping)[0], + id_attribute: attribute.id_attribute, + id_entity: entity.id_entity, + }, + }); + } } - }); - return contactData; + } } /* */ @@ -160,7 +178,13 @@ export class ContactService { }); const job_id = job_resp_create.id_job; //TODO: add field mappings data too - await this.addContactToDb(unifiedContactData, job_id); + await this.addContactToDb( + unifiedContactData, + job_id, + integrationId, + linkedUserId, + unifiedContactData.field_mappings, + ); const job_resp_update = await this.prisma.jobs.update({ where: { id_job: job_id,