Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 57 additions & 34 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import type {
Request,
TaxRatesWithDefault,
Transaction,
TransactionViolations,
} from '@src/types/onyx';
import type {Errors} from '@src/types/onyx/OnyxCommon';
import type {Attributes, CompanyAddress, CustomUnit, NetSuiteCustomList, NetSuiteCustomSegment, Rate, TaxRate} from '@src/types/onyx/Policy';
Expand Down Expand Up @@ -228,6 +229,13 @@ Onyx.connect({
callback: (value) => (activePolicyID = value),
});

let allTransactionViolations: OnyxCollection<TransactionViolations> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
waitForCollectionCallback: true,
callback: (value) => (allTransactionViolations = value),
});

/**
* Stores in Onyx the policy ID of the last workspace that was accessed by the user
*/
Expand Down Expand Up @@ -330,13 +338,33 @@ function deleteWorkspace(policyID: string, policyName: string) {
: []),
];

const policy = getPolicy(policyID);
// Restore the old report stateNum and statusNum
const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
errors: reimbursementAccount?.errors ?? null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
avatarURL: policy?.avatarURL,
pendingAction: null,
},
},
];

const reportsToArchive = Object.values(allReports ?? {}).filter(
(report) => ReportUtils.isPolicyRelatedReport(report, policyID) && (ReportUtils.isChatRoom(report) || ReportUtils.isPolicyExpenseChat(report) || ReportUtils.isTaskReport(report)),
);
const finallyData: OnyxUpdate[] = [];
const currentTime = DateUtils.getDBTime();
reportsToArchive.forEach((report) => {
const {reportID, ownerAccountID} = report ?? {};
const {reportID, ownerAccountID, oldPolicyName} = report ?? {};
const isInvoiceReceiverReport = report?.invoiceReceiver && 'policyID' in report.invoiceReceiver && report.invoiceReceiver.policyID === policyID;
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -379,39 +407,6 @@ function deleteWorkspace(policyID: string, policyName: string) {
},
});

// We are temporarily adding this workaround because 'DeleteWorkspace' doesn't
// support receiving the optimistic reportActions' ids for the moment.
finallyData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
value: {
[optimisticClosedReportAction.reportActionID]: null,
},
});
});

const policy = getPolicy(policyID);
// Restore the old report stateNum and statusNum
const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
errors: reimbursementAccount?.errors ?? null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
avatarURL: policy?.avatarURL,
pendingAction: null,
},
},
];

reportsToArchive.forEach((report) => {
const {reportID, oldPolicyName} = report ?? {};
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
Expand All @@ -421,13 +416,41 @@ function deleteWorkspace(policyID: string, policyName: string) {
isPinned: report?.isPinned,
},
});

failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`,
value: {
private_isArchived: null,
},
});

// We are temporarily adding this workaround because 'DeleteWorkspace' doesn't
// support receiving the optimistic reportActions' ids for the moment.
finallyData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
value: {
[optimisticClosedReportAction.reportActionID]: null,
},
});

if (report?.iouReportID) {
const reportTransactions = ReportUtils.getReportTransactions(report.iouReportID);
for (const transaction of reportTransactions) {
const violations = allTransactionViolations?.[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`];
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`,
value: violations?.filter((violation) => violation.type !== CONST.VIOLATION_TYPES.VIOLATION),
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`,
Copy link
Contributor

@DylanDylann DylanDylann Oct 7, 2025

Choose a reason for hiding this comment

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

This key in the failure data is different from the key in the optimistic data. @bernhardoj @sobitneupane Is this intentional or a mistake?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, this must be a mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you have an ongoing PR that touches this code? If not, I can open a PR to fix this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixing here: #71473

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

value: violations,
});
}
}
});

const params: DeleteWorkspaceParams = {policyID};
Expand Down
39 changes: 38 additions & 1 deletion tests/actions/PolicyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import CONST from '@src/CONST';
import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager';
import * as Policy from '@src/libs/actions/Policy/Policy';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy as PolicyType, Report, ReportAction, ReportActions} from '@src/types/onyx';
import type {Policy as PolicyType, Report, ReportAction, ReportActions, TransactionViolations} from '@src/types/onyx';
import type {Participant} from '@src/types/onyx/Report';
import createRandomPolicy from '../utils/collections/policies';
import createRandomReport from '../utils/collections/reports';
Expand Down Expand Up @@ -444,6 +444,43 @@ describe('actions/Policy', () => {
});
});
});

it('should remove violation from expense report', async () => {
const policyID = '123';
const expenseChatReportID = '1';
const expenseReportID = '2';
const transactionID = '3';

await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${expenseChatReportID}`, {
...createRandomReport(Number(expenseChatReportID)),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
policyID,
iouReportID: expenseReportID,
});

await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
reportID: expenseReportID,
transactionID,
});

await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, [
{name: 'cashExpenseWithNoReceipt', type: CONST.VIOLATION_TYPES.VIOLATION},
{name: 'hold', type: CONST.VIOLATION_TYPES.WARNING},
]);

Policy.deleteWorkspace(policyID, 'test');

await waitForBatchedUpdates();

const violations = await new Promise<OnyxEntry<TransactionViolations>>((resolve) => {
Onyx.connect({
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`,
callback: resolve,
});
});

expect(violations?.every((violation) => violation.type !== CONST.VIOLATION_TYPES.VIOLATION)).toBe(true);
});
});

describe('enablePolicyWorkflows', () => {
Expand Down
Loading