@@ -3,6 +3,7 @@ import Onyx from 'react-native-onyx';
33import CONST from '@src/CONST' ;
44import ONYXKEYS from '@src/ONYXKEYS' ;
55import type { OnyxKey } from '@src/ONYXKEYS' ;
6+ import type { Transaction } from '@src/types/onyx' ;
67import type Policy from '@src/types/onyx/Policy' ;
78import type Report from '@src/types/onyx/Report' ;
89import Timing from './actions/Timing' ;
@@ -18,13 +19,16 @@ import {getTitleReportField, isArchivedReport} from './ReportUtils';
1819/**
1920 * Get the object type from an Onyx key
2021 */
21- function determineObjectTypeByKey ( key : string ) : 'report' | 'policy' | 'unknown' {
22+ function determineObjectTypeByKey ( key : string ) : 'report' | 'policy' | 'transaction' | ' unknown' {
2223 if ( key . startsWith ( ONYXKEYS . COLLECTION . REPORT ) ) {
2324 return 'report' ;
2425 }
2526 if ( key . startsWith ( ONYXKEYS . COLLECTION . POLICY ) ) {
2627 return 'policy' ;
2728 }
29+ if ( key . startsWith ( ONYXKEYS . COLLECTION . TRANSACTION ) ) {
30+ return 'transaction' ;
31+ }
2832 return 'unknown' ;
2933}
3034
@@ -67,6 +71,13 @@ function getPolicyByID(policyID: string | undefined, allPolicies: Record<string,
6771 return allPolicies [ `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` ] ;
6872}
6973
74+ /**
75+ * Get transaction by ID from the transactions collection
76+ */
77+ function getTransactionByID ( transactionID : string , allTransactions : Record < string , Transaction > ) : Transaction | undefined {
78+ return allTransactions [ `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ transactionID } ` ] ;
79+ }
80+
7081/**
7182 * Get all reports associated with a policy ID
7283 */
@@ -100,6 +111,24 @@ function getReportsByPolicyID(policyID: string, allReports: Record<string, Repor
100111 } ) ;
101112}
102113
114+ /**
115+ * Get the report associated with a transaction ID
116+ */
117+ function getReportByTransactionID ( transactionID : string , context : UpdateContext ) : Report | undefined {
118+ if ( ! transactionID ) {
119+ return undefined ;
120+ }
121+
122+ const transaction = getTransactionByID ( transactionID , context . allTransactions ) ;
123+
124+ if ( ! transaction ?. reportID ) {
125+ return undefined ;
126+ }
127+
128+ // Get the report using the transaction's reportID from context
129+ return getReportByID ( transaction . reportID , context . allReports ) ;
130+ }
131+
103132/**
104133 * Generate the Onyx key for a report
105134 */
@@ -180,13 +209,18 @@ function computeReportNameIfNeeded(report: Report | undefined, incomingUpdate: O
180209 const formula = titleField . defaultValue ;
181210 const formulaParts = parse ( formula ) ;
182211
212+ let transaction : Transaction | undefined ;
213+ if ( updateType === 'transaction' ) {
214+ transaction = getTransactionByID ( ( incomingUpdate . value as Transaction ) . transactionID , context . allTransactions ) ;
215+ }
216+
183217 // Check if any formula part might be affected by this update
184218 const isAffected = formulaParts . some ( ( part ) => {
185219 if ( part . type === FORMULA_PART_TYPES . REPORT ) {
186220 // Checking if the formula part is affected in this manner works, but it could certainly be more precise.
187221 // For example, a policy update only affects the part if the formula in the policy changed, or if the report part references a field on the policy.
188222 // However, if we run into performance problems, this would be a good place to optimize.
189- return updateType === 'report' || updateType === 'policy' ;
223+ return updateType === 'report' || updateType === 'transaction' || updateType === ' policy';
190224 }
191225 if ( part . type === FORMULA_PART_TYPES . FIELD ) {
192226 return updateType === 'report' ;
@@ -206,10 +240,13 @@ function computeReportNameIfNeeded(report: Report | undefined, incomingUpdate: O
206240
207241 const updatedPolicy = updateType === 'policy' && targetReport . policyID === getPolicyIDFromKey ( incomingUpdate . key ) ? { ...( policy ?? { } ) , ...( incomingUpdate . value as Policy ) } : policy ;
208242
243+ const updatedTransaction = updateType === 'transaction' ? { ...( transaction ?? { } ) , ...( incomingUpdate . value as Transaction ) } : undefined ;
244+
209245 // Compute the new name
210246 const formulaContext : FormulaContext = {
211247 report : updatedReport ,
212248 policy : updatedPolicy ,
249+ transaction : updatedTransaction ,
213250 } ;
214251
215252 const newName = compute ( formula , formulaContext ) ;
@@ -260,7 +297,6 @@ function updateOptimisticReportNamesFromUpdates(updates: OnyxUpdate[], context:
260297
261298 for ( const update of updates ) {
262299 const objectType = determineObjectTypeByKey ( update . key ) ;
263- let affectedReports : Report [ ] = [ ] ;
264300
265301 switch ( objectType ) {
266302 case 'report' : {
@@ -284,26 +320,50 @@ function updateOptimisticReportNamesFromUpdates(updates: OnyxUpdate[], context:
284320
285321 case 'policy' : {
286322 const policyID = getPolicyIDFromKey ( update . key ) ;
287- affectedReports = getReportsByPolicyID ( policyID , allReports , context ) ;
323+ const affectedReports = getReportsByPolicyID ( policyID , allReports , context ) ;
324+ for ( const report of affectedReports ) {
325+ const reportNameUpdate = computeReportNameIfNeeded ( report , update , context ) ;
326+
327+ if ( reportNameUpdate ) {
328+ additionalUpdates . push ( {
329+ key : getReportKey ( report . reportID ) ,
330+ onyxMethod : Onyx . METHOD . MERGE ,
331+ value : {
332+ reportName : reportNameUpdate ,
333+ } ,
334+ } ) ;
335+ }
336+ }
288337 break ;
289338 }
290339
291- default :
292- continue ;
293- }
340+ case 'transaction' : {
341+ let report : Report | undefined ;
342+ const transactionUpdate = update . value as Partial < Transaction > ;
343+ if ( transactionUpdate . reportID ) {
344+ report = getReportByID ( transactionUpdate . reportID , allReports ) ;
345+ } else {
346+ report = getReportByTransactionID ( getTransactionIDFromKey ( update . key ) , context ) ;
347+ }
294348
295- for ( const report of affectedReports ) {
296- const reportNameUpdate = computeReportNameIfNeeded ( report , update , context ) ;
297-
298- if ( reportNameUpdate ) {
299- additionalUpdates . push ( {
300- key : getReportKey ( report . reportID ) ,
301- onyxMethod : Onyx . METHOD . MERGE ,
302- value : {
303- reportName : reportNameUpdate ,
304- } ,
305- } ) ;
349+ if ( report ) {
350+ const reportNameUpdate = computeReportNameIfNeeded ( report , update , context ) ;
351+
352+ if ( reportNameUpdate ) {
353+ additionalUpdates . push ( {
354+ key : getReportKey ( report . reportID ) ,
355+ onyxMethod : Onyx . METHOD . MERGE ,
356+ value : {
357+ reportName : reportNameUpdate ,
358+ } ,
359+ } ) ;
360+ }
361+ }
362+ break ;
306363 }
364+
365+ default :
366+ continue ;
307367 }
308368 }
309369
@@ -326,5 +386,5 @@ function createUpdateContext(): Promise<UpdateContext> {
326386 return getUpdateContextAsync ( ) ;
327387}
328388
329- export { updateOptimisticReportNamesFromUpdates , computeReportNameIfNeeded , createUpdateContext , shouldComputeReportName } ;
389+ export { updateOptimisticReportNamesFromUpdates , computeReportNameIfNeeded , createUpdateContext , shouldComputeReportName , getReportByTransactionID } ;
330390export type { UpdateContext } ;
0 commit comments