Skip to content

[HOLD for payment 2024-04-15] [$250] Remove deprecated ReportUtils.getPolicy() method #38959

Closed

Description

@tgolen

This is coming from #27262. You can read the issue description there to get context behind the problem being solved and the mess being cleaned up.

Problem

ReportUtils.getPolicy() is called from a view component and other action files which is an anti-pattern.

Why this is important to fix

It maintains a more pure and exact flow of data through the react application. If the view is using policy data, then it needs to subscribe to the policy in Onyx so that it's garaunteed that the data will never be stale or out-of-date.

Solution

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~010ae8bb081ab888f1
  • Upwork Job ID: 1772360821776945152
  • Last Price Increase: 2024-03-25
  • Automatic offers:
    • DylanDylann | Contributor | 0
Issue OwnerCurrent Issue Owner: @CortneyOfstad

Activity

  1. added
    ExternalAdded to denote the issue can be worked on by a contributor
    BugSomething is broken. Auto assigns a BugZero manager.
    on Mar 25, 2024
  2. self-assigned this
    on Mar 25, 2024
  3. melvin-bot commented on Mar 25, 2024

    @melvin-bot
  4. added
    Help WantedApply this label when an issue is open to proposals by contributors
    on Mar 25, 2024
  5. melvin-bot commented on Mar 25, 2024

    @melvin-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @eVoloshchak (External)

  6. melvin-bot commented on Mar 25, 2024

    @melvin-bot

    Triggered auto assignment to @CortneyOfstad (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details.

  7. Krishna2323 commented on Mar 25, 2024

    @Krishna2323
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    Remove deprecated ReportUtils.getPolicy() method

    What is the root cause of that problem?

    Remove deprecated ReportUtils.getPolicy() method

    What changes do you think we should make in order to solve the problem?

    Steps:

    1. Need to remove getPolicy from here:

      App/src/libs/ReportUtils.ts

      Lines 570 to 579 in c1947c2

      /**
      * @deprecated Use withOnyx or Onyx.connect() instead
      */
      function getPolicy(policyID: string | undefined): Policy | EmptyObject {
      if (!allPolicies || !policyID) {
      return {};
      }
      return allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] ?? {};
      }
    2. There are total 2 components & 18 util functions which uses getPolicy, from there we need to remove the use of getPolicy and add extra parameter for policy.

    Note: For components we will use withOnyx()

    Result

  8. abzokhattab commented on Mar 25, 2024

    @abzokhattab
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    Remove deprecated ReportUtils.getPolicy() method

    What is the root cause of that problem?

    Remove deprecated ReportUtils.getPolicy() method.

    What changes do you think we should make in order to solve the problem?

    we need to remove the ReportUtil.getPolicy()

    const policy = ReportUtils.getPolicy(policyID);

    and, here

    const policy = ReportUtils.getPolicy(policyID);

    and replace it with oynx similar to the following snippet

    withOnyx({
      policy: {
            key: ({route}) => `${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`,
        },
    })

    then here

    const policy = ReportUtils.getPolicy(policyID);
    , we should use Onyx.connect on top to get the policy using the given id.

    also we need to the do same in all the occurencies inside the IOU.js and in the Policy.ts files

  9. nayabatir1 commented on Mar 25, 2024

    @nayabatir1

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    Remove deprecated ReportUtils.getPolicy().

    What is the root cause of that problem?

    Since ReportUtils.getPolicy() is deprecated need to replace it everywhere with withOnyx or Onyx.connect() to get data

    What changes do you think we should make in order to solve the problem?

    • List of files for implementing this change
      • SettlementButton.tsx
      • NextStepUtils.ts
      • ReportUtils.ts
      • Policy.ts
      • WorkspaceJoinUserPage.tsx
    • add test case to check getPolicy is not exported from ReportUtils.ts

    What alternative solutions did you explore? (Optional)

  10. nkdengineer commented on Mar 26, 2024

    @nkdengineer
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    Remove deprecated ReportUtils.getPolicy() method

    What is the root cause of that problem?

    What changes do you think we should make in order to solve the problem?

    1. For component we should get policy via withOnyx of if the component already subscribe the policy collection, we will get the policy from this
    policy: {
        key: ({policyID}) =>  `${ONYXKEYS.COLLECTION.POLICY}${policyID}`
    }
    

    export default withOnyx<SettlementButtonProps, SettlementButtonOnyxProps>({

    const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]
    

    const policy = ReportUtils.getPolicy(policyID);

    1. For the libs action, in we should use Onyx.connect to get allPolicies and then use this to get the policy
    let allPolicies: OnyxCollection<Policy>;
    Onyx.connect({
        key: ONYXKEYS.COLLECTION.POLICY,
        waitForCollectionCallback: true,
        callback: (value) => (allPolicies = value),
    });
    

    OPTION: We can create a local function in action file to get the policy with policyID and use this in other place of this file that we want.

    1. Add a test here to verify that we do not export getPolicy function in ReportUtils
    it('does not export getPolicy', () => {
        // @ts-expect-error the test is asserting that it's undefined, so the TS error is normal
        expect(ReportUtils.getPolicy).toBeUndefined();
    });
    

    describe('ReportUtils', () => {

    What alternative solutions did you explore? (Optional)

    NA

  11. 35 remaining items

  12. melvin-bot commented on Apr 8, 2024

    @melvin-bot

    Reviewing label has been removed, please complete the "BugZero Checklist".

  13. melvin-bot commented on Apr 8, 2024

    @melvin-bot

    The solution for this issue has been 🚀 deployed to production 🚀 in version 1.4.60-13 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:

    If no regressions arise, payment will be issued on 2024-04-15. 🎊

    For reference, here are some details about the assignees on this issue:

  14. melvin-bot commented on Apr 8, 2024

    @melvin-bot

    BugZero Checklist: The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

    • [@DylanDylann] The PR that introduced the bug has been identified. Link to the PR:
    • [@DylanDylann] The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake. Link to comment:
    • [@DylanDylann] A discussion in #expensify-bugs has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner. Link to discussion:
    • [@DylanDylann] Determine if we should create a regression test for this bug.
    • [@DylanDylann] If we decide to create a regression test for the bug, please propose the regression test steps to ensure the same bug will not reach production again.
    • [@CortneyOfstad] Link the GH issue for creating/updating the regression test once above steps have been agreed upon:
  15. nkdengineer commented on Apr 10, 2024

    @nkdengineer
    Contributor

    @DylanDylann Create a follow up PR here #39991 to fix the bug that is mentioned by @Pujan92 above #38959 (comment).

  16. CortneyOfstad commented on Apr 15, 2024

    @CortneyOfstad
    Contributor

    So there was a regression, so this will result in a price of $250 instead of $500 for both @DylanDylann and @nkdengineer.

    @DylanDylann can you handle the checklist here by EOD?

    @nkdengineer I sent you a manual offer in Upwork. Please let me know once you accept 👍

  17. DylanDylann commented on Apr 15, 2024

    @DylanDylann
    Contributor

    BugZero Checklist: The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

    [@DylanDylann] The PR that introduced the bug has been identified. Link to the PR: NA
    [@DylanDylann] The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake. Link to comment: NA
    [@DylanDylann] A discussion in #expensify-bugs has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner. Link to discussion: NA
    [@DylanDylann] Determine if we should create a regression test for this bug. No
    [@DylanDylann] If we decide to create a regression test for the bug, please propose the regression test steps to ensure the same bug will not reach production again.

  18. DylanDylann commented on Apr 15, 2024

    @DylanDylann
    Contributor

    This is clean up issue, so I think we don't need regression test step

  19. CortneyOfstad commented on Apr 15, 2024

    @CortneyOfstad
    Contributor

    Sounds good @DylanDylann!

  20. CortneyOfstad commented on Apr 15, 2024

    @CortneyOfstad
    Contributor

    Payment Summary

    @DylanDylann — paid $250 via Upwork
    @nkdengineer — paid $250 via Upwork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Awaiting PaymentAuto-added when associated PR is deployed to productionBugSomething is broken. Auto assigns a BugZero manager.DailyKSv2ExternalAdded to denote the issue can be worked on by a contributorReviewingHas a PR in review

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions