Skip to content

Commit

Permalink
Many iOU updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Beamanator committed Jun 8, 2023
1 parent 651309a commit 1e97491
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 43 deletions.
6 changes: 6 additions & 0 deletions src/components/ReportActionItem/IOUPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ const propTypes = {
/** Email address of the manager in this iou report */
managerEmail: PropTypes.string,

/** Account ID of the manager in this iou report */
managerID: PropTypes.number,

/** Email address of the creator of this iou report */
ownerEmail: PropTypes.string,

/** Account ID of the creator of this iou report */
ownerAccountID: PropTypes.number,

/** Outstanding amount in cents of this transaction */
total: PropTypes.number,

Expand Down
4 changes: 3 additions & 1 deletion src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,14 @@ function getParticipantsOptions(report, personalDetails) {
return _.map(getPersonalDetailsForAccountIDs(participants, personalDetails), (details) => ({
keyForList: details.login,
login: details.login,
accountID: details.accountID,
text: details.displayName,
firstName: lodashGet(details, 'firstName', ''),
lastName: lodashGet(details, 'lastName', ''),
alternateText: Str.isSMSLogin(details.login || '') ? LocalePhoneNumber.formatPhoneNumber(details.login) : details.login,
icons: [
{
source: UserUtils.getAvatar(details.avatar, details.login),
source: UserUtils.getAvatar(details.avatar, details.accountID),
name: details.login,
type: CONST.ICON_TYPE_AVATAR,
},
Expand Down Expand Up @@ -849,6 +850,7 @@ function getIOUConfirmationOptionsFromPayeePersonalDetail(personalDetail, amount
],
descriptiveText: amountText,
login: personalDetail.login,
accountID: personalDetail.accountID,
};
}

Expand Down
12 changes: 9 additions & 3 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1202,25 +1202,28 @@ function buildOptimisticTaskCommentReportAction(taskReportID, taskTitle, taskAss
* Builds an optimistic IOU report with a randomly generated reportID
*
* @param {String} payeeEmail - Email of the person generating the IOU.
* @param {String} payerEmail - Email of the other person participating in the IOU.
* @param {Number} payerAccountID - AccountID of the other person participating in the IOU.
* @param {Number} total - IOU amount in the smallest unit of the currency.
* @param {String} chatReportID - Report ID of the chat where the IOU is.
* @param {String} currency - IOU currency.
* @param {Boolean} isSendingMoney - If we send money the IOU should be created as settled
*
* @returns {Object}
*/
function buildOptimisticIOUReport(payeeEmail, payerEmail, total, chatReportID, currency, isSendingMoney = false) {
function buildOptimisticIOUReport(payeeEmail, payerAccountID, total, chatReportID, currency, isSendingMoney = false) {
const formattedTotal = CurrencyUtils.convertToDisplayString(total, currency);
// TODO: REPLACE ME
const payerEmail = 'REPLACE ME WITH PERSONAL DETAILS';
return {
// If we're sending money, hasOutstandingIOU should be false
hasOutstandingIOU: !isSendingMoney,
type: CONST.REPORT.TYPE.IOU,
cachedTotal: formattedTotal,
chatReportID,
currency,
managerEmail: payerEmail,
managerID: payerAccountID,
ownerEmail: payeeEmail,
// ownerAccountID: 0,
reportID: generateReportID(),
state: CONST.REPORT.STATE.SUBMITTED,
stateNum: isSendingMoney ? CONST.REPORT.STATE_NUM.SUBMITTED : CONST.REPORT.STATE_NUM.PROCESSING,
Expand Down Expand Up @@ -1483,16 +1486,19 @@ function buildOptimisticChatReport(
isOwnPolicyExpenseChat,
isPinned: reportName === CONST.REPORT.WORKSPACE_CHAT_ROOMS.ADMINS,
lastActorEmail: '',
lastActorAccountID: 0,
lastMessageHtml: '',
lastMessageText: null,
lastReadTime: currentTime,
lastVisibleActionCreated: currentTime,
notificationPreference,
oldPolicyName,
ownerEmail: ownerEmail || CONST.REPORT.OWNER_EMAIL_FAKE,
// ownerAccountID: ownerAccountID || 0,
parentReportActionID,
parentReportID,
participants: participantList,
participantAccountIDs: participantList,
policyID,
reportID: generateReportID(),
reportName,
Expand Down
38 changes: 21 additions & 17 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ function requestMoney(report, amount, currency, payeeEmail, participant, comment
} else {
iouReport = isPolicyExpenseChat
? ReportUtils.buildOptimisticExpenseReport(chatReport.reportID, chatReport.policyID, payeeEmail, amount, currency)
: ReportUtils.buildOptimisticIOUReport(payeeEmail, payerEmail, amount, chatReport.reportID, currency);
: ReportUtils.buildOptimisticIOUReport(payeeEmail, payerAccountID, amount, chatReport.reportID, currency);
}

// STEP 3: Build optimistic transaction
Expand Down Expand Up @@ -391,9 +391,10 @@ function requestMoney(report, amount, currency, payeeEmail, participant, comment
function createSplitsAndOnyxData(participants, currentUserLogin, amount, comment, currency, existingGroupChatReportID = '') {
const currentUserEmail = OptionsListUtils.addSMSDomainIfPhoneNumber(currentUserLogin);
const participantLogins = _.map(participants, (participant) => OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login).toLowerCase());
const participantAccountIDs = _.map(participants, (participant) => participant.accountID);
const existingGroupChatReport = existingGroupChatReportID
? chatReports[`${ONYXKEYS.COLLECTION.REPORT}${existingGroupChatReportID}`]
: ReportUtils.getChatByParticipants(participantLogins);
: ReportUtils.getChatByParticipants(participantAccountIDs);
const groupChatReport = existingGroupChatReport || ReportUtils.buildOptimisticChatReport(participantLogins);

// ReportID is -2 (aka "deleted") on the group transaction: https://github.com/Expensify/Auth/blob/3fa2698654cd4fbc30f9de38acfca3fbeb7842e4/auth/command/SplitTransaction.cpp#L24-L27
Expand Down Expand Up @@ -503,11 +504,12 @@ function createSplitsAndOnyxData(participants, currentUserLogin, amount, comment

// Loop through participants creating individual chats, iouReports and reportActionIDs as needed
const splitAmount = IOUUtils.calculateAmount(participants.length, amount, false);
const splits = [{email: currentUserEmail, amount: IOUUtils.calculateAmount(participants.length, amount, true)}];
const splits = [{email: currentUserEmail, accountID: currentUserAccountID, amount: IOUUtils.calculateAmount(participants.length, amount, true)}];

const hasMultipleParticipants = participants.length > 1;
_.each(participants, (participant) => {
const email = OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login).toLowerCase();
const accountID = participant.accountID;
if (email === currentUserEmail) {
return;
}
Expand All @@ -516,7 +518,7 @@ function createSplitsAndOnyxData(participants, currentUserLogin, amount, comment
// If we only have one participant and the request was initiated from the global create menu, i.e. !existingGroupChatReportID, the oneOnOneChatReport is the groupChatReport
let oneOnOneChatReport;
let isNewOneOnOneChatReport = false;
oneOnOneChatReport = !hasMultipleParticipants && !existingGroupChatReportID ? groupChatReport : ReportUtils.getChatByParticipants([email]);
oneOnOneChatReport = !hasMultipleParticipants && !existingGroupChatReportID ? groupChatReport : ReportUtils.getChatByParticipants([accountID]);

if (!oneOnOneChatReport) {
isNewOneOnOneChatReport = true;
Expand All @@ -529,7 +531,7 @@ function createSplitsAndOnyxData(participants, currentUserLogin, amount, comment
if (!isNewOneOnOneIOUReport) {
oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(iouReports[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`], currentUserEmail, splitAmount, currency);
} else {
oneOnOneIOUReport = ReportUtils.buildOptimisticIOUReport(currentUserEmail, email, splitAmount, oneOnOneChatReport.reportID, currency);
oneOnOneIOUReport = ReportUtils.buildOptimisticIOUReport(currentUserEmail, accountID, splitAmount, oneOnOneChatReport.reportID, currency);
}

// STEP 3: Build optimistic transaction
Expand Down Expand Up @@ -816,12 +818,13 @@ function buildPayPalPaymentUrl(amount, submitterPayPalMeAddress, currency) {
* @param {String} currency
* @param {String} comment
* @param {String} paymentMethodType
* @param {String} managerEmail - Email of the person sending the money
* @param {String} managerID - Account ID of the person sending the money
* @param {Object} recipient - The user receiving the money
* @returns {Object}
*/
function getSendMoneyParams(report, amount, currency, comment, paymentMethodType, managerEmail, recipient) {
function getSendMoneyParams(report, amount, currency, comment, paymentMethodType, managerID, recipient) {
const recipientEmail = OptionsListUtils.addSMSDomainIfPhoneNumber(recipient.login);
const recipientAccountID = recipient.accountID;
const newIOUReportDetails = JSON.stringify({
amount,
currency,
Expand All @@ -839,7 +842,7 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType
chatReport = ReportUtils.buildOptimisticChatReport([recipientEmail]);
isNewChat = true;
}
const optimisticIOUReport = ReportUtils.buildOptimisticIOUReport(recipientEmail, managerEmail, amount, chatReport.reportID, currency, true);
const optimisticIOUReport = ReportUtils.buildOptimisticIOUReport(recipientEmail, managerID, amount, chatReport.reportID, currency, true);

const optimisticTransaction = TransactionUtils.buildOptimisticTransaction(amount * 100, currency, optimisticIOUReport.reportID, comment);
const optimisticTransactionData = {
Expand Down Expand Up @@ -1103,11 +1106,11 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho
* @param {Number} amount
* @param {String} currency
* @param {String} comment
* @param {String} managerEmail - Email of the person sending the money
* @param {String} managerID - Account ID of the person sending the money
* @param {Object} recipient - The user receiving the money
*/
function sendMoneyElsewhere(report, amount, currency, comment, managerEmail, recipient) {
const {params, optimisticData, successData, failureData} = getSendMoneyParams(report, amount, currency, comment, CONST.IOU.PAYMENT_TYPE.ELSEWHERE, managerEmail, recipient);
function sendMoneyElsewhere(report, amount, currency, comment, managerID, recipient) {
const {params, optimisticData, successData, failureData} = getSendMoneyParams(report, amount, currency, comment, CONST.IOU.PAYMENT_TYPE.ELSEWHERE, managerID, recipient);

API.write('SendMoneyElsewhere', params, {optimisticData, successData, failureData});

Expand All @@ -1119,11 +1122,11 @@ function sendMoneyElsewhere(report, amount, currency, comment, managerEmail, rec
* @param {Number} amount
* @param {String} currency
* @param {String} comment
* @param {String} managerEmail - Email of the person sending the money
* @param {String} managerID - Account ID of the person sending the money
* @param {Object} recipient - The user receiving the money
*/
function sendMoneyWithWallet(report, amount, currency, comment, managerEmail, recipient) {
const {params, optimisticData, successData, failureData} = getSendMoneyParams(report, amount, currency, comment, CONST.IOU.PAYMENT_TYPE.EXPENSIFY, managerEmail, recipient);
function sendMoneyWithWallet(report, amount, currency, comment, managerID, recipient) {
const {params, optimisticData, successData, failureData} = getSendMoneyParams(report, amount, currency, comment, CONST.IOU.PAYMENT_TYPE.EXPENSIFY, managerID, recipient);

API.write('SendMoneyWithWallet', params, {optimisticData, successData, failureData});

Expand All @@ -1135,11 +1138,11 @@ function sendMoneyWithWallet(report, amount, currency, comment, managerEmail, re
* @param {Number} amount
* @param {String} currency
* @param {String} comment
* @param {String} managerEmail - Email of the person sending the money
* @param {String} managerID - Account ID of the person sending the money
* @param {Object} recipient - The user receiving the money
*/
function sendMoneyViaPaypal(report, amount, currency, comment, managerEmail, recipient) {
const {params, optimisticData, successData, failureData} = getSendMoneyParams(report, amount, currency, comment, CONST.IOU.PAYMENT_TYPE.PAYPAL_ME, managerEmail, recipient);
function sendMoneyViaPaypal(report, amount, currency, comment, managerID, recipient) {
const {params, optimisticData, successData, failureData} = getSendMoneyParams(report, amount, currency, comment, CONST.IOU.PAYMENT_TYPE.PAYPAL_ME, managerID, recipient);

API.write('SendMoneyViaPaypal', params, {optimisticData, successData, failureData});

Expand All @@ -1157,6 +1160,7 @@ function sendMoneyViaPaypal(report, amount, currency, comment, managerEmail, rec
function payMoneyRequest(paymentType, chatReport, iouReport) {
const recipient = {
login: iouReport.ownerEmail,
accountID: iouReport.ownerAccountID,
payPalMeAddress: iouReport.submitterPayPalMeAddress,
};
const {params, optimisticData, successData, failureData} = getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentType);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/NewChatPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class NewChatPage extends Component {
this.createChat = this.createChat.bind(this);
this.createGroup = this.createGroup.bind(this);
this.updateOptionsWithSearchTerm = this.updateOptionsWithSearchTerm.bind(this);
this.excludedGroupEmails = _.without(CONST.EXPENSIFY_EMAILS, CONST.EMAIL.CONCIERGE);
this.excludedGroupEmails = _.without(CONST.EXPENSIFY_ACCOUNT_IDS, CONST.ACCOUNT_ID.CONCIERGE);

const {recentReports, personalDetails, userToInvite} = OptionsListUtils.getNewChatOptions(
props.reports,
Expand Down
5 changes: 2 additions & 3 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ class ReportActionCompose extends React.Component {
// We only prevent the task option from showing if it's a DM and the other user is an Expensify default email
if (
!Permissions.canUseTasks(this.props.betas) ||
(lodashGet(this.props.report, 'participants', []).length === 1 && _.some(reportParticipants, (email) => _.contains(CONST.EXPENSIFY_EMAILS, email)))
(lodashGet(this.props.report, 'participantAccountIDs', []).length === 1 && _.some(reportParticipants, (accountID) => _.contains(CONST.EXPENSIFY_ACCOUNT_IDS, accountID)))
) {
return [];
}
Expand Down Expand Up @@ -905,8 +905,7 @@ class ReportActionCompose extends React.Component {

render() {
const reportParticipants = _.without(lodashGet(this.props.report, 'participantAccountIDs', []), this.props.currentUserPersonalDetails.accountID);
// TODO: ACCOUNT IDS!!!
const participantsWithoutExpensifyAccountIDs = _.difference(reportParticipants, CONST.EXPENSIFY_EMAILS);
const participantsWithoutExpensifyAccountIDs = _.difference(reportParticipants, CONST.EXPENSIFY_ACCOUNT_IDS);
const reportRecipient = this.props.personalDetails[participantsWithoutExpensifyAccountIDs[0]];
const shouldShowReportRecipientLocalTime =
ReportUtils.canShowReportRecipientLocalTime(this.props.personalDetails, this.props.report, this.props.currentUserPersonalDetails.login) && !this.props.isComposerFullSize;
Expand Down
10 changes: 5 additions & 5 deletions src/pages/iou/MoneyRequestModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,20 @@ const MoneyRequestModal = (props) => {
const participant = selectedOptions[0];

if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.ELSEWHERE) {
IOU.sendMoneyElsewhere(props.report, amount, currency, trimmedComment, props.currentUserPersonalDetails.login, participant);
IOU.sendMoneyElsewhere(props.report, amount, currency, trimmedComment, props.currentUserPersonalDetails.accountID, participant);
return;
}

if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.PAYPAL_ME) {
IOU.sendMoneyViaPaypal(props.report, amount, currency, trimmedComment, props.currentUserPersonalDetails.login, participant);
IOU.sendMoneyViaPaypal(props.report, amount, currency, trimmedComment, props.currentUserPersonalDetails.accountID, participant);
return;
}

if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.EXPENSIFY) {
IOU.sendMoneyWithWallet(props.report, amount, currency, trimmedComment, props.currentUserPersonalDetails.login, participant);
IOU.sendMoneyWithWallet(props.report, amount, currency, trimmedComment, props.currentUserPersonalDetails.accountID, participant);
}
},
[amount, props.iou.comment, selectedOptions, props.currentUserPersonalDetails.login, props.iou.selectedCurrencyCode, props.report],
[amount, props.iou.comment, selectedOptions, props.currentUserPersonalDetails.accountID, props.iou.selectedCurrencyCode, props.report],
);

/**
Expand Down Expand Up @@ -372,7 +372,7 @@ const MoneyRequestModal = (props) => {
ReportScrollManager.scrollToBottom();
}}
hasMultipleParticipants={props.hasMultipleParticipants}
participants={_.filter(selectedOptions, (email) => props.currentUserPersonalDetails.login !== email.login)}
participants={_.filter(selectedOptions, (option) => props.currentUserPersonalDetails.accountID !== option.accountID)}
iouAmount={amount}
iouType={props.iouType}
// The participants can only be modified when the action is initiated from directly within a group chat and not the floating-action-button.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class MoneyRequestParticipantsSelector extends Component {
this.props.betas,
searchTerm,
[],
CONST.EXPENSIFY_EMAILS,
CONST.EXPENSIFY_ACCOUNT_IDS,

// If we are using this component in the "Request money" flow then we pass the includeOwnedWorkspaceChats argument so that the current user
// sees the option to request money from their admin on their own Workspace Chat.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const propTypes = {
participants: PropTypes.arrayOf(
PropTypes.shape({
login: PropTypes.string.isRequired,
accountID: PropTypes.number.isRequired,
alternateText: PropTypes.string,
hasDraftComment: PropTypes.bool,
icons: PropTypes.arrayOf(avatarPropTypes),
Expand Down Expand Up @@ -73,7 +74,7 @@ class MoneyRequestParticipantsSplitSelector extends Component {
props.betas,
'',
props.participants,
CONST.EXPENSIFY_EMAILS,
CONST.EXPENSIFY_ACCOUNT_IDS,
);

this.state = {
Expand Down Expand Up @@ -148,7 +149,7 @@ class MoneyRequestParticipantsSplitSelector extends Component {
this.props.betas,
searchTerm,
this.props.participants,
CONST.EXPENSIFY_EMAILS,
CONST.EXPENSIFY_ACCOUNT_IDS,
);
this.setState({
searchTerm,
Expand Down Expand Up @@ -189,8 +190,7 @@ class MoneyRequestParticipantsSplitSelector extends Component {
this.props.betas,
isOptionInList ? prevState.searchTerm : '',
newSelectedOptions,
// TODO: need to update :D
CONST.EXPENSIFY_EMAILS,
CONST.EXPENSIFY_ACCOUNT_IDS,
);
return {
recentReports,
Expand Down
Loading

0 comments on commit 1e97491

Please sign in to comment.