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

[Fix] Add Action By User To Base Entity #8811

Merged

Conversation

rahul-rocket
Copy link
Collaborator

@rahul-rocket rahul-rocket commented Feb 28, 2025

PR

Please note: we will close your PR without comment if you do not check the boxes above and provide ALL requested information.


Summary by CodeRabbit

  • New Features

    • Integrated enhanced record creation tracking by adding user action attributes to core data models.
  • Refactor

    • Removed redundant user tracking fields from dashboards, deals, teams, payments, tasks, and screening tasks.
    • Streamlined interface inheritance and simplified entity structures.
    • Updated naming conventions for migrations to ensure consistency and clarity.

Copy link
Contributor

coderabbitai bot commented Feb 28, 2025

Walkthrough

This update enhances the entity models by introducing a new interface for user action tracking and by removing legacy user creator properties. In the contracts package, a new IBaseEntityActionByUserModel is added and incorporated into IBaseEntityModel, while multiple interfaces such as Dashboard, Deal, Organization Team, and others have the IHasUserCreator dependency removed. In the core package, similar removals are made in entity classes with a new BaseEntityActionByUser class added for tracking user creation. Additionally, a migration class is renamed and minor formatting adjustments are applied.

Changes

File(s) Change Summary
packages/contracts/src/lib/base-entity.model.ts Added IBaseEntityActionByUserModel and updated IBaseEntityModel to extend both IBaseEntityActionByUserModel and IBaseSoftDeleteEntityModel.
packages/contracts/src/lib/dashboard.model.ts,
packages/contracts/src/lib/deal.model.ts,
packages/contracts/src/lib/organization-project-module.model.ts,
packages/contracts/src/lib/organization-team.model.ts,
packages/contracts/src/lib/payment.model.ts,
packages/contracts/src/lib/screening-task.model.ts,
packages/contracts/src/lib/task.model.ts
Removed IHasUserCreator from interface inheritance and updated signatures accordingly.
packages/contracts/src/lib/user.model.ts Removed the IHasUserCreator interface entirely.
packages/core/src/lib/core/entities/base.entity.ts Added BaseEntityActionByUser with createdByUser and createdByUserId properties; updated BaseEntity to extend it.
packages/core/src/lib/dashboard/dashboard.entity.ts,
packages/core/src/lib/deal/deal.entity.ts,
packages/core/src/lib/organization-project-module/organization-project-module.entity.ts,
packages/core/src/lib/organization-team/organization-team.entity.ts,
packages/core/src/lib/payment/payment.entity.ts,
packages/core/src/lib/tasks/screening-tasks/screening-task.entity.ts,
packages/core/src/lib/tasks/task.entity.ts
Removed createdByUser and createdByUserId properties to eliminate user tracking in entities.
packages/core/src/lib/database/migrations/1740727466186-RenameSubscriptionToEntitySubscriptionEntityTable.ts Renamed migration class and updated the name property accordingly.
packages/core/src/lib/role/role.entity.ts Reformatted the rolePermissions decorator to a single-line format and removed extra blank lines.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Controller
    participant Service
    participant "BaseEntityActionByUser"
    Client->>Controller: Request to create entity
    Controller->>Service: Validate and process creation
    Service->>BaseEntityActionByUser: Instantiate entity with user info
    BaseEntityActionByUser-->>Service: Return new entity instance
    Service-->>Controller: Persist entity and return result
    Controller-->>Client: Respond with created entity
Loading

Possibly related PRs

  • [Fix] Alter "ScreeningTask" Entity Table #8801: The changes introduce a new interface for user action tracking in the IBaseEntityModel, related to the modifications in the IScreeningTask interface, as both involve user-related properties and their management within the entity models.
  • [Fix] Alter "OrganizationProjectModule" Entity #8799: The changes introduce a new interface for user action tracking in the IBaseEntityModel, related to the modifications in the OrganizationProjectModule entity by renaming the creatorId property to createdByUserId.
  • [Fix] Alter "Task" Entity Table #8800: The changes introduce a new interface for user action tracking in the IBaseEntityModel, related to modifications in the Task entity, specifically changing creatorId to createdByUserId.

Poem

In the garden of code, I happily hop,
Adding new fields with each joyful stop.
User actions now tracked with a brand new view,
Old creator trails gently bid adieu.
I nibble on changes, so light and free—
A rabbit’s delight in clarity! 🐇💻


📜 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 08a03aa and 288418c.

📒 Files selected for processing (2)
  • packages/contracts/src/lib/base-entity.model.ts (3 hunks)
  • packages/core/src/lib/core/entities/base.entity.ts (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/contracts/src/lib/base-entity.model.ts
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: test
  • GitHub Check: build
🔇 Additional comments (9)
packages/core/src/lib/core/entities/base.entity.ts (9)

5-5: New import RelationId

The added import for RelationId from 'typeorm' is correctly included to support the relation ID fields in the new BaseEntityActionByUser class.


9-9: Added IUser import

Good addition of the IUser interface import, needed for the user relationship types in the new tracking fields.


11-11: Added MultiORMManyToOne import

The MultiORMManyToOne import is necessary for defining the user relationship decorators that support multiple ORM frameworks.


13-13: Import of User entity

Correctly imports the User entity from the internal barrel file for use in the relationship definitions.


63-64: Documentation formatting improvement

Removing trailing commas in the JSDoc descriptions improves consistency in the code formatting.

Also applies to: 80-81


102-157: Well-structured BaseEntityActionByUser class

The new BaseEntityActionByUser class is well-designed with:

  • Clear documentation for the class and each property
  • Proper relationship definitions using MultiORMManyToOne
  • Indexed relation ID columns for performance
  • Appropriate nullability and cascade settings

This enhancement enables tracking of which users created, updated, or deleted entities, which is valuable for audit trails and accountability.


110-113: Verify cascade delete behavior

The onDelete: 'CASCADE' setting means that if a user is deleted, all entities they created, updated, or deleted will have their respective user references set to null. Confirm this is the intended behavior, as an alternative approach would be to prevent user deletion if they have created entities.

Also applies to: 127-130, 144-147


119-122: Good practice: Indexed relation ID columns

Adding @ColumnIndex() to the user ID columns is a good practice as it will improve query performance when filtering entities by creator, updater, or deleter.

Also applies to: 136-139, 153-156


162-162: Updated inheritance hierarchy

The BaseEntity class now extends BaseEntityActionByUser instead of AccessTimestamps, correctly incorporating the new user tracking functionality into the base entity structure.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.
  • @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: 0

🧹 Nitpick comments (3)
packages/core/src/lib/organization-team/organization-team.entity.ts (1)

165-167: Use caution with eager loading to avoid performance overhead.

Marking this relation as eager: true means the ImageAsset entity will always be fetched with the OrganizationTeam. For large queries or rarely accessed associations, consider lazy loading (i.e., eager: false) to minimize performance costs.

packages/core/src/lib/core/entities/base.entity.ts (2)

98-98: Potential timezone consistency concern.

return new Date(); ensures timestamps use the local server time. Consider forcing UTC to avoid inconsistent date handling in distributed environments.


102-123: Centralized user action tracking is a good pattern.

Introducing BaseEntityActionByUser neatly consolidates the user creation reference. If you plan to track user updates as well, consider extending this pattern for full audit logging.

Would you like a follow-up implementation for update-by-user tracking?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 26f568c and 5e866a4.

📒 Files selected for processing (19)
  • packages/contracts/src/lib/base-entity.model.ts (3 hunks)
  • packages/contracts/src/lib/dashboard.model.ts (1 hunks)
  • packages/contracts/src/lib/deal.model.ts (1 hunks)
  • packages/contracts/src/lib/organization-project-module.model.ts (1 hunks)
  • packages/contracts/src/lib/organization-team.model.ts (1 hunks)
  • packages/contracts/src/lib/payment.model.ts (1 hunks)
  • packages/contracts/src/lib/screening-task.model.ts (1 hunks)
  • packages/contracts/src/lib/task.model.ts (1 hunks)
  • packages/contracts/src/lib/user.model.ts (0 hunks)
  • packages/core/src/lib/core/entities/base.entity.ts (4 hunks)
  • packages/core/src/lib/dashboard/dashboard.entity.ts (1 hunks)
  • packages/core/src/lib/database/migrations/1740727466186-RenameSubscriptionToEntitySubscriptionEntityTable.ts (1 hunks)
  • packages/core/src/lib/deal/deal.entity.ts (1 hunks)
  • packages/core/src/lib/organization-project-module/organization-project-module.entity.ts (2 hunks)
  • packages/core/src/lib/organization-team/organization-team.entity.ts (1 hunks)
  • packages/core/src/lib/payment/payment.entity.ts (1 hunks)
  • packages/core/src/lib/role/role.entity.ts (1 hunks)
  • packages/core/src/lib/tasks/screening-tasks/screening-task.entity.ts (1 hunks)
  • packages/core/src/lib/tasks/task.entity.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/contracts/src/lib/user.model.ts
✅ Files skipped from review due to trivial changes (2)
  • packages/core/src/lib/role/role.entity.ts
  • packages/core/src/lib/database/migrations/1740727466186-RenameSubscriptionToEntitySubscriptionEntityTable.ts
🧰 Additional context used
🧠 Learnings (1)
packages/core/src/lib/tasks/screening-tasks/screening-task.entity.ts (1)
Learnt from: rahul-rocket
PR: ever-co/ever-gauzy#8800
File: packages/core/src/lib/tasks/screening-tasks/screening-tasks.service.ts:81-81
Timestamp: 2025-02-24T05:25:58.665Z
Learning: The `creatorId` column in the `screening-task` entity will be renamed to `createdByUserId` in a future PR to maintain consistency with the task entity.
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: test
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: build
🔇 Additional comments (19)
packages/contracts/src/lib/deal.model.ts (1)

16-16: Removal of IHasUserCreator is part of refactoring user attribution

The removal of IHasUserCreator from the IDeal interface aligns with the PR's goal of moving user attribution to a base entity. This simplifies the interface and centralizes user tracking functionality.

packages/contracts/src/lib/organization-project-module.model.ts (1)

37-37: IHasUserCreator dependency correctly removed

Removing the IHasUserCreator interface from IProjectModuleAssociations is consistent with the architectural changes being made across the codebase to centralize user action tracking.

packages/core/src/lib/tasks/task.entity.ts (1)

51-51: Imports updated consistently with user tracking refactoring

The update to imports reflects the removal of user creator properties from the Task entity, consistent with moving user action tracking to a base entity approach.

packages/core/src/lib/dashboard/dashboard.entity.ts (1)

7-8: User import dependencies properly removed

The removal of User-related imports aligns with the architectural changes to centralize user action tracking at the base entity level instead of in each individual entity.

packages/core/src/lib/payment/payment.entity.ts (1)

1-205: LGTM: Removal of user creator properties aligns with broader refactoring.

Based on the AI summary, this file has had the createdByUser and createdByUserId properties removed from the Payment class, which aligns with the broader refactoring being done across the codebase.

packages/core/src/lib/tasks/screening-tasks/screening-task.entity.ts (1)

6-8: Verify alignment with future planned changes.

The imports have been updated to remove IUser and MultiORMManyToOne, which aligns with removing the user creator properties. However, based on the retrieved learning, there was a plan to rename creatorId to createdByUserId in a future PR, but this PR is removing createdByUserId entirely. Make sure this change is coordinated with those future plans.

packages/contracts/src/lib/payment.model.ts (1)

19-19: LGTM: Interface update aligns with entity changes.

The removal of IHasUserCreator from the interface inheritance aligns with the removal of user creator properties from the corresponding entity classes.

packages/contracts/src/lib/screening-task.model.ts (1)

18-21: LGTM: Interface simplification aligns with entity changes.

The IScreeningTaskAssociations interface no longer extends IHasUserCreator, which aligns with the removal of user creator properties from the corresponding entity class.

packages/contracts/src/lib/dashboard.model.ts (1)

9-9: Interface inheritance updated to remove IHasUserCreator.

The IDashboard interface no longer directly extends IHasUserCreator. This change is part of the larger refactoring effort to centralize user action tracking through the base entity hierarchy instead of requiring direct interface implementation.

packages/core/src/lib/deal/deal.entity.ts (1)

1-1: Import statements refactored to remove IUser dependency.

The removal of the IUser import aligns with the elimination of the direct createdByUser and createdByUserId properties from the Deal entity. This is part of the PR's approach to consolidate user action tracking at the base entity level.

Also applies to: 5-5

packages/contracts/src/lib/base-entity.model.ts (2)

37-37: Enhanced IBaseEntityModel to extend from IBaseEntityActionByUserModel.

The IBaseEntityModel now inherits from both IBaseEntityActionByUserModel and IBaseSoftDeleteEntityModel, which is a good architectural decision. This change centralizes user action tracking in the inheritance hierarchy rather than having it implemented in individual entity interfaces.


48-51: New interface created for tracking user actions.

The introduction of the IBaseEntityActionByUserModel interface provides a centralized way to track which user created an entity. This approach improves code organization and maintainability by removing the need for individual entities to implement the same user tracking properties repeatedly.

packages/contracts/src/lib/organization-team.model.ts (1)

43-43: Interface inheritance updated to remove IHasUserCreator.

The IOrganizationTeam interface no longer directly extends IHasUserCreator. This aligns with the architectural changes in this PR, which centralize user tracking at the base entity level rather than requiring direct interface implementation in each entity type.

packages/contracts/src/lib/task.model.ts (1)

24-24: Ensure references to removed interface are updated.

By removing IHasUserCreator, any code or services depending on creator-related properties must be aligned with the new approach (e.g., the BaseEntityActionByUser in the core). Verify that no lingering calls or assumptions about a creator property remain.

packages/core/src/lib/organization-project-module/organization-project-module.entity.ts (2)

1-1: New import aligns with advanced relation usage.

Adding RelationId to the import statement is appropriate for referencing foreign key columns. No concerns here.


33-33: Confirm alignment with multi-tenant architecture.

Switching or reaffirming to use TenantOrganizationBaseEntity is sensible if you need consistent tenant/organization scoping. Verify that all existing behaviors (e.g., user creation tracking, organization scoping) are correct under this new base class.

packages/core/src/lib/core/entities/base.entity.ts (3)

5-5: Additional imports for user tracking.

The newly added imports (RelationId, IUser, MultiORMManyToOne, and User) are consistent with implementing user-linked entity behavior. Implementation looks good.

Also applies to: 9-9, 12-12, 13-13


64-64: Documentation improvement.

Minor docstring updates clarify the meaning of creation and update timestamps; no functional impact.

Also applies to: 80-80


128-128: Extending the new user-tracking base class.

Shifting from AccessTimestamps to BaseEntityActionByUser is logical. Ensure existing entities expecting only timestamps are updated if they rely on the old inheritance structure. Double-check references and migrations to avoid unexpected breakages.

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

🧹 Nitpick comments (1)
packages/core/src/lib/core/entities/base.entity.ts (1)

102-140: Introduction of BaseEntityActionByUser.
This abstract class encapsulates user references for creation and update actions, extending the existing timestamp functionality. The onDelete: 'CASCADE' setting on user relationships can lead to records losing references if a user is deleted. If retaining historical data is critical, consider setting onDelete to SET NULL or keeping a soft deletion approach for users.

Would you like to confirm or test whether cascade deletes on users might cause unintended data loss?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 77aa1f5 and 08a03aa.

📒 Files selected for processing (2)
  • packages/contracts/src/lib/base-entity.model.ts (3 hunks)
  • packages/core/src/lib/core/entities/base.entity.ts (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: test
  • GitHub Check: build
🔇 Additional comments (8)
packages/contracts/src/lib/base-entity.model.ts (3)

3-3: Import usage looks consistent.
No concerns here; pulling in IUser is necessary for referencing user-specific properties.


37-37: Confirm expanded inheritance.
By extending both IBaseEntityActionByUserModel and IBaseSoftDeleteEntityModel, the IBaseEntityModel now incorporates user action tracking alongside soft-delete functionality. Ensure all existing references to IBaseEntityModel are compatible with this addition.


48-54: New interface for user action tracking.
The newly introduced IBaseEntityActionByUserModel cleanly encapsulates createdByUser, createdByUserId, updatedByUser, and updatedByUserId. This aligns well with typical audit fields. Confirm these fields are optional only when partial creation or updates are valid business cases.

packages/core/src/lib/core/entities/base.entity.ts (5)

5-5: Added imports for new user-action functionality.
These imports properly bring in the RelationId, IUser, and custom decorators needed for the new user-related fields. No issues noted.

Also applies to: 9-9, 11-11, 13-13


63-63: Minor doc update: creation timestamp description.
This clarifies the field’s usage without altering functionality.


80-80: Minor doc update: update timestamp description.
Again, purely a doc clarification. No further concerns.


98-98: No change in actual behavior.
Using return new Date(); remains a valid approach for populating timestamps.


145-145: Inheritance change to incorporate user-tracking.
BaseEntity now extends BaseEntityActionByUser, gaining user action fields. Confirm that application logic correctly handles these new properties (e.g., populating updatedByUser).

Copy link

nx-cloud bot commented Feb 28, 2025

View your CI Pipeline Execution ↗ for commit 288418c.

Command Status Duration Result
nx build gauzy -c=production --prod --verbose ✅ Succeeded 3m 31s View ↗
nx build api -c=production --prod ✅ Succeeded 1m 14s View ↗
nx build desktop-ui-lib --configuration=develop... ✅ Succeeded 29s View ↗
nx build desktop-ui-lib --configuration=production ✅ Succeeded 28s View ↗
nx build plugin-integration-wakatime ✅ Succeeded 4s View ↗
nx build desktop-lib ✅ Succeeded 6s View ↗
nx build desktop-window ✅ Succeeded <1s View ↗
nx build desktop-core ✅ Succeeded <1s View ↗
Additional runs (52) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-03-01 07:50:20 UTC

@rahul-rocket rahul-rocket merged commit a82645b into develop Mar 1, 2025
22 of 23 checks passed
@rahul-rocket rahul-rocket deleted the fix/alter-base-entity-add-created-by-user-id-column branch March 1, 2025 07:17
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