-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Use new ResolveDuplicates when approver is resolving duplicates #48522
Merged
Merged
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
36b7e06
fix: Use new ResolveDuplicates when approver is resolving duplicates
nkdengineer 2d1d4ca
add new api params
nkdengineer 6325f73
Merge branch 'main' into fix/48416
nkdengineer 8086b11
complete parameter for API
nkdengineer 06bdd10
Merge branch 'main' into fix/48416
nkdengineer 107be8e
fix the order issue
nkdengineer c5023dc
fix lint
nkdengineer e881a3b
Merge branch 'main' into fix/48416
nkdengineer b549c50
merge main
nkdengineer 96d0da4
create a util to get iou action
nkdengineer 754932d
rename variable
nkdengineer b7a27c3
fix lint
nkdengineer b331032
use getIOUActionForTransactions
nkdengineer 85cc2e9
merge main
nkdengineer 5fcf063
remove unuse variable
nkdengineer f56ffab
Update src/libs/API/parameters/ResolveDuplicatesParams.ts
nkdengineer c076bdc
Merge branch 'main' into fix/48416
nkdengineer aee9154
fix lint
nkdengineer 0ba5ac8
Merge branch 'main' into fix/48416
nkdengineer c776014
update param
nkdengineer 46c75e1
Merge branch 'main' into fix/48416
nkdengineer e32ed93
Merge branch 'main' into fix/48416
nkdengineer 98287b6
fix native bug
nkdengineer 23e2313
Update src/libs/actions/IOU.ts
nkdengineer b3cce2d
Merge branch 'main' into fix/48416
nkdengineer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
type ResolveDuplicatesParams = { | ||
/** The ID of the transaction that we want to keep */ | ||
transactionID: string; | ||
/** The list of other duplicated transactions */ | ||
transactionIDList: string[]; | ||
created: string; | ||
merchant: string; | ||
amount: number; | ||
currency: string; | ||
category: string; | ||
comment: string; | ||
billable: boolean; | ||
reimbursable: boolean; | ||
tag: string; | ||
/** The reportActionID of the dismiss violation action of the kept transaction thread report */ | ||
optimisticReportActionID: string; | ||
/** The list ID of hold report action corresponds to transactionIDList */ | ||
reportActionIDList: string[]; | ||
}; | ||
|
||
export default ResolveDuplicatesParams; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
PayMoneyRequestParams, | ||
ReplaceReceiptParams, | ||
RequestMoneyParams, | ||
ResolveDuplicatesParams, | ||
SendInvoiceParams, | ||
SendMoneyParams, | ||
SetNameValuePairParams, | ||
|
@@ -7954,6 +7955,21 @@ | |
return workspaceSender?.policyID ?? report?.policyID ?? '-1'; | ||
} | ||
|
||
function getIOUActionForTransactions(transactionIDList: string[], iouReportID: string): ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU>[] { | ||
return Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`] ?? {})?.filter( | ||
(reportAction): reportAction is ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> => { | ||
if (!ReportActionsUtils.isMoneyRequestAction(reportAction)) { | ||
return false; | ||
} | ||
const message = ReportActionsUtils.getOriginalMessage(reportAction); | ||
if (!message?.IOUTransactionID) { | ||
return false; | ||
} | ||
return transactionIDList.includes(message.IOUTransactionID); | ||
}, | ||
); | ||
} | ||
|
||
/** Merge several transactions into one by updating the fields of the one we want to keep and deleting the rest */ | ||
function mergeDuplicates(params: TransactionMergeParams) { | ||
const originalSelectedTransaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`]; | ||
|
@@ -8038,18 +8054,7 @@ | |
}, | ||
}; | ||
|
||
const iouActionsToDelete = Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${params.reportID}`] ?? {})?.filter( | ||
(reportAction): reportAction is ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> => { | ||
if (!ReportActionsUtils.isMoneyRequestAction(reportAction)) { | ||
return false; | ||
} | ||
const message = ReportActionsUtils.getOriginalMessage(reportAction); | ||
if (!message?.IOUTransactionID) { | ||
return false; | ||
} | ||
return params.transactionIDList.includes(message.IOUTransactionID); | ||
}, | ||
); | ||
const iouActionsToDelete = getIOUActionForTransactions(params.transactionIDList, params.reportID); | ||
|
||
const deletedTime = DateUtils.getDBTime(); | ||
const expenseReportActionsOptimisticData: OnyxUpdate = { | ||
|
@@ -8110,6 +8115,136 @@ | |
API.write(WRITE_COMMANDS.TRANSACTION_MERGE, params, {optimisticData, failureData}); | ||
} | ||
|
||
function resolveDuplicates(params: TransactionMergeParams) { | ||
nkdengineer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const originalSelectedTransaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`]; | ||
|
||
const optimisticTransactionData: OnyxUpdate = { | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`, | ||
value: { | ||
...originalSelectedTransaction, | ||
billable: params.billable, | ||
comment: { | ||
comment: params.comment, | ||
}, | ||
category: params.category, | ||
created: params.created, | ||
currency: params.currency, | ||
modifiedMerchant: params.merchant, | ||
reimbursable: params.reimbursable, | ||
tag: params.tag, | ||
}, | ||
}; | ||
|
||
const failureTransactionData: OnyxUpdate = { | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${params.transactionID}`, | ||
// eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style | ||
value: originalSelectedTransaction as OnyxTypes.Transaction, | ||
}; | ||
|
||
const optimisticTransactionViolations: OnyxUpdate[] = [...params.transactionIDList, params.transactionID].map((id) => { | ||
const violations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${id}`] ?? []; | ||
const newViolation = {name: CONST.VIOLATIONS.HOLD, type: CONST.VIOLATION_TYPES.VIOLATION}; | ||
const updatedViolations = id === params.transactionID ? violations : [...violations, newViolation]; | ||
return { | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${id}`, | ||
value: updatedViolations.filter((violation) => violation.name !== CONST.VIOLATIONS.DUPLICATED_TRANSACTION), | ||
}; | ||
}); | ||
|
||
const failureTransactionViolations: OnyxUpdate[] = [...params.transactionIDList, params.transactionID].map((id) => { | ||
const violations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${id}`] ?? []; | ||
return { | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${id}`, | ||
value: violations, | ||
}; | ||
}); | ||
|
||
const iouActionList = getIOUActionForTransactions(params.transactionIDList, params.reportID); | ||
const transactionThreadReportIDList = iouActionList.map((action) => action?.childReportID); | ||
const orderedTransactionIDList = iouActionList.map((action) => { | ||
const message = ReportActionsUtils.getOriginalMessage(action); | ||
return message?.IOUTransactionID ?? ''; | ||
}); | ||
|
||
const optimisticHoldActions: OnyxUpdate[] = []; | ||
const failureHoldActions: OnyxUpdate[] = []; | ||
const reportActionIDList: string[] = []; | ||
transactionThreadReportIDList.forEach((transactionThreadReportID) => { | ||
const createdReportAction = ReportUtils.buildOptimisticHoldReportAction(); | ||
reportActionIDList.push(createdReportAction.reportActionID); | ||
optimisticHoldActions.push({ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`, | ||
value: { | ||
[createdReportAction.reportActionID]: createdReportAction, | ||
}, | ||
}); | ||
failureHoldActions.push({ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`, | ||
value: { | ||
[createdReportAction.reportActionID]: { | ||
errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericHoldExpenseFailureMessage'), | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
const transactionThreadReportID = Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${params.reportID}`] ?? {})?.find( | ||
(reportAction): reportAction is ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> => { | ||
if (!ReportActionsUtils.isMoneyRequestAction(reportAction)) { | ||
return false; | ||
} | ||
const message = ReportActionsUtils.getOriginalMessage(reportAction); | ||
if (!message?.IOUTransactionID) { | ||
return false; | ||
} | ||
return params.transactionID === message.IOUTransactionID; | ||
}, | ||
)?.childReportID; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can also be replaced with that new function getIOUActionForTransactions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated. |
||
|
||
const optimisticReportAction = ReportUtils.buildOptimisticDismissedViolationReportAction({ | ||
reason: 'manual', | ||
violationName: CONST.VIOLATIONS.DUPLICATED_TRANSACTION, | ||
}); | ||
|
||
const optimisticReportActionData: OnyxUpdate = { | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`, | ||
value: { | ||
[optimisticReportAction.reportActionID]: optimisticReportAction, | ||
}, | ||
}; | ||
|
||
const failureReportActionData: OnyxUpdate = { | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`, | ||
value: { | ||
[optimisticReportAction.reportActionID]: null, | ||
}, | ||
}; | ||
|
||
const optimisticData: OnyxUpdate[] = []; | ||
const failureData: OnyxUpdate[] = []; | ||
|
||
optimisticData.push(optimisticTransactionData, ...optimisticTransactionViolations, ...optimisticHoldActions, optimisticReportActionData); | ||
failureData.push(failureTransactionData, ...failureTransactionViolations, ...failureHoldActions, failureReportActionData); | ||
const {reportID, transactionIDList, receiptID, ...otherParams} = params; | ||
|
||
const parameters: ResolveDuplicatesParams = { | ||
...otherParams, | ||
reportActionIDList, | ||
transactionIDList: orderedTransactionIDList, | ||
optimisticReportActionID: optimisticReportAction.reportActionID, | ||
}; | ||
|
||
API.write(WRITE_COMMANDS.RESOLVE_DUPLICATES, parameters, {optimisticData, failureData}); | ||
} | ||
|
||
export { | ||
adjustRemainingSplitShares, | ||
approveMoneyRequest, | ||
|
@@ -8180,6 +8315,7 @@ | |
updateMoneyRequestTaxAmount, | ||
updateMoneyRequestTaxRate, | ||
mergeDuplicates, | ||
resolveDuplicates, | ||
prepareToCleanUpMoneyRequest, | ||
}; | ||
export type {GPSPoint as GpsPoint, IOURequestType}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename this file too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as below.