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

Add orderId and createdAt to Payout Log schema and aggregation logic #125

Merged
merged 3 commits into from
Apr 1, 2024

Conversation

jamalsoueidan
Copy link
Owner

@jamalsoueidan jamalsoueidan commented Apr 1, 2024

Type

enhancement


Description

  • Enhanced payout log creation logic and schema to include orderId and orderCreatedAt.
  • Updated tests and OpenAPI documentation to reflect these changes.
  • Minor formatting adjustment in payout log pagination.

Changes walkthrough

Relevant files
Formatting
paginate.ts
Minor Formatting Adjustment in Payout Log Pagination         

src/functions/customer/services/payout-log/paginate.ts

  • Minor formatting change in the aggregation pipeline.
+6/-1     
Enhancement
balance.spec.ts
Include Order ID in Payout Log Model Creation for Tests   

src/functions/customer/services/payout/balance.spec.ts

  • Added orderId to PayoutLogModel.create calls in tests.
+3/-0     
create.ts
Enhance Payout Creation with Order ID and Creation Date   

src/functions/customer/services/payout/create.ts

  • Renamed lineItems to orders and included orderId and orderCreatedAt in
    shipping and line item logs.
  • Adjusted aggregation to include id and created_at.
  • Enhanced payout log creation with additional order information.
  • +42/-20 
    payout-log.schema.ts
    Extend Payout Log Schema with Order ID and Creation Date 

    src/functions/payout-log/payout-log.schema.ts

  • Added orderId and orderCreatedAt fields to the
    PayoutLogMongooseSchema.
  • +6/-0     
    payout-log.types.ts
    Update Payout Log Types to Include Order Details                 

    src/functions/payout-log/payout-log.types.ts

    • Introduced orderId and orderCreatedAt in PayoutLogZodSchema.
    +3/-1     
    Documentation
    payout-log.yaml
    Update OpenAPI Schema for Payout Logs with New Fields       

    openapi/paths/customer/payout-log/_types/payout-log.yaml

  • Added orderId and orderCreatedAt to the OpenAPI schema for payout
    logs.
  • +5/-0     

    PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @github-actions github-actions bot added documentation Improvements or additions to documentation enhancement New feature or request labels Apr 1, 2024
    Copy link

    github-actions bot commented Apr 1, 2024

    PR Description updated to latest commit (ca3a845)

    Copy link

    github-actions bot commented Apr 1, 2024

    PR Review

    ⏱️ Estimated effort to review [1-5]

    3, because the PR involves multiple files across different layers (schema, service logic, tests, and documentation). The changes are not overly complex but require careful consideration to ensure data consistency and correct functionality.

    🧪 Relevant tests

    Yes

    🔍 Possible issues

    Possible Bug: The aggregation logic in paginate.ts merges created_at from the parent document into each line_item. This might not be the intended behavior if created_at is supposed to be specific to each line_item.

    Data Consistency: The introduction of orderId in the schema and its usage across different services need to ensure that the orderId is always correctly associated with the right line_item and shipping references.

    🔒 Security concerns

    No

    Code feedback:
    relevant filesrc/functions/customer/services/payout-log/paginate.ts
    suggestion      

    Consider validating the existence of created_at before merging it into line_item. This ensures that the data merged into line_item is always expected and avoids potential runtime errors. [important]

    relevant linecreated_at: "$created_at",

    relevant filesrc/functions/customer/services/payout/create.ts
    suggestion      

    Ensure error handling for PayoutLogModel.insertMany operations is robust and integrates with the application's error monitoring system, rather than just logging to console. This will help in proactive issue resolution. [important]

    relevant line).catch((error) => console.error("Error inserting shipping logs:", error)); //<< needs to send to application inisight

    relevant filesrc/functions/payout-log/payout-log.schema.ts
    suggestion      

    Since orderId is marked as required and indexed, ensure that all existing data and future insertions comply with this requirement to avoid insertion errors or performance issues. [important]

    relevant linerequired: true,

    relevant filesrc/functions/customer/services/payout/create.ts
    suggestion      

    When reducing orders to calculate totalLineItems, ensure that line_items.price is always defined and is a number. Consider adding validation or a fallback value to prevent runtime errors. [medium]

    relevant lineconst totalLineItems = orders.reduce(


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link

    github-actions bot commented Apr 1, 2024

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Enhancement
    Use $addFields for adding new fields to documents.

    Consider using $addFields instead of $project for adding created_at to line_items.
    $addFields is more intuitive for adding new fields to documents and maintains all existing
    fields without the need to explicitly include them.

    src/functions/customer/services/payout-log/paginate.ts [68-78]

     {
    -  $project: {
    +  $addFields: {
         line_item: {
           $mergeObjects: [
             "$line_items",
             {
               created_at: "$created_at",
             },
           ],
         },
    -    _id: 0,
       },
     }
     
    Best practice
    Improve error logging mechanism for better error tracking.

    Replace the console.error call with a more robust error handling mechanism that logs
    errors to an application monitoring service. This ensures better visibility and tracking
    of errors in production environments.

    src/functions/customer/services/payout/create.ts [83]

    -.catch((error) => console.error("Error inserting shipping logs:", error));
    +.catch((error) => {
    +  // Replace with your application's error logging service
    +  applicationErrorLogger.log("Error inserting shipping logs:", error);
    +});
     
    Ensure type consistency for orderId in the schema.

    Ensure orderId type consistency across the application. If orderId is expected to be a
    numeric value everywhere, consider using mongoose.Schema.Types.Number instead of Number to
    maintain consistency and leverage Mongoose's type validation.

    src/functions/payout-log/payout-log.schema.ts [29-33]

     orderId: {
    -  type: Number,
    +  type: mongoose.Schema.Types.Number,
       required: true,
       index: true,
     }
     
    Use async/await for error handling for consistency.

    Use async/await with try/catch for error handling instead of .catch for consistency and
    readability, especially when the rest of your codebase uses async/await.

    src/functions/customer/services/payout/create.ts [83]

    -.catch((error) => console.error("Error inserting shipping logs:", error));
    +try {
    +  await PayoutLogModel.insertMany(...);
    +} catch (error) {
    +  console.error("Error inserting shipping logs:", error);
    +}
     
    Maintainability
    Extract calculation logic into separate functions for better maintainability.

    For better maintainability, consider extracting the logic for calculating totalLineItems
    and totalShippingAmount into separate functions. This makes the code more modular and
    easier to test.

    src/functions/customer/services/payout/create.ts [42-62]

    -const totalLineItems = orders.reduce(
    +const calculateTotalLineItems = (orders) => orders.reduce(
       (accumulator, { line_items }) => accumulator + parseFloat(line_items.price),
       0
     );
    -const totalShippingAmount = uniqueShippings.reduce(
    +const calculateTotalShippingAmount = (uniqueShippings) => uniqueShippings.reduce(
       (accumulator, { shipping }) => accumulator + shipping.cost.value,
       0
     );
    +const totalLineItems = calculateTotalLineItems(orders);
    +const totalShippingAmount = calculateTotalShippingAmount(uniqueShippings);
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    @jamalsoueidan
    Copy link
    Owner Author

    /describe

    @jamalsoueidan
    Copy link
    Owner Author

    @github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 1, 2024
    Copy link

    github-actions bot commented Apr 1, 2024

    PR Description updated to latest commit (0715cdc)

    @jamalsoueidan jamalsoueidan self-assigned this Apr 1, 2024
    @jamalsoueidan jamalsoueidan merged commit c0d7069 into main Apr 1, 2024
    3 checks passed
    @jamalsoueidan jamalsoueidan deleted the payout-log-update branch April 1, 2024 19:54
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    Projects
    Status: Done
    Development

    Successfully merging this pull request may close these issues.

    1 participant