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

Edit merchant field of money requests #25702

Merged
merged 6 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,9 @@ const CONST = {
DARK: 'dark',
SYSTEM: 'system',
},
TRANSACTION: {
DEFAULT_MERCHANT: 'Request',
},
JSON_CODE: {
SUCCESS: 200,
BAD_REQUEST: 400,
Expand Down
2 changes: 1 addition & 1 deletion src/components/MoneyRequestConfirmationList.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ function MoneyRequestConfirmationList(props) {
/>
<MenuItemWithTopDescription
shouldShowRightIcon={!props.isReadOnly && isTypeRequest}
title={props.iouMerchant}
title={props.iouMerchant || CONST.TRANSACTION.DEFAULT_MERCHANT}
mountiny marked this conversation as resolved.
Show resolved Hide resolved
description={translate('common.merchant')}
style={[styles.moneyRequestMenuItem, styles.mb2]}
titleStyle={styles.flex1}
Expand Down
6 changes: 4 additions & 2 deletions src/components/ReportActionItem/MoneyRequestView.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ function MoneyRequestView({report, parentReport, shouldShowHorizontalRule, polic
<MenuItemWithTopDescription
description={translate('common.merchant')}
title={transactionMerchant}
shouldShowRightIcon={false}
disabled
disabled={isSettled || !canEdit}
shouldShowRightIcon={canEdit}
titleStyle={styles.flex1}
onPress={() => Navigation.navigate(ROUTES.getEditRequestRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.MERCHANT))}
/>
</OfflineWithFeedback>
{shouldShowHorizontalRule && <View style={styles.reportHorizontalRule} />}
Expand Down
14 changes: 9 additions & 5 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1411,18 +1411,18 @@ function getModifiedExpenseMessage(reportAction) {
return getProperSchemaForModifiedExpenseMessage(reportActionOriginalMessage.newComment, reportActionOriginalMessage.oldComment, 'description', true);
}

const hasModifiedMerchant = _.has(reportActionOriginalMessage, 'oldMerchant') && _.has(reportActionOriginalMessage, 'merchant');
if (hasModifiedMerchant) {
return getProperSchemaForModifiedExpenseMessage(reportActionOriginalMessage.merchant, reportActionOriginalMessage.oldMerchant, 'merchant', true);
}

const hasModifiedCreated = _.has(reportActionOriginalMessage, 'oldCreated') && _.has(reportActionOriginalMessage, 'created');
if (hasModifiedCreated) {
// Take only the YYYY-MM-DD value as the original date includes timestamp
let formattedOldCreated = parseISO(reportActionOriginalMessage.oldCreated);
formattedOldCreated = format(formattedOldCreated, CONST.DATE.FNS_FORMAT_STRING);
return getProperSchemaForModifiedExpenseMessage(reportActionOriginalMessage.created, formattedOldCreated, 'date', false);
}

const hasModifiedMerchant = _.has(reportActionOriginalMessage, 'oldMerchant') && _.has(reportActionOriginalMessage, 'merchant');
if (hasModifiedMerchant) {
return getProperSchemaForModifiedExpenseMessage(reportActionOriginalMessage.merchant, reportActionOriginalMessage.oldMerchant, 'merchant', true);
}
}

/**
Expand All @@ -1449,6 +1449,10 @@ function getModifiedExpenseOriginalMessage(oldTransaction, transactionChanges, i
originalMessage.oldCreated = TransactionUtils.getCreated(oldTransaction);
originalMessage.created = transactionChanges.created;
}
if (_.has(transactionChanges, 'merchant')) {
originalMessage.oldMerchant = TransactionUtils.getMerchant(oldTransaction);
originalMessage.merchant = transactionChanges.merchant;
}

// The amount is always a combination of the currency and the number value so when one changes we need to store both
// to match how we handle the modified expense action in oldDot
Expand Down
5 changes: 3 additions & 2 deletions src/libs/TransactionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function buildOptimisticTransaction(
created = '',
source = '',
originalTransactionID = '',
merchant = CONST.REPORT.TYPE.IOU,
merchant = CONST.TRANSACTION.DEFAULT_MERCHANT,
receipt = {},
filename = '',
existingTransactionID = null,
Expand All @@ -66,7 +66,7 @@ function buildOptimisticTransaction(
currency,
reportID,
comment: commentJSON,
merchant: merchant || CONST.REPORT.TYPE.IOU,
merchant: merchant || CONST.TRANSACTION.DEFAULT_MERCHANT,
created: created || DateUtils.getDBTime(),
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
receipt,
Expand Down Expand Up @@ -128,6 +128,7 @@ function getUpdatedTransaction(transaction, transactionChanges, isFromExpenseRep
...(_.has(transactionChanges, 'created') && {created: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(_.has(transactionChanges, 'amount') && {amount: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(_.has(transactionChanges, 'currency') && {currency: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(_.has(transactionChanges, 'merchant') && {merchant: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
};

return updatedTransaction;
Expand Down
4 changes: 3 additions & 1 deletion src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ function editMoneyRequest(transactionID, transactionThreadReportID, transactionC
amount: null,
created: null,
currency: null,
merchant: null,
},
},
},
Expand All @@ -1032,7 +1033,7 @@ function editMoneyRequest(transactionID, transactionThreadReportID, transactionC
];

// STEP 6: Call the API endpoint
const {created, amount, currency, comment} = ReportUtils.getTransactionDetails(updatedTransaction);
const {created, amount, currency, comment, merchant} = ReportUtils.getTransactionDetails(updatedTransaction);
API.write(
'EditMoneyRequest',
{
Expand All @@ -1042,6 +1043,7 @@ function editMoneyRequest(transactionID, transactionThreadReportID, transactionC
amount,
currency,
comment,
merchant,
},
{optimisticData, successData, failureData},
);
Expand Down
61 changes: 61 additions & 0 deletions src/pages/EditRequestMerchantPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React, {useRef} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import TextInput from '../components/TextInput';
import ScreenWrapper from '../components/ScreenWrapper';
import HeaderWithBackButton from '../components/HeaderWithBackButton';
import Form from '../components/Form';
import ONYXKEYS from '../ONYXKEYS';
import styles from '../styles/styles';
import Navigation from '../libs/Navigation/Navigation';
import CONST from '../CONST';
import useLocalize from '../hooks/useLocalize';

const propTypes = {
/** Transaction default merchant value */
defaultMerchant: PropTypes.string.isRequired,

/** Callback to fire when the Save button is pressed */
onSubmit: PropTypes.func.isRequired,
};

function EditRequestMerchantPage({defaultMerchant, onSubmit}) {
const {translate} = useLocalize();
const merchantInputRef = useRef(null);
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
onEntryTransitionEnd={() => merchantInputRef.current && merchantInputRef.current.focus()}
>
<HeaderWithBackButton
title={translate('common.merchant')}
onBackButtonPress={() => Navigation.goBack()}
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB

Suggested change
onBackButtonPress={() => Navigation.goBack()}
onBackButtonPress={Navigation.goBack}

/>
<Form
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.MONEY_REQUEST_MERCHANT_FORM}
onSubmit={onSubmit}
submitButtonText={translate('common.save')}
enabledWhenOffline
>
<View style={styles.mb4}>
<TextInput
inputID="merchant"
name="merchant"
defaultValue={defaultMerchant}
label={translate('common.merchant')}
accessibilityLabel={translate('common.merchant')}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT}
ref={(e) => (merchantInputRef.current = e)}
/>
</View>
</Form>
</ScreenWrapper>
);
}

EditRequestMerchantPage.propTypes = propTypes;
EditRequestMerchantPage.displayName = 'EditRequestMerchantPage';

export default EditRequestMerchantPage;
19 changes: 18 additions & 1 deletion src/pages/EditRequestPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as TransactionUtils from '../libs/TransactionUtils';
import * as Policy from '../libs/actions/Policy';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsPropTypes} from '../components/withCurrentUserPersonalDetails';
import EditRequestDescriptionPage from './EditRequestDescriptionPage';
import EditRequestMerchantPage from './EditRequestMerchantPage';
import EditRequestCreatedPage from './EditRequestCreatedPage';
import EditRequestAmountPage from './EditRequestAmountPage';
import reportPropTypes from './reportPropTypes';
Expand Down Expand Up @@ -67,7 +68,7 @@ const defaultProps = {
function EditRequestPage({report, route, parentReport, policy, session}) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const transaction = TransactionUtils.getLinkedTransaction(parentReportAction);
const {amount: transactionAmount, currency: transactionCurrency, comment: transactionDescription} = ReportUtils.getTransactionDetails(transaction);
const {amount: transactionAmount, currency: transactionCurrency, comment: transactionDescription, merchant: transactionMerchant} = ReportUtils.getTransactionDetails(transaction);

const defaultCurrency = lodashGet(route, 'params.currency', '') || transactionCurrency;

Expand Down Expand Up @@ -151,6 +152,22 @@ function EditRequestPage({report, route, parentReport, policy, session}) {
);
}

if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.MERCHANT) {
return (
<EditRequestMerchantPage
defaultMerchant={transactionMerchant}
onSubmit={(transactionChanges) => {
// In case the merchant hasn't been changed, do not make the API request.
if (transactionChanges.merchant.trim() === transactionMerchant) {
Navigation.dismissModal();
return;
}
editMoneyRequest({merchant: transactionChanges.merchant.trim()});
}}
/>
);
}

return null;
}

Expand Down
12 changes: 6 additions & 6 deletions tests/actions/IOUTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ describe('actions/IOU', () => {
// The comment should be correct
expect(transaction.comment.comment).toBe(comment);

expect(transaction.merchant).toBe(CONST.REPORT.TYPE.IOU);
expect(transaction.merchant).toBe(CONST.TRANSACTION.DEFAULT_MERCHANT);

// It should be pending
expect(transaction.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
Expand Down Expand Up @@ -477,7 +477,7 @@ describe('actions/IOU', () => {
expect(newTransaction.reportID).toBe(iouReportID);
expect(newTransaction.amount).toBe(amount);
expect(newTransaction.comment.comment).toBe(comment);
expect(newTransaction.merchant).toBe(CONST.REPORT.TYPE.IOU);
expect(newTransaction.merchant).toBe(CONST.TRANSACTION.DEFAULT_MERCHANT);
expect(newTransaction.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);

// The transactionID on the iou action should match the one from the transactions collection
Expand Down Expand Up @@ -620,7 +620,7 @@ describe('actions/IOU', () => {
expect(transaction.reportID).toBe(iouReportID);
expect(transaction.amount).toBe(amount);
expect(transaction.comment.comment).toBe(comment);
expect(transaction.merchant).toBe(CONST.REPORT.TYPE.IOU);
expect(transaction.merchant).toBe(CONST.TRANSACTION.DEFAULT_MERCHANT);
expect(transaction.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);

// The transactionID on the iou action should match the one from the transactions collection
Expand Down Expand Up @@ -1079,9 +1079,9 @@ describe('actions/IOU', () => {
expect(vitTransaction.comment.comment).toBe(comment);
expect(groupTransaction.comment.comment).toBe(comment);

expect(carlosTransaction.merchant).toBe(CONST.REPORT.TYPE.IOU);
expect(julesTransaction.merchant).toBe(CONST.REPORT.TYPE.IOU);
expect(vitTransaction.merchant).toBe(CONST.REPORT.TYPE.IOU);
expect(carlosTransaction.merchant).toBe(CONST.TRANSACTION.DEFAULT_MERCHANT);
expect(julesTransaction.merchant).toBe(CONST.TRANSACTION.DEFAULT_MERCHANT);
expect(vitTransaction.merchant).toBe(CONST.TRANSACTION.DEFAULT_MERCHANT);
expect(groupTransaction.merchant).toBe(
`Split bill with ${RORY_EMAIL}, ${CARLOS_EMAIL}, ${JULES_EMAIL}, and ${VIT_EMAIL} [${DateUtils.getDBTime().slice(0, 10)}]`,
);
Expand Down
Loading