Skip to content
2 changes: 1 addition & 1 deletion src/hooks/useSelectedTransactionsActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function useSelectedTransactionsActions({
if (!action?.childReportID) {
continue;
}
unholdRequest(transactionID, action?.childReportID);
unholdRequest(transactionID, action?.childReportID, policy);
}
clearSelectedTransactions(true);
},
Expand Down
10 changes: 5 additions & 5 deletions src/libs/NextStepUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function buildOptimisticNextStep(params: BuildNextStepNewParams): ReportNextStep
};

// Scheduled submit enabled
if (policy?.harvesting?.enabled && autoReportingFrequency !== CONST.POLICY.AUTO_REPORTING_FREQUENCIES.MANUAL && isReportContainingTransactions) {
if (policy?.harvesting?.enabled && isReportContainingTransactions) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The autoReportingFrequency is never set to manual.

Manual auto reporting is actually === harvesting.enabled = false && autoReportingFrequency = daily.

Checking for harvesting.enabled is enough.

(same applies for other similar changes below)

nextStep = {
messageKey: CONST.NEXT_STEP.MESSAGE_KEY.WAITING_FOR_AUTOMATIC_SUBMIT,
icon: CONST.NEXT_STEP.ICONS.HOURGLASS,
Expand Down Expand Up @@ -183,7 +183,7 @@ function buildOptimisticNextStep(params: BuildNextStepNewParams): ReportNextStep
}

// Manual submission
if (report?.total !== 0 && !policy?.harvesting?.enabled && autoReportingFrequency === CONST.POLICY.AUTO_REPORTING_FREQUENCIES.MANUAL) {
if (report?.total !== 0 && !policy?.harvesting?.enabled) {
nextStep = {
messageKey: CONST.NEXT_STEP.MESSAGE_KEY.WAITING_TO_SUBMIT,
icon: CONST.NEXT_STEP.ICONS.HOURGLASS,
Expand Down Expand Up @@ -520,7 +520,7 @@ function buildNextStepNew(params: BuildNextStepNewParams): ReportNextStepDepreca
text: ' to ',
},
{
text: 'add',
text: isReportContainingTransactions ? 'submit' : 'add',
},
{
text: ' %expenses.',
Expand All @@ -529,7 +529,7 @@ function buildNextStepNew(params: BuildNextStepNewParams): ReportNextStepDepreca
};

// Scheduled submit enabled
if (policy?.harvesting?.enabled && autoReportingFrequency !== CONST.POLICY.AUTO_REPORTING_FREQUENCIES.MANUAL && isReportContainingTransactions) {
if (policy?.harvesting?.enabled && isReportContainingTransactions) {
optimisticNextStep.message = [
{
text: 'Waiting for ',
Expand Down Expand Up @@ -583,7 +583,7 @@ function buildNextStepNew(params: BuildNextStepNewParams): ReportNextStepDepreca
}

// Manual submission
if (report?.total !== 0 && !policy?.harvesting?.enabled && autoReportingFrequency === CONST.POLICY.AUTO_REPORTING_FREQUENCIES.MANUAL) {
if (report?.total !== 0 && !policy?.harvesting?.enabled) {
optimisticNextStep.message = [
{
text: 'Waiting for ',
Expand Down
4 changes: 2 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4878,11 +4878,11 @@ const changeMoneyRequestHoldStatus = (reportAction: OnyxEntry<ReportAction>): vo

const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? ({} as Transaction);
const isOnHold = isOnHoldTransactionUtils(transaction);
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${moneyRequestReport.policyID}`] ?? null;
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${moneyRequestReport.policyID}`];

if (isOnHold) {
if (reportAction.childReportID) {
unholdRequest(transactionID, reportAction.childReportID);
unholdRequest(transactionID, reportAction.childReportID, policy);
} else {
Log.warn('Missing reportAction.childReportID during money request unhold');
}
Expand Down
159 changes: 152 additions & 7 deletions src/libs/actions/IOU/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@
};

let allPersonalDetails: OnyxTypes.PersonalDetailsList = {};
Onyx.connect({

Check warning on line 740 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
allPersonalDetails = value ?? {};
Expand Down Expand Up @@ -839,7 +839,7 @@
};

let allTransactions: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {};
Onyx.connect({

Check warning on line 842 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -853,7 +853,7 @@
});

let allTransactionDrafts: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {};
Onyx.connect({

Check warning on line 856 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_DRAFT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -862,7 +862,7 @@
});

let allTransactionViolations: NonNullable<OnyxCollection<OnyxTypes.TransactionViolations>> = {};
Onyx.connect({

Check warning on line 865 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -876,7 +876,7 @@
});

let allNextSteps: NonNullable<OnyxCollection<OnyxTypes.ReportNextStepDeprecated>> = {};
Onyx.connect({

Check warning on line 879 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.NEXT_STEP,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -885,7 +885,7 @@
});

let allReports: OnyxCollection<OnyxTypes.Report>;
Onyx.connect({

Check warning on line 888 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -894,7 +894,7 @@
});

let allReportNameValuePairs: OnyxCollection<OnyxTypes.ReportNameValuePairs>;
Onyx.connect({

Check warning on line 897 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -904,7 +904,7 @@

let userAccountID = -1;
let currentUserEmail = '';
Onyx.connect({

Check warning on line 907 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
currentUserEmail = value?.email ?? '';
Expand All @@ -913,7 +913,7 @@
});

let deprecatedCurrentUserPersonalDetails: OnyxEntry<OnyxTypes.PersonalDetails>;
Onyx.connect({

Check warning on line 916 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
deprecatedCurrentUserPersonalDetails = value?.[userAccountID] ?? undefined;
Expand All @@ -922,7 +922,7 @@

let deprecatedQuickAction: OnyxEntry<OnyxTypes.QuickAction> = {};
// eslint-disable-next-line @typescript-eslint/no-deprecated
Onyx.connect({

Check warning on line 925 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE,
callback: (value) => {
deprecatedQuickAction = value;
Expand Down Expand Up @@ -11823,7 +11823,9 @@
optimisticData.push(parentActionData);
}

const successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.REPORT_METADATA>> = [
const successData: Array<
OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_METADATA>
> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
Expand All @@ -11834,7 +11836,13 @@
];

const failureData: Array<
OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_METADATA>
OnyxUpdate<
| typeof ONYXKEYS.COLLECTION.TRANSACTION
| typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS
| typeof ONYXKEYS.COLLECTION.REPORT
| typeof ONYXKEYS.COLLECTION.REPORT_METADATA
| typeof ONYXKEYS.COLLECTION.NEXT_STEP
>
> = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -11938,6 +11946,67 @@
);
}

if (iouReport) {
// buildOptimisticNextStep is used in parallel
// eslint-disable-next-line @typescript-eslint/no-deprecated
const optimisticNextStepDeprecated = buildNextStepNew({
report: iouReport,
predictedNextStatus: iouReport.statusNum ?? CONST.REPORT.STATUS_NUM.OPEN,
shouldFixViolations: true,
currentUserAccountIDParam: userAccountID,
currentUserEmailParam: currentUserEmail,
});
const optimisticNextStep = buildOptimisticNextStep({
report: iouReport,
predictedNextStatus: iouReport.statusNum ?? CONST.REPORT.STATUS_NUM.OPEN,
shouldFixViolations: true,
currentUserAccountIDParam: userAccountID,
currentUserEmailParam: currentUserEmail,
});

optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`,
value: optimisticNextStepDeprecated,
});
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: {
nextStep: optimisticNextStep,
pendingFields: {
nextStep: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
},
});

successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: {
pendingFields: {
nextStep: null,
},
},
});

failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`,
value: null,
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: {
nextStep: iouReport.nextStep ?? null,
pendingFields: {
nextStep: null,
},
},
});
}

const params: HoldMoneyRequestParams = {
transactionID,
comment,
Expand Down Expand Up @@ -11966,15 +12035,22 @@
/**
* Remove expense from HOLD
*/
function unholdRequest(transactionID: string, reportID: string) {
function unholdRequest(transactionID: string, reportID: string, policy: OnyxEntry<OnyxTypes.Policy>) {
const createdReportAction = buildOptimisticUnHoldReportAction();
const transactionViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`];
const updatedTransactionViolations = transactionViolations?.filter((violation) => violation.name !== CONST.VIOLATIONS.HOLD) ?? [];
const transaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`];
const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transaction?.reportID}`];
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];

const optimisticData: Array<
OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.TRANSACTION | typeof ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS | typeof ONYXKEYS.COLLECTION.REPORT>
OnyxUpdate<
| typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS
| typeof ONYXKEYS.COLLECTION.TRANSACTION
| typeof ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS
| typeof ONYXKEYS.COLLECTION.REPORT
| typeof ONYXKEYS.COLLECTION.NEXT_STEP
>
> = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -11996,7 +12072,7 @@
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`,
value: transactionViolations?.filter((violation) => violation.name !== CONST.VIOLATIONS.HOLD) ?? [],
value: updatedTransactionViolations,
},
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -12021,7 +12097,7 @@
});
}

const successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION>> = [
const successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.TRANSACTION | typeof ONYXKEYS.COLLECTION.REPORT>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
Expand All @@ -12035,7 +12111,13 @@
];

const failureData: Array<
OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.TRANSACTION | typeof ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS | typeof ONYXKEYS.COLLECTION.REPORT>
OnyxUpdate<
| typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS
| typeof ONYXKEYS.COLLECTION.TRANSACTION
| typeof ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS
| typeof ONYXKEYS.COLLECTION.REPORT
| typeof ONYXKEYS.COLLECTION.NEXT_STEP
>
> = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -12069,6 +12151,69 @@
},
];

if (iouReport) {
// buildOptimisticNextStep is used in parallel
// eslint-disable-next-line @typescript-eslint/no-deprecated
const optimisticNextStepDeprecated = buildNextStepNew({
report: iouReport,
policy,
predictedNextStatus: iouReport.statusNum ?? CONST.REPORT.STATUS_NUM.OPEN,
shouldFixViolations: updatedTransactionViolations.length > 0,
currentUserAccountIDParam: userAccountID,
currentUserEmailParam: currentUserEmail,
});
const optimisticNextStep = buildOptimisticNextStep({
report: iouReport,
policy,
predictedNextStatus: iouReport.statusNum ?? CONST.REPORT.STATUS_NUM.OPEN,
shouldFixViolations: updatedTransactionViolations.length > 0,
currentUserAccountIDParam: userAccountID,
currentUserEmailParam: currentUserEmail,
});

optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`,
value: optimisticNextStepDeprecated,
});
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: {
nextStep: optimisticNextStep,
pendingFields: {
nextStep: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
},
});

successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: {
pendingFields: {
nextStep: null,
},
},
});

failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`,
value: null,
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: {
nextStep: iouReport.nextStep ?? null,
pendingFields: {
nextStep: null,
},
},
});
}

API.write(
WRITE_COMMANDS.UNHOLD_MONEY_REQUEST,
{
Expand Down
22 changes: 18 additions & 4 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6634,7 +6634,14 @@ describe('actions/IOU', () => {

describe('unHoldRequest', () => {
test("should update the transaction thread report's lastVisibleActionCreated to the optimistically added unhold report action created timestamp", () => {
const iouReport = buildOptimisticIOUReport(1, 2, 100, '1', 'USD');
const policyID = '577';
const policy: Policy = {
...createRandomPolicy(Number(policyID)),
};
const iouReport: Report = {
...buildOptimisticIOUReport(1, 2, 100, '1', 'USD'),
policyID,
};
const transaction = buildOptimisticTransaction({
transactionParams: {
amount: 100,
Expand Down Expand Up @@ -6672,7 +6679,7 @@ describe('actions/IOU', () => {
})
.then(() => {
// When an expense is unhold
unholdRequest(transaction.transactionID, transactionThread.reportID);
unholdRequest(transaction.transactionID, transactionThread.reportID, policy);
return waitForBatchedUpdates();
})
.then(() => {
Expand Down Expand Up @@ -6700,7 +6707,14 @@ describe('actions/IOU', () => {
});

test('should rollback unhold request on API failure', () => {
const iouReport = buildOptimisticIOUReport(1, 2, 100, '1', 'USD');
const policyID = '577';
const policy: Policy = {
...createRandomPolicy(Number(policyID)),
};
const iouReport: Report = {
...buildOptimisticIOUReport(1, 2, 100, '1', 'USD'),
policyID,
};
const transaction = buildOptimisticTransaction({
transactionParams: {
amount: 100,
Expand Down Expand Up @@ -6739,7 +6753,7 @@ describe('actions/IOU', () => {
.then(() => {
mockFetch.fail();
mockFetch?.resume?.();
unholdRequest(transaction.transactionID, transactionThread.reportID);
unholdRequest(transaction.transactionID, transactionThread.reportID, policy);
return waitForBatchedUpdates();
})
.then(() => {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/NextStepUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('libs/NextStepUtils', () => {
test('self review', () => {
optimisticNextStep.icon = CONST.NEXT_STEP.ICONS.HOURGLASS;

// Waiting for userSubmitter to add expense(s).
// Waiting for userSubmitter to submit expense(s).
optimisticNextStep.message = [
{
text: 'Waiting for ',
Expand All @@ -175,7 +175,7 @@ describe('libs/NextStepUtils', () => {
text: ' to ',
},
{
text: 'add',
text: 'submit',
},
{
text: ' %expenses.',
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/hooks/useSelectedTransactionsActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ describe('useSelectedTransactionsActions', () => {

unholdOption?.onSelected?.();

expect(unholdRequest).toHaveBeenCalledWith(transactionID, 'child123');
expect(unholdRequest).toHaveBeenCalledWith(transactionID, 'child123', undefined);
expect(mockClearSelectedTransactions).toHaveBeenCalledWith(true);
});

Expand Down
Loading