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

Store merchants in the Unified API DB #2976

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open

Store merchants in the Unified API DB #2976

wants to merge 6 commits into from

Conversation

MatanYadaev
Copy link
Collaborator

@MatanYadaev MatanYadaev commented Jan 20, 2025

Summary by CodeRabbit

Release Notes

  • New Features

    • Added internal endpoint for syncing businesses.
    • Enhanced business data management with unified API client.
  • Improvements

    • Updated business repository with unscoped retrieval methods.
    • Refined business service transaction handling.
    • Added optional feature disabled timestamp for customer configurations.
  • Bug Fixes

    • Improved error handling for business creation and updates.
  • Refactor

    • Simplified business controller and service interactions.
    • Updated workflow service to use business service for entity updates.
    • Removed deprecated synchronization endpoint from the business report controller.
    • Removed unnecessary dependencies from the cron module.

Copy link

changeset-bot bot commented Jan 20, 2025

⚠️ No Changeset found

Latest commit: 54eb777

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

Copy link
Contributor

coderabbitai bot commented Jan 20, 2025

Walkthrough

This pull request introduces several modifications across the workflows service, focusing on business and customer-related functionality. The changes include updating the business repository, enhancing the business service with unified API client integration, adding new methods to controllers, and introducing more robust transaction and error handling. The modifications aim to improve data management, synchronization, and feature configuration for businesses and customers.

Changes

File Change Summary
services/workflows-service/prisma/data-migrations Subproject commit hash updated from cccba77231119da4db772844cc83c55785d938de to 89e12cdfcfd40e2f60f05815ec0242d65bb523ad
services/workflows-service/src/business-report/business-report.controller.external.ts Removed list() method (GET /sync endpoint) from BusinessReportControllerExternal
services/workflows-service/src/business/business.controller.external.ts Modified updateOngoingMonitoringState method to use businessService.updateById instead of raw SQL
services/workflows-service/src/business/business.controller.internal.ts Added constructor dependencies, new GET /internal/businesses/sync endpoint
services/workflows-service/src/business/business.repository.ts Added findManyUnscoped method, updated method signatures and return types
services/workflows-service/src/business/business.service.ts Integrated unified API client, added transaction management, retry mechanism
services/workflows-service/src/common/utils/unified-api-client/unified-api-client.ts Added methods for creating and formatting business entities
services/workflows-service/src/customer/types.ts Added optional disabledAt property to TCustomerFeaturesConfig
services/workflows-service/src/workflow/workflow.service.ts Updated business entity update method to use businessService.updateById
services/workflows-service/src/workflow/cron/cron.module.ts Removed all imports and providers from CronModule
services/workflows-service/src/workflow/cron/ongoing-monitoring.cron.intg.test.ts Deleted integration tests for OngoingMonitoringCron service
services/workflows-service/src/workflow/cron/ongoing-monitoring.cron.ts Deleted implementation of OngoingMonitoringCron class

Possibly related PRs

Suggested reviewers

  • tomer-shvadron
  • alonp99

Poem

🐰 Hopping through code with glee,
Workflows dancing, services free!
Businesses sync, metadata flows,
Where rabbit magic cleverly grows 🌱
A code ballet of pure delight! 🚀


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a73d796 and 54eb777.

📒 Files selected for processing (3)
  • services/workflows-service/src/workflow/cron/cron.module.ts (1 hunks)
  • services/workflows-service/src/workflow/cron/ongoing-monitoring.cron.intg.test.ts (0 hunks)
  • services/workflows-service/src/workflow/cron/ongoing-monitoring.cron.ts (0 hunks)
💤 Files with no reviewable changes (2)
  • services/workflows-service/src/workflow/cron/ongoing-monitoring.cron.intg.test.ts
  • services/workflows-service/src/workflow/cron/ongoing-monitoring.cron.ts
✅ Files skipped from review due to trivial changes (1)
  • services/workflows-service/src/workflow/cron/cron.module.ts
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: test_windows
  • GitHub Check: test_linux
  • GitHub Check: build (windows-latest)
  • GitHub Check: build (ubuntu-latest)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: lint

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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: 5

🧹 Nitpick comments (4)
services/workflows-service/src/business/business.service.ts (1)

315-316: Use static import for p-retry instead of dynamic import

Dynamically importing p-retry inside the retry function can introduce unnecessary overhead. Consider importing p-retry statically at the top of the file for better performance and readability.

Apply this diff:

+ import pRetry from 'p-retry';

 const retry = async (fn: () => Promise<unknown>) => {
-   const { default: pRetry } = await import('p-retry');

   return await pRetry(fn, {
     retries: 5,
     randomize: true,
     minTimeout: 100,
     maxTimeout: 10_000,
   });
 };
services/workflows-service/src/common/utils/unified-api-client/unified-api-client.ts (2)

82-89: Add error handling for the API call in createOrUpdateBusiness

The createOrUpdateBusiness method performs an API call without error handling. To improve resilience and debugging, wrap the API call in a try-catch block and handle exceptions appropriately.

For example:

 public async createOrUpdateBusiness(payload: BusinessPayload) {
   const formattedPayload = this.formatBusiness(payload);

+  try {
     return await this.axiosInstance.put(
       `/customers/${payload.customerId}/merchants/${payload.id}`,
       formattedPayload,
     );
+  } catch (error) {
+    this.logger.error('Failed to create or update business', error);
+    throw error;
+  }
 }

91-113: Improve type safety and readability in formatBusiness

The use of type assertions and nested ternary operators can impact code clarity and safety. Define a specific type for metadata and refactor the logic to enhance readability and maintainability.

Define an interface for BusinessMetadata:

+ interface BusinessMetadata {
+   featureConfig: TCustomerWithFeatures['features'];
+   lastOngoingReportInvokedAt: number;
+ }

Refactor the formatBusiness method:

 public formatBusiness(business: BusinessPayload) {
-   const metadata = business.metadata as unknown as {
-     featureConfig: TCustomerWithFeatures['features'];
-     lastOngoingReportInvokedAt: number;
-   } | null;
+   const metadata = business.metadata as BusinessMetadata | null;

+   let unsubscribedMonitoringAt: Date | null = null;
+   const ongoingReport = metadata?.featureConfig?.[FEATURE_LIST.ONGOING_MERCHANT_REPORT];

+   if (ongoingReport?.disabledAt) {
+     unsubscribedMonitoringAt = new Date(ongoingReport.disabledAt);
+   } else if (ongoingReport?.enabled === false) {
+     unsubscribedMonitoringAt = new Date();
+   }

   return {
     id: business.id,
     correlationId: business.correlationId,
     companyName: business.companyName,
     customerId: business.customerId,
     unsubscribedMonitoringAt: unsubscribedMonitoringAt?.toISOString() ?? null,
     createdAt: business.createdAt.toISOString(),
     updatedAt: business.updatedAt.toISOString(),
   };
 }
services/workflows-service/src/business/business.controller.internal.ts (1)

51-89: Verify error handling for the sync endpoint.

The sync endpoint looks good but could benefit from error handling and pagination for large datasets.

Consider adding:

  1. Error handling for database queries
  2. Pagination to handle large datasets
  3. Rate limiting for the admin endpoint
 @common.Get('sync')
 @common.UseGuards(AdminAuthGuard)
 @ApiExcludeEndpoint()
 async getAllBusinesses() {
+  try {
     const businesses = await this.repository.findManyUnscoped({
       select: {
         id: true,
         createdAt: true,
         updatedAt: true,
         correlationId: true,
         companyName: true,
         metadata: true,
         project: {
           select: {
             customer: { select: { id: true } },
           },
         },
       },
       where: {
         project: {
           customer: {
             config: {
               path: ['isMerchantMonitoringEnabled'],
               equals: true,
             },
           },
         },
       },
+      take: 100, // Add pagination
+      skip: 0,
     });

     const unifiedApiClient = new UnifiedApiClient();

     return businesses.map(business => {
       return unifiedApiClient.formatBusiness({
         ...business,
         customerId: business.project.customer.id,
       });
     });
+  } catch (error) {
+    throw new common.InternalServerErrorException('Failed to sync businesses');
+  }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bc8c22b and 7c14122.

📒 Files selected for processing (9)
  • services/workflows-service/prisma/data-migrations (1 hunks)
  • services/workflows-service/src/business-report/business-report.controller.external.ts (0 hunks)
  • services/workflows-service/src/business/business.controller.external.ts (1 hunks)
  • services/workflows-service/src/business/business.controller.internal.ts (2 hunks)
  • services/workflows-service/src/business/business.repository.ts (4 hunks)
  • services/workflows-service/src/business/business.service.ts (3 hunks)
  • services/workflows-service/src/common/utils/unified-api-client/unified-api-client.ts (2 hunks)
  • services/workflows-service/src/customer/types.ts (1 hunks)
  • services/workflows-service/src/workflow/workflow.service.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • services/workflows-service/src/business-report/business-report.controller.external.ts
✅ Files skipped from review due to trivial changes (1)
  • services/workflows-service/prisma/data-migrations
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: test_windows
  • GitHub Check: test_linux
  • GitHub Check: build (windows-latest)
  • GitHub Check: build (ubuntu-latest)
  • GitHub Check: lint
🔇 Additional comments (5)
services/workflows-service/src/business/business.controller.internal.ts (1)

25-29: LGTM! Constructor dependencies properly injected.

The constructor has been updated to include BusinessRepository and PrismaService, which are required for the new sync functionality.

services/workflows-service/src/business/business.repository.ts (3)

42-46: LGTM! New unscoped query method added.

The findManyUnscoped method is correctly implemented to bypass project scope filters, which is necessary for the sync functionality.


64-72: LGTM! Transaction support added to findByIdUnscoped.

The method now accepts an optional transaction parameter, maintaining consistency with other repository methods.


109-109: LGTM! Return type updated for better type safety.

The updateById method now explicitly returns Promise instead of the implicit return type.

services/workflows-service/src/business/business.controller.external.ts (1)

170-174: LGTM! Raw SQL replaced with service method.

The change from raw SQL to businessService.updateById improves maintainability and type safety.

@@ -258,23 +258,6 @@ export class BusinessReportControllerExternal {
});
}

@common.Get('/sync')
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This endpoint is no longer necessary. We completed the data migration.

WHERE "id" = ${businessId}
AND "projectId" = ${currentProjectId};
`;
await this.businessService.updateById(businessId, {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updating has to go through the BusinessService to sync the new data with the Unified API.

:wq
Merge branch 'dev' of github.com:ballerine-io/ballerine into bal-3400
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: 1

♻️ Duplicate comments (2)
services/workflows-service/src/business/business.service.ts (2)

30-30: 🛠️ Refactor suggestion

Inject UnifiedApiClient via the constructor for better testability.

Currently, UnifiedApiClient is instantiated directly within the class. It's recommended to inject it via the constructor to adhere to the dependency inversion principle and improve testability by allowing for easier mocking.


41-75: 🛠️ Refactor suggestion

Move HTTP calls outside the transaction.

Making external HTTP calls within a transaction can lead to prolonged transaction times and potential deadlocks. Move the HTTP calls outside the transaction.

Also applies to: 114-148

🧹 Nitpick comments (2)
services/workflows-service/src/business/business.controller.internal.ts (1)

54-87: Consider adding error handling and pagination.

While the implementation is correct, consider these improvements:

  1. Add error handling for potential database errors
  2. Add pagination to handle large datasets efficiently
  3. Add logging for monitoring and debugging

Apply this diff:

  @common.Get('sync')
  @common.UseGuards(AdminAuthGuard)
  @ApiExcludeEndpoint()
  async getAllBusinesses() {
+   this.logger.log('Starting business sync');
+   try {
      const businesses = (await this.repository.findManyUnscoped({
+       take: 100, // Add pagination
        select: {
          id: true,
          createdAt: true,
          updatedAt: true,
          correlationId: true,
          companyName: true,
          metadata: true,
          project: {
            select: {
              customer: { select: { id: true, config: true } },
            },
          },
        },
        where: {
          project: {
            customer: {
              config: {
                path: ['isMerchantMonitoringEnabled'],
                equals: true,
              },
            },
          },
        },
      })) as BusinessPayload[];

      const unifiedApiClient = new UnifiedApiClient();

+     this.logger.log(`Syncing ${businesses.length} businesses`);
      return businesses.map(business => unifiedApiClient.formatBusiness(business));
+   } catch (error) {
+     this.logger.error('Failed to sync businesses', error);
+     throw new common.InternalServerErrorException('Failed to sync businesses');
+   }
  }
services/workflows-service/src/common/utils/unified-api-client/unified-api-client.ts (1)

96-118: Consider adding input validation.

The formatBusiness method should validate the input payload to ensure all required fields are present and have the correct types.

Apply this diff:

  public formatBusiness(business: BusinessPayload) {
+   if (!business?.id || !business?.project?.customer?.id) {
+     throw new Error('Invalid business payload: missing required fields');
+   }

    const metadata = business.metadata as unknown as {
      featureConfig: TCustomerWithFeatures['features'];
      lastOngoingReportInvokedAt: number;
    } | null;

    const unsubscribedMonitoringAt = metadata?.featureConfig?.[FEATURE_LIST.ONGOING_MERCHANT_REPORT]
      ?.disabledAt
      ? new Date(metadata.featureConfig[FEATURE_LIST.ONGOING_MERCHANT_REPORT]!.disabledAt!)
      : metadata?.featureConfig?.[FEATURE_LIST.ONGOING_MERCHANT_REPORT]?.enabled === false
      ? new Date()
      : null;

    return {
      id: business.id,
      correlationId: business.correlationId,
      companyName: business.companyName,
      customerId: business.project.customer.id,
      unsubscribedMonitoringAt: unsubscribedMonitoringAt?.toISOString() ?? null,
      createdAt: business.createdAt.toISOString(),
      updatedAt: business.updatedAt.toISOString(),
    };
  }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7c14122 and a73d796.

⛔ Files ignored due to path filters (1)
  • apps/backoffice-v2/src/pages/MerchantMonitoringUploadMultiple/hooks/useMerchantMonitoringUploadMultiplePageLogic/batch-report-template.csv is excluded by !**/*.csv
📒 Files selected for processing (5)
  • services/workflows-service/prisma/data-migrations (1 hunks)
  • services/workflows-service/src/business/business.controller.external.ts (1 hunks)
  • services/workflows-service/src/business/business.controller.internal.ts (2 hunks)
  • services/workflows-service/src/business/business.service.ts (3 hunks)
  • services/workflows-service/src/common/utils/unified-api-client/unified-api-client.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • services/workflows-service/prisma/data-migrations
  • services/workflows-service/src/business/business.controller.external.ts
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: test_windows
  • GitHub Check: build (windows-latest)
  • GitHub Check: test_linux
  • GitHub Check: build (ubuntu-latest)
  • GitHub Check: lint
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (3)
services/workflows-service/src/business/business.controller.internal.ts (1)

28-32: LGTM! Dependencies are properly injected.

The constructor properly injects the required dependencies, following NestJS best practices.

services/workflows-service/src/common/utils/unified-api-client/unified-api-client.ts (1)

9-14: LGTM! Type definition is well-structured.

The BusinessPayload type properly defines the required fields and nested customer configuration.

services/workflows-service/src/business/business.service.ts (1)

290-299: LGTM! Retry mechanism is well implemented.

The retry mechanism is properly implemented with:

  • Reasonable retry count (5 attempts)
  • Randomized delays
  • Proper timeout bounds (100ms - 10s)

Note: The dynamic import of p-retry is necessary due to ESM/CJS compatibility, as explained in the previous review comments.

Comment on lines +83 to +94
public async createOrUpdateBusiness(payload: BusinessPayload) {
if (!this.shouldUpdateBusiness(payload)) {
return;
}

const formattedPayload = this.formatBusiness(payload);

return await this.axiosInstance.put(
`/customers/${payload.project.customer.id}/merchants/${payload.id}`,
formattedPayload,
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for API calls.

The createOrUpdateBusiness method should handle potential API errors and provide proper error context.

Apply this diff:

  public async createOrUpdateBusiness(payload: BusinessPayload) {
    if (!this.shouldUpdateBusiness(payload)) {
      return;
    }

    const formattedPayload = this.formatBusiness(payload);

+   try {
      return await this.axiosInstance.put(
        `/customers/${payload.project.customer.id}/merchants/${payload.id}`,
        formattedPayload,
      );
+   } catch (error) {
+     this.logger.error('Failed to create/update business in Unified API', {
+       error,
+       businessId: payload.id,
+       customerId: payload.project.customer.id,
+     });
+     throw error;
+   }
  }

Committable suggestion skipped: line range outside the PR's diff.

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.

2 participants