Skip to content

[Fix] tariffs undefined values #3645

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

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

Conversation

IraHrechyn
Copy link
Contributor

@IraHrechyn IraHrechyn commented Apr 29, 2025

This pull request includes:

  • A fix for missing name, description, nameEng, and descriptionEng fields in the tariffs section by transforming the API response using transformBag.
  • Updated the getAllTariffsForService method to apply the transformation before assigning data to bags.
  • Refactored related unit tests to reflect the new structure and prevent undefined field errors.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of missing bag data when updating tariff settings to prevent errors.
  • New Features
    • Enhanced normalization of tariff bag details to ensure consistent display of names and descriptions in both Ukrainian and English.
  • Tests
    • Updated and expanded tests for tariff retrieval and bag selection to better reflect component behavior and handle asynchronous operations.

Copy link

coderabbitai bot commented Apr 29, 2025

Walkthrough

The changes update the pricing page component and its tests in the tariffs admin module. The onChecked method now has explicit parameter types and a guard clause to prevent errors if a bag with the provided ID is not found. A new transformBag method is introduced to normalize bag objects, ensuring consistent naming and description fields. The component now transforms bags received from the service before storing them. Corresponding tests are updated to handle asynchronous operations more precisely and to reflect the internal state changes within the component.

Changes

File(s) Summary
src/app/ubs/ubs-admin/components/ubs-admin-tariffs/ubs-admin-tariffs-pricing-page/ubs-admin-tariffs-pricing-page.component.ts Updated onChecked with explicit types and guard clause; added transformBag method; applied transformation in data retrieval.
src/app/ubs/ubs-admin/components/ubs-admin-tariffs/ubs-admin-tariffs-pricing-page/ubs-admin-tariffs-pricing-page.component.spec.ts Enhanced test imports; expanded test fixtures; updated tests for async handling and to verify component state changes.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant PricingPageComponent
    participant TariffService

    User->>PricingPageComponent: Triggers getAllTariffsForService
    PricingPageComponent->>TariffService: getAllTariffsForService()
    TariffService-->>PricingPageComponent: Returns bags array
    PricingPageComponent->>PricingPageComponent: transformBag(bag) for each bag
    PricingPageComponent->>PricingPageComponent: Store transformed bags in bags array

    User->>PricingPageComponent: Calls onChecked(id, event)
    PricingPageComponent->>PricingPageComponent: Find bag by id
    alt Bag found
        PricingPageComponent->>PricingPageComponent: Update bag.limitIncluded
        PricingPageComponent->>PricingPageComponent: Mark form as dirty
    else Bag not found
        PricingPageComponent->>PricingPageComponent: Return early
    end
Loading

Poem

In the garden of tariffs, a bunny hops near,
With bags now transformed, their details are clear.
Types are explicit, guards keep us safe,
Tests tick along, no need for a waif.
A hop, a mark, a limit included,
Code neat and tidy, never deluded!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 generate sequence diagram to generate a sequence diagram of the changes in 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

@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 (4)
src/app/ubs/ubs-admin/components/ubs-admin-tariffs/ubs-admin-tariffs-pricing-page/ubs-admin-tariffs-pricing-page.component.spec.ts (1)

581-596: Add test for case when bag is not found

The onChecked method now has a guard clause that returns early if the bag with the specified ID is not found, but this case isn't tested.

Consider adding a test that checks the behavior when a bag ID doesn't exist:

it('onCheck should do nothing if bag with specified ID is not found', () => {
  const fakeBag: Bag = {
    id: 1,
    limitIncluded: false,
    capacity: 20,
    price: 100,
    commission: 10
  };
  component.bags = [fakeBag];

  const nonExistentId = 999;
  const fakeEvent = {
    checked: true
  };
  
  component.onChecked(nonExistentId, fakeEvent);
  expect(component.bags[0].limitIncluded).toEqual(false); // Should remain unchanged
});
src/app/ubs/ubs-admin/components/ubs-admin-tariffs/ubs-admin-tariffs-pricing-page/ubs-admin-tariffs-pricing-page.component.ts (3)

243-254: Improved robustness in onChecked method

Good improvements to the onChecked method:

  1. Added explicit typing for the id parameter
  2. Added a guard clause to prevent errors when a bag with specified ID isn't found
  3. The form is marked as dirty to ensure changes are properly tracked

Consider using a more specific type for the event parameter instead of any:

-onChecked(id: number, event: any): void {
+onChecked(id: number, event: { checked: boolean }): void {

358-366: Good implementation of bag data normalization

The transformBag method effectively normalizes bag objects by ensuring consistent field naming regardless of the input format. It handles both naming conventions (nameUk/nameEn vs name/nameEng) through fallback values.

Consider adding a more specific input type and documentation for clarity:

-transformBag(bag: any): Bag {
+/**
+ * Normalizes bag data by ensuring consistent field naming for multilingual content
+ * Handles both `nameUk`/`nameEn` and `name`/`nameEng` conventions
+ */
+transformBag(bag: Partial<Bag>): Bag {

358-379: Consider adding a unit test for the transformBag method

While the component changes look good, there's no specific test for the transformBag method itself, which is a key part of fixing the issue with missing fields.

Consider adding a dedicated unit test for this important transformation function:

it('should transform bag data correctly with various input formats', () => {
  // Test case 1: Input with nameUk/nameEn format
  const input1 = {
    id: 1,
    nameUk: 'УкрНазва',
    nameEn: 'EnglishName',
    descriptionUk: 'УкрОпис',
    descriptionEn: 'EnglishDescription',
    capacity: 20,
    price: 100
  };
  
  // Test case 2: Input with name/nameEng format
  const input2 = {
    id: 2,
    name: 'Назва',
    nameEng: 'Name',
    description: 'Опис',
    descriptionEng: 'Description',
    capacity: 30,
    price: 150
  };
  
  // Test case 3: Input with mixed or missing fields
  const input3 = {
    id: 3,
    nameUk: 'УкрНазва',
    nameEng: 'EngName',
    description: 'Опис',
    capacity: 40,
    price: 200
  };
  
  const result1 = component.transformBag(input1);
  const result2 = component.transformBag(input2);
  const result3 = component.transformBag(input3);
  
  // Verify all formats are correctly normalized
  expect(result1.name).toEqual('УкрНазва');
  expect(result1.nameEng).toEqual('EnglishName');
  expect(result1.description).toEqual('УкрОпис');
  expect(result1.descriptionEng).toEqual('EnglishDescription');
  
  expect(result2.name).toEqual('Назва');
  expect(result2.nameEng).toEqual('Name');
  
  expect(result3.name).toEqual('УкрНазва');
  expect(result3.nameEng).toEqual('EngName');
  expect(result3.description).toEqual('Опис');
  expect(result3.descriptionEng).toBeUndefined();
});
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 04f39a2 and f0766dd.

📒 Files selected for processing (2)
  • src/app/ubs/ubs-admin/components/ubs-admin-tariffs/ubs-admin-tariffs-pricing-page/ubs-admin-tariffs-pricing-page.component.spec.ts (4 hunks)
  • src/app/ubs/ubs-admin/components/ubs-admin-tariffs/ubs-admin-tariffs-pricing-page/ubs-admin-tariffs-pricing-page.component.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/app/ubs/ubs-admin/components/ubs-admin-tariffs/ubs-admin-tariffs-pricing-page/ubs-admin-tariffs-pricing-page.component.ts (2)
src/app/ubs/ubs-admin/models/tariffs.interface.ts (1)
  • Bag (1-25)
src/app/ubs/ubs/models/ubs.interface.ts (1)
  • Bag (3-12)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build (18.x)
🔇 Additional comments (5)
src/app/ubs/ubs-admin/components/ubs-admin-tariffs/ubs-admin-tariffs-pricing-page/ubs-admin-tariffs-pricing-page.component.spec.ts (4)

1-1: Angular testing utilities properly imported for async testing

Good addition of fakeAsync and tick utilities, which are essential for properly testing asynchronous operations in Angular components.


121-131: Test data properly updated with complete field set

The fakeBag object has been updated to include the necessary fields (name, nameEng, description, descriptionEng) that align with the bag transformation implemented in the component.


543-566: Properly structured test for asynchronous operation

The test for getAllTariffsForService now correctly:

  1. Uses fakeAsync and tick() to handle async operations
  2. Sets up a complete test response with the expected fields
  3. Verifies the component state after the observable completes

This ensures the transformations on the bags are properly tested.


581-596: Testing component state rather than local variables

Good improvement to initialize the component's bags array with the test data and then verify the state change within the component rather than in the local variable. This better reflects how the component behaves in actual usage.

src/app/ubs/ubs-admin/components/ubs-admin-tariffs/ubs-admin-tariffs-pricing-page/ubs-admin-tariffs-pricing-page.component.ts (1)

373-376: Properly implemented bag transformation in service response handling

The getAllTariffsForService method now transforms each bag from the API response to ensure consistent field naming before assigning to the component's state. This effectively addresses the issue with missing fields mentioned in the PR objectives.

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
33.3% Coverage on New Code (required ≥ 75%)

See analysis details on SonarQube Cloud

@IraHrechyn IraHrechyn self-assigned this Apr 29, 2025
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