Skip to content

Commit

Permalink
feat: fixed logs
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Nov 27, 2023
1 parent 4eb60f1 commit ad3e994
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 37 deletions.
4 changes: 3 additions & 1 deletion packages/api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ ZENDESK_CLIENT_ID=
ZENDESK_CLIENT_SECRET=


OAUTH_REDIRECT_BASE='https://api-staging.panora.dev/'
OAUTH_REDIRECT_BASE='https://api-staging.panora.dev/'

ENCRYPT_CRYPTO_SECRET_KEY= # key for encrypting tokens ! NEVER SHARE !
4 changes: 0 additions & 4 deletions packages/api/src/@core/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export class AuthService {
}

hashApiKey(apiKey: string): string {
console.log('hey hashing...');
return crypto.createHash('sha256').update(apiKey).digest('hex');
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({

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

View workflow job for this annotation

GitHub Actions / build-api (16.x)

'db_res' is assigned a value but never used
data: {
id_connection: uuidv4(),
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({

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

View workflow job for this annotation

GitHub Actions / build-api (16.x)

'db_res' is assigned a value but never used
data: {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({

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

View workflow job for this annotation

GitHub Actions / build-api (16.x)

'db_res' is assigned a value but never used
data: {
id_connection: uuidv4(),
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/@core/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 2 additions & 3 deletions packages/api/src/@core/utils/crypto.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/crm/contact/contact.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ export class ContactController {
remote_data,
);
}

//TODO: batch add Contact
// TODO: update contact
// TODO: delete a contact
}
56 changes: 40 additions & 16 deletions packages/api/src/crm/contact/services/contact.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>[],
) {
const { first_name, last_name, email_addresses, phone_numbers } = data;
const { normalizedEmails, normalizedPhones } =
this.normalizeEmailsAndNumbers(email_addresses, phone_numbers);
Expand All @@ -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;
}
}

/* */
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit ad3e994

Please sign in to comment.