From e16779a333724e95c985f9351d3fd5380b875164 Mon Sep 17 00:00:00 2001 From: paulnjs Date: Thu, 30 Oct 2025 18:32:17 +0700 Subject: [PATCH 01/11] fix: Prohibited expense violation shows multiple lines instead of grouping them --- src/hooks/useTransactionViolations.ts | 26 ++++++++++++++++--- src/languages/de.ts | 36 ++++++++++++++------------ src/languages/en.ts | 34 +++++++++++++----------- src/languages/es.ts | 36 ++++++++++++++------------ src/languages/fr.ts | 34 +++++++++++++----------- src/languages/it.ts | 34 +++++++++++++----------- src/languages/ja.ts | 34 +++++++++++++----------- src/languages/nl.ts | 34 +++++++++++++----------- src/languages/params.ts | 2 +- src/languages/pl.ts | 34 +++++++++++++----------- src/languages/pt-BR.ts | 34 +++++++++++++----------- src/languages/zh-hans.ts | 34 +++++++++++++----------- src/libs/Violations/ViolationsUtils.ts | 2 +- src/types/onyx/TransactionViolation.ts | 2 +- 14 files changed, 217 insertions(+), 159 deletions(-) diff --git a/src/hooks/useTransactionViolations.ts b/src/hooks/useTransactionViolations.ts index b4fa29a09db3..8746c971ab20 100644 --- a/src/hooks/useTransactionViolations.ts +++ b/src/hooks/useTransactionViolations.ts @@ -1,12 +1,28 @@ import {useMemo} from 'react'; import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID'; import {isViolationDismissed, shouldShowViolation} from '@libs/TransactionUtils'; +import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {TransactionViolation, TransactionViolations} from '@src/types/onyx'; import getEmptyArray from '@src/types/utils/getEmptyArray'; import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; import useOnyx from './useOnyx'; +function mergeProhibitedViolations(transactionViolations: TransactionViolations): TransactionViolations { + const prohibitedViolations = transactionViolations.filter((violation: TransactionViolation) => violation.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); + + const prohibitedExpenses = prohibitedViolations.flatMap((violation: TransactionViolation) => violation.data?.prohibitedExpenseRule ?? []); + const mergedProhibitedViolations: TransactionViolation = { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + data: { + prohibitedExpenseRule: prohibitedExpenses, + }, + type: CONST.VIOLATION_TYPES.VIOLATION, + }; + + return [...transactionViolations.filter((violation: TransactionViolation) => violation.name !== CONST.VIOLATIONS.PROHIBITED_EXPENSE), mergedProhibitedViolations]; +} + function useTransactionViolations(transactionID?: string, shouldShowRterForSettledReport = true): TransactionViolations { const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(transactionID)}`, { canBeMissing: true, @@ -24,10 +40,12 @@ function useTransactionViolations(transactionID?: string, shouldShowRterForSettl return useMemo( () => - transactionViolations.filter( - (violation: TransactionViolation) => - !isViolationDismissed(transaction, violation, currentUserDetails.email ?? '') && - shouldShowViolation(iouReport, policy, violation.name, currentUserDetails.email ?? '', shouldShowRterForSettledReport), + mergeProhibitedViolations( + transactionViolations.filter( + (violation: TransactionViolation) => + !isViolationDismissed(transaction, violation, currentUserDetails.email ?? '') && + shouldShowViolation(iouReport, policy, violation.name, currentUserDetails.email ?? '', shouldShowRterForSettledReport), + ), ), [transaction, transactionViolations, iouReport, policy, shouldShowRterForSettledReport, currentUserDetails.email], ); diff --git a/src/languages/de.ts b/src/languages/de.ts index 2bd3f0d26878..c6d66b4de253 100644 --- a/src/languages/de.ts +++ b/src/languages/de.ts @@ -6773,22 +6773,26 @@ ${amount} für ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseType}: ViolationsProhibitedExpenseParams) => { - const preMessage = 'Verbotene Ausgabe:'; - switch (prohibitedExpenseType) { - case 'alcohol': - return `${preMessage} Alkohol`; - case 'gambling': - return `${preMessage} Glücksspiel`; - case 'tobacco': - return `${preMessage} Tabak`; - case 'adultEntertainment': - return `${preMessage} Erwachsenenunterhaltung`; - case 'hotelIncidentals': - return `${preMessage} Hotelnebenkosten`; - default: - return `${preMessage}${prohibitedExpenseType}`; - } + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { + const preMessage = 'Verbotene Ausgaben:'; + const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { + switch (prohibitedExpenseType) { + case 'alcohol': + return `Alkohol`; + case 'gambling': + return `Glücksspiel`; + case 'tobacco': + return `Tabak`; + case 'adultEntertainment': + return `Erwachsenenunterhaltung`; + case 'hotelIncidentals': + return `Hotelnebenkosten`; + default: + return `${prohibitedExpenseType}`; + } + }; + const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, reviewRequired: 'Überprüfung erforderlich', diff --git a/src/languages/en.ts b/src/languages/en.ts index 445f97f58ef7..da1acc3bd719 100755 --- a/src/languages/en.ts +++ b/src/languages/en.ts @@ -6731,22 +6731,26 @@ const translations = { } return message; }, - prohibitedExpense: ({prohibitedExpenseType}: ViolationsProhibitedExpenseParams) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Prohibited expense:'; - switch (prohibitedExpenseType) { - case 'alcohol': - return `${preMessage} alcohol`; - case 'gambling': - return `${preMessage} gambling`; - case 'tobacco': - return `${preMessage} tobacco`; - case 'adultEntertainment': - return `${preMessage} adult entertainment`; - case 'hotelIncidentals': - return `${preMessage} hotel incidentals`; - default: - return `${preMessage}${prohibitedExpenseType}`; - } + const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { + switch (prohibitedExpenseType) { + case 'alcohol': + return `alcohol`; + case 'gambling': + return `gambling`; + case 'tobacco': + return `tobacco`; + case 'adultEntertainment': + return `adult entertainment`; + case 'hotelIncidentals': + return `hotel incidentals`; + default: + return `${prohibitedExpenseType}`; + } + }; + const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, reviewRequired: 'Review required', diff --git a/src/languages/es.ts b/src/languages/es.ts index 05f168b0369f..740f04a99579 100644 --- a/src/languages/es.ts +++ b/src/languages/es.ts @@ -7220,22 +7220,26 @@ ${amount} para ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseType}: ViolationsProhibitedExpenseParams) => { - const preMessage = 'Gasto prohibido:'; - switch (prohibitedExpenseType) { - case 'alcohol': - return `${preMessage} alcohol`; - case 'gambling': - return `${preMessage} juegos de apuestas`; - case 'tobacco': - return `${preMessage} tabaco`; - case 'adultEntertainment': - return `${preMessage} entretenimiento para adultos`; - case 'hotelIncidentals': - return `${preMessage} gastos adicionales de hotel`; - default: - return `${preMessage}${prohibitedExpenseType}`; - } + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { + const preMessage = 'Gastos prohibidos:'; + const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { + switch (prohibitedExpenseType) { + case 'alcohol': + return `alcohol`; + case 'gambling': + return `juegos de apuestas`; + case 'tobacco': + return `tabaco`; + case 'adultEntertainment': + return `entretenimiento para adultos`; + case 'hotelIncidentals': + return `gastos adicionales de hotel`; + default: + return `${prohibitedExpenseType}`; + } + }; + const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, reviewRequired: 'Revisión requerida', diff --git a/src/languages/fr.ts b/src/languages/fr.ts index 41041203c045..f580a89cd430 100644 --- a/src/languages/fr.ts +++ b/src/languages/fr.ts @@ -6778,22 +6778,26 @@ ${amount} pour ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseType}: ViolationsProhibitedExpenseParams) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Dépense interdite :'; - switch (prohibitedExpenseType) { - case 'alcohol': - return `${preMessage} alcool`; - case 'gambling': - return `${preMessage} jeu d'argent`; - case 'tobacco': - return `${preMessage} tabac`; - case 'adultEntertainment': - return `${preMessage} divertissement pour adultes`; - case 'hotelIncidentals': - return `${preMessage} frais accessoires d'hôtel`; - default: - return `${preMessage}${prohibitedExpenseType}`; - } + const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { + switch (prohibitedExpenseType) { + case 'alcohol': + return `alcool`; + case 'gambling': + return `jeu d'argent`; + case 'tobacco': + return `tabac`; + case 'adultEntertainment': + return `divertissement pour adultes`; + case 'hotelIncidentals': + return `frais accessoires d'hôtel`; + default: + return `${prohibitedExpenseType}`; + } + }; + const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, reviewRequired: 'Examen requis', diff --git a/src/languages/it.ts b/src/languages/it.ts index 947068e6ac30..7ae77766856e 100644 --- a/src/languages/it.ts +++ b/src/languages/it.ts @@ -6784,22 +6784,26 @@ ${amount} per ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseType}: ViolationsProhibitedExpenseParams) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Spesa vietata:'; - switch (prohibitedExpenseType) { - case 'alcohol': - return `${preMessage} alcol`; - case 'gambling': - return `${preMessage} gioco d'azzardo`; - case 'tobacco': - return `${preMessage} tabacco`; - case 'adultEntertainment': - return `${preMessage} intrattenimento per adulti`; - case 'hotelIncidentals': - return `${preMessage} spese accessorie dell'hotel`; - default: - return `${preMessage}${prohibitedExpenseType}`; - } + const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { + switch (prohibitedExpenseType) { + case 'alcohol': + return `alcol`; + case 'gambling': + return `gioco d'azzardo`; + case 'tobacco': + return `tabacco`; + case 'adultEntertainment': + return `intrattenimento per adulti`; + case 'hotelIncidentals': + return `spese accessorie dell'hotel`; + default: + return `${prohibitedExpenseType}`; + } + }; + const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, reviewRequired: 'Revisione richiesta', diff --git a/src/languages/ja.ts b/src/languages/ja.ts index b21529a4a43d..1fc62fc01844 100644 --- a/src/languages/ja.ts +++ b/src/languages/ja.ts @@ -6719,22 +6719,26 @@ ${date} - ${merchant}に${amount}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseType}: ViolationsProhibitedExpenseParams) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = '禁止された経費:'; - switch (prohibitedExpenseType) { - case 'alcohol': - return `${preMessage} アルコール`; - case 'gambling': - return `${preMessage} ギャンブル`; - case 'tobacco': - return `${preMessage} タバコ`; - case 'adultEntertainment': - return `${preMessage} アダルトエンターテインメント`; - case 'hotelIncidentals': - return `${preMessage} ホテル雑費`; - default: - return `${preMessage}${prohibitedExpenseType}`; - } + const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { + switch (prohibitedExpenseType) { + case 'alcohol': + return `アルコール`; + case 'gambling': + return `ギャンブル`; + case 'tobacco': + return `タバコ`; + case 'adultEntertainment': + return `アダルトエンターテインメント`; + case 'hotelIncidentals': + return `ホテル雑費`; + default: + return `${prohibitedExpenseType}`; + } + }; + const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, reviewRequired: 'レビューが必要です', diff --git a/src/languages/nl.ts b/src/languages/nl.ts index 04dbe1ef57e8..94ab5e6e2bac 100644 --- a/src/languages/nl.ts +++ b/src/languages/nl.ts @@ -6769,22 +6769,26 @@ ${amount} voor ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseType}: ViolationsProhibitedExpenseParams) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Verboden uitgave:'; - switch (prohibitedExpenseType) { - case 'alcohol': - return `${preMessage} alcohol`; - case 'gambling': - return `${preMessage} gokken`; - case 'tobacco': - return `${preMessage} tabak`; - case 'adultEntertainment': - return `${preMessage} volwassen entertainment`; - case 'hotelIncidentals': - return `${preMessage} hotelbijzaken`; - default: - return `${preMessage}${prohibitedExpenseType}`; - } + const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { + switch (prohibitedExpenseType) { + case 'alcohol': + return `alcohol`; + case 'gambling': + return `gokken`; + case 'tobacco': + return `tabak`; + case 'adultEntertainment': + return `volwassenen entertainment`; + case 'hotelIncidentals': + return `hotelbijzaken`; + default: + return `${prohibitedExpenseType}`; + } + }; + const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, reviewRequired: 'Beoordeling vereist', diff --git a/src/languages/params.ts b/src/languages/params.ts index 6ee4cffa34ff..015b122af270 100644 --- a/src/languages/params.ts +++ b/src/languages/params.ts @@ -334,7 +334,7 @@ type ViolationsRterParams = { type ViolationsTagOutOfPolicyParams = {tagName?: string} | undefined; -type ViolationsProhibitedExpenseParams = {prohibitedExpenseType: string}; +type ViolationsProhibitedExpenseParams = {prohibitedExpenseTypes: string[]}; type ViolationsTaxOutOfPolicyParams = {taxName?: string} | undefined; diff --git a/src/languages/pl.ts b/src/languages/pl.ts index 9699057292ec..ca82d3402c71 100644 --- a/src/languages/pl.ts +++ b/src/languages/pl.ts @@ -6756,22 +6756,26 @@ ${amount} dla ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseType}: ViolationsProhibitedExpenseParams) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Zabroniony wydatek:'; - switch (prohibitedExpenseType) { - case 'alcohol': - return `${preMessage} alkohol`; - case 'gambling': - return `${preMessage} hazardowanie`; - case 'tobacco': - return `${preMessage} tytoń`; - case 'adultEntertainment': - return `${preMessage} rozrywka dla dorosłych`; - case 'hotelIncidentals': - return `${preMessage} wydatki hotelowe`; - default: - return `${preMessage}${prohibitedExpenseType}`; - } + const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { + switch (prohibitedExpenseType) { + case 'alcohol': + return `alkohol`; + case 'gambling': + return `hazardowanie`; + case 'tobacco': + return `tytoń`; + case 'adultEntertainment': + return `rozrywka dla dorosłych`; + case 'hotelIncidentals': + return `wydatki hotelowe`; + default: + return `${prohibitedExpenseType}`; + } + }; + const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, reviewRequired: 'Wymagana recenzja', diff --git a/src/languages/pt-BR.ts b/src/languages/pt-BR.ts index 7050ce68dc56..3ce0a3d774e0 100644 --- a/src/languages/pt-BR.ts +++ b/src/languages/pt-BR.ts @@ -6764,22 +6764,26 @@ ${amount} para ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseType}: ViolationsProhibitedExpenseParams) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Despesa proibida:'; - switch (prohibitedExpenseType) { - case 'alcohol': - return `${preMessage} álcool`; - case 'gambling': - return `${preMessage} jogos de azar`; - case 'tobacco': - return `${preMessage} tabaco`; - case 'adultEntertainment': - return `${preMessage} entretenimento adulto`; - case 'hotelIncidentals': - return `${preMessage} despesas incidentais de hotel`; - default: - return `${preMessage}${prohibitedExpenseType}`; - } + const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { + switch (prohibitedExpenseType) { + case 'alcohol': + return `álcool`; + case 'gambling': + return `jogos de azar`; + case 'tobacco': + return `tabaco`; + case 'adultEntertainment': + return `entretenimento adulto`; + case 'hotelIncidentals': + return `despesas incidentais de hotel`; + default: + return `${prohibitedExpenseType}`; + } + }; + const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, reviewRequired: 'Revisão necessária', diff --git a/src/languages/zh-hans.ts b/src/languages/zh-hans.ts index 2db0379d1000..acd518d5e254 100644 --- a/src/languages/zh-hans.ts +++ b/src/languages/zh-hans.ts @@ -6632,22 +6632,26 @@ ${merchant}的${amount} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseType}: ViolationsProhibitedExpenseParams) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = '禁止的费用:'; - switch (prohibitedExpenseType) { - case 'alcohol': - return `${preMessage} 酒精`; - case 'gambling': - return `${preMessage} 赌博`; - case 'tobacco': - return `${preMessage} 烟草`; - case 'adultEntertainment': - return `${preMessage} 成人娱乐`; - case 'hotelIncidentals': - return `${preMessage} 酒店杂费`; - default: - return `${preMessage}${prohibitedExpenseType}`; - } + const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { + switch (prohibitedExpenseType) { + case 'alcohol': + return `酒精`; + case 'gambling': + return `赌博`; + case 'tobacco': + return `烟草`; + case 'adultEntertainment': + return `成人娱乐`; + case 'hotelIncidentals': + return `酒店杂费`; + default: + return `${prohibitedExpenseType}`; + } + }; + const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, reviewRequired: '需要审核', diff --git a/src/libs/Violations/ViolationsUtils.ts b/src/libs/Violations/ViolationsUtils.ts index c7da8f1239ee..a0729f1aee62 100644 --- a/src/libs/Violations/ViolationsUtils.ts +++ b/src/libs/Violations/ViolationsUtils.ts @@ -569,7 +569,7 @@ const ViolationsUtils = { return translate('violations.hold'); case CONST.VIOLATIONS.PROHIBITED_EXPENSE: return translate('violations.prohibitedExpense', { - prohibitedExpenseType: violation.data?.prohibitedExpenseRule ?? '', + prohibitedExpenseTypes: violation.data?.prohibitedExpenseRule ?? [], }); case CONST.VIOLATIONS.RECEIPT_GENERATED_WITH_AI: return translate('violations.receiptGeneratedWithAI'); diff --git a/src/types/onyx/TransactionViolation.ts b/src/types/onyx/TransactionViolation.ts index e0e0ee79f21e..01e60905e905 100644 --- a/src/types/onyx/TransactionViolation.ts +++ b/src/types/onyx/TransactionViolation.ts @@ -99,7 +99,7 @@ type TransactionViolationData = { tooltip?: string; /** What prohibitive expense rule did they break? */ - prohibitedExpenseRule?: string; + prohibitedExpenseRule?: string[]; /** Comment that triggered the violation */ comment?: string; From 359ba470aea1feba99a304247287b91dc6b3cbc7 Mon Sep 17 00:00:00 2001 From: paulnjs Date: Thu, 30 Oct 2025 18:46:07 +0700 Subject: [PATCH 02/11] fix: error lint --- src/languages/de.ts | 7 ++++++- src/languages/en.ts | 7 ++++++- src/languages/es.ts | 7 ++++++- src/languages/fr.ts | 7 ++++++- src/languages/it.ts | 7 ++++++- src/languages/ja.ts | 7 ++++++- src/languages/nl.ts | 7 ++++++- src/languages/pl.ts | 7 ++++++- src/languages/pt-BR.ts | 7 ++++++- src/languages/zh-hans.ts | 7 ++++++- 10 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/languages/de.ts b/src/languages/de.ts index c6d66b4de253..62cdb02480b6 100644 --- a/src/languages/de.ts +++ b/src/languages/de.ts @@ -6791,7 +6791,12 @@ ${amount} für ${merchant} - ${date}`, return `${prohibitedExpenseType}`; } }; - const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + let types: string[] = []; + if (Array.isArray(prohibitedExpenseTypes)) { + types = prohibitedExpenseTypes; + } else if (prohibitedExpenseTypes) { + types = [prohibitedExpenseTypes]; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/en.ts b/src/languages/en.ts index da1acc3bd719..512c98a95e1d 100755 --- a/src/languages/en.ts +++ b/src/languages/en.ts @@ -6749,7 +6749,12 @@ const translations = { return `${prohibitedExpenseType}`; } }; - const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + let types: string[] = []; + if (Array.isArray(prohibitedExpenseTypes)) { + types = prohibitedExpenseTypes; + } else if (prohibitedExpenseTypes) { + types = [prohibitedExpenseTypes]; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/es.ts b/src/languages/es.ts index 740f04a99579..52bbd4ebcd85 100644 --- a/src/languages/es.ts +++ b/src/languages/es.ts @@ -7238,7 +7238,12 @@ ${amount} para ${merchant} - ${date}`, return `${prohibitedExpenseType}`; } }; - const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + let types: string[] = []; + if (Array.isArray(prohibitedExpenseTypes)) { + types = prohibitedExpenseTypes; + } else if (prohibitedExpenseTypes) { + types = [prohibitedExpenseTypes]; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/fr.ts b/src/languages/fr.ts index f580a89cd430..5ad33c37b0f3 100644 --- a/src/languages/fr.ts +++ b/src/languages/fr.ts @@ -6796,7 +6796,12 @@ ${amount} pour ${merchant} - ${date}`, return `${prohibitedExpenseType}`; } }; - const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + let types: string[] = []; + if (Array.isArray(prohibitedExpenseTypes)) { + types = prohibitedExpenseTypes; + } else if (prohibitedExpenseTypes) { + types = [prohibitedExpenseTypes]; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/it.ts b/src/languages/it.ts index 7ae77766856e..9247cdbed8a3 100644 --- a/src/languages/it.ts +++ b/src/languages/it.ts @@ -6802,7 +6802,12 @@ ${amount} per ${merchant} - ${date}`, return `${prohibitedExpenseType}`; } }; - const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + let types: string[] = []; + if (Array.isArray(prohibitedExpenseTypes)) { + types = prohibitedExpenseTypes; + } else if (prohibitedExpenseTypes) { + types = [prohibitedExpenseTypes]; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/ja.ts b/src/languages/ja.ts index 1fc62fc01844..ae6ea8605def 100644 --- a/src/languages/ja.ts +++ b/src/languages/ja.ts @@ -6737,7 +6737,12 @@ ${date} - ${merchant}に${amount}`, return `${prohibitedExpenseType}`; } }; - const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + let types: string[] = []; + if (Array.isArray(prohibitedExpenseTypes)) { + types = prohibitedExpenseTypes; + } else if (prohibitedExpenseTypes) { + types = [prohibitedExpenseTypes]; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/nl.ts b/src/languages/nl.ts index 94ab5e6e2bac..01548fdbba52 100644 --- a/src/languages/nl.ts +++ b/src/languages/nl.ts @@ -6787,7 +6787,12 @@ ${amount} voor ${merchant} - ${date}`, return `${prohibitedExpenseType}`; } }; - const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + let types: string[] = []; + if (Array.isArray(prohibitedExpenseTypes)) { + types = prohibitedExpenseTypes; + } else if (prohibitedExpenseTypes) { + types = [prohibitedExpenseTypes]; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/pl.ts b/src/languages/pl.ts index ca82d3402c71..2df6e9e47275 100644 --- a/src/languages/pl.ts +++ b/src/languages/pl.ts @@ -6774,7 +6774,12 @@ ${amount} dla ${merchant} - ${date}`, return `${prohibitedExpenseType}`; } }; - const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + let types: string[] = []; + if (Array.isArray(prohibitedExpenseTypes)) { + types = prohibitedExpenseTypes; + } else if (prohibitedExpenseTypes) { + types = [prohibitedExpenseTypes]; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/pt-BR.ts b/src/languages/pt-BR.ts index 3ce0a3d774e0..5b53daf37f1c 100644 --- a/src/languages/pt-BR.ts +++ b/src/languages/pt-BR.ts @@ -6782,7 +6782,12 @@ ${amount} para ${merchant} - ${date}`, return `${prohibitedExpenseType}`; } }; - const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + let types: string[] = []; + if (Array.isArray(prohibitedExpenseTypes)) { + types = prohibitedExpenseTypes; + } else if (prohibitedExpenseTypes) { + types = [prohibitedExpenseTypes]; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/zh-hans.ts b/src/languages/zh-hans.ts index acd518d5e254..1319678d9446 100644 --- a/src/languages/zh-hans.ts +++ b/src/languages/zh-hans.ts @@ -6650,7 +6650,12 @@ ${merchant}的${amount} - ${date}`, return `${prohibitedExpenseType}`; } }; - const types = Array.isArray(prohibitedExpenseTypes) ? prohibitedExpenseTypes : prohibitedExpenseTypes ? [prohibitedExpenseTypes] : []; + let types: string[] = []; + if (Array.isArray(prohibitedExpenseTypes)) { + types = prohibitedExpenseTypes; + } else if (prohibitedExpenseTypes) { + types = [prohibitedExpenseTypes]; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, From d6007ce44f3fb96c5428302ae60f3f9bf152a079 Mon Sep 17 00:00:00 2001 From: paulnjs Date: Mon, 3 Nov 2025 23:06:39 +0700 Subject: [PATCH 03/11] add unit test --- src/hooks/useTransactionViolations.ts | 4 + src/languages/de.ts | 3 + src/languages/en.ts | 3 + src/languages/es.ts | 3 + src/languages/fr.ts | 3 + src/languages/it.ts | 3 + src/languages/ja.ts | 6 + src/languages/nl.ts | 3 + src/languages/params.ts | 2 +- src/languages/pl.ts | 3 + src/languages/pt-BR.ts | 3 + src/languages/zh-hans.ts | 3 + src/types/onyx/TransactionViolation.ts | 2 +- tests/unit/useTransactionViolationsTest.ts | 511 +++++++++++++++++++++ 14 files changed, 550 insertions(+), 2 deletions(-) create mode 100644 tests/unit/useTransactionViolationsTest.ts diff --git a/src/hooks/useTransactionViolations.ts b/src/hooks/useTransactionViolations.ts index 8746c971ab20..ceb11f3ac0aa 100644 --- a/src/hooks/useTransactionViolations.ts +++ b/src/hooks/useTransactionViolations.ts @@ -11,6 +11,10 @@ import useOnyx from './useOnyx'; function mergeProhibitedViolations(transactionViolations: TransactionViolations): TransactionViolations { const prohibitedViolations = transactionViolations.filter((violation: TransactionViolation) => violation.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); + if (prohibitedViolations.length === 0) { + return transactionViolations; + } + const prohibitedExpenses = prohibitedViolations.flatMap((violation: TransactionViolation) => violation.data?.prohibitedExpenseRule ?? []); const mergedProhibitedViolations: TransactionViolation = { name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, diff --git a/src/languages/de.ts b/src/languages/de.ts index 750f5464776c..9e2c044733cd 100644 --- a/src/languages/de.ts +++ b/src/languages/de.ts @@ -6827,6 +6827,9 @@ ${amount} für ${merchant} - ${date}`, } else if (prohibitedExpenseTypes) { types = [prohibitedExpenseTypes]; } + if (types.length === 0) { + return preMessage; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/en.ts b/src/languages/en.ts index 9cf465c538b7..1295b3416322 100755 --- a/src/languages/en.ts +++ b/src/languages/en.ts @@ -6784,6 +6784,9 @@ const translations = { } else if (prohibitedExpenseTypes) { types = [prohibitedExpenseTypes]; } + if (types.length === 0) { + return preMessage; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/es.ts b/src/languages/es.ts index 8fb9739a04b7..522459325056 100644 --- a/src/languages/es.ts +++ b/src/languages/es.ts @@ -7274,6 +7274,9 @@ ${amount} para ${merchant} - ${date}`, } else if (prohibitedExpenseTypes) { types = [prohibitedExpenseTypes]; } + if (types.length === 0) { + return preMessage; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/fr.ts b/src/languages/fr.ts index 23db196bf58f..d13039d5e32e 100644 --- a/src/languages/fr.ts +++ b/src/languages/fr.ts @@ -6832,6 +6832,9 @@ ${amount} pour ${merchant} - ${date}`, } else if (prohibitedExpenseTypes) { types = [prohibitedExpenseTypes]; } + if (types.length === 0) { + return preMessage; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/it.ts b/src/languages/it.ts index d441c99f716f..7e50798ff36f 100644 --- a/src/languages/it.ts +++ b/src/languages/it.ts @@ -6838,6 +6838,9 @@ ${amount} per ${merchant} - ${date}`, } else if (prohibitedExpenseTypes) { types = [prohibitedExpenseTypes]; } + if (types.length === 0) { + return preMessage; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/ja.ts b/src/languages/ja.ts index 5d7e949b2c5d..eea0e38b3ad6 100644 --- a/src/languages/ja.ts +++ b/src/languages/ja.ts @@ -6772,6 +6772,12 @@ ${date} - ${merchant}に${amount}`, } else if (prohibitedExpenseTypes) { types = [prohibitedExpenseTypes]; } + if (types.length === 0) { + return preMessage; + } + if (types.length === 0) { + return preMessage; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/nl.ts b/src/languages/nl.ts index 1fe9d264c9d5..39730302100f 100644 --- a/src/languages/nl.ts +++ b/src/languages/nl.ts @@ -6823,6 +6823,9 @@ ${amount} voor ${merchant} - ${date}`, } else if (prohibitedExpenseTypes) { types = [prohibitedExpenseTypes]; } + if (types.length === 0) { + return preMessage; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/params.ts b/src/languages/params.ts index 015b122af270..597fb4365a9f 100644 --- a/src/languages/params.ts +++ b/src/languages/params.ts @@ -334,7 +334,7 @@ type ViolationsRterParams = { type ViolationsTagOutOfPolicyParams = {tagName?: string} | undefined; -type ViolationsProhibitedExpenseParams = {prohibitedExpenseTypes: string[]}; +type ViolationsProhibitedExpenseParams = {prohibitedExpenseTypes: string | string[]}; type ViolationsTaxOutOfPolicyParams = {taxName?: string} | undefined; diff --git a/src/languages/pl.ts b/src/languages/pl.ts index 6f143527eea9..f21700add797 100644 --- a/src/languages/pl.ts +++ b/src/languages/pl.ts @@ -6810,6 +6810,9 @@ ${amount} dla ${merchant} - ${date}`, } else if (prohibitedExpenseTypes) { types = [prohibitedExpenseTypes]; } + if (types.length === 0) { + return preMessage; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/pt-BR.ts b/src/languages/pt-BR.ts index 4b1ab5e5d831..86ab380f3547 100644 --- a/src/languages/pt-BR.ts +++ b/src/languages/pt-BR.ts @@ -6818,6 +6818,9 @@ ${amount} para ${merchant} - ${date}`, } else if (prohibitedExpenseTypes) { types = [prohibitedExpenseTypes]; } + if (types.length === 0) { + return preMessage; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/languages/zh-hans.ts b/src/languages/zh-hans.ts index 047ea5dc54b1..1fa4756753e1 100644 --- a/src/languages/zh-hans.ts +++ b/src/languages/zh-hans.ts @@ -6678,6 +6678,9 @@ ${merchant}的${amount} - ${date}`, } else if (prohibitedExpenseTypes) { types = [prohibitedExpenseTypes]; } + if (types.length === 0) { + return preMessage; + } return `${preMessage} ${types.map(getProhibitedExpenseTypeText).join(', ')}`; }, customRules: ({message}: ViolationsCustomRulesParams) => message, diff --git a/src/types/onyx/TransactionViolation.ts b/src/types/onyx/TransactionViolation.ts index 01e60905e905..76b6e9410d68 100644 --- a/src/types/onyx/TransactionViolation.ts +++ b/src/types/onyx/TransactionViolation.ts @@ -99,7 +99,7 @@ type TransactionViolationData = { tooltip?: string; /** What prohibitive expense rule did they break? */ - prohibitedExpenseRule?: string[]; + prohibitedExpenseRule?: string | string[]; /** Comment that triggered the violation */ comment?: string; diff --git a/tests/unit/useTransactionViolationsTest.ts b/tests/unit/useTransactionViolationsTest.ts new file mode 100644 index 000000000000..2073d6b11027 --- /dev/null +++ b/tests/unit/useTransactionViolationsTest.ts @@ -0,0 +1,511 @@ +import {renderHook} from '@testing-library/react-native'; +import Onyx from 'react-native-onyx'; +import useTransactionViolations from '@hooks/useTransactionViolations'; +import * as TransactionUtils from '@libs/TransactionUtils'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy, Report, Transaction, TransactionViolations} from '@src/types/onyx'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; + +// Mock the required modules +jest.mock('@libs/TransactionUtils', () => ({ + isViolationDismissed: jest.fn(), + shouldShowViolation: jest.fn(), +})); + +jest.mock('@hooks/useCurrentUserPersonalDetails', () => ({ + __esModule: true, + default: jest.fn(() => ({ + email: 'test@example.com', + accountID: 1, + })), +})); + +describe('useTransactionViolations', () => { + beforeAll(() => { + Onyx.init({ + keys: ONYXKEYS, + }); + }); + + beforeEach(() => { + jest.clearAllMocks(); + Onyx.clear(); + + // Default mock implementations + (TransactionUtils.isViolationDismissed as jest.Mock).mockReturnValue(false); + (TransactionUtils.shouldShowViolation as jest.Mock).mockReturnValue(true); + }); + + describe('mergeProhibitedViolations', () => { + it('should return original array when there are no prohibited violations', async () => { + const transactionID = '123'; + const violations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.MISSING_CATEGORY, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + { + name: CONST.VIOLATIONS.MISSING_TAG, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + ]; + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, violations); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toHaveLength(2); + expect(result.current[0].name).toBe(CONST.VIOLATIONS.MISSING_CATEGORY); + expect(result.current[1].name).toBe(CONST.VIOLATIONS.MISSING_TAG); + }); + + it('should handle a single prohibited violation correctly', async () => { + const transactionID = '123'; + const violations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: { + prohibitedExpenseRule: 'alcohol', + }, + }, + ]; + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, violations); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toHaveLength(1); + expect(result.current[0].name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(result.current[0].data?.prohibitedExpenseRule).toEqual(['alcohol']); + expect(result.current[0].type).toBe(CONST.VIOLATION_TYPES.VIOLATION); + }); + + it('should merge multiple prohibited violations into one', async () => { + const transactionID = '123'; + const violations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: { + prohibitedExpenseRule: 'alcohol', + }, + }, + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: { + prohibitedExpenseRule: 'gambling', + }, + }, + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: { + prohibitedExpenseRule: 'tobacco', + }, + }, + ]; + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, violations); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toHaveLength(1); + expect(result.current[0].name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(result.current[0].data?.prohibitedExpenseRule).toEqual(['alcohol', 'gambling', 'tobacco']); + expect(result.current[0].type).toBe(CONST.VIOLATION_TYPES.VIOLATION); + }); + + it('should handle empty prohibitedExpenseRule arrays', async () => { + const transactionID = '123'; + const violations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: { + prohibitedExpenseRule: undefined, + }, + }, + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: {}, + }, + ]; + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, violations); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toHaveLength(1); + expect(result.current[0].name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(result.current[0].data?.prohibitedExpenseRule).toEqual([]); + expect(result.current[0].type).toBe(CONST.VIOLATION_TYPES.VIOLATION); + }); + + it('should handle mixed violations (some prohibited, some not)', async () => { + const transactionID = '123'; + const violations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.MISSING_CATEGORY, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: { + prohibitedExpenseRule: 'alcohol', + }, + }, + { + name: CONST.VIOLATIONS.MISSING_TAG, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: { + prohibitedExpenseRule: 'gambling', + }, + }, + { + name: CONST.VIOLATIONS.MISSING_COMMENT, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + ]; + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, violations); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toHaveLength(4); + + // Check that all non-prohibited violations are present + const violationNames = result.current.map((v) => v.name); + expect(violationNames).toContain(CONST.VIOLATIONS.MISSING_CATEGORY); + expect(violationNames).toContain(CONST.VIOLATIONS.MISSING_TAG); + expect(violationNames).toContain(CONST.VIOLATIONS.MISSING_COMMENT); + expect(violationNames).toContain(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + + // Check that prohibited violations are merged + const prohibitedViolation = result.current.find((v) => v.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(prohibitedViolation?.data?.prohibitedExpenseRule).toEqual(['alcohol', 'gambling']); + }); + + it('should handle prohibitedExpenseRule as an array', async () => { + const transactionID = '123'; + const violations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: { + prohibitedExpenseRule: ['alcohol', 'tobacco'], + }, + }, + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: { + prohibitedExpenseRule: 'gambling', + }, + }, + ]; + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, violations); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toHaveLength(1); + expect(result.current[0].name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(result.current[0].data?.prohibitedExpenseRule).toEqual(['alcohol', 'tobacco', 'gambling']); + }); + }); + + describe('full hook behavior', () => { + it('should filter out dismissed violations', async () => { + const transactionID = '123'; + const violations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.MISSING_CATEGORY, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + { + name: CONST.VIOLATIONS.MISSING_TAG, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + ]; + + // Mock the first violation as dismissed + (TransactionUtils.isViolationDismissed as jest.Mock).mockImplementation((transaction, violation) => { + return violation.name === CONST.VIOLATIONS.MISSING_CATEGORY; + }); + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, violations); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toHaveLength(1); + expect(result.current[0].name).toBe(CONST.VIOLATIONS.MISSING_TAG); + }); + + it('should filter violations based on shouldShowViolation', async () => { + const transactionID = '123'; + const reportID = 'report123'; + const policyID = 'policy123'; + + const violations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.MISSING_CATEGORY, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + { + name: CONST.VIOLATIONS.RTER, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + ]; + + // Mock shouldShowViolation to hide RTER violations + (TransactionUtils.shouldShowViolation as jest.Mock).mockImplementation((iouReport, policy, violationName) => { + return violationName !== CONST.VIOLATIONS.RTER; + }); + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + reportID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { + reportID, + policyID, + } as Report); + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {} as Policy); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, violations); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toHaveLength(1); + expect(result.current[0].name).toBe(CONST.VIOLATIONS.MISSING_CATEGORY); + }); + + it('should return empty array when transactionID is undefined', async () => { + await waitForBatchedUpdates(); + const {result} = renderHook(() => useTransactionViolations(undefined)); + + expect(result.current).toEqual([]); + }); + + it('should handle empty violations array', async () => { + const transactionID = '123'; + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, []); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toEqual([]); + }); + + it('should update when violations change', async () => { + const transactionID = '123'; + const initialViolations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.MISSING_CATEGORY, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + ]; + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, initialViolations); + + await waitForBatchedUpdates(); + + const {result, rerender} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toHaveLength(1); + expect(result.current[0].name).toBe(CONST.VIOLATIONS.MISSING_CATEGORY); + + // Update violations + const updatedViolations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.MISSING_TAG, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + ]; + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, updatedViolations); + await waitForBatchedUpdates(); + + rerender(undefined); + + expect(result.current).toHaveLength(1); + expect(result.current[0].name).toBe(CONST.VIOLATIONS.MISSING_TAG); + }); + + it('should respect shouldShowRterForSettledReport parameter', async () => { + const transactionID = '123'; + const reportID = 'report123'; + const policyID = 'policy123'; + + const violations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.RTER, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + ]; + + let capturedShouldShowRterParam: boolean | undefined; + (TransactionUtils.shouldShowViolation as jest.Mock).mockImplementation((iouReport, policy, violationName, email, shouldShowRter) => { + capturedShouldShowRterParam = shouldShowRter; + return true; + }); + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + reportID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { + reportID, + policyID, + } as Report); + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {} as Policy); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, violations); + + await waitForBatchedUpdates(); + + // Test with default (true) + renderHook(() => useTransactionViolations(transactionID)); + expect(capturedShouldShowRterParam).toBe(true); + + // Test with explicit false + renderHook(() => useTransactionViolations(transactionID, false)); + expect(capturedShouldShowRterParam).toBe(false); + }); + }); + + describe('edge cases', () => { + it('should handle violations with no data property', async () => { + const transactionID = '123'; + const violations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + ]; + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, violations); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toHaveLength(1); + expect(result.current[0].name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(result.current[0].data?.prohibitedExpenseRule).toEqual([]); + }); + + it('should handle complex scenario with all violation types', async () => { + const transactionID = '123'; + const violations: TransactionViolations = [ + { + name: CONST.VIOLATIONS.MISSING_CATEGORY, + type: CONST.VIOLATION_TYPES.VIOLATION, + }, + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: { + prohibitedExpenseRule: ['alcohol', 'tobacco'], + }, + }, + { + name: CONST.VIOLATIONS.MISSING_TAG, + type: CONST.VIOLATION_TYPES.NOTICE, + }, + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: { + prohibitedExpenseRule: 'gambling', + }, + }, + { + name: CONST.VIOLATIONS.SOME_TAG_LEVELS_REQUIRED, + type: CONST.VIOLATION_TYPES.WARNING, + }, + { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + type: CONST.VIOLATION_TYPES.VIOLATION, + data: {}, + }, + ]; + + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { + transactionID, + } as Transaction); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, violations); + + await waitForBatchedUpdates(); + + const {result} = renderHook(() => useTransactionViolations(transactionID)); + + expect(result.current).toHaveLength(4); + + const violationNames = result.current.map((v) => v.name); + expect(violationNames).toContain(CONST.VIOLATIONS.MISSING_CATEGORY); + expect(violationNames).toContain(CONST.VIOLATIONS.MISSING_TAG); + expect(violationNames).toContain(CONST.VIOLATIONS.SOME_TAG_LEVELS_REQUIRED); + expect(violationNames).toContain(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + + const prohibitedViolation = result.current.find((v) => v.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(prohibitedViolation?.data?.prohibitedExpenseRule).toEqual(['alcohol', 'tobacco', 'gambling']); + expect(prohibitedViolation?.type).toBe(CONST.VIOLATION_TYPES.VIOLATION); + }); + }); +}); From 628a602a01fa11e198efe1c26d8905a92ed9de16 Mon Sep 17 00:00:00 2001 From: paulnjs Date: Mon, 3 Nov 2025 23:28:58 +0700 Subject: [PATCH 04/11] fix lint --- tests/unit/useTransactionViolationsTest.ts | 53 ++++++++++++---------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/tests/unit/useTransactionViolationsTest.ts b/tests/unit/useTransactionViolationsTest.ts index 2073d6b11027..30c8e0dfb183 100644 --- a/tests/unit/useTransactionViolationsTest.ts +++ b/tests/unit/useTransactionViolationsTest.ts @@ -1,7 +1,7 @@ import {renderHook} from '@testing-library/react-native'; import Onyx from 'react-native-onyx'; import useTransactionViolations from '@hooks/useTransactionViolations'; -import * as TransactionUtils from '@libs/TransactionUtils'; +import {isViolationDismissed, shouldShowViolation} from '@libs/TransactionUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, Report, Transaction, TransactionViolations} from '@src/types/onyx'; @@ -14,6 +14,7 @@ jest.mock('@libs/TransactionUtils', () => ({ })); jest.mock('@hooks/useCurrentUserPersonalDetails', () => ({ + // eslint-disable-next-line @typescript-eslint/naming-convention __esModule: true, default: jest.fn(() => ({ email: 'test@example.com', @@ -33,8 +34,8 @@ describe('useTransactionViolations', () => { Onyx.clear(); // Default mock implementations - (TransactionUtils.isViolationDismissed as jest.Mock).mockReturnValue(false); - (TransactionUtils.shouldShowViolation as jest.Mock).mockReturnValue(true); + (isViolationDismissed as jest.Mock).mockReturnValue(false); + (shouldShowViolation as jest.Mock).mockReturnValue(true); }); describe('mergeProhibitedViolations', () => { @@ -61,8 +62,8 @@ describe('useTransactionViolations', () => { const {result} = renderHook(() => useTransactionViolations(transactionID)); expect(result.current).toHaveLength(2); - expect(result.current[0].name).toBe(CONST.VIOLATIONS.MISSING_CATEGORY); - expect(result.current[1].name).toBe(CONST.VIOLATIONS.MISSING_TAG); + expect(result.current.at(0)?.name).toBe(CONST.VIOLATIONS.MISSING_CATEGORY); + expect(result.current.at(1)?.name).toBe(CONST.VIOLATIONS.MISSING_TAG); }); it('should handle a single prohibited violation correctly', async () => { @@ -87,9 +88,9 @@ describe('useTransactionViolations', () => { const {result} = renderHook(() => useTransactionViolations(transactionID)); expect(result.current).toHaveLength(1); - expect(result.current[0].name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); - expect(result.current[0].data?.prohibitedExpenseRule).toEqual(['alcohol']); - expect(result.current[0].type).toBe(CONST.VIOLATION_TYPES.VIOLATION); + expect(result.current.at(0)?.name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(result.current.at(0)?.data?.prohibitedExpenseRule).toEqual(['alcohol']); + expect(result.current.at(0)?.type).toBe(CONST.VIOLATION_TYPES.VIOLATION); }); it('should merge multiple prohibited violations into one', async () => { @@ -128,9 +129,9 @@ describe('useTransactionViolations', () => { const {result} = renderHook(() => useTransactionViolations(transactionID)); expect(result.current).toHaveLength(1); - expect(result.current[0].name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); - expect(result.current[0].data?.prohibitedExpenseRule).toEqual(['alcohol', 'gambling', 'tobacco']); - expect(result.current[0].type).toBe(CONST.VIOLATION_TYPES.VIOLATION); + expect(result.current.at(0)?.name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(result.current.at(0)?.data?.prohibitedExpenseRule).toEqual(['alcohol', 'gambling', 'tobacco']); + expect(result.current.at(0)?.type).toBe(CONST.VIOLATION_TYPES.VIOLATION); }); it('should handle empty prohibitedExpenseRule arrays', async () => { @@ -160,9 +161,9 @@ describe('useTransactionViolations', () => { const {result} = renderHook(() => useTransactionViolations(transactionID)); expect(result.current).toHaveLength(1); - expect(result.current[0].name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); - expect(result.current[0].data?.prohibitedExpenseRule).toEqual([]); - expect(result.current[0].type).toBe(CONST.VIOLATION_TYPES.VIOLATION); + expect(result.current.at(0)?.name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(result.current.at(0)?.data?.prohibitedExpenseRule).toEqual([]); + expect(result.current.at(0)?.type).toBe(CONST.VIOLATION_TYPES.VIOLATION); }); it('should handle mixed violations (some prohibited, some not)', async () => { @@ -248,8 +249,8 @@ describe('useTransactionViolations', () => { const {result} = renderHook(() => useTransactionViolations(transactionID)); expect(result.current).toHaveLength(1); - expect(result.current[0].name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); - expect(result.current[0].data?.prohibitedExpenseRule).toEqual(['alcohol', 'tobacco', 'gambling']); + expect(result.current.at(0)?.name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(result.current.at(0)?.data?.prohibitedExpenseRule).toEqual(['alcohol', 'tobacco', 'gambling']); }); }); @@ -268,7 +269,8 @@ describe('useTransactionViolations', () => { ]; // Mock the first violation as dismissed - (TransactionUtils.isViolationDismissed as jest.Mock).mockImplementation((transaction, violation) => { + (isViolationDismissed as jest.Mock).mockImplementation((transaction, violation) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access return violation.name === CONST.VIOLATIONS.MISSING_CATEGORY; }); @@ -282,7 +284,7 @@ describe('useTransactionViolations', () => { const {result} = renderHook(() => useTransactionViolations(transactionID)); expect(result.current).toHaveLength(1); - expect(result.current[0].name).toBe(CONST.VIOLATIONS.MISSING_TAG); + expect(result.current.at(0)?.name).toBe(CONST.VIOLATIONS.MISSING_TAG); }); it('should filter violations based on shouldShowViolation', async () => { @@ -302,7 +304,7 @@ describe('useTransactionViolations', () => { ]; // Mock shouldShowViolation to hide RTER violations - (TransactionUtils.shouldShowViolation as jest.Mock).mockImplementation((iouReport, policy, violationName) => { + (shouldShowViolation as jest.Mock).mockImplementation((iouReport, policy, violationName) => { return violationName !== CONST.VIOLATIONS.RTER; }); @@ -322,7 +324,7 @@ describe('useTransactionViolations', () => { const {result} = renderHook(() => useTransactionViolations(transactionID)); expect(result.current).toHaveLength(1); - expect(result.current[0].name).toBe(CONST.VIOLATIONS.MISSING_CATEGORY); + expect(result.current.at(0)?.name).toBe(CONST.VIOLATIONS.MISSING_CATEGORY); }); it('should return empty array when transactionID is undefined', async () => { @@ -366,7 +368,7 @@ describe('useTransactionViolations', () => { const {result, rerender} = renderHook(() => useTransactionViolations(transactionID)); expect(result.current).toHaveLength(1); - expect(result.current[0].name).toBe(CONST.VIOLATIONS.MISSING_CATEGORY); + expect(result.current.at(0)?.name).toBe(CONST.VIOLATIONS.MISSING_CATEGORY); // Update violations const updatedViolations: TransactionViolations = [ @@ -382,7 +384,7 @@ describe('useTransactionViolations', () => { rerender(undefined); expect(result.current).toHaveLength(1); - expect(result.current[0].name).toBe(CONST.VIOLATIONS.MISSING_TAG); + expect(result.current.at(0)?.name).toBe(CONST.VIOLATIONS.MISSING_TAG); }); it('should respect shouldShowRterForSettledReport parameter', async () => { @@ -398,7 +400,8 @@ describe('useTransactionViolations', () => { ]; let capturedShouldShowRterParam: boolean | undefined; - (TransactionUtils.shouldShowViolation as jest.Mock).mockImplementation((iouReport, policy, violationName, email, shouldShowRter) => { + (shouldShowViolation as jest.Mock).mockImplementation((iouReport, policy, violationName, email, shouldShowRter) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment capturedShouldShowRterParam = shouldShowRter; return true; }); @@ -446,8 +449,8 @@ describe('useTransactionViolations', () => { const {result} = renderHook(() => useTransactionViolations(transactionID)); expect(result.current).toHaveLength(1); - expect(result.current[0].name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); - expect(result.current[0].data?.prohibitedExpenseRule).toEqual([]); + expect(result.current.at(0)?.name).toBe(CONST.VIOLATIONS.PROHIBITED_EXPENSE); + expect(result.current.at(0)?.data?.prohibitedExpenseRule).toEqual([]); }); it('should handle complex scenario with all violation types', async () => { From 99a0d420396e7f1c58e32d8111bcde3879d085dd Mon Sep 17 00:00:00 2001 From: paulnjs Date: Tue, 4 Nov 2025 00:05:52 +0700 Subject: [PATCH 05/11] udpate code --- .../MoneyRequestReportTransactionList.tsx | 32 +++++++++++++++---- src/hooks/useTransactionViolations.ts | 22 +------------ src/libs/TransactionUtils/index.ts | 23 +++++++++++++ 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx index c801dbdf40b3..9082f755c009 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx @@ -40,7 +40,18 @@ import { isExpenseReport, } from '@libs/ReportUtils'; import {compareValues, getColumnsToShow, isTransactionAmountTooLong, isTransactionTaxAmountTooLong} from '@libs/SearchUIUtils'; -import {getAmount, getCategory, getCreated, getMerchant, getTag, getTransactionPendingAction, isTransactionPendingDelete, shouldShowViolation} from '@libs/TransactionUtils'; +import { + getAmount, + getCategory, + getCreated, + getMerchant, + getTag, + getTransactionPendingAction, + isTransactionPendingDelete, + isViolationDismissed, + mergeProhibitedViolations, + shouldShowViolation, +} from '@libs/TransactionUtils'; import shouldShowTransactionYear from '@libs/TransactionUtils/shouldShowTransactionYear'; import Navigation from '@navigation/Navigation'; import type {ReportsSplitNavigatorParamList} from '@navigation/types'; @@ -175,12 +186,21 @@ function MoneyRequestReportTransactionList({ const filtered: Record = {}; for (const transaction of transactions) { - const transactionViolations = violations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`]; - if (transactionViolations) { - const filteredTransactionViolations = transactionViolations.filter((violation) => shouldShowViolation(report, policy, violation.name, currentUserDetails.email ?? '')); + const violationKey = `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`; + const transactionViolations = violations[violationKey]; + + if (transactionViolations && transactionViolations.length > 0) { + // Filter out dismissed violations and those that shouldn't be shown + const filteredTransactionViolations = transactionViolations.filter( + (violation) => + !isViolationDismissed(transaction, violation, currentUserDetails.email ?? '') && shouldShowViolation(report, policy, violation.name, currentUserDetails.email ?? ''), + ); + + // Merge prohibited violations + const mergedViolations = mergeProhibitedViolations(filteredTransactionViolations); - if (filteredTransactionViolations.length > 0) { - filtered[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`] = filteredTransactionViolations; + if (mergedViolations.length > 0) { + filtered[violationKey] = mergedViolations; } } } diff --git a/src/hooks/useTransactionViolations.ts b/src/hooks/useTransactionViolations.ts index ceb11f3ac0aa..724ca3945dcc 100644 --- a/src/hooks/useTransactionViolations.ts +++ b/src/hooks/useTransactionViolations.ts @@ -1,32 +1,12 @@ import {useMemo} from 'react'; import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID'; -import {isViolationDismissed, shouldShowViolation} from '@libs/TransactionUtils'; -import CONST from '@src/CONST'; +import {isViolationDismissed, mergeProhibitedViolations, shouldShowViolation} from '@libs/TransactionUtils'; import ONYXKEYS from '@src/ONYXKEYS'; import type {TransactionViolation, TransactionViolations} from '@src/types/onyx'; import getEmptyArray from '@src/types/utils/getEmptyArray'; import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; import useOnyx from './useOnyx'; -function mergeProhibitedViolations(transactionViolations: TransactionViolations): TransactionViolations { - const prohibitedViolations = transactionViolations.filter((violation: TransactionViolation) => violation.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); - - if (prohibitedViolations.length === 0) { - return transactionViolations; - } - - const prohibitedExpenses = prohibitedViolations.flatMap((violation: TransactionViolation) => violation.data?.prohibitedExpenseRule ?? []); - const mergedProhibitedViolations: TransactionViolation = { - name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, - data: { - prohibitedExpenseRule: prohibitedExpenses, - }, - type: CONST.VIOLATION_TYPES.VIOLATION, - }; - - return [...transactionViolations.filter((violation: TransactionViolation) => violation.name !== CONST.VIOLATIONS.PROHIBITED_EXPENSE), mergedProhibitedViolations]; -} - function useTransactionViolations(transactionID?: string, shouldShowRterForSettledReport = true): TransactionViolations { const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(transactionID)}`, { canBeMissing: true, diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index 2dae9da0f958..ca0a941c7671 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -1340,6 +1340,28 @@ function isViolationDismissed(transaction: OnyxEntry, violation: Tr return !!transaction?.comment?.dismissedViolations?.[violation.name]?.[currentUserEmail ?? deprecatedCurrentUserEmail]; } +/** + * Merges multiple prohibited expense violations into a single violation + */ +function mergeProhibitedViolations(transactionViolations: TransactionViolations): TransactionViolations { + const prohibitedViolations = transactionViolations.filter((violation: TransactionViolation) => violation.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); + + if (prohibitedViolations.length === 0) { + return transactionViolations; + } + + const prohibitedExpenses = prohibitedViolations.flatMap((violation: TransactionViolation) => violation.data?.prohibitedExpenseRule ?? []); + const mergedProhibitedViolations: TransactionViolation = { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + data: { + prohibitedExpenseRule: prohibitedExpenses, + }, + type: CONST.VIOLATION_TYPES.VIOLATION, + }; + + return [...transactionViolations.filter((violation: TransactionViolation) => violation.name !== CONST.VIOLATIONS.PROHIBITED_EXPENSE), mergedProhibitedViolations]; +} + /** * Checks if violations are supported for the given transaction */ @@ -2126,6 +2148,7 @@ export { getCategoryTaxCodeAndAmount, isPerDiemRequest, isViolationDismissed, + mergeProhibitedViolations, isBrokenConnectionViolation, shouldShowRTERViolationMessage, isPartialTransaction, From 4da824f809c08a8ddc1168fdc630acfa1354bb73 Mon Sep 17 00:00:00 2001 From: paulnjs Date: Tue, 4 Nov 2025 00:25:53 +0700 Subject: [PATCH 06/11] Revert "udpate code" This reverts commit 99a0d420396e7f1c58e32d8111bcde3879d085dd. --- .../MoneyRequestReportTransactionList.tsx | 32 ++++--------------- src/hooks/useTransactionViolations.ts | 22 ++++++++++++- src/libs/TransactionUtils/index.ts | 23 ------------- 3 files changed, 27 insertions(+), 50 deletions(-) diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx index 9082f755c009..c801dbdf40b3 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx @@ -40,18 +40,7 @@ import { isExpenseReport, } from '@libs/ReportUtils'; import {compareValues, getColumnsToShow, isTransactionAmountTooLong, isTransactionTaxAmountTooLong} from '@libs/SearchUIUtils'; -import { - getAmount, - getCategory, - getCreated, - getMerchant, - getTag, - getTransactionPendingAction, - isTransactionPendingDelete, - isViolationDismissed, - mergeProhibitedViolations, - shouldShowViolation, -} from '@libs/TransactionUtils'; +import {getAmount, getCategory, getCreated, getMerchant, getTag, getTransactionPendingAction, isTransactionPendingDelete, shouldShowViolation} from '@libs/TransactionUtils'; import shouldShowTransactionYear from '@libs/TransactionUtils/shouldShowTransactionYear'; import Navigation from '@navigation/Navigation'; import type {ReportsSplitNavigatorParamList} from '@navigation/types'; @@ -186,21 +175,12 @@ function MoneyRequestReportTransactionList({ const filtered: Record = {}; for (const transaction of transactions) { - const violationKey = `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`; - const transactionViolations = violations[violationKey]; - - if (transactionViolations && transactionViolations.length > 0) { - // Filter out dismissed violations and those that shouldn't be shown - const filteredTransactionViolations = transactionViolations.filter( - (violation) => - !isViolationDismissed(transaction, violation, currentUserDetails.email ?? '') && shouldShowViolation(report, policy, violation.name, currentUserDetails.email ?? ''), - ); - - // Merge prohibited violations - const mergedViolations = mergeProhibitedViolations(filteredTransactionViolations); + const transactionViolations = violations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`]; + if (transactionViolations) { + const filteredTransactionViolations = transactionViolations.filter((violation) => shouldShowViolation(report, policy, violation.name, currentUserDetails.email ?? '')); - if (mergedViolations.length > 0) { - filtered[violationKey] = mergedViolations; + if (filteredTransactionViolations.length > 0) { + filtered[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`] = filteredTransactionViolations; } } } diff --git a/src/hooks/useTransactionViolations.ts b/src/hooks/useTransactionViolations.ts index 724ca3945dcc..ceb11f3ac0aa 100644 --- a/src/hooks/useTransactionViolations.ts +++ b/src/hooks/useTransactionViolations.ts @@ -1,12 +1,32 @@ import {useMemo} from 'react'; import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID'; -import {isViolationDismissed, mergeProhibitedViolations, shouldShowViolation} from '@libs/TransactionUtils'; +import {isViolationDismissed, shouldShowViolation} from '@libs/TransactionUtils'; +import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {TransactionViolation, TransactionViolations} from '@src/types/onyx'; import getEmptyArray from '@src/types/utils/getEmptyArray'; import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; import useOnyx from './useOnyx'; +function mergeProhibitedViolations(transactionViolations: TransactionViolations): TransactionViolations { + const prohibitedViolations = transactionViolations.filter((violation: TransactionViolation) => violation.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); + + if (prohibitedViolations.length === 0) { + return transactionViolations; + } + + const prohibitedExpenses = prohibitedViolations.flatMap((violation: TransactionViolation) => violation.data?.prohibitedExpenseRule ?? []); + const mergedProhibitedViolations: TransactionViolation = { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + data: { + prohibitedExpenseRule: prohibitedExpenses, + }, + type: CONST.VIOLATION_TYPES.VIOLATION, + }; + + return [...transactionViolations.filter((violation: TransactionViolation) => violation.name !== CONST.VIOLATIONS.PROHIBITED_EXPENSE), mergedProhibitedViolations]; +} + function useTransactionViolations(transactionID?: string, shouldShowRterForSettledReport = true): TransactionViolations { const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(transactionID)}`, { canBeMissing: true, diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index ca0a941c7671..2dae9da0f958 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -1340,28 +1340,6 @@ function isViolationDismissed(transaction: OnyxEntry, violation: Tr return !!transaction?.comment?.dismissedViolations?.[violation.name]?.[currentUserEmail ?? deprecatedCurrentUserEmail]; } -/** - * Merges multiple prohibited expense violations into a single violation - */ -function mergeProhibitedViolations(transactionViolations: TransactionViolations): TransactionViolations { - const prohibitedViolations = transactionViolations.filter((violation: TransactionViolation) => violation.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); - - if (prohibitedViolations.length === 0) { - return transactionViolations; - } - - const prohibitedExpenses = prohibitedViolations.flatMap((violation: TransactionViolation) => violation.data?.prohibitedExpenseRule ?? []); - const mergedProhibitedViolations: TransactionViolation = { - name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, - data: { - prohibitedExpenseRule: prohibitedExpenses, - }, - type: CONST.VIOLATION_TYPES.VIOLATION, - }; - - return [...transactionViolations.filter((violation: TransactionViolation) => violation.name !== CONST.VIOLATIONS.PROHIBITED_EXPENSE), mergedProhibitedViolations]; -} - /** * Checks if violations are supported for the given transaction */ @@ -2148,7 +2126,6 @@ export { getCategoryTaxCodeAndAmount, isPerDiemRequest, isViolationDismissed, - mergeProhibitedViolations, isBrokenConnectionViolation, shouldShowRTERViolationMessage, isPartialTransaction, From c98736983c3a30f171d227a47aaf688979fd9075 Mon Sep 17 00:00:00 2001 From: paulnjs Date: Tue, 11 Nov 2025 00:05:28 +0700 Subject: [PATCH 07/11] update code --- .../MoneyRequestReportTransactionList.tsx | 17 +++++++++++++-- src/hooks/useTransactionViolations.ts | 21 +------------------ src/languages/de.ts | 2 +- src/languages/fr.ts | 2 +- src/languages/it.ts | 2 +- src/languages/ja.ts | 2 +- src/languages/nl.ts | 2 +- src/languages/pl.ts | 2 +- src/languages/pt-BR.ts | 2 +- src/libs/TransactionUtils/index.ts | 20 ++++++++++++++++++ 10 files changed, 43 insertions(+), 29 deletions(-) diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx index bb7a83ca47fb..9e15d939b7e3 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx @@ -40,7 +40,17 @@ import { isExpenseReport, } from '@libs/ReportUtils'; import {compareValues, getColumnsToShow, isTransactionAmountTooLong, isTransactionTaxAmountTooLong} from '@libs/SearchUIUtils'; -import {getAmount, getCategory, getCreated, getMerchant, getTag, getTransactionPendingAction, isTransactionPendingDelete, shouldShowViolation} from '@libs/TransactionUtils'; +import { + getAmount, + getCategory, + getCreated, + getMerchant, + getTag, + getTransactionPendingAction, + isTransactionPendingDelete, + mergeProhibitedViolations, + shouldShowViolation, +} from '@libs/TransactionUtils'; import shouldShowTransactionYear from '@libs/TransactionUtils/shouldShowTransactionYear'; import Navigation from '@navigation/Navigation'; import type {ReportsSplitNavigatorParamList} from '@navigation/types'; @@ -53,6 +63,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type * as OnyxTypes from '@src/types/onyx'; +import type {TransactionViolation, TransactionViolations} from '@src/types/onyx'; import MoneyRequestReportTableHeader from './MoneyRequestReportTableHeader'; import MoneyRequestReportTotalSpend from './MoneyRequestReportTotalSpend'; import MoneyRequestReportTransactionItem from './MoneyRequestReportTransactionItem'; @@ -179,7 +190,9 @@ function MoneyRequestReportTransactionList({ for (const transaction of transactions) { const transactionViolations = violations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`]; if (transactionViolations) { - const filteredTransactionViolations = transactionViolations.filter((violation) => shouldShowViolation(report, policy, violation.name, currentUserDetails.email ?? '')); + const filteredTransactionViolations = mergeProhibitedViolations( + transactionViolations.filter((violation) => shouldShowViolation(report, policy, violation.name, currentUserDetails.email ?? '')), + ); if (filteredTransactionViolations.length > 0) { filtered[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`] = filteredTransactionViolations; diff --git a/src/hooks/useTransactionViolations.ts b/src/hooks/useTransactionViolations.ts index ceb11f3ac0aa..b12156300ef7 100644 --- a/src/hooks/useTransactionViolations.ts +++ b/src/hooks/useTransactionViolations.ts @@ -1,6 +1,6 @@ import {useMemo} from 'react'; import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID'; -import {isViolationDismissed, shouldShowViolation} from '@libs/TransactionUtils'; +import {isViolationDismissed, mergeProhibitedViolations, shouldShowViolation} from '@libs/TransactionUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {TransactionViolation, TransactionViolations} from '@src/types/onyx'; @@ -8,25 +8,6 @@ import getEmptyArray from '@src/types/utils/getEmptyArray'; import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; import useOnyx from './useOnyx'; -function mergeProhibitedViolations(transactionViolations: TransactionViolations): TransactionViolations { - const prohibitedViolations = transactionViolations.filter((violation: TransactionViolation) => violation.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); - - if (prohibitedViolations.length === 0) { - return transactionViolations; - } - - const prohibitedExpenses = prohibitedViolations.flatMap((violation: TransactionViolation) => violation.data?.prohibitedExpenseRule ?? []); - const mergedProhibitedViolations: TransactionViolation = { - name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, - data: { - prohibitedExpenseRule: prohibitedExpenses, - }, - type: CONST.VIOLATION_TYPES.VIOLATION, - }; - - return [...transactionViolations.filter((violation: TransactionViolation) => violation.name !== CONST.VIOLATIONS.PROHIBITED_EXPENSE), mergedProhibitedViolations]; -} - function useTransactionViolations(transactionID?: string, shouldShowRterForSettledReport = true): TransactionViolations { const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(transactionID)}`, { canBeMissing: true, diff --git a/src/languages/de.ts b/src/languages/de.ts index 2425073e3c58..af4c63cdc138 100644 --- a/src/languages/de.ts +++ b/src/languages/de.ts @@ -6800,7 +6800,7 @@ ${amount} für ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseTypes}) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Verbotene Ausgaben:'; const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { switch (prohibitedExpenseType) { diff --git a/src/languages/fr.ts b/src/languages/fr.ts index 649f0e610d5e..7738bd5bd780 100644 --- a/src/languages/fr.ts +++ b/src/languages/fr.ts @@ -6808,7 +6808,7 @@ ${amount} pour ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseTypes}) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Dépense interdite :'; const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { switch (prohibitedExpenseType) { diff --git a/src/languages/it.ts b/src/languages/it.ts index ca68d48f35ce..0cd290904692 100644 --- a/src/languages/it.ts +++ b/src/languages/it.ts @@ -6816,7 +6816,7 @@ ${amount} per ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseTypes}) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Spesa vietata:'; const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { switch (prohibitedExpenseType) { diff --git a/src/languages/ja.ts b/src/languages/ja.ts index 5516bb77d675..833ba85851ba 100644 --- a/src/languages/ja.ts +++ b/src/languages/ja.ts @@ -6749,7 +6749,7 @@ ${date} - ${merchant}に${amount}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseTypes}) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = '禁止された経費:'; const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { switch (prohibitedExpenseType) { diff --git a/src/languages/nl.ts b/src/languages/nl.ts index fb868418df47..567cfebc921c 100644 --- a/src/languages/nl.ts +++ b/src/languages/nl.ts @@ -6798,7 +6798,7 @@ ${amount} voor ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseTypes}) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Verboden uitgave:'; const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { switch (prohibitedExpenseType) { diff --git a/src/languages/pl.ts b/src/languages/pl.ts index e38b6f4ce6ed..47e1ac5bd9ca 100644 --- a/src/languages/pl.ts +++ b/src/languages/pl.ts @@ -6785,7 +6785,7 @@ ${amount} dla ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseTypes}) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Zabroniony wydatek:'; const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { switch (prohibitedExpenseType) { diff --git a/src/languages/pt-BR.ts b/src/languages/pt-BR.ts index 55c77c556246..78651ed88dde 100644 --- a/src/languages/pt-BR.ts +++ b/src/languages/pt-BR.ts @@ -6798,7 +6798,7 @@ ${amount} para ${merchant} - ${date}`, } return message; }, - prohibitedExpense: ({prohibitedExpenseTypes}) => { + prohibitedExpense: ({prohibitedExpenseTypes}: ViolationsProhibitedExpenseParams) => { const preMessage = 'Despesa proibida:'; const getProhibitedExpenseTypeText = (prohibitedExpenseType: string) => { switch (prohibitedExpenseType) { diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index da4424cb4280..bd4e395d8c11 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -1158,6 +1158,25 @@ function shouldShowBrokenConnectionViolationForMultipleTransactions( return shouldShowBrokenConnectionViolationInternal(brokenConnectionViolations, report, policy); } +function mergeProhibitedViolations(transactionViolations: TransactionViolations): TransactionViolations { + const prohibitedViolations = transactionViolations.filter((violation: TransactionViolation) => violation.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); + + if (prohibitedViolations.length === 0) { + return transactionViolations; + } + + const prohibitedExpenses = prohibitedViolations.flatMap((violation: TransactionViolation) => violation.data?.prohibitedExpenseRule ?? []); + const mergedProhibitedViolations: TransactionViolation = { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + data: { + prohibitedExpenseRule: prohibitedExpenses, + }, + type: CONST.VIOLATION_TYPES.VIOLATION, + }; + + return [...transactionViolations.filter((violation: TransactionViolation) => violation.name !== CONST.VIOLATIONS.PROHIBITED_EXPENSE), mergedProhibitedViolations]; +} + /** * Check if the user should see the violation */ @@ -2148,6 +2167,7 @@ export { getAttendeesListDisplayString, isCorporateCardTransaction, isExpenseUnreported, + mergeProhibitedViolations, }; export type {TransactionChanges}; From 07d6bc91bc440e8125f05899619d934905582e90 Mon Sep 17 00:00:00 2001 From: paulnjs Date: Tue, 11 Nov 2025 00:23:48 +0700 Subject: [PATCH 08/11] fix lint --- .../MoneyRequestReportTransactionList.tsx | 1 - src/hooks/useTransactionViolations.ts | 1 - src/languages/zh-hans.ts | 1 - src/libs/TransactionUtils/index.ts | 3 ++ tests/unit/useTransactionViolationsTest.ts | 34 ++++++++++++++++--- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx index 9e15d939b7e3..a489f0d6e755 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx @@ -63,7 +63,6 @@ import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type * as OnyxTypes from '@src/types/onyx'; -import type {TransactionViolation, TransactionViolations} from '@src/types/onyx'; import MoneyRequestReportTableHeader from './MoneyRequestReportTableHeader'; import MoneyRequestReportTotalSpend from './MoneyRequestReportTotalSpend'; import MoneyRequestReportTransactionItem from './MoneyRequestReportTransactionItem'; diff --git a/src/hooks/useTransactionViolations.ts b/src/hooks/useTransactionViolations.ts index b12156300ef7..724ca3945dcc 100644 --- a/src/hooks/useTransactionViolations.ts +++ b/src/hooks/useTransactionViolations.ts @@ -1,7 +1,6 @@ import {useMemo} from 'react'; import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID'; import {isViolationDismissed, mergeProhibitedViolations, shouldShowViolation} from '@libs/TransactionUtils'; -import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {TransactionViolation, TransactionViolations} from '@src/types/onyx'; import getEmptyArray from '@src/types/utils/getEmptyArray'; diff --git a/src/languages/zh-hans.ts b/src/languages/zh-hans.ts index 1b04d5b7876a..52824a36247c 100644 --- a/src/languages/zh-hans.ts +++ b/src/languages/zh-hans.ts @@ -291,7 +291,6 @@ import type { ViolationsOverCategoryLimitParams, ViolationsOverLimitParams, ViolationsPerDayLimitParams, - ViolationsProhibitedExpenseParams, ViolationsReceiptRequiredParams, ViolationsRterParams, ViolationsTagOutOfPolicyParams, diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index bd4e395d8c11..0b5b38abd0be 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -1158,6 +1158,9 @@ function shouldShowBrokenConnectionViolationForMultipleTransactions( return shouldShowBrokenConnectionViolationInternal(brokenConnectionViolations, report, policy); } +/** + * Merge prohibited violations into one violation. + */ function mergeProhibitedViolations(transactionViolations: TransactionViolations): TransactionViolations { const prohibitedViolations = transactionViolations.filter((violation: TransactionViolation) => violation.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); diff --git a/tests/unit/useTransactionViolationsTest.ts b/tests/unit/useTransactionViolationsTest.ts index 30c8e0dfb183..48590843b59b 100644 --- a/tests/unit/useTransactionViolationsTest.ts +++ b/tests/unit/useTransactionViolationsTest.ts @@ -8,10 +8,36 @@ import type {Policy, Report, Transaction, TransactionViolations} from '@src/type import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; // Mock the required modules -jest.mock('@libs/TransactionUtils', () => ({ - isViolationDismissed: jest.fn(), - shouldShowViolation: jest.fn(), -})); +jest.mock('@libs/TransactionUtils', () => { + const CONST = jest.requireActual('@src/CONST').default; + return { + isViolationDismissed: jest.fn(), + shouldShowViolation: jest.fn(), + mergeProhibitedViolations: (transactionViolations: Array<{name: string; type: string; data?: {prohibitedExpenseRule?: string | string[]}}>) => { + const prohibitedViolations = transactionViolations.filter((violation) => violation.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); + + if (prohibitedViolations.length === 0) { + return transactionViolations; + } + + const prohibitedExpenses = prohibitedViolations.flatMap((violation) => { + const rule = violation.data?.prohibitedExpenseRule; + if (!rule) return []; + return Array.isArray(rule) ? rule : [rule]; + }); + + const mergedProhibitedViolations = { + name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + data: { + prohibitedExpenseRule: prohibitedExpenses, + }, + type: CONST.VIOLATION_TYPES.VIOLATION, + }; + + return [...transactionViolations.filter((violation) => violation.name !== CONST.VIOLATIONS.PROHIBITED_EXPENSE), mergedProhibitedViolations]; + }, + }; +}); jest.mock('@hooks/useCurrentUserPersonalDetails', () => ({ // eslint-disable-next-line @typescript-eslint/naming-convention From 7119824b7c76022acd0ff21d55e269b5eddbaa1b Mon Sep 17 00:00:00 2001 From: paulnjs Date: Tue, 11 Nov 2025 00:34:20 +0700 Subject: [PATCH 09/11] refactor code --- tests/unit/useTransactionViolationsTest.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/unit/useTransactionViolationsTest.ts b/tests/unit/useTransactionViolationsTest.ts index 48590843b59b..b403aea707eb 100644 --- a/tests/unit/useTransactionViolationsTest.ts +++ b/tests/unit/useTransactionViolationsTest.ts @@ -7,14 +7,14 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, Report, Transaction, TransactionViolations} from '@src/types/onyx'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; -// Mock the required modules jest.mock('@libs/TransactionUtils', () => { - const CONST = jest.requireActual('@src/CONST').default; + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + const CONST_MOCK = jest.requireActual('@src/CONST').default as typeof import('@src/CONST').default; return { isViolationDismissed: jest.fn(), shouldShowViolation: jest.fn(), mergeProhibitedViolations: (transactionViolations: Array<{name: string; type: string; data?: {prohibitedExpenseRule?: string | string[]}}>) => { - const prohibitedViolations = transactionViolations.filter((violation) => violation.name === CONST.VIOLATIONS.PROHIBITED_EXPENSE); + const prohibitedViolations = transactionViolations.filter((violation) => violation.name === CONST_MOCK.VIOLATIONS.PROHIBITED_EXPENSE); if (prohibitedViolations.length === 0) { return transactionViolations; @@ -22,19 +22,21 @@ jest.mock('@libs/TransactionUtils', () => { const prohibitedExpenses = prohibitedViolations.flatMap((violation) => { const rule = violation.data?.prohibitedExpenseRule; - if (!rule) return []; + if (!rule) { + return []; + } return Array.isArray(rule) ? rule : [rule]; }); const mergedProhibitedViolations = { - name: CONST.VIOLATIONS.PROHIBITED_EXPENSE, + name: CONST_MOCK.VIOLATIONS.PROHIBITED_EXPENSE, data: { prohibitedExpenseRule: prohibitedExpenses, }, - type: CONST.VIOLATION_TYPES.VIOLATION, + type: CONST_MOCK.VIOLATION_TYPES.VIOLATION, }; - return [...transactionViolations.filter((violation) => violation.name !== CONST.VIOLATIONS.PROHIBITED_EXPENSE), mergedProhibitedViolations]; + return [...transactionViolations.filter((violation) => violation.name !== CONST_MOCK.VIOLATIONS.PROHIBITED_EXPENSE), mergedProhibitedViolations]; }, }; }); From 01811f9656d39666f0d8c18a8d77fcabbbd4b375 Mon Sep 17 00:00:00 2001 From: paulnjs Date: Tue, 11 Nov 2025 00:41:32 +0700 Subject: [PATCH 10/11] fix lint --- tests/unit/useTransactionViolationsTest.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/useTransactionViolationsTest.ts b/tests/unit/useTransactionViolationsTest.ts index b403aea707eb..42e457646d87 100644 --- a/tests/unit/useTransactionViolationsTest.ts +++ b/tests/unit/useTransactionViolationsTest.ts @@ -2,6 +2,7 @@ import {renderHook} from '@testing-library/react-native'; import Onyx from 'react-native-onyx'; import useTransactionViolations from '@hooks/useTransactionViolations'; import {isViolationDismissed, shouldShowViolation} from '@libs/TransactionUtils'; +import type CONST_TYPE from '@src/CONST'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, Report, Transaction, TransactionViolations} from '@src/types/onyx'; @@ -9,7 +10,7 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; jest.mock('@libs/TransactionUtils', () => { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - const CONST_MOCK = jest.requireActual('@src/CONST').default as typeof import('@src/CONST').default; + const CONST_MOCK = jest.requireActual('@src/CONST').default as typeof CONST_TYPE; return { isViolationDismissed: jest.fn(), shouldShowViolation: jest.fn(), From e1e14734856edfc2759361a2b479959b93229f8e Mon Sep 17 00:00:00 2001 From: paulnjs Date: Wed, 12 Nov 2025 17:06:58 +0700 Subject: [PATCH 11/11] update code --- src/components/Search/index.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/Search/index.tsx b/src/components/Search/index.tsx index b91a445af280..6198a8f6430b 100644 --- a/src/components/Search/index.tsx +++ b/src/components/Search/index.tsx @@ -55,7 +55,7 @@ import { shouldShowEmptyState, shouldShowYear as shouldShowYearUtil, } from '@libs/SearchUIUtils'; -import {isOnHold, isTransactionPendingDelete, shouldShowViolation} from '@libs/TransactionUtils'; +import {isOnHold, isTransactionPendingDelete, mergeProhibitedViolations, shouldShowViolation} from '@libs/TransactionUtils'; import Navigation, {navigationRef} from '@navigation/Navigation'; import type {SearchFullscreenNavigatorParamList} from '@navigation/types'; import EmptySearchView from '@pages/Search/EmptySearchView'; @@ -281,8 +281,8 @@ function Search({ if (report && policy) { const transactionViolations = violations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`]; if (transactionViolations) { - const filteredTransactionViolations = transactionViolations.filter((violation) => - shouldShowViolation(report as OnyxEntry, policy as OnyxEntry, violation.name, email ?? ''), + const filteredTransactionViolations = mergeProhibitedViolations( + transactionViolations.filter((violation) => shouldShowViolation(report as OnyxEntry, policy as OnyxEntry, violation.name, email ?? '')), ); if (filteredTransactionViolations.length > 0) {