Skip to content
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

Refactor buildOptimisticTransaction function #54491

Merged
merged 10 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 47 additions & 23 deletions src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ import type DeepValueOf from '@src/types/utils/DeepValueOf';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import getDistanceInMeters from './getDistanceInMeters';

type TransactionParams = {
amount: number;
currency: string;
reportID: string;
comment?: string;
attendees?: Attendee[];
created?: string;
merchant?: string;
receipt?: OnyxEntry<Receipt>;
category?: string;
tag?: string;
taxCode?: string;
taxAmount?: number;
billable?: boolean;
pendingFields?: Partial<{[K in TransactionPendingFieldsKey]: ValueOf<typeof CONST.RED_BRICK_ROAD_PENDING_ACTION>}>;
reimbursable?: boolean;
};

type BuildOptimisticTransactionParams = {
source?: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't you think source and filename should be part of the transactionParams? They relate to the transaction specifically so I would like to move them there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@neil-marcellini I updated.

originalTransactionID?: string;
filename?: string;
existingTransactionID?: string;
existingTransaction?: OnyxEntry<Transaction>;
policy?: OnyxEntry<Policy>;
transactionParams: TransactionParams;
};

let allTransactions: OnyxCollection<Transaction> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
Expand Down Expand Up @@ -130,29 +158,25 @@ function isManualRequest(transaction: Transaction): boolean {
* @param [existingTransactionID] When creating a distance expense, an empty transaction has already been created with a transactionID. In that case, the transaction here needs to have
* it's transactionID match what was already generated.
*/
function buildOptimisticTransaction(
amount: number,
currency: string,
reportID: string,
comment = '',
attendees: Attendee[] = [],
created = '',
source = '',
originalTransactionID = '',
merchant = '',
receipt?: OnyxEntry<Receipt>,
filename = '',
existingTransactionID: string | null = null,
category = '',
tag = '',
taxCode = '',
taxAmount = 0,
billable = false,
pendingFields: Partial<{[K in TransactionPendingFieldsKey]: ValueOf<typeof CONST.RED_BRICK_ROAD_PENDING_ACTION>}> | undefined = undefined,
reimbursable = true,
existingTransaction: OnyxEntry<Transaction> | undefined = undefined,
policy: OnyxEntry<Policy> = undefined,
): Transaction {
function buildOptimisticTransaction(params: BuildOptimisticTransactionParams): Transaction {
const {source = '', originalTransactionID = '', existingTransactionID, existingTransaction, filename = '', policy, transactionParams} = params;
const {
amount,
currency,
reportID,
comment = '',
attendees = [],
created = '',
merchant = '',
receipt,
category = '',
tag = '',
taxCode = '',
taxAmount = 0,
billable = false,
pendingFields,
reimbursable = true,
} = transactionParams;
// transactionIDs are random, positive, 64-bit numeric strings.
// Because JS can only handle 53-bit numbers, transactionIDs are strings in the front-end (just like reportActionID)
const transactionID = existingTransactionID ?? NumberUtils.rand64();
Expand Down
Loading
Loading