Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/libs/actions/IOU/Duplicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ function createExpenseByType({
},
hasViolations: false,
customUnitPolicyID,
chatReportActions: undefined,
};
return submitPerDiemExpense(perDiemParams);
}
Expand Down
7 changes: 6 additions & 1 deletion src/libs/actions/IOU/PerDiem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ type PerDiemExpenseInformation = {
shouldDeferAutoSubmit?: boolean;
optimisticChatReportID?: string;
optimisticTransactionID?: string;
chatReportActions: OnyxEntry<OnyxTypes.ReportActions>;
// TODO: delegateAccountID will be made required in PR 13 when all callers pass the value (https://github.com/Expensify/App/issues/66425)
delegateAccountID?: number | undefined;
};
Expand All @@ -263,6 +264,7 @@ type PerDiemExpenseInformationParams = {
personalDetails: OnyxEntry<OnyxTypes.PersonalDetailsList>;
optimisticChatReportID?: string;
optimisticTransactionID?: string;
chatReportActions: OnyxEntry<OnyxTypes.ReportActions>;
// TODO: delegateAccountID will be made required in PR 13 when all callers pass the value (https://github.com/Expensify/App/issues/66425)
delegateAccountID?: number | undefined;
};
Expand Down Expand Up @@ -319,6 +321,7 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI
personalDetails,
optimisticChatReportID,
optimisticTransactionID: uiProvidedOptimisticTransactionID,
chatReportActions,
delegateAccountID,
} = perDiemExpenseInformation;
const {payeeAccountID = currentUserAccountIDParam, payeeEmail = currentUserEmailParam, participant} = participantParams;
Expand Down Expand Up @@ -474,7 +477,7 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI
delegateAccountIDParam: delegateAccountID,
});

let reportPreviewAction = shouldCreateNewMoneyRequestReport ? null : getReportPreviewAction(chatReport.reportID, iouReport.reportID);
let reportPreviewAction = shouldCreateNewMoneyRequestReport ? null : getReportPreviewAction(chatReport.reportID, iouReport.reportID, chatReportActions);

if (reportPreviewAction) {
reportPreviewAction = updateReportPreview(iouReport, reportPreviewAction, false, comment, optimisticTransaction);
Expand Down Expand Up @@ -924,6 +927,7 @@ function submitPerDiemExpense(submitPerDiemExpenseInformation: PerDiemExpenseInf
shouldDeferAutoSubmit,
optimisticChatReportID,
optimisticTransactionID,
chatReportActions,
delegateAccountID,
} = submitPerDiemExpenseInformation;
const {currency, comment = '', category, tag, created, customUnit, attendees, isFromGlobalCreate} = transactionParams;
Expand Down Expand Up @@ -976,6 +980,7 @@ function submitPerDiemExpense(submitPerDiemExpenseInformation: PerDiemExpenseInf
personalDetails,
optimisticChatReportID,
optimisticTransactionID,
chatReportActions,
delegateAccountID,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ function useExpenseSubmission(params: UseExpenseSubmissionParams) {
betas,
personalDetails,
optimisticChatReportID,
chatReportActions: allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`],
});
const targetReportID = backToReport ?? activeReportID;
// When backToReport exists we are creating the expense from chat, not the expense report, so no pending transaction registration needed.
Expand Down
94 changes: 93 additions & 1 deletion tests/actions/IOU/PerDiemTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type RequestMoneyParticipantParams from '@libs/actions/IOU/types/RequestM
import CONST from '@src/CONST';
import DateUtils from '@src/libs/DateUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetailsList, RecentlyUsedTags, Report} from '@src/types/onyx';
import type {PersonalDetailsList, RecentlyUsedTags, Report, ReportActions} from '@src/types/onyx';
import type {CurrentUserPersonalDetails} from '@src/types/onyx/PersonalDetails';
import type Transaction from '@src/types/onyx/Transaction';
import type {TransactionCustomUnit} from '@src/types/onyx/Transaction';
Expand Down Expand Up @@ -326,6 +326,7 @@ describe('PerDiem', () => {
quickAction: undefined,
betas: [CONST.BETAS.ALL],
personalDetails: {[mockParticipantParams.payeeAccountID]: {accountID: mockParticipantParams.payeeAccountID, login: 'payee@example.com'}},
chatReportActions: undefined,
});

expect(result.onyxData).toBeDefined();
Expand Down Expand Up @@ -416,6 +417,7 @@ describe('PerDiem', () => {
quickAction: undefined,
betas: [CONST.BETAS.ALL],
personalDetails: {[mockParticipant.accountID]: {accountID: mockParticipant.accountID, login: 'existing@example.com'}},
chatReportActions: undefined,
});

// Then: Verify the result structure and key values
Expand Down Expand Up @@ -550,6 +552,7 @@ describe('PerDiem', () => {
quickAction: undefined,
betas: [CONST.BETAS.ALL],
personalDetails: {[mockParticipant.accountID]: {accountID: mockParticipant.accountID, login: 'existing@example.com'}},
chatReportActions: undefined,
});

// Then: Verify the result uses existing chat report
Expand Down Expand Up @@ -638,6 +641,7 @@ describe('PerDiem', () => {
quickAction: undefined,
betas: [CONST.BETAS.ALL],
personalDetails: {[mockParticipant.accountID]: {accountID: mockParticipant.accountID, login: 'existing@example.com'}},
chatReportActions: undefined,
});

// Then: Verify policy expense chat handling
Expand All @@ -648,6 +652,90 @@ describe('PerDiem', () => {
expect(result.billable).toBe(true);
expect(result.reimbursable).toBe(true);
});

it('should reuse the existing report preview action supplied via chatReportActions', () => {
const existingChatReportID = 'chat_with_actions';
const existingIOUReportID = 'iou_existing';
const existingPreviewActionID = 'preview_existing';

const existingChatReport: Report = {
reportID: existingChatReportID,
chatType: CONST.REPORT.CHAT_TYPE.GROUP,
iouReportID: existingIOUReportID,
type: CONST.REPORT.TYPE.CHAT,
};
const existingIOUReport: Report = {
reportID: existingIOUReportID,
chatReportID: existingChatReportID,
type: CONST.REPORT.TYPE.IOU,
ownerAccountID: 456,
managerID: 123,
currency: 'USD',
total: 0,
};

const chatReportActions: ReportActions = {
[existingPreviewActionID]: {
reportActionID: existingPreviewActionID,
reportID: existingChatReportID,
actionName: CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW,
originalMessage: {linkedReportID: existingIOUReportID},
message: [{type: CONST.REPORT.MESSAGE.TYPE.COMMENT, html: '', text: '', isEdited: false}],
created: DateUtils.getDBTime(),
accountID: 123,
actorAccountID: 456,
childReportID: existingIOUReportID,
childMoneyRequestCount: 1,
},
};

const mockCustomUnit: TransactionCustomUnit = {
customUnitID: 'per_diem_reuse',
customUnitRateID: 'rate_reuse',
name: CONST.CUSTOM_UNITS.NAME_PER_DIEM_INTERNATIONAL,
attributes: {dates: {start: '2024-03-01', end: '2024-03-01'}},
subRates: [{id: 'meal_1', name: 'Meal', rate: 30, quantity: 1}],
quantity: 1,
};

const mockTransactionParams: PerDiemExpenseTransactionParams = {
comment: 'reuse preview',
currency: 'USD',
created: '2024-03-01',
category: 'Meals',
tag: '',
customUnit: mockCustomUnit,
billable: false,
attendees: [],
reimbursable: true,
};

const mockParticipantParams: RequestMoneyParticipantParams = {
payeeAccountID: 456,
payeeEmail: 'payee@example.com',
participant: {accountID: 123, login: 'existing@example.com'},
};

const result = getPerDiemExpenseInformation({
parentChatReport: existingChatReport,
existingIOUReport,
transactionParams: mockTransactionParams,
participantParams: mockParticipantParams,
recentlyUsedParams: {},
isASAPSubmitBetaEnabled: false,
currentUserAccountIDParam: 456,
currentUserEmailParam: 'payee@example.com',
hasViolations: false,
policyRecentlyUsedCurrencies: [],
quickAction: undefined,
betas: [CONST.BETAS.ALL],
// eslint-disable-next-line @typescript-eslint/naming-convention
personalDetails: {123: {accountID: 123, login: 'existing@example.com'}},
chatReportActions,
});

expect(result.reportPreviewAction.reportActionID).toBe(existingPreviewActionID);
});
});

describe('submitPerDiemExpense', () => {
Expand Down Expand Up @@ -713,6 +801,7 @@ describe('PerDiem', () => {
quickAction: undefined,
betas: [CONST.BETAS.ALL],
personalDetails: {[RORY_ACCOUNT_ID]: {accountID: RORY_ACCOUNT_ID, login: RORY_EMAIL}},
chatReportActions: undefined,
});

await waitForBatchedUpdates();
Expand Down Expand Up @@ -781,6 +870,7 @@ describe('PerDiem', () => {
betas: [CONST.BETAS.ALL],
personalDetails: {[RORY_ACCOUNT_ID]: {accountID: RORY_ACCOUNT_ID, login: RORY_EMAIL}},
optimisticTransactionID,
chatReportActions: undefined,
});

await waitForBatchedUpdates();
Expand Down Expand Up @@ -854,6 +944,7 @@ describe('PerDiem', () => {
quickAction: undefined,
betas: [CONST.BETAS.ALL],
personalDetails: personalDetailsList,
chatReportActions: undefined,
});

// Then the result should be valid (personalDetails is correctly passed through the chain)
Expand Down Expand Up @@ -918,6 +1009,7 @@ describe('PerDiem', () => {
quickAction: undefined,
betas: [CONST.BETAS.ALL],
personalDetails: personalDetailsList,
chatReportActions: undefined,
});

await waitForBatchedUpdates();
Expand Down
Loading