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

Payouts Account (models, api payout-account) #120

Merged
merged 17 commits into from
Mar 30, 2024
Merged

Payouts Account (models, api payout-account) #120

merged 17 commits into from
Mar 30, 2024

Conversation

jamalsoueidan
Copy link
Owner

@jamalsoueidan jamalsoueidan commented Mar 28, 2024

Type

enhancement, tests


Description

  • Introduced Mongoose schemas for payouts and payout accounts with validation using Zod.
  • Implemented services and tests for creating, retrieving, and deleting payout accounts.
  • Defined and exported models, schemas, and types for payouts and payout accounts.

Changes walkthrough

Relevant files
Enhancement
10 files
payout-account.schema.ts
Implement Mongoose Schema for Payout Accounts with Zod Validation

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

  • Introduced Mongoose schema for payout accounts with pre-save
    validation using Zod.
  • Supports two types of payout accounts: BANK_ACCOUNT and MOBILE_PAY.
  • Implements custom error handling for unsupported payout types and
    validation failures.
  • +61/-0   
    payout.schema.ts
    Create Mongoose Schema for Payouts                                             

    src/functions/payout/payout.schema.ts

  • Created Mongoose schema for payouts.
  • Schema includes fields for customer ID, date, amount, currency code,
    status, payout type, and payout details.
  • Uses enums for payout status and payout type to ensure data integrity.

  • +47/-0   
    payout-account.types.ts
    Define Types and Zod Schemas for Payout Account Details   

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

  • Defines types and Zod schemas for payout account details.
  • Supports both bank account and mobile pay payout types.
  • +39/-0   
    get.ts
    Service to Retrieve Customer Payout Account                           

    src/functions/customer/services/payout-account/get.ts

  • Implements service to retrieve a customer's payout account by ID.
  • Includes error handling for not found payout accounts.
  • +27/-0   
    destroy.ts
    Service to Delete Customer Payout Account                               

    src/functions/customer/services/payout-account/destroy.ts

    • Implements service to delete a customer's payout account by ID.
    +18/-0   
    payout-account.model.ts
    Define Mongoose Model for Payout Accounts                               

    src/functions/payout-account/payout-account.model.ts

    • Defines the Mongoose model for payout accounts.
    +10/-0   
    payout.model.ts
    Define Mongoose Model for Payouts                                               

    src/functions/payout/payout.model.ts

    • Defines the Mongoose model for payouts.
    +8/-0     
    create.ts
    Service to Create New Payout Account                                         

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

    • Implements service to create a new payout account.
    +6/-0     
    index.ts
    Export Payout Account Components                                                 

    src/functions/payout-account/index.ts

    • Exports payout account model, schema, and types.
    +3/-0     
    index.ts
    Export Payout Components                                                                 

    src/functions/payout/index.ts

    • Exports payout model, schema, and types.
    +3/-0     
    Tests
    3 files
    create.spec.ts
    Test Suite for Creating Payout Accounts                                   

    src/functions/customer/services/payout-account/create.spec.ts

  • Adds tests for creating payout account documents.
  • Validates successful creation and error handling for invalid payout
    details.
  • +39/-0   
    get.spec.ts
    Test Suite for Retrieving Payout Accounts                               

    src/functions/customer/services/payout-account/get.spec.ts

  • Adds tests for retrieving payout account documents.
  • Validates successful retrieval and correct data mapping.
  • +21/-0   
    destroy.spec.ts
    Test Suite for Deleting Payout Accounts                                   

    src/functions/customer/services/payout-account/destroy.spec.ts

  • Adds tests for deleting payout account documents.
  • Validates successful deletion.
  • +16/-0   

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

    @jamalsoueidan
    Copy link
    Owner Author

    @github-actions github-actions bot added enhancement New feature or request tests labels Mar 28, 2024
    Copy link

    PR Description updated to latest commit (a02362c)

    Copy link

    PR Review

    ⏱️ Estimated effort to review [1-5]

    4, due to the introduction of new schemas, types, and services along with their tests. The complexity of handling different payout types and integrating Zod for validation also adds to the review effort.

    🧪 Relevant tests

    Yes

    🔍 Possible issues

    Possible Bug: In payout-account.schema.ts, the use of mongoose.Schema.Types.Mixed for payoutDetails lacks explicit validation for the structure of the data stored. This could lead to inconsistent data shapes being saved.

    Error Handling: In payout-account.schema.ts, the error handling in the pre-save hook could be improved by distinguishing between validation errors and other types of errors.

    🔒 Security concerns

    No

    Code feedback:
    relevant filesrc/functions/payout-account/payout-account.schema.ts
    suggestion      

    Consider using a more structured type instead of mongoose.Schema.Types.Mixed for payoutDetails to ensure data consistency. You could define separate schemas for each payout type and use them based on the payoutType field. [important]

    relevant linepayoutDetails: mongoose.Schema.Types.Mixed,

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

    Improve error handling in the pre-save hook by specifically catching Zod's ZodError for validation errors. This allows for more precise error messages and handling. [important]

    relevant line} catch (error) {

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

    Add an index on the payoutType field if queries are frequently performed based on this field, to improve query performance. [medium]

    relevant lineenum: [PayoutAccountType.BANK_ACCOUNT, PayoutAccountType.MOBILE_PAY],

    relevant filesrc/functions/customer/services/payout-account/create.spec.ts
    suggestion      

    Include tests for handling different payoutDetails structures based on the payoutType to ensure that the validation works as expected for both types. [important]

    relevant lineconst payoutType = PayoutAccountType.MOBILE_PAY;


    ✨ 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

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Enhancement
    Use realistic phone numbers in test cases.

    Consider using a more realistic phone number for payoutDetails in your test cases to
    better reflect real-world scenarios. This can help ensure that your validation logic is
    robust and can handle typical user input.

    src/functions/customer/services/payout-account/create.spec.ts [10-11]

     const payoutDetails = {
    -  phoneNumber: 1212121,
    +  phoneNumber: 1234567890,
     };
     
    Ensure non-empty strings with Zod validation.

    For better type safety and validation, consider using z.string().min(1) for bankName in
    PayoutAccountBankZodSchema to ensure that the name is not an empty string.

    src/functions/payout-account/payout-account.types.ts [9]

    -bankName: z.string(),
    +bankName: z.string().min(1),
     
    Best practice
    Define explicit return type for functions.

    It's recommended to explicitly define the return type of
    CustomerPayoutAccountServiceCreate to improve code readability and maintainability.

    src/functions/customer/services/payout-account/create.ts [3]

    -export const CustomerPayoutAccountServiceCreate = (props: PayoutAccount) => {
    +export const CustomerPayoutAccountServiceCreate = (props: PayoutAccount): Promise<IPayoutAccountDocument> => {
     
    Validate ObjectId before database operations.

    Use mongoose.isValidObjectId to check if payoutAccountId is a valid MongoDB ObjectId
    before attempting to delete, to avoid unnecessary database operations and potential
    errors.

    src/functions/customer/services/payout-account/destroy.ts [15]

    -_id: new mongoose.Types.ObjectId(payoutAccountId),
    +_id: mongoose.isValidObjectId(payoutAccountId) ? new mongoose.Types.ObjectId(payoutAccountId) : null,
     
    Use custom error classes for precise error handling.

    Instead of throwing a generic Error in the pre save hook, consider creating a custom error
    class that extends Error. This allows for more precise error handling and makes the
    codebase more consistent.

    src/functions/payout-account/payout-account.schema.ts [48]

    -throw new Error(`Unsupported payout type: ${payoutType}`);
    +throw new UnsupportedPayoutTypeError(payoutType);
     

    ✨ 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.

     - Rename BlockDateZodSchema to BlockedZodSchema and update references
     - Implement CRUD operations for customer payout accounts with new controllers and services
     - Adjust types to allow string or number for customer IDs and phone numbers
     • Add new endpoints for creating, retrieving, and deleting customer payout accounts.
     • Define new OpenAPI components for payout account types, including mobile pay and bank account details.
     • Remove specific payout account ID requirement from destroy service and controller, changing to delete by customer ID.
     • Delete the unused blocked.yaml file.
     • Implement CustomerPayoutServiceBalance to calculate customer payout balances
     • Add balance.spec.ts for testing payout balance functionality
     • Update webhook.function.ts to log JSON stringified webhook responses
     - Implement MongoDB aggregation pipeline for calculating customer payout balance
     - Add lookup to PayoutLog to exclude paid out line items
     - Remove unused PayoutModel import and related code
     - Add new dummy data fixture for balance calculation tests
     - Update tests to reflect new balance calculation logic
    @jamalsoueidan jamalsoueidan changed the title Payouts Payouts (models, api payout-account) Mar 30, 2024
    @jamalsoueidan jamalsoueidan merged commit 10a163a into main Mar 30, 2024
    3 checks passed
    @jamalsoueidan jamalsoueidan deleted the payouts branch March 30, 2024 06:10
    @jamalsoueidan jamalsoueidan changed the title Payouts (models, api payout-account) Payouts Account (models, api payout-account) Mar 30, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    Status: Done
    Development

    Successfully merging this pull request may close these issues.

    1 participant