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

Implement entity persistence for project creation #1326

Open
wants to merge 36 commits into
base: console
Choose a base branch
from

Conversation

ashish-egov
Copy link
Collaborator

@ashish-egov ashish-egov commented Jan 6, 2025

Summary by CodeRabbit

Release Notes

  • New Features

    • Added campaign project management functionality.
    • Introduced new process tracking and status management for campaigns.
    • Enhanced boundary and project relationship handling.
    • Implemented new Kafka message processing for campaign workflows.
    • Added new asynchronous functions for retrieving and managing campaign projects and processes.
  • Database Changes

    • Created new database tables for campaign projects and process tracking.
    • Added indexes to optimize query performance for campaign-related data.
  • API Improvements

    • Updated project creation and management APIs.
    • Added new endpoints for campaign process handling.
    • Improved boundary and target data retrieval mechanisms.
  • Performance Enhancements

    • Optimized database queries with new indexing strategies.
    • Implemented more efficient boundary and project data processing.
  • Bug Fixes

    • Refined error handling in campaign and project creation processes.
    • Improved data validation and consistency checks.

Copy link
Contributor

coderabbitai bot commented Jan 6, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This pull request introduces comprehensive changes to the project-factory service, focusing on enhancing campaign project management. The modifications include creating new database tables for tracking campaign projects and process statuses, adding utility functions for handling campaign-related operations, and updating various API and service components to support more robust project creation and tracking mechanisms. The changes span across database migrations, API endpoints, Kafka message handling, and utility functions, providing a more structured approach to managing campaign projects.

Changes

File Path Change Summary
migration/main/V20250106120000__create_campaign_project.sql Created eg_cm_campaign_projects table with comprehensive columns and indexes.
migration/main/V20250113154600__create_campaign_process_table.sql Created eg_cm_campaign_creation_process_status table for tracking campaign process statuses.
migration/main/V20250113163400__create_idx_campaignnumber.sql Added index on campaignnumber in eg_cm_campaign_details table.
src/server/api/boundaryApis.ts Added getBoundaryRelationshipData function for boundary relationship searches.
src/server/api/campaignApis.ts Updated project creation and update functions with new parameter handling.
src/server/config/constants.ts Added new constants for process names and campaign process statuses.
src/server/config/index.ts Updated Kafka topic configurations and added new database table names.
src/server/controllers/index.ts Added mainProcessController to the controllers array.
src/server/controllers/mainProcessController/mainProcess.controller.ts Introduced mainProcessController class for handling project creation requests.
src/server/kafka/Listener.ts Updated Kafka listener to handle new topics and associated processing functions.
src/server/service/mainProcessService.ts Added processProjectCreation function to manage project creation processes.
src/server/utils/boundaryUtils.ts Introduced functions for retrieving boundary data related to campaigns.
src/server/utils/campaignMappingUtils.ts Changed getAllBoundaries to be an exported function.
src/server/utils/campaignUtils.ts Removed obsolete functions and added new ones for campaign processing.
src/server/utils/excelUtils.ts Added getLocaleFromCampaignFiles function for retrieving locale data from campaign files.
src/server/utils/generateUtils.ts Updated data source for boundaries in callGenerateIfBoundariesOrCampaignTypeDiffer.
src/server/utils/genericUtils.ts Removed getBoundaryRelationshipData and added getLocalizedMessagesHandlerViaLocale.
src/server/utils/logger/index.ts Modified error logging levels in getFormattedStringForDebug.
src/server/utils/onGoingCampaignUpdateUtils.ts Updated boundary handling and project fetching logic.
src/server/utils/processTrackUtils.ts Added functions for managing campaign process statuses.
src/server/utils/projectCampaignUtils.ts Introduced multiple utility functions for managing campaign projects.
src/server/utils/targetUtils.ts Added functions for handling campaign targets.
src/server/utils/transforms/projectTypeUtils.ts Streamlined return structure in enrichProjectDetailsFromCampaignDetails.
src/server/validators/campaignValidators.ts Updated boundary validation logic to reference new structure.
src/server/validators/genericValidator.ts Modified imports and function calls related to boundary data retrieval.

Sequence Diagram

sequenceDiagram
    participant Client
    participant MainProcessController
    participant CampaignService
    participant ProjectCampaignUtils
    participant KafkaProducer
    participant Database

    Client->>MainProcessController: Start Project Creation
    MainProcessController->>CampaignService: Process Project Creation
    CampaignService->>ProjectCampaignUtils: Persist Campaign Project
    ProjectCampaignUtils->>KafkaProducer: Produce Save Campaign Project Message
    ProjectCampaignUtils->>Database: Save Campaign Project
    Database-->>ProjectCampaignUtils: Project Saved
    ProjectCampaignUtils-->>CampaignService: Project Creation Confirmed
    CampaignService-->>MainProcessController: Process Completed
    MainProcessController-->>Client: Project Creation Response
Loading

Possibly related PRs

Suggested reviewers

  • jagankumar-egov

Poem

🐰 Hop, hop, through code's green field,
Campaign projects now revealed!
Tables dance, indexes shine bright,
Tracking progress with delight 🚀
A rabbit's code, precise and neat! 🌟


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.

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.

@ashish-egov
Copy link
Collaborator Author

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jan 17, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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

🔭 Outside diff range comments (3)
health-services/project-factory/src/server/utils/transforms/projectTypeUtils.ts (1)

Line range hint 1-118: Consider splitting utilities into separate modules.

The file currently handles multiple concerns:

  1. Project type configuration
  2. Data transformation
  3. Validation logic
  4. Type definitions

Consider splitting these into separate modules for better maintainability and separation of concerns:

  • types.ts for type definitions
  • config.ts for configuration
  • transformers.ts for transformation logic
  • validators.ts for validation logic

Also, clean up the commented-out code as it adds noise to the codebase.

Would you like me to help restructure the code into separate modules?

🧰 Tools
🪛 Biome (1.9.4)

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

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 98-98: Use let or const instead of var.

A variable declared with var is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared.
See MDN web docs for more details.
Unsafe fix: Use 'const' instead.

(lint/style/noVar)

health-services/project-factory/src/server/utils/onGoingCampaignUpdateUtils.ts (1)

Line range hint 1769-1770: Replace var declarations with let/const.

Using var can lead to scoping issues. Use let or const instead.

Apply this diff:

-  var projectFound = false;
-  var retry = 20;
+  let projectFound = false;
+  let retry = 20;
health-services/project-factory/src/server/utils/generateUtils.ts (1)

Line range hint 32-67: Improve type safety and error handling.

  1. Consider defining an interface for the request object structure to improve type safety.
  2. Extract the magic number timeout (2000ms) to a named constant.
  3. Consider breaking down the complex generate logic into smaller, focused functions.

Example refactor:

interface CampaignRequest {
  body: {
    RequestInfo: any;
    CampaignDetails: {
      boundaries: any[];
      tenantId: string;
      hierarchyType: string;
      id: string;
    };
    ExistingCampaignDetails?: {
      boundaries: any[];
      projectType: string;
    };
  };
  query: any;
}

const RESOURCE_GENERATION_DELAY_MS = 2000;

async function generateResources(request: CampaignRequest, boundaries: any[]) {
  const baseParams = {
    tenantId: request.body.CampaignDetails.tenantId,
    forceUpdate: 'true',
    hierarchyType: request.body.CampaignDetails.hierarchyType,
    campaignId: request.body.CampaignDetails.id
  };

  await Promise.all([
    generateBoundaryResources(request, boundaries, baseParams),
    generateFacilityResources(request, boundaries, baseParams),
    generateUserResources(request, boundaries, baseParams)
  ]);
}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 396673c and f7ab7bc.

📒 Files selected for processing (27)
  • health-services/project-factory/migration/main/V20250106120000__create_campaign_project.sql (1 hunks)
  • health-services/project-factory/migration/main/V20250113154600__create_campaign_process_table.sql (1 hunks)
  • health-services/project-factory/migration/main/V20250113163400__create_idx_campaignnumber.sql (1 hunks)
  • health-services/project-factory/src/server/api/boundaryApis.ts (1 hunks)
  • health-services/project-factory/src/server/api/campaignApis.ts (6 hunks)
  • health-services/project-factory/src/server/api/genericApis.ts (4 hunks)
  • health-services/project-factory/src/server/api/projectApis.ts (1 hunks)
  • health-services/project-factory/src/server/config/constants.ts (1 hunks)
  • health-services/project-factory/src/server/config/index.ts (2 hunks)
  • health-services/project-factory/src/server/controllers/index.ts (1 hunks)
  • health-services/project-factory/src/server/controllers/mainProcessController/mainProcess.controller.ts (1 hunks)
  • health-services/project-factory/src/server/kafka/Listener.ts (3 hunks)
  • health-services/project-factory/src/server/service/mainProcessService.ts (1 hunks)
  • health-services/project-factory/src/server/utils/boundaryUtils.ts (2 hunks)
  • health-services/project-factory/src/server/utils/campaignMappingUtils.ts (1 hunks)
  • health-services/project-factory/src/server/utils/campaignUtils.ts (20 hunks)
  • health-services/project-factory/src/server/utils/excelUtils.ts (2 hunks)
  • health-services/project-factory/src/server/utils/generateUtils.ts (1 hunks)
  • health-services/project-factory/src/server/utils/genericUtils.ts (2 hunks)
  • health-services/project-factory/src/server/utils/logger/index.ts (1 hunks)
  • health-services/project-factory/src/server/utils/onGoingCampaignUpdateUtils.ts (5 hunks)
  • health-services/project-factory/src/server/utils/processTrackUtils.ts (2 hunks)
  • health-services/project-factory/src/server/utils/projectCampaignUtils.ts (1 hunks)
  • health-services/project-factory/src/server/utils/targetUtils.ts (3 hunks)
  • health-services/project-factory/src/server/utils/transforms/projectTypeUtils.ts (2 hunks)
  • health-services/project-factory/src/server/validators/campaignValidators.ts (2 hunks)
  • health-services/project-factory/src/server/validators/genericValidator.ts (2 hunks)
🧰 Additional context used
📓 Learnings (2)
health-services/project-factory/src/server/utils/campaignMappingUtils.ts (1)
Learnt from: nitish-egov
PR: egovernments/health-campaign-services#1082
File: health-services/project-factory/src/server/utils/campaignMappingUtils.ts:640-646
Timestamp: 2024-12-04T12:30:22.889Z
Learning: In `health-services/project-factory/src/server/utils/campaignMappingUtils.ts`, the `processMapping` function already includes validation for `mappingObject.CampaignDetails.campaignDetails.boundaries` before accessing it, so additional null checks are unnecessary.
health-services/project-factory/src/server/validators/campaignValidators.ts (1)
Learnt from: nitish-egov
PR: egovernments/health-campaign-services#1082
File: health-services/project-factory/src/server/utils/campaignMappingUtils.ts:640-646
Timestamp: 2024-12-04T12:30:22.889Z
Learning: In `health-services/project-factory/src/server/utils/campaignMappingUtils.ts`, the `processMapping` function already includes validation for `mappingObject.CampaignDetails.campaignDetails.boundaries` before accessing it, so additional null checks are unnecessary.
🪛 Biome (1.9.4)
health-services/project-factory/src/server/utils/transforms/projectTypeUtils.ts

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

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 98-98: Use let or const instead of var.

A variable declared with var is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared.
See MDN web docs for more details.
Unsafe fix: Use 'const' instead.

(lint/style/noVar)

health-services/project-factory/src/server/utils/boundaryUtils.ts

[error] 77-80: Use let or const instead of var.

A variable declared with var is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared.
See MDN web docs for more details.
Unsafe fix: Use 'const' instead.

(lint/style/noVar)

health-services/project-factory/src/server/utils/onGoingCampaignUpdateUtils.ts

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

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

health-services/project-factory/src/server/utils/targetUtils.ts

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

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

health-services/project-factory/src/server/utils/processTrackUtils.ts

[error] 311-311: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)


[error] 313-313: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)

health-services/project-factory/src/server/api/campaignApis.ts

[error] 1769-1769: Use let or const instead of var.

A variable declared with var is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared.
See MDN web docs for more details.
Unsafe fix: Use 'let' instead.

(lint/style/noVar)


[error] 1770-1770: Use let or const instead of var.

A variable declared with var is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared.
See MDN web docs for more details.
Unsafe fix: Use 'let' instead.

(lint/style/noVar)

health-services/project-factory/src/server/utils/campaignUtils.ts

[error] 993-993: This type annotation is trivially inferred from its initialization.

Safe fix: Remove the type annotation.

(lint/style/noInferrableTypes)


[error] 1049-1049: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


[error] 1054-1054: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


[error] 1081-1081: Use let or const instead of var.

A variable declared with var is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared.
See MDN web docs for more details.
Unsafe fix: Use 'const' instead.

(lint/style/noVar)


[error] 1186-1186: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


[error] 1181-1184: Use let or const instead of var.

A variable declared with var is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared.
See MDN web docs for more details.
Unsafe fix: Use 'const' instead.

(lint/style/noVar)


[error] 1209-1209: Use let or const instead of var.

A variable declared with var is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared.
See MDN web docs for more details.
Unsafe fix: Use 'const' instead.

(lint/style/noVar)


[error] 1237-1237: This type annotation is trivially inferred from its initialization.

Safe fix: Remove the type annotation.

(lint/style/noInferrableTypes)


[error] 1257-1257: This type annotation is trivially inferred from its initialization.

Safe fix: Remove the type annotation.

(lint/style/noInferrableTypes)


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

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


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

'rowData' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)

health-services/project-factory/src/server/utils/projectCampaignUtils.ts

[error] 81-81: This type annotation is trivially inferred from its initialization.

Safe fix: Remove the type annotation.

(lint/style/noInferrableTypes)


[error] 296-296: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


[error] 318-318: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


[error] 110-110: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)


[error] 124-124: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)


[error] 125-125: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)

🔇 Additional comments (28)
health-services/project-factory/src/server/utils/transforms/projectTypeUtils.ts (1)

Line range hint 6-8: Address TODO comments for critical functionality.

There are multiple TODO comments indicating incomplete implementations:

  1. Configuration object needs appropriate values
  2. Project type master fetching logic is missing

These implementations are crucial for the proper functioning of the project type system.

Would you like me to help implement these missing functionalities or create GitHub issues to track them?

Also applies to: 11-11

health-services/project-factory/src/server/utils/targetUtils.ts (1)

108-108: Verify boundary access path change.

The change in accessing boundaries (CampaignDetails.boundaries instead of boundariesCombined) might affect other parts of the codebase.

✅ Verification successful

Boundary access change is consistent with codebase patterns

The change from boundariesCombined to request?.body?.CampaignDetails?.boundaries follows the established pattern used consistently throughout the codebase. No instances of the old pattern remain, confirming a complete migration.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other usages of boundariesCombined to ensure consistent updates
rg "boundariesCombined" --type ts

Length of output: 33


Script:

#!/bin/bash
# Search for various boundary access patterns in request bodies
rg "request\?\.body.*boundaries?" --type ts

# Search for CampaignDetails structure usage
rg "CampaignDetails.*boundaries?" --type ts

# Search for any boundary-related properties in request bodies
ast-grep --pattern 'request?.body.$_boundaries'

Length of output: 5617

health-services/project-factory/src/server/api/genericApis.ts (4)

12-12: Imported getBoundaryRelationshipData correctly.

The function getBoundaryRelationshipData is now imported from ./boundaryApis, which is appropriate if the function has been moved to a new module.


223-226: Confirm handling when "${codeColumnName}" is not found in sheet.

When the column codeColumnName is not found in the sheet, the code logs a warning and continues processing. Please confirm that skipping sheets without this column is the intended behavior and that downstream logic accounts for potentially missing data from these sheets.


193-193: Verify all calls to getTargetSheetDataAfterCode are updated with new parameter.

The parameter of getTargetSheetDataAfterCode has changed from request to campaignDetails. Please ensure all calls to this function are updated accordingly to pass campaignDetails to prevent runtime errors.

To verify that all function calls have been updated, you can run the following script:

✅ Verification successful

All calls to getTargetSheetDataAfterCode are properly updated

The only call to this function exists in campaignUtils.ts and is already using the correct CampaignDetails parameter. No instances of the old request parameter were found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find calls to `getTargetSheetDataAfterCode` that may still use `request` as the first argument.

# Search for function calls where the first argument is `request`
ast-grep --pattern 'getTargetSheetDataAfterCode(request, $$$)' --lang ts

Length of output: 74


Script:

#!/bin/bash
# Search for all calls to getTargetSheetDataAfterCode with any arguments
ast-grep --pattern 'getTargetSheetDataAfterCode($_)' --lang ts

# Backup search using ripgrep to find any mentions of the function
rg "getTargetSheetDataAfterCode" -A 1

Length of output: 873


590-590: Ensure compatibility with getBoundaryRelationshipData.

The getBoundarySheetData function now uses getBoundaryRelationshipData to retrieve boundary data. Verify that the params object passed to this function contains the correct properties and that the returned data aligns with the expected structure used in the subsequent logic.

If needed, you can check the usage of getBoundaryRelationshipData with the following script:

✅ Verification successful

✓ The getBoundaryRelationshipData integration is correctly implemented

The params object contains all required properties and the returned data structure is properly handled with appropriate null checks and fallback mechanisms.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `getBoundaryRelationshipData` is used correctly across the codebase.

# Search for calls to `getBoundaryRelationshipData` and display surrounding lines for context.
rg 'getBoundaryRelationshipData\(' -A 5 -B 2

Length of output: 4160


Script:

#!/bin/bash
# Get more context around the usage in genericApis.ts
ast-grep --pattern 'async function getBoundarySheetData($_) {
  $$$
  const boundaryData = await getBoundaryRelationshipData(params);
  $$$
}'

Length of output: 144


Script:

#!/bin/bash
# Get the full context of getBoundarySheetData function
rg -B 10 -A 10 'async function getBoundarySheetData'

Length of output: 1856


Script:

#!/bin/bash
# Get extended context after the getBoundaryRelationshipData call
rg -B 2 -A 15 'const boundaryData = await getBoundaryRelationshipData\(params\);' health-services/project-factory/src/server/api/genericApis.ts

Length of output: 804

health-services/project-factory/src/server/utils/processTrackUtils.ts (1)

324-335: Handle the case when no process details are found.

In the function markProcessStatus, if no processes are found for the given campaign number and process name, it's logged as an error. Depending on the application's requirements, you might want to handle this case more gracefully or take corrective action.

Please confirm whether additional handling is needed when no process is found. If this is expected behavior, consider adjusting the log level from error to warning or info.

health-services/project-factory/src/server/utils/campaignUtils.ts (1)

1963-1964: 🧹 Nitpick (assertive)

Prefer template literals over string concatenation

Using template literals improves code readability and maintainability. Replace string concatenation with a template literal.

Apply this diff:

-        throw new Error('Project Creation process did not complete after 75 attempts.');
+        throw new Error(`Project Creation process did not complete after 75 attempts.`);

Likely invalid or redundant comment.

🧰 Tools
🪛 Biome (1.9.4)

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

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

health-services/project-factory/src/server/controllers/index.ts (2)

4-4: Import statement is correct

The import of mainProcessController is properly added and follows the existing import structure.


10-11: Controllers array updated appropriately

The controllers array now includes an instance of mainProcessController, ensuring that the new controller is registered.

health-services/project-factory/src/server/api/boundaryApis.ts (1)

1-16: New function getBoundaryRelationshipData implemented correctly

The getBoundaryRelationshipData function is well-defined and integrates correctly with the boundary APIs. The use of optional chaining and default headers is appropriate.

health-services/project-factory/src/server/utils/logger/index.ts (1)

35-37: LGTM! Good error handling improvement.

The change from error to warn level is appropriate for these scenarios as they are recoverable conditions.

health-services/project-factory/src/server/config/constants.ts (1)

161-170: LGTM! Well-structured constants.

The new constants follow the existing pattern and provide clear status definitions for campaign processes.

health-services/project-factory/src/server/utils/excelUtils.ts (1)

10-10: LGTM!

The import statement is correctly added and follows the project's import organization.

health-services/project-factory/src/server/utils/onGoingCampaignUpdateUtils.ts (2)

13-13: LGTM!

The import statement is correctly added and follows the project's import organization.


271-271: LGTM!

The modification correctly updates the boundaries by combining parent and new boundaries.

health-services/project-factory/src/server/utils/campaignMappingUtils.ts (1)

60-60: LGTM!

The function is correctly exported to support reusability across modules.

health-services/project-factory/src/server/validators/campaignValidators.ts (1)

1049-1049: LGTM! Consistent with campaign details structure.

The update to use CampaignDetails.boundaries aligns with the standardized way of accessing campaign data.

health-services/project-factory/migration/main/V20250113163400__create_idx_campaignnumber.sql (1)

1-2: LGTM! Index will improve query performance.

The index on campaignnumber is properly defined with idempotent creation using IF NOT EXISTS.

health-services/project-factory/migration/main/V20250113154600__create_campaign_process_table.sql (3)

1-12: LGTM! Well-structured table definition with proper constraints.

The table definition includes:

  • Primary key on id
  • Required fields marked as NOT NULL
  • JSONB for flexible additional details
  • Audit fields for tracking changes
  • Unique constraint to prevent duplicates

14-16: LGTM! Index will improve campaign number lookups.

The single-column index on campaignnumber will optimize queries filtering by campaign number.


18-20: LGTM! Composite index supports unique constraint.

The composite index on (campaignnumber, processname) will:

  • Support the unique constraint
  • Optimize queries filtering by both columns
health-services/project-factory/migration/main/V20250106120000__create_campaign_project.sql (1)

1-15: 🧹 Nitpick (assertive)

🛠️ Refactor suggestion

Consider adding foreign key constraint and index for projectid.

The projectid column appears to reference another table but lacks a foreign key constraint and index, which could lead to data integrity issues and suboptimal query performance.

Reconsider unique constraint design.

Including isactive in the unique constraint (unique_campaign_boundary_active) might cause issues when trying to soft-delete and recreate records with the same campaign number and boundary code.

Consider using TIMESTAMP type for temporal columns.

Using BIGINT for createdtime and lastmodifiedtime instead of TIMESTAMP WITH TIME ZONE:

  1. Makes it harder to perform date/time operations
  2. Doesn't enforce valid timestamp values
  3. Doesn't handle timezone information properly

Document JSONB structure.

Add comments describing the expected structure and schema of the additionaldetails JSONB column to help maintain data consistency.

health-services/project-factory/src/server/utils/generateUtils.ts (1)

32-32: Verify the impact of boundaries path change.

The path to boundaries has changed from boundariesCombined to CampaignDetails.boundaries. Ensure all consumers of this API are updated to use the new path.

Run this script to find potential usages that need updating:

health-services/project-factory/src/server/api/campaignApis.ts (4)

32-32: LGTM! New imports are correctly added.

The new imports are properly added and used in the modified functions.

Also applies to: 53-54


1751-1754: LGTM! Function signature and null check improvements.

The function signature is improved with separate parameters and early return for null/undefined projectId.


1829-1829: LGTM! Added persistence for campaign project.

The function now correctly persists campaign project data after successful creation.


Line range hint 1841-1856: LGTM! Enhanced project update functionality.

The function improvements include:

  • Added campaign project persistence
  • Better error handling with detailed logging
  • Type-safe parameter for campaign project ID
🧰 Tools
🪛 Biome (1.9.4)

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

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

@ashish-egov
Copy link
Collaborator Author

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jan 21, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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

♻️ Duplicate comments (9)
health-services/project-factory/src/server/validators/genericValidator.ts (1)

224-224: ⚠️ Potential issue

Add error handling for boundary data retrieval.

The function call has been updated to only pass params, but it still lacks proper error handling which could lead to unhandled exceptions.

Apply this diff to improve error handling:

-    const boundaryData = await getBoundaryRelationshipData(params);
+    let boundaryData;
+    try {
+        boundaryData = await getBoundaryRelationshipData(params);
+    } catch (error) {
+        throwError("BOUNDARY", 500, "BOUNDARY_RELATIONSHIP_ERROR", "Error retrieving boundary relationship data");
+    }

Additionally, verify that the updated function signature in boundaryApis.ts properly handles all the required data that was previously available through the request parameter.

Run this script to check the implementation:

#!/bin/bash
# Description: Check if getBoundaryRelationshipData implementation in boundaryApis.ts handles all required data

# Search for the implementation
ast-grep --pattern 'export $_ getBoundaryRelationshipData($_) {
  $$$
}'

# Check for any other usages to ensure consistency
rg -A 5 'getBoundaryRelationshipData'
health-services/project-factory/src/server/kafka/Listener.ts (1)

32-33: 🛠️ Refactor suggestion

Consider adding message validation.

Add validation for the message structure before processing to prevent runtime errors.

health-services/project-factory/src/server/api/projectApis.ts (4)

6-34: 🛠️ Refactor suggestion

Specify Return Types and Improve Error Handling

The function getProjectsWithProjectIds lacks explicit return type annotations and proper error handling for the httpRequest call. Additionally, the code after throwError is unreachable.

Apply this diff to enhance type safety and remove unreachable code:

-export async function getProjectsWithProjectIds(projectIds: string[], tenantId: string, userUuid: string) {
+export async function getProjectsWithProjectIds(projectIds: string[], tenantId: string, userUuid: string): Promise<any[]> {
     // ...existing code...
     const response = await httpRequest(url, requestBody, params);
+    if (!response) {
+        throwError("PROJECT", 500, "PROJECT_SEARCH_ERROR", "No response from project search");
+    }
     if (Array.isArray(response?.Project)) {
         return response?.Project;
     } else {
         throwError("PROJECT", 500, "PROJECT_SEARCH_ERROR", "Error occurred during project search");
-        return [];
     }
 }

36-64: ⚠️ Potential issue

Fix Return Type Inconsistency and Remove Unreachable Code

The function getProjectsCountsWithProjectIds should consistently return a number representing the count. Currently, it returns response?.TotalCount or an empty array, which could lead to type inconsistencies.

Apply this diff to fix the return type and remove unreachable code:

-export async function getProjectsCountsWithProjectIds(projectIds: string[], tenantId: string, userUuid: string) {
+export async function getProjectsCountsWithProjectIds(projectIds: string[], tenantId: string, userUuid: string): Promise<number> {
     // ...existing code...
     if (Array.isArray(response?.Project)) {
         return response?.TotalCount || 0;
     } else {
         throwError("PROJECT", 500, "PROJECT_SEARCH_ERROR", "Error occurred during project search");
-        return [];
     }
 }

66-80: 🛠️ Refactor suggestion

Add Error Handling and Specify Types

The function updateProjects lacks error handling for the httpRequest call and uses generic any types.

Enhance the function with error handling and type definitions:

-export async function updateProjects(projects: any[], userUuid: string) {
+export async function updateProjects(projects: Project[], userUuid: string): Promise<void> {
     // ...existing code...
     try {
         await httpRequest(url, requestBody);
     } catch (error) {
         throwError("PROJECT", 500, "PROJECT_UPDATE_ERROR", "Error occurred during project update");
     }
 }

Also, define the Project interface for better type safety.


82-103: 🛠️ Refactor suggestion

Ensure Consistent Error Handling and Return Types

The function createProjectsAndGetCreatedProjects should include error handling for the httpRequest and specify return types.

Modify the function as follows:

-export async function createProjectsAndGetCreatedProjects(projects: any[], userUuid: string) {
+export async function createProjectsAndGetCreatedProjects(projects: Project[], userUuid: string): Promise<Project[]> {
     // ...existing code...
     const response = await httpRequest(url, requestBody);
     if (Array.isArray(response?.Project)) {
         return response?.Project;
     } else {
         throwError("PROJECT", 500, "PROJECT_CREATION_ERROR", "Error occurred during project creation");
-        return [];
     }
 }

This ensures that the function consistently returns an array of Project objects.

health-services/project-factory/src/server/utils/projectCampaignUtils.ts (2)

110-110: 🧹 Nitpick (assertive)

Use Number.parseInt Instead of the Global parseInt

For consistency with ES2015 standards, replace parseInt with Number.parseInt.

Apply these diffs to update the code:

At line 110:

-                return parseInt(queryResponse.rows[0].count, 10);
+                return Number.parseInt(queryResponse.rows[0].count, 10);

At lines 124-125:

-                    createdTime: parseInt(row.createdtime),
-                    lastModifiedTime: parseInt(row.lastmodifiedtime)
+                    createdTime: Number.parseInt(row.createdtime, 10),
+                    lastModifiedTime: Number.parseInt(row.lastmodifiedtime, 10)

Also applies to: 124-125

🧰 Tools
🪛 Biome (1.9.4)

[error] 110-110: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)


296-296: 🧹 Nitpick (assertive)

Avoid Using the delete Operator for Better Performance

Using the delete operator can impact performance due to changes in object shapes. Consider setting the property to undefined instead.

Apply this diff to modify the code:

At line 296:

-    delete campaignProject.additionalDetails.newTargets;
+    campaignProject.additionalDetails.newTargets = undefined;

At line 318:

-    delete boundaryCodesForTargetUpdateMappings[boundaryCode].targets;
+    boundaryCodesForTargetUpdateMappings[boundaryCode].targets = undefined;

Also applies to: 318-318

🧰 Tools
🪛 Biome (1.9.4)

[error] 296-296: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

health-services/project-factory/src/server/service/mainProcessService.ts (1)

11-27: 🛠️ Refactor suggestion

Replace console.log with Proper Logging

Using console.log is not recommended for logging errors in a production environment. Replace it with a proper logging utility like logger.error for better error tracking and consistency.

Apply this diff to update the error handling:

     } catch (error: any) {
-        console.log(error);
+        logger.error("Error in processProjectCreation:", error);
         await markProcessStatus(campaignDetailsAndRequestInfo?.CampaignDetails?.campaignNumber, processNamesConstantsInOrder.projectCreation, campaignProcessStatus.failed, error?.message);
     }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f7ab7bc and e4cbbc4.

📒 Files selected for processing (9)
  • health-services/project-factory/src/server/api/genericApis.ts (7 hunks)
  • health-services/project-factory/src/server/api/projectApis.ts (1 hunks)
  • health-services/project-factory/src/server/kafka/Listener.ts (3 hunks)
  • health-services/project-factory/src/server/service/mainProcessService.ts (1 hunks)
  • health-services/project-factory/src/server/utils/campaignMappingUtils.ts (2 hunks)
  • health-services/project-factory/src/server/utils/genericUtils.ts (2 hunks)
  • health-services/project-factory/src/server/utils/projectCampaignUtils.ts (1 hunks)
  • health-services/project-factory/src/server/utils/targetUtils.ts (3 hunks)
  • health-services/project-factory/src/server/validators/genericValidator.ts (2 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
health-services/project-factory/src/server/utils/targetUtils.ts

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

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

health-services/project-factory/src/server/utils/projectCampaignUtils.ts

[error] 81-81: This type annotation is trivially inferred from its initialization.

Safe fix: Remove the type annotation.

(lint/style/noInferrableTypes)


[error] 296-296: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


[error] 318-318: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


[error] 110-110: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)


[error] 124-124: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)


[error] 125-125: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)

🔇 Additional comments (13)
health-services/project-factory/src/server/validators/genericValidator.ts (1)

13-13: LGTM! Good code organization.

Moving the getBoundaryRelationshipData function to boundaryApis improves code organization by properly separating API-related functions into a dedicated module.

health-services/project-factory/src/server/utils/campaignMappingUtils.ts (2)

60-60: LGTM! Function export change looks good.

The getAllBoundaries function is now exported, making it accessible to other modules. The function's implementation is robust with proper error handling and data sanitization.


685-688: Verify the impact of removing boundary merging.

The commented-out code suggests that boundary merging from parent campaigns has been removed. Please ensure this change:

  1. Is intentional and aligns with the new requirements
  2. Won't break existing functionality that might depend on merged boundaries
  3. Has been properly tested with parent-child campaign scenarios

Run this script to check for potential impacts:

✅ Verification successful

Boundary merging removal is safe - functionality exists elsewhere with better validation

The removal of boundary merging code is intentional and safe because:

  • Boundary merging is properly handled in onGoingCampaignUpdateUtils.ts with explicit validation to prevent duplicate boundaries
  • The system maintains parent-child boundary relationships through getBoundariesFromCampaignSearchResponse
  • The removed code was redundant and less robust than current implementations
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for dependencies on boundary merging functionality
# Look for references to boundary merging in the codebase

# Search for boundary-related function calls
echo "Searching for boundary-related function calls..."
rg -l "boundaries.*parent" 

# Search for tests related to parent campaign boundaries
echo "Searching for related test cases..."
rg -l "test.*parent.*campaign.*boundar" 

Length of output: 459


Script:

#!/bin/bash
# Check actual implementations and usages

# Check boundary usage in campaign utils
echo "=== Campaign Utils Implementation ==="
rg -A 5 -B 5 "boundaries.*parent|parent.*boundaries" health-services/project-factory/src/server/utils/campaignUtils.ts

echo -e "\n=== Ongoing Campaign Update Utils Implementation ==="
rg -A 5 -B 5 "boundaries.*parent|parent.*boundaries" health-services/project-factory/src/server/utils/onGoingCampaignUpdateUtils.ts

# Look for any boundary-related tests with different patterns
echo -e "\n=== Boundary-related Tests ==="
rg -l "test.*boundar|spec.*boundar"

# Check the API expectations in Postman collection
echo -e "\n=== Postman Collection Boundary Usage ==="
rg -A 5 -B 5 "boundaries.*parent|parent.*boundaries" health-services/project-factory/postman_collection.json

Length of output: 18489

health-services/project-factory/src/server/kafka/Listener.ts (4)

6-6: LGTM! Import statement follows project conventions.

The new import statement correctly imports the campaign processing handlers from the utils directory, maintaining consistency with the project's structure.


32-33: LGTM! Improved logging placement.

Moving the logging statements before the switch provides better visibility into all incoming messages, regardless of their topic.


20-21: Verify topic configuration in config file.

Ensure that the new Kafka topics are properly defined in the config file.

✅ Verification successful

Kafka topics are properly configured

Both KAFKA_PROCESS_HANDLER_TOPIC and KAFKA_SUB_PROCESS_HANDLER_TOPIC are correctly defined in the config file with appropriate default values.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the new Kafka topics are defined in the config file
rg -A 2 "KAFKA_PROCESS_HANDLER_TOPIC|KAFKA_SUB_PROCESS_HANDLER_TOPIC" health-services/project-factory/src/server/config/

Length of output: 829


39-44: 🛠️ Refactor suggestion

Enhance error handling for new message processors.

While the async/await usage is correct, consider adding dedicated error boundaries for each handler to provide better error context and recovery options.

 case config.kafka.KAFKA_PROCESS_HANDLER_TOPIC:
-    await handleCampaignProcessing(messageObject);
+    try {
+        await handleCampaignProcessing(messageObject);
+    } catch (error) {
+        logger.error(`Error processing campaign: ${error.message}`, {
+            topic: message.topic,
+            error: error.stack
+        });
+        throw error; // Re-throw to trigger message retry
+    }
     break;
 case config.kafka.KAFKA_SUB_PROCESS_HANDLER_TOPIC:
-    await handleCampaignSubProcessing(messageObject);
+    try {
+        await handleCampaignSubProcessing(messageObject);
+    } catch (error) {
+        logger.error(`Error processing campaign sub-process: ${error.message}`, {
+            topic: message.topic,
+            error: error.stack
+        });
+        throw error; // Re-throw to trigger message retry
+    }
     break;

Likely invalid or redundant comment.

health-services/project-factory/src/server/utils/targetUtils.ts (4)

108-108: Verify the assignment of CampaignDetails.boundaries

The code changes the source of boundaries from request?.body?.boundariesCombined to request?.body?.CampaignDetails?.boundaries. Please verify that CampaignDetails.boundaries is correctly populated and that this change aligns with the updated data structure.


133-154: Add type definitions for parameters and return value

The function getTargetListForCampaign would benefit from explicit type definitions for its parameters and return value, enhancing type safety and code clarity.

🧰 Tools
🪛 Biome (1.9.4)

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

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


156-164: Add type definitions for better type safety

Adding explicit type definitions for campaignDetails and the return type in getTargetFileIdFromCampaignDetails will enhance code maintainability and reduce potential runtime errors.


146-147: 🧹 Nitpick (assertive)

Use template literals instead of string concatenation

Template literals improve readability and maintainability by embedding expressions within strings.

Apply this diff to refactor the code:

-                "codesTargetMapping mapping :: " +
-                getFormattedStringForDebug(targetData)
+                `codesTargetMapping mapping :: ${getFormattedStringForDebug(targetData)}`

Likely invalid or redundant comment.

🧰 Tools
🪛 Biome (1.9.4)

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

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

health-services/project-factory/src/server/api/genericApis.ts (1)

223-225: Verify the handling of missing code column in sheet data.

If the column specified by codeColumnName ("Boundary Code") is not found in the sheet, the current implementation logs a warning and continues processing. Should this scenario be treated as an error instead? Continuing without the required column may lead to incorrect or incomplete data processing downstream.

health-services/project-factory/src/server/utils/projectCampaignUtils.ts (1)

81-81: 🧹 Nitpick (assertive)

Remove Unnecessary Type Annotation for fetchCountOnly

The type of fetchCountOnly can be inferred from its default value. It's redundant to declare it explicitly.

Apply this diff to simplify the code:

-    fetchCountOnly: boolean = false
+    fetchCountOnly = false

Likely invalid or redundant comment.

🧰 Tools
🪛 Biome (1.9.4)

[error] 81-81: This type annotation is trivially inferred from its initialization.

Safe fix: Remove the type annotation.

(lint/style/noInferrableTypes)

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.

1 participant