Skip to content

[Due for payment 2025-02-18] [HOLD for payment 2025-02-07] [HOLD for payment 2025-02-04] [HOLD for payment 2025-01-28] [HOLD for payment 2025-01-24] [HOLD for payment 2025-01-22] [HOLD for payment 2025-01-15] [HOLD for payment 2025-01-02] [HOLD for payment 2024-12-30] [$250] Undo defaults of -1 and "-1" for account/report/policy IDs  #50360

Closed
2 of 2 issues completed

Description

@iwiznia

Context https://expensify.slack.com/archives/C01GTK53T8Q/p1728329641957179

Problem

Here we decided to use -1 or "-1" as defaults for when there was no report/account/policy IDs (not sure if there are others), but in most places we don't check if the IDs are -1, instead we do !id which for -1 would return true. That means we are applying logic to the -1 ids when we are really intending to skip the logic for those altogether.

Solution

Undo that and instead use 0 for numbers and "" for strings

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~021844825805028000985
  • Upwork Job ID: 1844825805028000985
  • Last Price Increase: 2024-10-11
  • Automatic offers:
    • paultsimura | Reviewer | 104595527
Issue OwnerCurrent Issue Owner: @laurenreidexpensify

Activity

  1. added
    Help WantedApply this label when an issue is open to proposals by contributors
    on Oct 7, 2024
  2. ChavdaSachin commented on Oct 7, 2024

    @ChavdaSachin
    Contributor

    Proposal

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

    Update default values for report/account/policy IDs from -1 to 0 and "-1" to empty string "".

    What is the root cause of that problem?

    • NA

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

    Fix all the occurrences where default report/account/policy IDs value is -1 or "-1" to 0 or empty string.

    What alternative solutions did you explore? (Optional)

    Reminder: Please use plain English, be brief and avoid jargon. Feel free to use images, charts or pseudo-code if necessary. Do not post large multi-line diffs or write walls of text. Do not create PRs unless you have been hired for this job.

  3. fabioh8010 commented on Oct 10, 2024

    @fabioh8010
    Contributor

    @iwiznia @neil-marcellini Based on our discussions on Slack, I did some experiments considering that we would want to remove default values for both strings and numbers, allowing them to be undefined where we would then handle the changes in the codebase.

    So first I did the following replaces in order in the codebase:

    1. Replace "ID ?? ''" with "ID"
    2. Replace "id ?? ''" with "id"
    3. Replace "ID ?? '-1'" with "ID"
    4. Replace "id ?? '-1'" with "id"
    5. Replace "ID ?? -1" with "ID"
    6. Replace "id ?? -1" with "id"
    7. Replace "ID ?? '0'" with "ID"
    8. Replace "id ?? '0'" with "id" // nothing
    9. Replace "ID ?? 0" with "ID"
    10. Replace "id ?? 0" with "id" // nothing
    11. Replace "ID || ''" with "ID"
    12. Replace "id || ''" with "id" // nothing
    13. Replace "ID || '-1'" with "ID"
    14. Replace "id || '-1'" with "id" // nothing
    15. Replace "ID || -1" with "ID"
    16. Replace "id || -1" with "id" // nothing
    17. Replace "ID || '0'" with "ID" // nothing
    18. Replace "id || '0'" with "id" // nothing
    19. Replace "id || 0" with "id" // nothing
    20. Replace " : '-1'" with " : undefined"
    21. Replace " : -1" with " : undefined"
    22. Replace " : '0'" with " : undefined"
    23. Replace " ?? '-1'" with ""
    24. Replace " ?? -1" with ""

    All these changes combined with Prettier format resulted in 377 changed files with 1127 additions and 1189 deletions, as we can see in this WIP PR.

    After running npm run typecheck to check for TS errors, it was found found 1212 errors in 290 files.

    And after running npm run lint to check for lint errors, it was found 348 errors in 44 files.

    It's worth noting that these changes and errors are only due to replacing the defaultings, and these numbers can be very different when we start fixing the codebase.

    The most common TS errors are:

    Argument of type 'X | undefined' is not assignable to parameter of type 'Y'.

    Let's take this example on ReportScreen.tsx

    src/pages/home/ReportScreen.tsx:261:90 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
      Type 'undefined' is not assignable to type 'string'.
    
    261     const transactionThreadReportID = ReportActionsUtils.getOneTransactionThreadReportID(reportID, reportActions ?? [], isOffline);
                                                                                                 ~~~~~~~~
    

    ReportActionsUtils.getOneTransactionThreadReportID() requires reportID to be string. In this case we can modify reportID to be string | undefined and we probably won't need to do anything else here.

    diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts
    index 8fe49eaa80e..9f9a146bda7 100644
    --- a/src/libs/ReportActionsUtils.ts
    +++ b/src/libs/ReportActionsUtils.ts
    @@ -1021,7 +1021,7 @@ const iouRequestTypes = new Set<ValueOf<typeof CONST.IOU.REPORT_ACTION_TYPE>>([
      * Gets the reportID for the transaction thread associated with a report by iterating over the reportActions and identifying the IOU report actions.
      * Returns a reportID if there is exactly one transaction thread for the report, and null otherwise.
      */
    -function getOneTransactionThreadReportID(reportID: string, reportActions: OnyxEntry<ReportActions> | ReportAction[], isOffline: boolean | undefined = undefined): string | undefined {
    +function getOneTransactionThreadReportID(reportID: string | undefined, reportActions: OnyxEntry<ReportActions> | ReportAction[], isOffline: boolean | undefined = undefined): string | undefined {
         // If the report is not an IOU, Expense report, or Invoice, it shouldn't be treated as one-transaction report.
         const report = ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
         if (report?.type !== CONST.REPORT.TYPE.IOU && report?.type !== CONST.REPORT.TYPE.EXPENSE && report?.type !== CONST.REPORT.TYPE.INVOICE) {

    Now let's take this example on ReportActionsView.tsx

    src/pages/home/report/ReportActionsView.tsx:272:40 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
      Type 'undefined' is not assignable to type 'string'.
    
    272                 Report.getNewerActions(newestActionCurrentReport?.reportID, newestActionCurrentReport?.reportActionID);
                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

    We need to change Report.getNewerActions() arguments to allow undefined. By doing that we could add a condition that return early if one of the parameters are falsy, protecting the code and making TS happy.

    diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts
    index f58f29d2640..4c8ab5be2cb 100644
    --- a/src/libs/actions/Report.ts
    +++ b/src/libs/actions/Report.ts
    @@ -1163,7 +1163,11 @@ function getOlderActions(reportID: string, reportActionID: string) {
      * Gets the newer actions that have not been read yet.
      * Normally happens when you are not located at the bottom of the list and scroll down on a chat.
      */
    -function getNewerActions(reportID: string, reportActionID: string) {
    +function getNewerActions(reportID: string | undefined, reportActionID: string | undefined) {
    +    if (!reportID || !reportActionID) {
    +        return;
    +    }
    +
         const optimisticData: OnyxUpdate[] = [
             {
                 onyxMethod: Onyx.METHOD.MERGE,

    The cases and difficult can greatly vary in the codebase, but I think the best move would be to handle these undefined values in the action / lib files like I demonstrated above, so every place that uses these functions can benefit from it.

    'X' is possibly 'undefined'.

    Let's take this example on ReconciliationAccountSettingsPage.tsx

    src/pages/workspace/accounting/reconciliation/ReconciliationAccountSettingsPage.tsx:39:65 - error TS18048: 'paymentBankAccountID' is possibly 'undefined'.
    
    39     const selectedBankAccount = useMemo(() => bankAccountList?.[paymentBankAccountID.toString()], [paymentBankAccountID, bankAccountList]);
                                                                       ~~~~~~~~~~~~~~~~~~~~
    

    This error is inside a component, so we can't just make conditions with early returns here. Also we can't do paymentBankAccountID?.toString() because we'll get another TS error: Type 'undefined' cannot be used as an index type.

    diff --git a/src/pages/workspace/accounting/reconciliation/ReconciliationAccountSettingsPage.tsx b/src/pages/workspace/accounting/reconciliation/ReconciliationAccountSettingsPage.tsx
    index ee65d11e147..e4bf0138dfe 100644
    --- a/src/pages/workspace/accounting/reconciliation/ReconciliationAccountSettingsPage.tsx
    +++ b/src/pages/workspace/accounting/reconciliation/ReconciliationAccountSettingsPage.tsx
    @@ -36,7 +36,7 @@ function ReconciliationAccountSettingsPage({route}: ReconciliationAccountSetting
         const [cardSettings] = useOnyx(`${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`);
         const paymentBankAccountID = cardSettings?.paymentBankAccountID;
     
    -    const selectedBankAccount = useMemo(() => bankAccountList?.[paymentBankAccountID.toString()], [paymentBankAccountID, bankAccountList]);
    +    const selectedBankAccount = useMemo(() => bankAccountList?.[String(paymentBankAccountID)], [paymentBankAccountID, bankAccountList]);
         const bankAccountNumber = useMemo(() => selectedBankAccount?.accountData?.accountNumber ?? '', [selectedBankAccount]);
         const settlementAccountEnding = getLastFourDigits(bankAccountNumber);

    A possible solution would be using String(paymentBankAccountID) to try to convert the value to string. If the value is undefined the result string will be "undefined", which will be used to find a record inside bankAccountList and, same as -1, would find nothing.

    Type 'undefined' cannot be used as an index type.

    Let's take this example on Report.ts

    src/libs/actions/Report.ts:901:63 - error TS2538: Type 'undefined' cannot be used as an index type.
    
    901             const isOptimisticAccount = !allPersonalDetails?.[accountID];
                                                                      ~~~~~~~~~
    

    Here the best solution is put a condition to make the function return early if accountID is falsy.

    diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts
    index f58f29d2640..b32d5b6abdf 100644
    --- a/src/libs/actions/Report.ts
    +++ b/src/libs/actions/Report.ts
    @@ -898,6 +898,10 @@ function openReport(
             const participantAccountIDs = PersonalDetailsUtils.getAccountIDsByLogins(participantLoginList);
             participantLoginList.forEach((login, index) => {
                 const accountID = participantAccountIDs.at(index);
    +            if (!accountID) {
    +                return;
    +            }
    +
                 const isOptimisticAccount = !allPersonalDetails?.[accountID];
     
                 if (!isOptimisticAccount) {

    We could use String() to convert accountID to string or "undefined", but this will create more TS errors as optimisticPersonalDetails[accountID] object expects a number in the accountID property.


    These are only some examples of errors with proposed solutions, but we will definitely have more complex scenarios that need to be evaluated individually.

    As you can see this is a big refactor in the application, so the cost and impact of this must be taken into consideration. Also we should decide if this is really the definitive move we want to do.

    I imagine that these would be the tasks:

    1. Make all these replacements in the codebase and fix them properly in one PR (maybe we could split into more?).
    2. Update the guidelines to reflect the new standards (don't default any ID values unless absolutely necessary), and provide some examples and common use cases.
    3. Maybe add a new item to the checklist for the reviewer to check this and ensure we keep the codebase standardised.
    4. Additionally, we could implement an improvement in Onyx to "ignore" any updates and retrievals of collection entries that has the key undefined or -1 (we can make this configurable in Onyx API), something that could be done in parallel or before/after this.
  4. melvin-bot commented on Oct 11, 2024

    @melvin-bot

    @neil-marcellini Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!

  5. added
    ExternalAdded to denote the issue can be worked on by a contributor
    on Oct 11, 2024
  6. melvin-bot commented on Oct 11, 2024

    @melvin-bot
  7. changed the title [-]Undo defaults of -1 and "-1" for account/report/policy IDs [/-] [+][$250] Undo defaults of -1 and "-1" for account/report/policy IDs [/+] on Oct 11, 2024
  8. removed
    Help WantedApply this label when an issue is open to proposals by contributors
    on Oct 11, 2024
  9. melvin-bot commented on Oct 11, 2024

    @melvin-bot

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

  10. 197 remaining items

  11. allgandalf commented on Feb 13, 2025

    @allgandalf
    Contributor

    @laurenreidexpensify can you also issue me payment as per this

  12. laurenreidexpensify commented on Feb 13, 2025

    @laurenreidexpensify
    Contributor

    Sure @allgandalf are you also Upwork payments still?

  13. allgandalf commented on Feb 13, 2025

    @allgandalf
    Contributor

    Sadly yessssss :))

  14. laurenreidexpensify commented on Feb 13, 2025

    @laurenreidexpensify
    Contributor
  15. paultsimura commented on Feb 13, 2025

    @paultsimura
    Contributor

    Hey @paultsimura we've confirmed internally that payment will be 11 x $250 here ($2250) for C+ review. I've issued this in Upwork 👍

    Woah, that's definitely more than I expected. Thank you so much for handling this😌

  16. laurenreidexpensify commented on Feb 17, 2025

    @laurenreidexpensify
    Contributor

    @allgandalf can you confirm that you've applied thanks

  17. allgandalf commented on Feb 19, 2025

    @allgandalf
    Contributor

    Apologies for the delay, I can confirm that i have applied for the job

  18. laurenreidexpensify commented on Feb 26, 2025

    @laurenreidexpensify
    Contributor

    Sorry @allgandalf delay was me now! Please accept offer 🙏

  19. laurenreidexpensify commented on Feb 26, 2025

    @laurenreidexpensify
    Contributor

    All payments issued, closing 👍

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 productionDailyKSv2ExternalAdded to denote the issue can be worked on by a contributor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions