From b0701482795e616e3ada61a0dda3796cc847ff5a Mon Sep 17 00:00:00 2001 From: daledah Date: Tue, 4 Mar 2025 22:52:41 +0700 Subject: [PATCH 1/3] fix: no RBR when deleting distance rates --- src/libs/actions/Policy/DistanceRate.ts | 55 ++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Policy/DistanceRate.ts b/src/libs/actions/Policy/DistanceRate.ts index a56cc3d576e97..f740002c71d81 100644 --- a/src/libs/actions/Policy/DistanceRate.ts +++ b/src/libs/actions/Policy/DistanceRate.ts @@ -17,7 +17,7 @@ import {getDistanceRateCustomUnit, goBackWhenEnableFeature, removePendingFieldsF import * as ReportUtils from '@libs/ReportUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Policy, Report} from '@src/types/onyx'; +import type {Policy, Report, Transaction, TransactionViolation} from '@src/types/onyx'; import type {ErrorFields} from '@src/types/onyx/OnyxCommon'; import type {Attributes, CustomUnit, Rate} from '@src/types/onyx/Policy'; import type {OnyxData} from '@src/types/onyx/Request'; @@ -62,6 +62,29 @@ Onyx.connect({ }, }); +let allTransactions: NonNullable> = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, + callback: (value) => { + if (!value) { + allTransactions = {}; + return; + } + + allTransactions = value; + }, +}); + +let transactionViolations: OnyxCollection; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, + waitForCollectionCallback: true, + callback: (value) => { + transactionViolations = value; + }, +}); + /** * Takes array of customUnitRates and removes pendingFields and errorFields from each rate - we don't want to send those via API */ @@ -550,6 +573,36 @@ function deletePolicyDistanceRates(policyID: string, customUnit: CustomUnit, rat }, ]; + const transactions = Object.values(allTransactions ?? {}).filter((transaction) => transaction?.comment?.customUnit?.customUnitID === customUnit.customUnitID); + const optimisticTransactionsViolations: OnyxUpdate[] = []; + + transactions.forEach((transaction) => { + const currentTransactionViolations = transactionViolations?.[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction?.transactionID}`] ?? []; + if (currentTransactionViolations.some((violation) => violation.name === CONST.VIOLATIONS.CUSTOM_UNIT_OUT_OF_POLICY)) { + return; + } + optimisticTransactionsViolations.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction?.transactionID}`, + value: [ + ...currentTransactionViolations, + { + type: CONST.VIOLATION_TYPES.VIOLATION, + name: CONST.VIOLATIONS.CUSTOM_UNIT_OUT_OF_POLICY, + showInReview: true, + }, + ], + }); + }); + + const failureTransactionsViolations: OnyxUpdate[] = transactions.map((transaction) => { + const currentTransactionViolations = transactionViolations?.[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction?.transactionID}`]; + return {onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction?.transactionID}`, value: currentTransactionViolations}; + }); + + optimisticData.push(...optimisticTransactionsViolations); + failureData.push(...failureTransactionsViolations); + const params: DeletePolicyDistanceRatesParams = { policyID, customUnitID: customUnit.customUnitID, From 4529623c39ea5a26688a346b9a486ea82855f4a7 Mon Sep 17 00:00:00 2001 From: daledah Date: Wed, 5 Mar 2025 13:47:42 +0700 Subject: [PATCH 2/3] fix: show #admin room when invite user through actionable whisper --- src/libs/ReportActionsUtils.ts | 4 + tests/unit/ReportActionsUtilsTest.ts | 130 +++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 9efe8c1cb0141..49ae2911a62de 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -148,6 +148,10 @@ function isCreatedAction(reportAction: OnyxInputOrEntry): boolean function isDeletedAction(reportAction: OnyxInputOrEntry): boolean { const message = reportAction?.message ?? []; + if (reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.POLICY_CHANGE_LOG.INVITE_TO_ROOM) { + return false; + } + if (!Array.isArray(message)) { return message?.html === '' || !!message?.deleted; } diff --git a/tests/unit/ReportActionsUtilsTest.ts b/tests/unit/ReportActionsUtilsTest.ts index 5e2c948875b2e..cb139466428e1 100644 --- a/tests/unit/ReportActionsUtilsTest.ts +++ b/tests/unit/ReportActionsUtilsTest.ts @@ -714,4 +714,134 @@ describe('ReportActionsUtils', () => { expect(res).toEqual(false); }); }); + + describe('isDeletedAction', () => { + it('should return true if reportAction is undefined', () => { + expect(ReportActionsUtils.isDeletedAction(undefined)).toBe(true); + }); + + it('should return false for POLICY_CHANGE_LOG.INVITE_TO_ROOM action', () => { + const reportAction = { + actionName: CONST.REPORT.ACTIONS.TYPE.POLICY_CHANGE_LOG.INVITE_TO_ROOM, + originalMessage: { + html: '', + whisperedTo: [], + }, + reportActionID: '1', + created: '1', + }; + expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(false); + }); + + it('should return true if message is an empty array', () => { + const reportAction = { + created: '2022-11-09 22:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + originalMessage: { + html: 'Hello world', + whisperedTo: [], + }, + }; + expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(true); + }); + + it('should return true if message html is empty', () => { + const reportAction = { + created: '2022-11-09 22:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + originalMessage: { + html: 'Hello world', + whisperedTo: [], + }, + message: { + html: '', + type: 'Action type', + text: 'Action text', + }, + }; + expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(true); + }); + + it('should return true if message is not an array and deleted is not empty', () => { + const reportAction = { + created: '2022-11-09 22:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + originalMessage: { + html: 'Hello world', + whisperedTo: [], + }, + message: { + html: 'Hello world', + deleted: 'deleted', + type: 'Action type', + text: 'Action text', + }, + }; + expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(true); + }); + + it('should return true if message an array and first element deleted is not empty', () => { + const reportAction = { + created: '2022-11-09 22:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + originalMessage: { + html: 'Hello world', + whisperedTo: [], + }, + message: [ + { + html: 'Hello world', + deleted: 'deleted', + type: 'Action type', + text: 'Action text', + }, + ], + }; + expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(true); + }); + + it('should return true if message is an object with html field with empty string as value is empty', () => { + const reportAction = { + created: '2022-11-09 22:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + originalMessage: { + html: 'Hello world', + whisperedTo: [], + }, + message: [ + { + html: '', + type: 'Action type', + text: 'Action text', + }, + ], + }; + expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(true); + }); + + it('should return false otherwise', () => { + const reportAction = { + created: '2022-11-09 22:27:01.825', + reportActionID: '8401445780099176', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + originalMessage: { + html: 'Hello world', + whisperedTo: [], + }, + message: [ + { + html: 'Hello world', + type: 'Action type', + text: 'Action text', + }, + ], + }; + expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(false); + }); + }); }); From e792ff98c4bf40703c445b285aaaab10eaa5aec3 Mon Sep 17 00:00:00 2001 From: daledah Date: Wed, 5 Mar 2025 13:48:32 +0700 Subject: [PATCH 3/3] Revert "fix: show #admin room when invite user through actionable whisper" This reverts commit 4529623c39ea5a26688a346b9a486ea82855f4a7. --- src/libs/ReportActionsUtils.ts | 4 - tests/unit/ReportActionsUtilsTest.ts | 130 --------------------------- 2 files changed, 134 deletions(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 49ae2911a62de..9efe8c1cb0141 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -148,10 +148,6 @@ function isCreatedAction(reportAction: OnyxInputOrEntry): boolean function isDeletedAction(reportAction: OnyxInputOrEntry): boolean { const message = reportAction?.message ?? []; - if (reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.POLICY_CHANGE_LOG.INVITE_TO_ROOM) { - return false; - } - if (!Array.isArray(message)) { return message?.html === '' || !!message?.deleted; } diff --git a/tests/unit/ReportActionsUtilsTest.ts b/tests/unit/ReportActionsUtilsTest.ts index cb139466428e1..5e2c948875b2e 100644 --- a/tests/unit/ReportActionsUtilsTest.ts +++ b/tests/unit/ReportActionsUtilsTest.ts @@ -714,134 +714,4 @@ describe('ReportActionsUtils', () => { expect(res).toEqual(false); }); }); - - describe('isDeletedAction', () => { - it('should return true if reportAction is undefined', () => { - expect(ReportActionsUtils.isDeletedAction(undefined)).toBe(true); - }); - - it('should return false for POLICY_CHANGE_LOG.INVITE_TO_ROOM action', () => { - const reportAction = { - actionName: CONST.REPORT.ACTIONS.TYPE.POLICY_CHANGE_LOG.INVITE_TO_ROOM, - originalMessage: { - html: '', - whisperedTo: [], - }, - reportActionID: '1', - created: '1', - }; - expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(false); - }); - - it('should return true if message is an empty array', () => { - const reportAction = { - created: '2022-11-09 22:27:01.825', - reportActionID: '8401445780099176', - actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, - originalMessage: { - html: 'Hello world', - whisperedTo: [], - }, - }; - expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(true); - }); - - it('should return true if message html is empty', () => { - const reportAction = { - created: '2022-11-09 22:27:01.825', - reportActionID: '8401445780099176', - actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, - originalMessage: { - html: 'Hello world', - whisperedTo: [], - }, - message: { - html: '', - type: 'Action type', - text: 'Action text', - }, - }; - expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(true); - }); - - it('should return true if message is not an array and deleted is not empty', () => { - const reportAction = { - created: '2022-11-09 22:27:01.825', - reportActionID: '8401445780099176', - actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, - originalMessage: { - html: 'Hello world', - whisperedTo: [], - }, - message: { - html: 'Hello world', - deleted: 'deleted', - type: 'Action type', - text: 'Action text', - }, - }; - expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(true); - }); - - it('should return true if message an array and first element deleted is not empty', () => { - const reportAction = { - created: '2022-11-09 22:27:01.825', - reportActionID: '8401445780099176', - actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, - originalMessage: { - html: 'Hello world', - whisperedTo: [], - }, - message: [ - { - html: 'Hello world', - deleted: 'deleted', - type: 'Action type', - text: 'Action text', - }, - ], - }; - expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(true); - }); - - it('should return true if message is an object with html field with empty string as value is empty', () => { - const reportAction = { - created: '2022-11-09 22:27:01.825', - reportActionID: '8401445780099176', - actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, - originalMessage: { - html: 'Hello world', - whisperedTo: [], - }, - message: [ - { - html: '', - type: 'Action type', - text: 'Action text', - }, - ], - }; - expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(true); - }); - - it('should return false otherwise', () => { - const reportAction = { - created: '2022-11-09 22:27:01.825', - reportActionID: '8401445780099176', - actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, - originalMessage: { - html: 'Hello world', - whisperedTo: [], - }, - message: [ - { - html: 'Hello world', - type: 'Action type', - text: 'Action text', - }, - ], - }; - expect(ReportActionsUtils.isDeletedAction(reportAction)).toBe(false); - }); - }); });