You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[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
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
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.
@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:
Replace "ID ?? ''" with "ID"
Replace "id ?? ''" with "id"
Replace "ID ?? '-1'" with "ID"
Replace "id ?? '-1'" with "id"
Replace "ID ?? -1" with "ID"
Replace "id ?? -1" with "id"
Replace "ID ?? '0'" with "ID"
Replace "id ?? '0'" with "id" // nothing
Replace "ID ?? 0" with "ID"
Replace "id ?? 0" with "id" // nothing
Replace "ID || ''" with "ID"
Replace "id || ''" with "id" // nothing
Replace "ID || '-1'" with "ID"
Replace "id || '-1'" with "id" // nothing
Replace "ID || -1" with "ID"
Replace "id || -1" with "id" // nothing
Replace "ID || '0'" with "ID" // nothing
Replace "id || '0'" with "id" // nothing
Replace "id || 0" with "id" // nothing
Replace " : '-1'" with " : undefined"
Replace " : -1" with " : undefined"
Replace " : '0'" with " : undefined"
Replace " ?? '-1'" with ""
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'.
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);
~~~~~~~~
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) {
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.
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.
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.
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:
Make all these replacements in the codebase and fix them properly in one PR (maybe we could split into more?).
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.
Maybe add a new item to the checklist for the reviewer to check this and ensure we keep the codebase standardised.
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.
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
Activity
ChavdaSachin commentedon Oct 7, 2024
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?
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.
fabioh8010 commentedon Oct 10, 2024
@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
undefinedwhere we would then handle the changes in the codebase.So first I did the following replaces in order in the codebase:
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 typecheckto check for TS errors, it was found found 1212 errors in 290 files.And after running
npm run lintto 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
ReportActionsUtils.getOneTransactionThreadReportID() requires
reportIDto bestring. In this case we can modifyreportIDto bestring | undefinedand we probably won't need to do anything else here.Now let's take this example on ReportActionsView.tsx
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.The cases and difficult can greatly vary in the codebase, but I think the best move would be to handle these
undefinedvalues 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
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.A possible solution would be using
String(paymentBankAccountID)to try to convert the value to string. If the value isundefinedthe result string will be "undefined", which will be used to find a record insidebankAccountListand, same as-1, would find nothing.Type 'undefined' cannot be used as an index type.
Let's take this example on Report.ts
Here the best solution is put a condition to make the function return early if
accountIDis falsy.We could use
String()to convertaccountIDto string or "undefined", but this will create more TS errors asoptimisticPersonalDetails[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:
undefinedor-1(we can make this configurable in Onyx API), something that could be done in parallel or before/after this.melvin-bot commentedon Oct 11, 2024
@neil-marcellini Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!
melvin-bot commentedon Oct 11, 2024
Job added to Upwork: https://www.upwork.com/jobs/~021844825805028000985
[-]Undo defaults of -1 and "-1" for account/report/policy IDs [/-][+][$250] Undo defaults of -1 and "-1" for account/report/policy IDs [/+]melvin-bot commentedon Oct 11, 2024
Triggered auto assignment to Contributor-plus team member for initial proposal review - @paultsimura (
External)197 remaining items
allgandalf commentedon Feb 13, 2025
@laurenreidexpensify can you also issue me payment as per this
laurenreidexpensify commentedon Feb 13, 2025
Sure @allgandalf are you also Upwork payments still?
allgandalf commentedon Feb 13, 2025
Sadly yessssss :))
laurenreidexpensify commentedon Feb 13, 2025
@allgandalf https://www.upwork.com/jobs/~021890089344619776971 👍
paultsimura commentedon Feb 13, 2025
Woah, that's definitely more than I expected. Thank you so much for handling this😌
laurenreidexpensify commentedon Feb 17, 2025
@allgandalf can you confirm that you've applied thanks
allgandalf commentedon Feb 19, 2025
Apologies for the delay, I can confirm that i have applied for the job
laurenreidexpensify commentedon Feb 26, 2025
Sorry @allgandalf delay was me now! Please accept offer 🙏
laurenreidexpensify commentedon Feb 26, 2025
All payments issued, closing 👍