Skip to content

Commit 1f59d37

Browse files
authored
Merge pull request Expensify#92774 from callstack-internal/refactor/hoist-isparticipantp2p
[NOQA] refactor: hoist isParticipantP2P into IOUUtils
2 parents 493722b + 9a20b4b commit 1f59d37

5 files changed

Lines changed: 71 additions & 85 deletions

File tree

src/components/MoneyRequestConfirmationList/sections/AmountField.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ import useOnyx from '@hooks/useOnyx';
1212
import useThemeStyles from '@hooks/useThemeStyles';
1313
import {clearMoneyRequestAmount, getMoneyRequestParticipantsFromReport, setMoneyRequestAmount, setMoneyRequestTaxAmount, setMoneyRequestTaxRate} from '@libs/actions/IOU/MoneyRequest';
1414
import {convertToBackendAmount, convertToFrontendAmountAsString, getLocalizedCurrencySymbol} from '@libs/CurrencyUtils';
15-
import {calculateAmount, isMovingTransactionFromTrackExpense} from '@libs/IOUUtils';
15+
import {calculateAmount, isMovingTransactionFromTrackExpense, isParticipantP2P} from '@libs/IOUUtils';
1616
import Navigation from '@libs/Navigation/Navigation';
1717
import {shouldEnableNegative} from '@libs/ReportUtils';
1818
import {calculateTaxAmount, getTaxCode, getTaxValue} from '@libs/TransactionUtils';
19-
import {isParticipantP2P} from '@pages/iou/request/step/IOURequestStepAmount';
2019
import IOURequestStepCurrencyModal from '@pages/iou/request/step/IOURequestStepCurrencyModal';
2120
import {resetSplitShares, setDraftSplitTransaction, setSplitShares} from '@userActions/IOU/Split';
2221
import CONST from '@src/CONST';

src/libs/IOUUtils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,14 @@ function resolveReportForMoneyRequest({
513513
return routeReport;
514514
}
515515

516+
/**
517+
* Check whether a money-request participant represents a P2P chat (i.e. another user, not a
518+
* policy-expense chat and not a self DM).
519+
*/
520+
function isParticipantP2P(participant: {accountID?: number; isPolicyExpenseChat?: boolean; isSelfDM?: boolean} | undefined): boolean {
521+
return !!(participant?.accountID && !participant.isPolicyExpenseChat && !participant.isSelfDM);
522+
}
523+
516524
export {
517525
calculateAmount,
518526
calculateSplitAmountFromPercentage,
@@ -531,6 +539,7 @@ export {
531539
calculateDefaultReimbursable,
532540
getInitialPerDiemTargetReport,
533541
getIsWorkspacesOnlyForTransaction,
542+
isParticipantP2P,
534543
resolveOptimisticChatReportID,
535544
resolveReportForMoneyRequest,
536545
};

src/pages/iou/request/step/IOURequestStepAmount.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
calculateDefaultReimbursable,
3131
getExistingTransactionID,
3232
isMovingTransactionFromTrackExpense,
33+
isParticipantP2P,
3334
navigateToConfirmationPage,
3435
navigateToParticipantPage,
3536
resolveOptimisticChatReportID,
@@ -581,13 +582,6 @@ function IOURequestStepAmount({
581582
);
582583
}
583584

584-
/**
585-
* Check if the participant is a P2P chat
586-
*/
587-
function isParticipantP2P(participant: {accountID?: number; isPolicyExpenseChat?: boolean; isSelfDM?: boolean} | undefined): boolean {
588-
return !!(participant?.accountID && !participant.isPolicyExpenseChat && !participant.isSelfDM);
589-
}
590-
591585
const IOURequestStepAmountWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepAmount, true);
592586

593587
const IOURequestStepAmountWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepAmountWithWritableReportOrNotFound, true);
@@ -596,4 +590,4 @@ const IOURequestStepAmountWithFullTransactionOrNotFound = withFullTransactionOrN
596590
const IOURequestStepAmountWithTransactionOnly = withFullTransactionOrNotFound(IOURequestStepAmount, true);
597591

598592
export default IOURequestStepAmountWithFullTransactionOrNotFound;
599-
export {isParticipantP2P, IOURequestStepAmountWithTransactionOnly};
593+
export {IOURequestStepAmountWithTransactionOnly};

tests/unit/IOURequestStepAmountTest.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

tests/unit/IOUUtilsTest.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,3 +944,62 @@ describe('getExistingTransactionID', () => {
944944
});
945945
});
946946
});
947+
948+
describe('isParticipantP2P', () => {
949+
it('should return true for P2P participant with accountID and isPolicyExpenseChat false', () => {
950+
const participant = {
951+
accountID: 123,
952+
isPolicyExpenseChat: false,
953+
};
954+
955+
expect(IOUUtils.isParticipantP2P(participant)).toBe(true);
956+
});
957+
958+
it('should return false when participant is undefined', () => {
959+
expect(IOUUtils.isParticipantP2P(undefined)).toBe(false);
960+
});
961+
962+
it('should return false when participant has no accountID', () => {
963+
const participant = {
964+
isPolicyExpenseChat: false,
965+
};
966+
967+
expect(IOUUtils.isParticipantP2P(participant)).toBe(false);
968+
});
969+
970+
it('should return false when participant is a policy expense chat', () => {
971+
const participant = {
972+
accountID: 123,
973+
isPolicyExpenseChat: true,
974+
};
975+
976+
expect(IOUUtils.isParticipantP2P(participant)).toBe(false);
977+
});
978+
979+
it('should return false when accountID is 0', () => {
980+
const participant = {
981+
accountID: 0,
982+
isPolicyExpenseChat: false,
983+
};
984+
985+
expect(IOUUtils.isParticipantP2P(participant)).toBe(false);
986+
});
987+
988+
it('should return true for P2P participant without isPolicyExpenseChat property', () => {
989+
const participant = {
990+
accountID: 456,
991+
};
992+
993+
expect(IOUUtils.isParticipantP2P(participant)).toBe(true);
994+
});
995+
996+
it('should return false for self-DM participant', () => {
997+
const participant = {
998+
accountID: 123,
999+
isPolicyExpenseChat: false,
1000+
isSelfDM: true,
1001+
};
1002+
1003+
expect(IOUUtils.isParticipantP2P(participant)).toBe(false);
1004+
});
1005+
});

0 commit comments

Comments
 (0)