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

Fix - Submit expense RHP doesn't include workspaces from Track flow #52061

Merged
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
28 changes: 28 additions & 0 deletions src/libs/API/parameters/AddTrackedExpenseToPolicyParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type {Receipt} from '@src/types/onyx/Transaction';

type AddTrackedExpenseToPolicyParams = {
amount: number;
currency: string;
created: string;
comment?: string;
merchant?: string;
category: string | undefined;
tag: string | undefined;
taxCode: string;
taxAmount: number;
reimbursable: boolean;
billable: boolean | undefined;
receipt: Receipt | undefined;
waypoints?: string;
customUnitRateID?: string;
policyID: string;
transactionID: string;
actionableWhisperReportActionID: string;
moneyRequestReportID: string;
reportPreviewReportActionID: string;
modifiedExpenseReportActionID: string;
moneyRequestCreatedReportActionID: string | undefined;
moneyRequestPreviewReportActionID: string;
};

export default AddTrackedExpenseToPolicyParams;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export type {default as RenamePolicyTaxParams} from './RenamePolicyTaxParams';
export type {default as UpdatePolicyTaxCodeParams} from './UpdatePolicyTaxCodeParams';
export type {default as CompleteGuidedSetupParams} from './CompleteGuidedSetupParams';
export type {default as DismissTrackExpenseActionableWhisperParams} from './DismissTrackExpenseActionableWhisperParams';
export type {default as AddTrackedExpenseToPolicyParams} from './AddTrackedExpenseToPolicyParams';
export type {default as ConvertTrackedExpenseToRequestParams} from './ConvertTrackedExpenseToRequestParams';
export type {default as ShareTrackedExpenseParams} from './ShareTrackedExpenseParams';
export type {default as CategorizeTrackedExpenseParams} from './CategorizeTrackedExpenseParams';
Expand Down
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ const WRITE_COMMANDS = {
SET_POLICY_DISTANCE_RATES_ENABLED: 'SetPolicyDistanceRatesEnabled',
DELETE_POLICY_DISTANCE_RATES: 'DeletePolicyDistanceRates',
DISMISS_TRACK_EXPENSE_ACTIONABLE_WHISPER: 'DismissActionableWhisper',
ADD_TRACKED_EXPENSE_TO_POLICY: 'AddTrackedExpenseToPolicy',
CONVERT_TRACKED_EXPENSE_TO_REQUEST: 'ConvertTrackedExpenseToRequest',
CATEGORIZE_TRACKED_EXPENSE: 'CategorizeTrackedExpense',
SHARE_TRACKED_EXPENSE: 'ShareTrackedExpense',
Expand Down Expand Up @@ -756,6 +757,7 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.DISMISS_TRACK_EXPENSE_ACTIONABLE_WHISPER]: Parameters.DismissTrackExpenseActionableWhisperParams;
[WRITE_COMMANDS.UPDATE_BILLING_CARD_CURRENCY]: Parameters.UpdateBillingCurrencyParams;
[WRITE_COMMANDS.CONVERT_TRACKED_EXPENSE_TO_REQUEST]: Parameters.ConvertTrackedExpenseToRequestParams;
[WRITE_COMMANDS.ADD_TRACKED_EXPENSE_TO_POLICY]: Parameters.AddTrackedExpenseToPolicyParams;
[WRITE_COMMANDS.CATEGORIZE_TRACKED_EXPENSE]: Parameters.CategorizeTrackedExpenseParams;
[WRITE_COMMANDS.SHARE_TRACKED_EXPENSE]: Parameters.ShareTrackedExpenseParams;
[WRITE_COMMANDS.LEAVE_POLICY]: Parameters.LeavePolicyParams;
Expand Down
102 changes: 90 additions & 12 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ import {
isOptimisticPersonalDetail,
isPayAtEndExpenseReport as isPayAtEndExpenseReportReportUtils,
isPayer as isPayerReportUtils,
isPolicyExpenseChat as isPolicyExpenseChatReportUtils,
isPolicyExpenseChat as isPolicyExpenseChatReportUtil,
isReportApproved,
isSelfDM,
isSettled,
Expand Down Expand Up @@ -319,6 +319,8 @@ type RequestMoneyTransactionParams = {
actionableWhisperReportActionID?: string;
linkedTrackedExpenseReportAction?: OnyxTypes.ReportAction;
linkedTrackedExpenseReportID?: string;
waypoints?: WaypointCollection;
customUnitRateID?: string;
};

type PerDiemExpenseTransactionParams = {
Expand Down Expand Up @@ -4125,6 +4127,38 @@ const getConvertTrackedExpenseInformation = (
return {optimisticData, successData, failureData, modifiedExpenseReportActionID: modifiedExpenseReportAction.reportActionID};
};

type ConvertTrackedWorkspaceParams = {
category: string | undefined;
tag: string | undefined;
taxCode: string;
taxAmount: number;
billable: boolean | undefined;
policyID: string;
receipt: Receipt | undefined;
waypoints?: string;
customUnitRateID?: string;
};

type AddTrackedExpenseToPolicyParam = {
amount: number;
currency: string;
comment: string;
created: string;
merchant: string;
transactionID: string;
reimbursable: boolean;
actionableWhisperReportActionID: string;
moneyRequestReportID: string;
reportPreviewReportActionID: string;
modifiedExpenseReportActionID: string;
moneyRequestCreatedReportActionID: string | undefined;
moneyRequestPreviewReportActionID: string;
} & ConvertTrackedWorkspaceParams;

function addTrackedExpenseToPolicy(parameters: AddTrackedExpenseToPolicyParam, onyxData: OnyxData) {
API.write(WRITE_COMMANDS.ADD_TRACKED_EXPENSE_TO_POLICY, parameters, onyxData);
}

function convertTrackedExpenseToRequest(
payerAccountID: number,
payerEmail: string,
Expand All @@ -4146,6 +4180,7 @@ function convertTrackedExpenseToRequest(
merchant: string,
created: string,
attendees?: Attendee[],
workspaceParams?: ConvertTrackedWorkspaceParams,
) {
const {optimisticData, successData, failureData} = onyxData;

Expand All @@ -4168,6 +4203,28 @@ function convertTrackedExpenseToRequest(
successData?.push(...moveTransactionSuccessData);
failureData?.push(...moveTransactionFailureData);

if (workspaceParams) {
const params = {
amount,
currency,
comment,
created,
merchant,
reimbursable: true,
transactionID,
actionableWhisperReportActionID,
moneyRequestReportID,
moneyRequestCreatedReportActionID,
moneyRequestPreviewReportActionID,
modifiedExpenseReportActionID,
reportPreviewReportActionID,
...workspaceParams,
};

addTrackedExpenseToPolicy(params, {optimisticData, successData, failureData});
return;
}

const parameters = {
attendees,
amount,
Expand Down Expand Up @@ -4313,13 +4370,22 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) {
actionableWhisperReportActionID,
linkedTrackedExpenseReportAction,
linkedTrackedExpenseReportID,
waypoints,
customUnitRateID,
} = transactionParams;

const sanitizedWaypoints = waypoints ? JSON.stringify(sanitizeRecentWaypoints(waypoints)) : undefined;

// If the report is iou or expense report, we should get the linked chat report to be passed to the getMoneyRequestInformation function
const isMoneyRequestReport = isMoneyRequestReportReportUtils(report);
const currentChatReport = isMoneyRequestReport ? getReportOrDraftReport(report?.chatReportID) : report;
const moneyRequestReportID = isMoneyRequestReport ? report?.reportID : '';
const isMovingTransactionFromTrackExpense = isMovingTransactionFromTrackExpenseIOUUtils(action);
const existingTransactionID =
isMovingTransactionFromTrackExpense && linkedTrackedExpenseReportAction && isMoneyRequestAction(linkedTrackedExpenseReportAction)
? getOriginalMessage(linkedTrackedExpenseReportAction)?.IOUTransactionID
: undefined;
const existingTransaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${existingTransactionID}`];

const {
payerAccountID,
Expand All @@ -4340,10 +4406,8 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) {
policyParams,
transactionParams,
moneyRequestReportID,
existingTransactionID:
isMovingTransactionFromTrackExpense && linkedTrackedExpenseReportAction && isMoneyRequestAction(linkedTrackedExpenseReportAction)
? getOriginalMessage(linkedTrackedExpenseReportAction)?.IOUTransactionID
: undefined,
existingTransactionID,
existingTransaction: isDistanceRequestTransactionUtils(existingTransaction) ? existingTransaction : undefined,
});
const activeReportID = isMoneyRequestReport ? report?.reportID : chatReport.reportID;

Expand All @@ -4352,7 +4416,20 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) {
if (!linkedTrackedExpenseReportAction || !actionableWhisperReportActionID || !linkedTrackedExpenseReportID) {
return;
}

const workspaceParams =
isPolicyExpenseChatReportUtil(chatReport) && chatReport.policyID
? {
receipt: receipt instanceof Blob ? receipt : undefined,
category,
tag,
taxCode,
taxAmount,
billable,
policyID: chatReport.policyID,
waypoints: sanitizedWaypoints,
customUnitRateID,
}
: undefined;
convertTrackedExpenseToRequest(
payerAccountID,
payerEmail,
Expand All @@ -4374,6 +4451,7 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) {
merchant,
created,
attendees,
workspaceParams,
);
break;
}
Expand Down Expand Up @@ -5038,7 +5116,7 @@ function createSplitsAndOnyxData(
const hasMultipleParticipants = participants.length > 1;
participants.forEach((participant) => {
// In a case when a participant is a workspace, even when a current user is not an owner of the workspace
const isPolicyExpenseChat = isPolicyExpenseChatReportUtils(participant);
const isPolicyExpenseChat = isPolicyExpenseChatReportUtil(participant);
const splitAmount = splitShares?.[participant.accountID ?? CONST.DEFAULT_NUMBER_ID]?.amount ?? calculateIOUAmount(participants.length, amount, currency, false);
const splitTaxAmount = calculateIOUAmount(participants.length, taxAmount, currency, false);

Expand Down Expand Up @@ -5664,7 +5742,7 @@ function startSplitBill({
});

participants.forEach((participant) => {
const isPolicyExpenseChat = isPolicyExpenseChatReportUtils(participant);
const isPolicyExpenseChat = isPolicyExpenseChatReportUtil(participant);
if (!isPolicyExpenseChat) {
return;
}
Expand Down Expand Up @@ -7273,7 +7351,7 @@ function getReportFromHoldRequestsOnyxData(
const newParentReportActionID = rand64();

const coefficient = isExpenseReport(iouReport) ? -1 : 1;
const isPolicyExpenseChat = isPolicyExpenseChatReportUtils(chatReport);
const isPolicyExpenseChat = isPolicyExpenseChatReportUtil(chatReport);
const holdAmount = ((iouReport?.total ?? 0) - (iouReport?.unheldTotal ?? 0)) * coefficient;
const holdNonReimbursableAmount = ((iouReport?.nonReimbursableTotal ?? 0) - (iouReport?.unheldNonReimbursableTotal ?? 0)) * coefficient;
const optimisticExpenseReport = isPolicyExpenseChat
Expand Down Expand Up @@ -7852,7 +7930,7 @@ function canIOUBePaid(
invoiceReceiverPolicy?: SearchPolicy,
shouldCheckApprovedState = true,
) {
const isPolicyExpenseChat = isPolicyExpenseChatReportUtils(chatReport);
const isPolicyExpenseChat = isPolicyExpenseChatReportUtil(chatReport);
const reportNameValuePairs = chatReportRNVP ?? getReportNameValuePairs(chatReport?.reportID);
const isChatReportArchived = isArchivedReport(reportNameValuePairs);
const iouSettled = isSettled(iouReport);
Expand Down Expand Up @@ -8769,8 +8847,8 @@ function setMoneyRequestParticipantsFromReport(transactionID: string, report: On
const shouldAddAsReport = !isEmptyObject(chatReport) && isSelfDM(chatReport);
let participants: Participant[] = [];

if (isPolicyExpenseChatReportUtils(chatReport) || shouldAddAsReport) {
participants = [{accountID: 0, reportID: chatReport?.reportID, isPolicyExpenseChat: isPolicyExpenseChatReportUtils(chatReport), selected: true}];
if (isPolicyExpenseChatReportUtil(chatReport) || shouldAddAsReport) {
participants = [{accountID: 0, reportID: chatReport?.reportID, isPolicyExpenseChat: isPolicyExpenseChatReportUtil(chatReport), selected: true}];
} else if (isInvoiceRoom(chatReport)) {
participants = [
{reportID: chatReport?.reportID, selected: true},
Expand Down
5 changes: 2 additions & 3 deletions src/pages/iou/request/MoneyRequestParticipantsSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import {canUseTouchScreen} from '@libs/DeviceCapabilities';
import {isMovingTransactionFromTrackExpense} from '@libs/IOUUtils';
import Navigation from '@libs/Navigation/Navigation';
import type {Section} from '@libs/OptionsListUtils';
import {
filterAndOrderOptions,
formatSectionsFromSearchTerm,
Expand All @@ -33,7 +34,6 @@ import {
isCurrentUser,
orderOptions,
} from '@libs/OptionsListUtils';
import type {Section} from '@libs/OptionsListUtils';
import {isPaidGroupPolicy as isPaidGroupPolicyUtil} from '@libs/PolicyUtils';
import type {OptionData} from '@libs/ReportUtils';
import {isInvoiceRoom} from '@libs/ReportUtils';
Expand Down Expand Up @@ -115,8 +115,7 @@ function MoneyRequestParticipantsSelector({participants = CONST.EMPTY_ARRAY, onF

// If we are using this component in the "Submit expense" or the combined submit/track flow then we pass the includeOwnedWorkspaceChats argument so that the current user
// sees the option to submit an expense from their admin on their own Workspace Chat.
includeOwnedWorkspaceChats:
(iouType === CONST.IOU.TYPE.SUBMIT || iouType === CONST.IOU.TYPE.CREATE || iouType === CONST.IOU.TYPE.SPLIT) && action !== CONST.IOU.ACTION.SUBMIT,
includeOwnedWorkspaceChats: iouType === CONST.IOU.TYPE.SUBMIT || iouType === CONST.IOU.TYPE.CREATE || iouType === CONST.IOU.TYPE.SPLIT,

includeP2P: !isCategorizeOrShareAction,
includeInvoiceRooms: iouType === CONST.IOU.TYPE.INVOICE,
Expand Down
16 changes: 15 additions & 1 deletion src/pages/iou/request/step/IOURequestStepConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,24 @@ function IOURequestStepConfirmation({
actionableWhisperReportActionID: transaction.actionableWhisperReportActionID,
linkedTrackedExpenseReportAction: transaction.linkedTrackedExpenseReportAction,
linkedTrackedExpenseReportID: transaction.linkedTrackedExpenseReportID,
waypoints: Object.keys(transaction.comment?.waypoints ?? {}).length ? getValidWaypoints(transaction.comment?.waypoints, true) : undefined,
customUnitRateID,
},
});
},
[report, transaction, transactionTaxCode, transactionTaxAmount, currentUserPersonalDetails.login, currentUserPersonalDetails.accountID, policy, policyTags, policyCategories, action],
[
report,
transaction,
transactionTaxCode,
transactionTaxAmount,
currentUserPersonalDetails.login,
currentUserPersonalDetails.accountID,
policy,
policyTags,
policyCategories,
action,
customUnitRateID,
],
);

const submitPerDiemExpense = useCallback(
Expand Down