Skip to content
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
11 changes: 9 additions & 2 deletions src/components/KYCWall/BaseKYCWall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useParentReportAction from '@hooks/useParentReportAction';
import usePermissions from '@hooks/usePermissions';
import useReportTransactions from '@hooks/useReportTransactions';
import {openPersonalBankAccountSetupView} from '@libs/actions/BankAccounts';
import {completePaymentOnboarding, savePreferredPaymentMethod} from '@libs/actions/IOU';
import {navigateToBankAccountRoute} from '@libs/actions/ReimbursementAccount';
Expand Down Expand Up @@ -70,6 +72,9 @@ function KYCWall({
const reportPreviewAction = useParentReportAction(iouReport);
const personalDetails = usePersonalDetails();
const employeeEmail = personalDetails?.[iouReport?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID]?.login ?? '';
const reportTransactions = useReportTransactions(iouReport?.reportID);
const {isBetaEnabled} = usePermissions();
const isCustomReportNamesBetaEnabled = isBetaEnabled(CONST.BETAS.CUSTOM_REPORT_NAMES);
const anchorRef = useRef<HTMLDivElement | View>(null);
const transferBalanceButtonRef = useRef<HTMLDivElement | View | null>(null);

Expand Down Expand Up @@ -135,7 +140,7 @@ function KYCWall({
if (iouReport && isIOUReport(iouReport)) {
const adminPolicy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`];
if (adminPolicy) {
const inviteResult = moveIOUReportToPolicyAndInviteSubmitter(iouReport?.reportID, adminPolicy, formatPhoneNumber);
const inviteResult = moveIOUReportToPolicyAndInviteSubmitter(iouReport?.reportID, adminPolicy, formatPhoneNumber, reportTransactions, isCustomReportNamesBetaEnabled);
if (inviteResult?.policyExpenseChatReportID) {
setNavigationActionToMicrotaskQueue(() => {
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(inviteResult.policyExpenseChatReportID));
Expand All @@ -145,7 +150,7 @@ function KYCWall({
Navigation.navigate(ROUTES.BANK_ACCOUNT_WITH_STEP_TO_OPEN.getRoute(adminPolicy.id));
});
} else {
const moveResult = moveIOUReportToPolicy(iouReport?.reportID, adminPolicy, true);
const moveResult = moveIOUReportToPolicy(iouReport?.reportID, adminPolicy, true, reportTransactions, isCustomReportNamesBetaEnabled);
savePreferredPaymentMethod(iouReport.policyID, adminPolicy.id, CONST.LAST_PAYMENT_METHOD.IOU, lastPaymentMethod?.[adminPolicy.id]);

if (moveResult?.policyExpenseChatReportID && !moveResult.useTemporaryOptimisticExpenseChatReportID) {
Expand Down Expand Up @@ -210,6 +215,8 @@ function KYCWall({
reportPreviewAction,
currentUserEmail,
employeeEmail,
reportTransactions,
isCustomReportNamesBetaEnabled,
],
);

Expand Down
51 changes: 37 additions & 14 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@
isSelfDM,
isUnread,
isValidReportIDFromPath,
populateOptimisticReportFormula,
prepareOnboardingOnyxData,
} from '@libs/ReportUtils';
import {getCurrentSearchQueryJSON} from '@libs/SearchQueryUtils';
Expand Down Expand Up @@ -285,7 +284,7 @@
let deprecatedCurrentUserLogin: string | undefined;

Onyx.connect({
key: ONYXKEYS.SESSION,

Check warning on line 287 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (value) => {
// When signed out, val is undefined
if (!value?.accountID) {
Expand All @@ -299,7 +298,7 @@
});

Onyx.connect({
key: ONYXKEYS.CONCIERGE_REPORT_ID,

Check warning on line 301 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (value) => (conciergeReportID = value),
});

Expand All @@ -307,7 +306,7 @@
const allReportActions: OnyxCollection<ReportActions> = {};

Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,

Check warning on line 309 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (actions, key) => {
if (!key || !actions) {
return;
Expand All @@ -319,7 +318,7 @@

let allReports: OnyxCollection<Report>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,

Check warning on line 321 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
waitForCollectionCallback: true,
callback: (value) => {
allReports = value;
Expand All @@ -328,7 +327,7 @@

let allPersonalDetails: OnyxEntry<PersonalDetailsList> = {};
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,

Check warning on line 330 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (value) => {
allPersonalDetails = value ?? {};
},
Expand All @@ -343,7 +342,7 @@

let onboarding: OnyxEntry<Onboarding>;
Onyx.connect({
key: ONYXKEYS.NVP_ONBOARDING,

Check warning on line 345 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (val) => {
if (Array.isArray(val)) {
return;
Expand All @@ -354,13 +353,13 @@

let introSelected: OnyxEntry<IntroSelected> = {};
Onyx.connect({
key: ONYXKEYS.NVP_INTRO_SELECTED,

Check warning on line 356 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (val) => (introSelected = val),
});

let allReportDraftComments: Record<string, string | undefined> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT,

Check warning on line 362 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
waitForCollectionCallback: true,
callback: (value) => (allReportDraftComments = value),
});
Expand Down Expand Up @@ -5345,14 +5344,13 @@

/**
* Moves an IOU report to a policy by converting it to an expense report
* @param reportID - The ID of the IOU report to move
* @param policy - The policy to move the report to
* @param isFromSettlementButton - Whether the action is from report preview
*/
function moveIOUReportToPolicy(
reportID: string,
policy: Policy,
isFromSettlementButton?: boolean,
reportTransactions: Transaction[] = [],
Comment thread
abbasifaizan70 marked this conversation as resolved.
Resolved
isCustomReportNamesBetaEnabled?: boolean,
): {policyExpenseChatReportID?: string; useTemporaryOptimisticExpenseChatReportID: boolean} | undefined {
const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];

Expand All @@ -5379,6 +5377,8 @@
policy,
policyID,
optimisticExpenseChatReportID,
reportTransactions,
isCustomReportNamesBetaEnabled,
);

const parameters: MoveIOUReportToExistingPolicyParams = {
Expand All @@ -5394,13 +5394,14 @@
}

/**
* Moves an IOU report to a policy by converting it to an expense report
* @param reportID - The ID of the IOU report to move
* Moves an IOU report to a policy by converting it to an expense report and invites the submitter
*/
function moveIOUReportToPolicyAndInviteSubmitter(
reportID: string,
policy: Policy,
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
reportTransactions: Transaction[] = [],
isCustomReportNamesBetaEnabled?: boolean,
): {policyExpenseChatReportID?: string} | undefined {
const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];

Expand Down Expand Up @@ -5532,7 +5533,7 @@
failureData: convertedFailureData,
movedExpenseReportAction,
movedReportAction,
} = convertIOUReportToExpenseReport(iouReport, policy, policyID, optimisticPolicyExpenseChatReportID);
} = convertIOUReportToExpenseReport(iouReport, policy, policyID, optimisticPolicyExpenseChatReportID, reportTransactions, isCustomReportNamesBetaEnabled);

optimisticData.push(...convertedOptimisticData);
successData.push(...convertedSuccessData);
Expand All @@ -5551,7 +5552,14 @@
return {policyExpenseChatReportID: optimisticPolicyExpenseChatReportID};
}

function convertIOUReportToExpenseReport(iouReport: Report, policy: Policy, policyID: string, optimisticPolicyExpenseChatReportID: string) {
function convertIOUReportToExpenseReport(
iouReport: Report,
policy: Policy,
policyID: string,
optimisticPolicyExpenseChatReportID: string,
reportTransactions: Transaction[] = [],
isCustomReportNamesBetaEnabled?: boolean,
) {
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.TRANSACTION | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>> = [];
const successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>> = [];
const failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.TRANSACTION | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>> = [];
Expand All @@ -5573,9 +5581,27 @@
expenseReport.managerID = nextApproverAccountID;
}

const titleReportField = getTitleReportField(getReportFieldsByPolicyID(policyID) ?? {});
if (!!titleReportField && isPaidGroupPolicy(policy)) {
expenseReport.reportName = populateOptimisticReportFormula(titleReportField.defaultValue, expenseReport, policy);
// Only compute optimistic report name if the user is on the CUSTOM_REPORT_NAMES beta
if (isCustomReportNamesBetaEnabled) {
const titleReportField = getTitleReportField(getReportFieldsByPolicyID(policyID) ?? {});
if (!!titleReportField && isPaidGroupPolicy(policy)) {
// Convert transactions array to Record<string, Transaction> for FormulaContext
const transactionsRecord: Record<string, Transaction> = {};
for (const transaction of reportTransactions) {
if (transaction?.transactionID) {
transactionsRecord[transaction.transactionID] = transaction;
}
}

// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const Formula = require('@libs/Formula') as {compute: (formula?: string, context?: {report: Report; policy: Policy; allTransactions?: Record<string, Transaction>}) => string};
const computedName = Formula.compute(titleReportField.defaultValue, {
report: expenseReport,
policy,
allTransactions: transactionsRecord,
});
expenseReport.reportName = computedName ?? expenseReport.reportName;
}
}

const reportID = iouReport.reportID;
Expand All @@ -5591,9 +5617,6 @@
value: iouReport,
});

// The expense report transactions need to have the amount reversed to negative values
const reportTransactions = getReportTransactions(reportID);

// For performance reasons, we are going to compose a merge collection data for transactions
const transactionsOptimisticData: Record<string, Transaction> = {};
const transactionFailureData: Record<string, Transaction> = {};
Expand Down
14 changes: 10 additions & 4 deletions src/pages/ReportChangeWorkspacePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import usePermissions from '@hooks/usePermissions';
import useReportIsArchived from '@hooks/useReportIsArchived';
import useReportTransactions from '@hooks/useReportTransactions';
import useThemeStyles from '@hooks/useThemeStyles';
import useWorkspaceList from '@hooks/useWorkspaceList';
import {changeReportPolicy, changeReportPolicyAndInviteSubmitter, moveIOUReportToPolicy, moveIOUReportToPolicyAndInviteSubmitter} from '@libs/actions/Report';
Expand Down Expand Up @@ -51,21 +52,24 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
const styles = useThemeStyles();
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState('');
const {translate, formatPhoneNumber, localeCompare} = useLocalize();
const reportTransactions = useReportTransactions(reportID);

const [policies, fetchStatus] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false});
const [reportNextStep] = useOnyx(`${ONYXKEYS.COLLECTION.NEXT_STEP}${reportID}`, {canBeMissing: true});
const [isChangePolicyTrainingModalDismissed = false] = useOnyx(ONYXKEYS.NVP_DISMISSED_PRODUCT_TRAINING, {canBeMissing: true, selector: changePolicyTrainingModalDismissedSelector});
const [isLoadingApp] = useOnyx(ONYXKEYS.IS_LOADING_APP, {canBeMissing: false});
const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: true});
const isReportLastVisibleArchived = useReportIsArchived(report?.parentReportID);
const reportOwnerAccountID = report?.ownerAccountID;
const submitterEmailSelector = useCallback(
(personalDetailsList: OnyxEntry<PersonalDetailsList>) => personalDetailsList?.[report?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID]?.login,
[report?.ownerAccountID],
(personalDetailsList: OnyxEntry<PersonalDetailsList>) => personalDetailsList?.[reportOwnerAccountID ?? CONST.DEFAULT_NUMBER_ID]?.login,
[reportOwnerAccountID],
);
const [submitterEmail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {canBeMissing: false, selector: submitterEmailSelector}, [submitterEmailSelector]);
const shouldShowLoadingIndicator = isLoadingApp && !isOffline;
const {isBetaEnabled} = usePermissions();
const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT);
const isCustomReportNamesBetaEnabled = isBetaEnabled(CONST.BETAS.CUSTOM_REPORT_NAMES);
const session = useSession();
const hasViolations = hasViolationsReportUtils(report?.reportID, transactionViolations, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? '');

Expand All @@ -82,9 +86,9 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
const {backTo} = route.params;
Navigation.goBack(backTo);
if (isIOUReport(reportID)) {
const invite = moveIOUReportToPolicyAndInviteSubmitter(reportID, policy, formatPhoneNumber);
const invite = moveIOUReportToPolicyAndInviteSubmitter(reportID, policy, formatPhoneNumber, reportTransactions, isCustomReportNamesBetaEnabled);
if (!invite?.policyExpenseChatReportID) {
moveIOUReportToPolicy(reportID, policy);
moveIOUReportToPolicy(reportID, policy, false, reportTransactions, isCustomReportNamesBetaEnabled);
}
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line @typescript-eslint/no-deprecated
Expand Down Expand Up @@ -122,11 +126,13 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
reportID,
report,
formatPhoneNumber,
reportTransactions,
isReportLastVisibleArchived,
session?.accountID,
session?.email,
hasViolations,
isASAPSubmitBetaEnabled,
isCustomReportNamesBetaEnabled,
reportNextStep,
isChangePolicyTrainingModalDismissed,
],
Expand Down
11 changes: 9 additions & 2 deletions src/pages/Search/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function SearchPage({route}: SearchPageProps) {
const {showConfirmModal} = useConfirmModal();
const {isBetaEnabled} = usePermissions();
const isDEWBetaEnabled = isBetaEnabled(CONST.BETAS.NEW_DOT_DEW);
const isCustomReportNamesBetaEnabled = isBetaEnabled(CONST.BETAS.CUSTOM_REPORT_NAMES);
const [isHoldEducationalModalVisible, setIsHoldEducationalModalVisible] = useState(false);
const [rejectModalAction, setRejectModalAction] = useState<ValueOf<
typeof CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS.HOLD | typeof CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS.REJECT
Expand Down Expand Up @@ -523,9 +524,13 @@ function SearchPage({route}: SearchPageProps) {
);
return;
}
const invite = moveIOUReportToPolicyAndInviteSubmitter(itemReportID, adminPolicy, formatPhoneNumber);
// Get transactions for this report
const reportTransactions = Object.values(allTransactions ?? {}).filter(
Comment thread
abbasifaizan70 marked this conversation as resolved.
Resolved
(transaction): transaction is NonNullable<typeof transaction> => !!transaction && transaction.reportID === itemReportID,
);
const invite = moveIOUReportToPolicyAndInviteSubmitter(itemReportID, adminPolicy, formatPhoneNumber, reportTransactions, isCustomReportNamesBetaEnabled);
if (!invite?.policyExpenseChatReportID) {
moveIOUReportToPolicy(itemReportID, adminPolicy);
moveIOUReportToPolicy(itemReportID, adminPolicy, false, reportTransactions, isCustomReportNamesBetaEnabled);
}
}
}
Expand Down Expand Up @@ -583,6 +588,8 @@ function SearchPage({route}: SearchPageProps) {
isDelegateAccessRestricted,
showDelegateNoAccessModal,
personalPolicyID,
allTransactions,
isCustomReportNamesBetaEnabled,
],
);

Expand Down
Loading