Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add integration with Redtail (#554) #643

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

tosifAN
Copy link

@tosifAN tosifAN commented Aug 10, 2024

This pull request addresses issue #554, which involves integrating the application with Redtail CRM. The integration(only mappers) focuses on managing the following objects:

Contact
Opportunity

References
Redtail CRM Integration with Make
Redtail Authentication Methods

Summary by CodeRabbit

  • New Features

    • Integrated Redtail CRM functionalities for managing contacts and deals, including addition and synchronization features.
    • Introduced services for handling deals with comprehensive RESTful API support.
    • Implemented mappers for transforming deal data between different formats.
    • Added automated synchronization services for updating deal records from third-party providers.
  • Documentation

    • Enhanced type safety and structure by defining new TypeScript interfaces and types for deals.

@rflihxyz
Copy link
Contributor

Qovery Preview

Qovery can create a Preview Environment for this PR.
To trigger its creation, please post a comment with one of the following command.

Command Blueprint environment
/qovery preview 783d0240-ec38-4387-a9a9-8e225f1bd3e1 dev
/qovery preview {all|UUID1,UUID2,...} To preview multiple environments

This comment has been generated from Qovery AI 🤖.
Below, a word from its wisdom :

Truth can only be found in one place: the code

Copy link

changeset-bot bot commented Aug 10, 2024

⚠️ No Changeset found

Latest commit: f78d00d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@CLAassistant
Copy link

CLAassistant commented Aug 10, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

coderabbitai bot commented Aug 10, 2024

Walkthrough

Walkthrough

This update enhances the CRM system's capabilities for managing contacts and deals by integrating with the Redtail API. It introduces services for adding and syncing both contacts and deals, along with mappers for data transformation. New TypeScript interfaces improve type safety and clarity, while structured classes streamline the overall architecture, facilitating efficient interactions with the Redtail CRM system.

Changes

File(s) Change Summary
packages/api/src/crm/contact/services/redtail/index.ts, mappers.ts, types.ts Added RedtailService for contact management and RetailDealMapper for mapping deal formats, including new type definitions.
packages/api/src/crm/deal/services/redtail/index.ts, mappers.ts, types.ts Introduced RedtailService for deal management with methods for adding and syncing deals, alongside type definitions.
packages/api/src/crm/deal/services/redtail/mappers.ts Implemented methods for converting between Unified CRM and Redtail deal formats.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 17

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 77fc6f9 and 1b1c061.

Files selected for processing (12)
  • packages/api/src/crm/contact/services/redtail/index.ts (1 hunks)
  • packages/api/src/crm/contact/services/redtail/mappers.ts (1 hunks)
  • packages/api/src/crm/contact/services/redtail/types.ts (1 hunks)
  • packages/api/src/crm/opportunity/opportunity.controller.ts (1 hunks)
  • packages/api/src/crm/opportunity/opportunity.module.ts (1 hunks)
  • packages/api/src/crm/opportunity/services/redtail/index.ts (1 hunks)
  • packages/api/src/crm/opportunity/services/redtail/mappers.ts (1 hunks)
  • packages/api/src/crm/opportunity/services/redtail/types.ts (1 hunks)
  • packages/api/src/crm/opportunity/sync/sync.processor.ts (1 hunks)
  • packages/api/src/crm/opportunity/sync/sync.service.ts (1 hunks)
  • packages/api/src/crm/opportunity/types/index.ts (1 hunks)
  • packages/api/src/crm/opportunity/types/model.unified.ts (1 hunks)
Additional context used
Biome
packages/api/src/crm/contact/services/redtail/index.ts

[error] 22-22: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 58-58: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)


[error] 89-89: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)

packages/api/src/crm/opportunity/services/redtail/index.ts

[error] 22-22: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

packages/api/src/crm/opportunity/services/redtail/mappers.ts

[error] 106-106: This let declares a variable that is only assigned once.

'opts' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)

packages/api/src/crm/opportunity/sync/sync.service.ts

[error] 43-43: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)


[error] 89-89: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)


[error] 93-93: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)


[error] 100-100: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)


[error] 118-118: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)


[error] 199-199: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)

Additional comments not posted (18)
packages/api/src/crm/opportunity/services/redtail/types.ts (1)

1-30: Ensure consistency in type definitions.

The RedtailOpportunity interface is well-structured, but there are a few points to consider:

  • The owner_id property is an object, which might be better named owner for clarity.
  • Consider using boolean for has_pic instead of number to improve type safety.
  • The value property within owner_id seems ambiguous; ensure it aligns with your data model.
  • Consider using Date for close_date, create_date, and update_date to ensure consistency in date handling.

If these fields are directly mapped from an external API, ensure that the naming and types align with the API documentation.

packages/api/src/crm/opportunity/types/index.ts (1)

1-5: Ensure type imports are necessary and used.

The imports are well-organized, but verify that all imported types are used within the file. Unused imports can clutter the code and lead to maintenance challenges.

Verification successful

All imported types are used in the file.

The verification confirms that each imported type is utilized within the file, ensuring no unnecessary imports.

  • DesunifyReturnType is used in opportunityData.
  • UnifiedOpportunityInput is used in source.
  • OriginalOpportunityOutput is used in multiple contexts.
  • ApiResponse is used in return types.
  • IBaseObjectService and SyncParam are used in the interface and method parameters.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that all imported types are used within the file.

# Test: Search for usage of imported types. Expect: All imports are used.
rg --type typescript 'DesunifyReturnType|UnifiedOpportunityInput|OriginalOpportunityOutput|ApiResponse|IBaseObjectService|SyncParam' packages/api/src/crm/opportunity/types/index.ts

Length of output: 217


Script:

#!/bin/bash
# Description: Verify that all imported types are used within the file.

# Test: Search for usage of imported types. Expect: All imports are used.
rg 'DesunifyReturnType|UnifiedOpportunityInput|OriginalOpportunityOutput|ApiResponse|IBaseObjectService|SyncParam' packages/api/src/crm/opportunity/types/index.ts

Length of output: 903

packages/api/src/crm/opportunity/opportunity.module.ts (1)

1-3: Verify the import paths for core services.

Ensure that the paths for BullQueueModule, IngestDataService, and WebhookService are correct and that these services are available for use in this module. Incorrect paths could lead to runtime errors.

Verification successful

Import paths for core services are correct.

The classes BullQueueModule, IngestDataService, and WebhookService are correctly defined in their respective files, indicating that the import paths in opportunity.module.ts are valid. No further action is required.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence and correctness of import paths for core services.

# Test: Check if the services are defined correctly in the expected paths.
fd 'queue.module.ts' | xargs rg 'export class BullQueueModule'
fd 'ingest-data.service.ts' | xargs rg 'export class IngestDataService'
fd 'webhook.service.ts' | xargs rg 'export class WebhookService'

Length of output: 294

packages/api/src/crm/contact/services/redtail/types.ts (1)

1-75: Ensure alignment with Redtail CRM data structure.

Verify that the RedtailContact interface accurately reflects the data structure provided by Redtail CRM. Any discrepancies could lead to data mapping issues.

packages/api/src/crm/opportunity/types/model.unified.ts (2)

44-76: Review optional fields in UnifiedOpportunityOutput.

Ensure that all optional fields in UnifiedOpportunityOutput are truly optional and that their absence will not cause issues in downstream processing.

Verification successful

Optional fields in UnifiedOpportunityOutput are handled correctly.

The codebase appropriately handles the optional fields in UnifiedOpportunityOutput, using optional chaining and checks to ensure that their absence does not cause issues. This is evident in the RetailOpportunityMapper class, where fields are accessed cautiously. No further action is needed.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the optional fields in `UnifiedOpportunityOutput`.

# Test: Check for any potential issues caused by missing optional fields.
echo "Please manually verify the optional fields in UnifiedOpportunityOutput for potential downstream issues."

Length of output: 216


Script:

#!/bin/bash
# Search for references to UnifiedOpportunityOutput in the codebase to understand its usage.
rg 'UnifiedOpportunityOutput' -A 5

Length of output: 9040


Script:

#!/bin/bash
# Check the mappers.ts file for any assumptions about the presence of optional fields in UnifiedOpportunityOutput.
rg 'UnifiedOpportunityOutput' packages/api/src/crm/opportunity/services/redtail/mappers.ts -A 10

Length of output: 1150


4-42: Ensure completeness of UnifiedOpportunityInput.

Verify that all necessary fields for creating an opportunity are included in UnifiedOpportunityInput. Missing fields could lead to incomplete data handling.

packages/api/src/crm/contact/services/redtail/mappers.ts (4)

13-15: Constructor logic is correct.

The constructor correctly registers the mapper service with the mappers registry.


17-73: Desunify method is implemented correctly.

The method correctly transforms unified contact input into Redtail contact input, including handling custom field mappings.


76-101: Unify method is implemented correctly.

The method correctly transforms Redtail contact output into unified contact output, handling both single and multiple contacts.


103-146: MapSingleContactToUnified method is implemented correctly.

The method effectively maps a single Redtail contact to a unified contact format, including handling field mappings and user ID transformation.

packages/api/src/crm/opportunity/services/redtail/mappers.ts (2)

14-18: Class setup looks good.

The class RetailOpportunityMapper is well-structured with appropriate use of dependency injection and service registration.


20-62: desunify method implementation looks good.

The method effectively converts unified opportunity data to Redtail-specific format, with appropriate handling of optional fields and transformations.

packages/api/src/crm/opportunity/sync/sync.service.ts (1)

22-37: Class setup looks good.

The class SyncService is well-structured with appropriate use of dependency injection and service registration.

packages/api/src/crm/opportunity/opportunity.controller.ts (5)

34-44: Class setup looks good.

The class OpportunityController is well-structured with appropriate use of dependency injection and logger context setting.


46-83: getOpportunities method implementation looks good.

The method effectively retrieves opportunities with appropriate handling of connection metadata and query parameters.


85-131: retrieve method implementation looks good.

The method effectively retrieves a single opportunity by ID with appropriate handling of connection metadata.


133-175: addOpportunity method implementation looks good.

The method effectively creates a new opportunity with appropriate handling of connection metadata and request body data.


177-266: updateOpportunity and deleteOpportunity methods look good.

Both methods effectively handle their respective operations with appropriate error handling and logging.

Comment on lines 32 to 33
export type RedtailOpportunityInput = Partial<RedtailOpportunity>;
export type RedtailOpportunityOutput = RedtailOpportunity;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify the purpose of input and output types.

The RedtailOpportunityInput and RedtailOpportunityOutput types are straightforward, but consider documenting their intended use cases. This can help developers understand when to use each type, especially if they are used in different contexts within the application.

Comment on lines 35 to 38
export const commonOpportunityRedtailProperties = {
defaultStage: 'Prospecting',
defaultProbability: 0.1,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extending common properties.

The commonOpportunityRedtailProperties object provides default values. If these defaults are used across multiple places, consider creating a utility function or a constant that can be reused to ensure consistency and reduce duplication.

export const getDefaultOpportunityProperties = () => ({
  defaultStage: 'Prospecting',
  defaultProbability: 0.1,
});

/* PROVIDERS MAPPERS */
RedtailOpportunityMapper,
],
exports: [SyncService, ServiceRegistry, WebhookService],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider exporting additional services.

If OpportunityService or other services are intended for use in other modules, consider exporting them to promote reusability and modularity.

Comment on lines 57 to 58
} catch (error) {
throw error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove redundant catch clause.

The catch clause that only rethrows the original error is unnecessary and can be removed to simplify the code.

- } catch (error) {
-   throw error;
+ }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} catch (error) {
throw error;
}
Tools
Biome

[error] 58-58: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)

Comment on lines 89 to 90
throw error;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove redundant catch clause.

The catch clause that only rethrows the original error is unnecessary and can be removed to simplify the code.

- } catch (error) {
-   throw error;
+ }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
throw error;
}
}
Tools
Biome

[error] 89-89: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)

Comment on lines 64 to 142
async unify(
source: RedtailOpportunityOutput | RedtailOpportunityOutput[],
connectionId: string,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): Promise<UnifiedOpportunityOutput | UnifiedOpportunityOutput[]> {
if (!Array.isArray(source)) {
return this.mapSingleOpportunityToUnified(
source,
connectionId,
customFieldMappings,
);
}

return Promise.all(
source.map((opportunity) =>
this.mapSingleOpportunityToUnified(
opportunity,
connectionId,
customFieldMappings,
),
),
);
}

private async mapSingleOpportunityToUnified(
opportunity: RedtailOpportunityOutput,
connectionId: string,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): Promise<UnifiedOpportunityOutput> {
const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = opportunity[mapping.remote_id];
}
}

let opts: any = {};
if (opportunity.owner_id?.id) {
const user_id = await this.utils.getUserUuidFromRemoteId(
opportunity.owner_id.id,
connectionId,
);
if (user_id) {
opts.owner_id = user_id;
}
}

if (opportunity.company_id) {
const account_id = await this.utils.getAccountUuidFromRemoteId(
opportunity.company_id.toString(),
connectionId,
);
if (account_id) {
opts.account_id = account_id;
}
}

return {
remote_id: opportunity.id,
remote_data: opportunity,
title: opportunity.title,
description: opportunity.description,
amount: opportunity.amount,
close_date: opportunity.close_date.toISOString(),
field_mappings,
...opts,
probability: opportunity.probability,
stage: opportunity.stage,
projected_revenue: opportunity.projected_revenue,
actual_revenue: opportunity.actual_revenue,
deleted: opportunity.deleted,
};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unify and mapSingleOpportunityToUnified methods look good.

The methods effectively handle the transformation of Redtail opportunity data to a unified format, with appropriate handling of field mappings and ID transformations.

Use const instead of let for opts.

The variable opts is only assigned once and can be declared as const.

-    let opts: any = {};
+    const opts: any = {};
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async unify(
source: RedtailOpportunityOutput | RedtailOpportunityOutput[],
connectionId: string,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): Promise<UnifiedOpportunityOutput | UnifiedOpportunityOutput[]> {
if (!Array.isArray(source)) {
return this.mapSingleOpportunityToUnified(
source,
connectionId,
customFieldMappings,
);
}
return Promise.all(
source.map((opportunity) =>
this.mapSingleOpportunityToUnified(
opportunity,
connectionId,
customFieldMappings,
),
),
);
}
private async mapSingleOpportunityToUnified(
opportunity: RedtailOpportunityOutput,
connectionId: string,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): Promise<UnifiedOpportunityOutput> {
const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = opportunity[mapping.remote_id];
}
}
let opts: any = {};
if (opportunity.owner_id?.id) {
const user_id = await this.utils.getUserUuidFromRemoteId(
opportunity.owner_id.id,
connectionId,
);
if (user_id) {
opts.owner_id = user_id;
}
}
if (opportunity.company_id) {
const account_id = await this.utils.getAccountUuidFromRemoteId(
opportunity.company_id.toString(),
connectionId,
);
if (account_id) {
opts.account_id = account_id;
}
}
return {
remote_id: opportunity.id,
remote_data: opportunity,
title: opportunity.title,
description: opportunity.description,
amount: opportunity.amount,
close_date: opportunity.close_date.toISOString(),
field_mappings,
...opts,
probability: opportunity.probability,
stage: opportunity.stage,
projected_revenue: opportunity.projected_revenue,
actual_revenue: opportunity.actual_revenue,
deleted: opportunity.deleted,
};
}
async unify(
source: RedtailOpportunityOutput | RedtailOpportunityOutput[],
connectionId: string,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): Promise<UnifiedOpportunityOutput | UnifiedOpportunityOutput[]> {
if (!Array.isArray(source)) {
return this.mapSingleOpportunityToUnified(
source,
connectionId,
customFieldMappings,
);
}
return Promise.all(
source.map((opportunity) =>
this.mapSingleOpportunityToUnified(
opportunity,
connectionId,
customFieldMappings,
),
),
);
}
private async mapSingleOpportunityToUnified(
opportunity: RedtailOpportunityOutput,
connectionId: string,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): Promise<UnifiedOpportunityOutput> {
const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = opportunity[mapping.remote_id];
}
}
const opts: any = {};
if (opportunity.owner_id?.id) {
const user_id = await this.utils.getUserUuidFromRemoteId(
opportunity.owner_id.id,
connectionId,
);
if (user_id) {
opts.owner_id = user_id;
}
}
if (opportunity.company_id) {
const account_id = await this.utils.getAccountUuidFromRemoteId(
opportunity.company_id.toString(),
connectionId,
);
if (account_id) {
opts.account_id = account_id;
}
}
return {
remote_id: opportunity.id,
remote_data: opportunity,
title: opportunity.title,
description: opportunity.description,
amount: opportunity.amount,
close_date: opportunity.close_date.toISOString(),
field_mappings,
...opts,
probability: opportunity.probability,
stage: opportunity.stage,
projected_revenue: opportunity.projected_revenue,
actual_revenue: opportunity.actual_revenue,
deleted: opportunity.deleted,
};
}
Tools
Biome

[error] 106-106: This let declares a variable that is only assigned once.

'opts' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)

Comment on lines 122 to 200
async saveToDb(
connection_id: string,
linkedUserId: string,
data: UnifiedOpportunityOutput[],
originSource: string,
remote_data: Record<string, any>[],
): Promise<CrmOpportunity[]> {
try {
const opportunities_results: CrmOpportunity[] = [];

const updateOrCreateOpportunity = async (
opportunity: UnifiedOpportunityOutput,
originId: string,
) => {
const existingOpportunity = await this.prisma.crm_opportunities.findFirst({
where: {
remote_id: originId,
id_connection: connection_id,
},
});

const baseData: any = {
title: opportunity.title ?? null,
description: opportunity.description ?? null,
amount: opportunity.amount ?? null,
id_crm_contact: opportunity.contact_id ?? null,
id_crm_company: opportunity.company_id ?? null,
id_crm_deal: opportunity.deal_id ?? null,
id_crm_user: opportunity.user_id ?? null,
modified_at: new Date(),
};

if (existingOpportunity) {
return await this.prisma.crm_opportunities.update({
where: {
id_crm_opportunity: existingOpportunity.id_crm_opportunity,
},
data: baseData,
});
} else {
return await this.prisma.crm_opportunities.create({
data: {
...baseData,
id_crm_opportunity: uuidv4(),
created_at: new Date(),
remote_id: originId ?? null,
id_connection: connection_id,
},
});
}
};

for (let i = 0; i < data.length; i++) {
const opportunity = data[i];
const originId = opportunity.remote_id;

if (!originId || originId === '') {
throw new ReferenceError(`Origin id not there, found ${originId}`);
}

const res = await updateOrCreateOpportunity(opportunity, originId);
const opportunity_id = res.id_crm_opportunity;
opportunities_results.push(res);

// Process field mappings
await this.ingestService.processFieldMappings(
opportunity.field_mappings,
opportunity_id,
originSource,
linkedUserId,
);

// Process remote data
await this.ingestService.processRemoteData(opportunity_id, remote_data[i]);
}
return opportunities_results;
} catch (error) {
throw error;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve error handling in saveToDb.

Consider logging errors or providing more context when throwing errors in saveToDb to aid in debugging.

-    try {
-      const opportunities_results: CrmOpportunity[] = [];
-      ...
-      return opportunities_results;
-    } catch (error) {
-      throw error;
-    }
+    const opportunities_results: CrmOpportunity[] = [];
+    ...
+    return opportunities_results;
+  } catch (error) {
+    this.logger.error('Error in saveToDb method', error.stack);
+    throw new Error(`Failed to save opportunities to DB: ${error.message}`);
+  }

Committable suggestion was skipped due to low confidence.

Tools
Biome

[error] 199-199: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)

Comment on lines 39 to 45
async onModuleInit() {
try {
await this.bullQueueService.queueSyncJob('crm-sync-opportunities', '0 0 * * *');
} catch (error) {
throw error;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove redundant try-catch block.

The try-catch block in onModuleInit is unnecessary as it only rethrows the original error. Consider removing it to simplify the code.

-    try {
-      await this.bullQueueService.queueSyncJob('crm-sync-opportunities', '0 0 * * *');
-    } catch (error) {
-      throw error;
-    }
+    await this.bullQueueService.queueSyncJob('crm-sync-opportunities', '0 0 * * *');
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async onModuleInit() {
try {
await this.bullQueueService.queueSyncJob('crm-sync-opportunities', '0 0 * * *');
} catch (error) {
throw error;
}
}
async onModuleInit() {
await this.bullQueueService.queueSyncJob('crm-sync-opportunities', '0 0 * * *');
}
Tools
Biome

[error] 43-43: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)

Comment on lines 51 to 101
async kickstartSync(user_id?: string) {
try {
this.logger.log(`Syncing opportunities....`);
const users = user_id
? [
await this.prisma.users.findUnique({
where: {
id_user: user_id,
},
}),
]
: await this.prisma.users.findMany();
if (users && users.length > 0) {
for (const user of users) {
const projects = await this.prisma.projects.findMany({
where: {
id_user: user.id_user,
},
});
for (const project of projects) {
const id_project = project.id_project;
const linkedUsers = await this.prisma.linked_users.findMany({
where: {
id_project: id_project,
},
});
linkedUsers.map(async (linkedUser) => {
try {
const providers = CRM_PROVIDERS.filter(
(provider) => provider !== 'zoho',
);
for (const provider of providers) {
try {
await this.syncForLinkedUser({
integrationId: provider,
linkedUserId: linkedUser.id_linked_user,
});
} catch (error) {
throw error;
}
}
} catch (error) {
throw error;
}
});
}
}
}
} catch (error) {
throw error;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove redundant try-catch blocks and fix async map usage.

The try-catch blocks are redundant as they only rethrow errors. Also, using map for async operations is incorrect; use for...of instead.

-    try {
-      this.logger.log(`Syncing opportunities....`);
-      const users = user_id
-        ? [
-            await this.prisma.users.findUnique({
-              where: {
-                id_user: user_id,
-              },
-            }),
-          ]
-        : await this.prisma.users.findMany();
-      if (users && users.length > 0) {
-        for (const user of users) {
-          const projects = await this.prisma.projects.findMany({
-            where: {
-              id_user: user.id_user,
-            },
-          });
-          for (const project of projects) {
-            const id_project = project.id_project;
-            const linkedUsers = await this.prisma.linked_users.findMany({
-              where: {
-                id_project: id_project,
-              },
-            });
-            linkedUsers.map(async (linkedUser) => {
-              try {
-                const providers = CRM_PROVIDERS.filter(
-                  (provider) => provider !== 'zoho',
-                );
-                for (const provider of providers) {
-                  try {
-                    await this.syncForLinkedUser({
-                      integrationId: provider,
-                      linkedUserId: linkedUser.id_linked_user,
-                    });
-                  } catch (error) {
-                    throw error;
-                  }
-                }
-              } catch (error) {
-                throw error;
-              }
-            });
-          }
-        }
-      }
-    } catch (error) {
-      throw error;
-    }
+    this.logger.log(`Syncing opportunities....`);
+    const users = user_id
+      ? [
+          await this.prisma.users.findUnique({
+            where: {
+              id_user: user_id,
+            },
+          }),
+        ]
+      : await this.prisma.users.findMany();
+    if (users && users.length > 0) {
+      for (const user of users) {
+        const projects = await this.prisma.projects.findMany({
+          where: {
+            id_user: user.id_user,
+          },
+        });
+        for (const project of projects) {
+          const id_project = project.id_project;
+          const linkedUsers = await this.prisma.linked_users.findMany({
+            where: {
+              id_project: id_project,
+            },
+          });
+          for (const linkedUser of linkedUsers) {
+            const providers = CRM_PROVIDERS.filter(
+              (provider) => provider !== 'zoho',
+            );
+            for (const provider of providers) {
+              await this.syncForLinkedUser({
+                integrationId: provider,
+                linkedUserId: linkedUser.id_linked_user,
+              });
+            }
+          }
+        }
+      }
+    }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async kickstartSync(user_id?: string) {
try {
this.logger.log(`Syncing opportunities....`);
const users = user_id
? [
await this.prisma.users.findUnique({
where: {
id_user: user_id,
},
}),
]
: await this.prisma.users.findMany();
if (users && users.length > 0) {
for (const user of users) {
const projects = await this.prisma.projects.findMany({
where: {
id_user: user.id_user,
},
});
for (const project of projects) {
const id_project = project.id_project;
const linkedUsers = await this.prisma.linked_users.findMany({
where: {
id_project: id_project,
},
});
linkedUsers.map(async (linkedUser) => {
try {
const providers = CRM_PROVIDERS.filter(
(provider) => provider !== 'zoho',
);
for (const provider of providers) {
try {
await this.syncForLinkedUser({
integrationId: provider,
linkedUserId: linkedUser.id_linked_user,
});
} catch (error) {
throw error;
}
}
} catch (error) {
throw error;
}
});
}
}
}
} catch (error) {
throw error;
}
this.logger.log(`Syncing opportunities....`);
const users = user_id
? [
await this.prisma.users.findUnique({
where: {
id_user: user_id,
},
}),
]
: await this.prisma.users.findMany();
if (users && users.length > 0) {
for (const user of users) {
const projects = await this.prisma.projects.findMany({
where: {
id_user: user.id_user,
},
});
for (const project of projects) {
const id_project = project.id_project;
const linkedUsers = await this.prisma.linked_users.findMany({
where: {
id_project: id_project,
},
});
for (const linkedUser of linkedUsers) {
const providers = CRM_PROVIDERS.filter(
(provider) => provider !== 'zoho',
);
for (const provider of providers) {
await this.syncForLinkedUser({
integrationId: provider,
linkedUserId: linkedUser.id_linked_user,
});
}
}
}
}
}
Tools
Biome

[error] 89-89: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)


[error] 93-93: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)


[error] 100-100: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)

Comment on lines 104 to 120
// Todo: HANDLE DATA REMOVED FROM PROVIDER
async syncForLinkedUser(param: SyncLinkedUserType) {
try {
const { integrationId, linkedUserId } = param;
const service: IOpportunityService =
this.serviceRegistry.getService(integrationId);
if (!service) return;

await this.ingestService.syncForLinkedUser<
UnifiedOpportunityOutput,
OriginalOpportunityOutput,
IOpportunityService
>(integrationId, linkedUserId, 'crm', 'opportunity', service, []);
} catch (error) {
throw error;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove redundant try-catch block in syncForLinkedUser.

The try-catch block is unnecessary as it only rethrows the original error. Consider removing it to simplify the code.

-    try {
-      const { integrationId, linkedUserId } = param;
-      const service: IOpportunityService =
-        this.serviceRegistry.getService(integrationId);
-      if (!service) return;
-
-      await this.ingestService.syncForLinkedUser<
-        UnifiedOpportunityOutput,
-        OriginalOpportunityOutput,
-        IOpportunityService
-      >(integrationId, linkedUserId, 'crm', 'opportunity', service, []);
-    } catch (error) {
-      throw error;
-    }
+    const { integrationId, linkedUserId } = param;
+    const service: IOpportunityService =
+      this.serviceRegistry.getService(integrationId);
+    if (!service) return;
+
+    await this.ingestService.syncForLinkedUser<
+      UnifiedOpportunityOutput,
+      OriginalOpportunityOutput,
+      IOpportunityService
+    >(integrationId, linkedUserId, 'crm', 'opportunity', service, []);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Todo: HANDLE DATA REMOVED FROM PROVIDER
async syncForLinkedUser(param: SyncLinkedUserType) {
try {
const { integrationId, linkedUserId } = param;
const service: IOpportunityService =
this.serviceRegistry.getService(integrationId);
if (!service) return;
await this.ingestService.syncForLinkedUser<
UnifiedOpportunityOutput,
OriginalOpportunityOutput,
IOpportunityService
>(integrationId, linkedUserId, 'crm', 'opportunity', service, []);
} catch (error) {
throw error;
}
}
// Todo: HANDLE DATA REMOVED FROM PROVIDER
async syncForLinkedUser(param: SyncLinkedUserType) {
const { integrationId, linkedUserId } = param;
const service: IOpportunityService =
this.serviceRegistry.getService(integrationId);
if (!service) return;
await this.ingestService.syncForLinkedUser<
UnifiedOpportunityOutput,
OriginalOpportunityOutput,
IOpportunityService
>(integrationId, linkedUserId, 'crm', 'opportunity', service, []);
}
Tools
Biome

[error] 118-118: The catch clause that only rethrows the original error is redundant.

These unnecessary catch clauses can be confusing. It is recommended to remove them.

(lint/complexity/noUselessCatch)

@naelob
Copy link
Member

naelob commented Aug 10, 2024

Opportunity is a deal within panora ! You shouldnt have built a new opportunity object!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 1b1c061 and f78d00d.

Files selected for processing (4)
  • packages/api/src/crm/contact/services/redtail/index.ts (1 hunks)
  • packages/api/src/crm/deal/services/redtail/index.ts (1 hunks)
  • packages/api/src/crm/deal/services/redtail/mappers.ts (1 hunks)
  • packages/api/src/crm/deal/services/redtail/types.ts (1 hunks)
Additional comments not posted (17)
packages/api/src/crm/deal/services/redtail/types.ts (1)

32-33: LGTM!

The use of Partial<RedtailDeal> for RedtailDealInput and RedtailDealOutput is appropriate for flexibility in input and output handling.

packages/api/src/crm/contact/services/redtail/index.ts (5)

21-23: Use template literals for string concatenation.

Instead of using string concatenation, prefer template literals for better readability.


61-67: Verify existence of connection object.

Ensure that the connection object is not null or undefined before accessing its properties to avoid runtime errors.


69-76: Enhance security by validating API keys.

Ensure that the API key and user key are validated before use to prevent security vulnerabilities.


31-37: Verify existence of connection object.

Ensure that the connection object is not null or undefined before accessing its properties to avoid runtime errors.


39-49: Enhance security by validating API keys.

Ensure that the API key and user key are validated before use to prevent security vulnerabilities.

packages/api/src/crm/deal/services/redtail/index.ts (6)

21-23: Use template literals for string concatenation.

Instead of using string concatenation, prefer template literals for better readability.


37-43: Verify existence of connection object.

Ensure that the connection object is not null or undefined before accessing its properties to avoid runtime errors.


66-69: Remove redundant catch clause.

The catch clause that only rethrows the original error is unnecessary and can be removed to simplify the code.


76-82: Verify existence of connection object.

Ensure that the connection object is not null or undefined before accessing its properties to avoid runtime errors.


84-88: Enhance security by validating API keys and credentials.

Ensure that the API key, username, and password are validated before use to prevent security vulnerabilities.


102-105: Remove redundant catch clause.

The catch clause that only rethrows the original error is unnecessary and can be removed to simplify the code.

packages/api/src/crm/deal/services/redtail/mappers.ts (5)

1-12: Imports look good.

The import statements are appropriate and necessary for the functionality provided in this file.


14-18: Class definition and constructor look good.

The RetailDealMapper class correctly implements IDealMapper and registers itself in the constructor.


20-59: desunify method looks correct.

The method effectively maps UnifiedCrmDealInput to RedtailDealInput, handling optional fields and custom mappings appropriately.


62-87: unify method looks correct.

The method handles both single and multiple Redtail deal outputs, converting them to unified CRM deal outputs effectively.


89-137: mapSingleDealToUnified method looks correct.

The method accurately maps a single Redtail deal to a UnifiedCrmDealOutput, including handling of optional fields and custom mappings.

Comment on lines +1 to +30
export interface RedtailDeal {
id: string;
title: string;
description: string;
amount: number;
probability: number;
stage: string;
close_date: Date;
create_date: Date;
update_date: Date;
owner_id: {
id: number;
name: string;
email: string;
has_pic: number;
pic_hash: string;
active_flag: boolean;
value: number;
};
company_id: number;
contact_id: string;
related_deals_count: number;
won_deals_count: number;
lost_deals_count: number;
active_flag: boolean;
projected_revenue?: number;
actual_revenue?: number;
deleted?: boolean;
[key: string]: any;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider improving type safety by avoiding any.

The index signature [key: string]: any can lead to type safety issues. Consider using a more specific type or removing it if not needed.

related_deals_count: number;
won_deals_count: number;
lost_deals_count: number;
active_flag: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming active_flag for clarity.

The property active_flag could be renamed to isActive for better readability and consistency with common naming conventions.

-  active_flag: boolean;
+  isActive: boolean;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
active_flag: boolean;
isActive: boolean;

Comment on lines +45 to +49
const authHeader = this.getBasicAuthHeader(
connection.username, // assuming these fields exist
connection.password,
connection.api_key,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

Enhance security by validating API keys and credentials.

There is no evidence of validation logic for the API key, username, and password in the packages/api/src/crm/deal/services/redtail/index.ts file. It is recommended to implement validation for these credentials to prevent potential security vulnerabilities.

  • Ensure that the API key, username, and password are validated before use.
Analysis chain

Enhance security by validating API keys and credentials.

Ensure that the API key, username, and password are validated before use to prevent security vulnerabilities.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the API key, username, and password are validated before use.

# Test: Search for validation logic. Expect: Validation checks for API key, username, and password.
rg --type typescript -A 3 'getBasicAuthHeader' | rg 'validate'

Length of output: 95


Script:

#!/bin/bash
# Search for the definition of the getBasicAuthHeader function and any related validation logic.
ast-grep --lang typescript --pattern 'function getBasicAuthHeader($$$) { $$$ }'

# Search for validation logic related to API keys, username, and password in the same or related files.
rg 'validate|check' -A 3 -B 3

Length of output: 130967


Script:

#!/bin/bash
# Search for validation logic specifically in the redtail service index file.
rg 'validate|check' -A 3 -B 3 packages/api/src/crm/deal/services/redtail/index.ts

Length of output: 83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants