Skip to content
13 changes: 12 additions & 1 deletion src/components/ReportActionItem/IOUPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,13 @@ function IOUPreview(props) {
const sessionAccountID = lodashGet(props.session, 'accountID', null);
const managerID = props.iouReport.managerID || '';
const ownerAccountID = props.iouReport.ownerAccountID || '';
const isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(props.chatReport);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should add this chatReport to the propTypes and defaultProps


const participantAccountIDs = props.isBillSplit ? lodashGet(props.action, 'originalMessage.participantAccountIDs', []) : [managerID, ownerAccountID];
const participantAvatars = OptionsListUtils.getAvatarsForAccountIDs(participantAccountIDs, props.personalDetails);
if (isPolicyExpenseChat && props.isBillSplit) {
participantAvatars.push(ReportUtils.getWorkspaceIcon(props.chatReport));
}

// Pay button should only be visible to the manager of the report.
const isCurrentUserManager = managerID === sessionAccountID;
Expand Down Expand Up @@ -235,7 +240,10 @@ function IOUPreview(props) {
{props.isBillSplit && !_.isEmpty(participantAccountIDs) && (
<Text style={[styles.textLabel, styles.colorMuted, styles.ml1]}>
{props.translate('iou.amountEach', {
amount: CurrencyUtils.convertToDisplayString(IOUUtils.calculateAmount(participantAccountIDs.length - 1, requestAmount, requestCurrency), requestCurrency),
amount: CurrencyUtils.convertToDisplayString(
IOUUtils.calculateAmount(isPolicyExpenseChat ? 1 : participantAccountIDs.length - 1, requestAmount, requestCurrency),
requestCurrency,
),
})}
</Text>
)}
Expand Down Expand Up @@ -273,6 +281,9 @@ export default compose(
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
chatReport: {
key: ({chatReportID}) => `${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`,
},
iouReport: {
key: ({iouReportID}) => `${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`,
},
Expand Down
2 changes: 1 addition & 1 deletion src/components/ReportActionItem/MoneyRequestAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ function MoneyRequestAction(props) {
<RenderHTML html={`<comment>${props.translate('parentReportAction.deletedRequest')}</comment>`} />
) : (
<IOUPreview
iouReportID={props.requestReportID}
chatReportID={props.chatReportID}
iouReportID={props.requestReportID}
isBillSplit={isSplitBillAction}
action={props.action}
contextMenuAnchor={props.contextMenuAnchor}
Expand Down
52 changes: 39 additions & 13 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ function isUserCreatedPolicyRoom(report) {
return getChatType(report) === CONST.REPORT.CHAT_TYPE.POLICY_ROOM;
}

/**
* Get the policy type from a given report
* @param {Object} report
* @param {String} report.policyID
* @param {Object} policies must have Onyxkey prefix (i.e 'policy_') for keys
* @returns {String}
*/
function getPolicyType(report, policies) {
return lodashGet(policies, [`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`, 'type'], '');
}

/**
* Whether the provided report is a Policy Expense chat.
* @param {Object} report
Expand All @@ -333,6 +344,22 @@ function isPolicyExpenseChat(report) {
return getChatType(report) === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT;
}

/** Wether the provided report belongs to a Control policy and is an epxense chat
* @param {Object} report
* @returns {Boolean}
*/
function isControlPolicyExpenseChat(report) {
return isPolicyExpenseChat(report) && getPolicyType(report, allPolicies) === CONST.POLICY.TYPE.CORPORATE;
}

/** Wether the provided report belongs to a Control policy and is an epxense report
* @param {Object} report
* @returns {Boolean}
*/
function isControlPolicyExpenseReport(report) {
return isExpenseReport(report) && getPolicyType(report, allPolicies) === CONST.POLICY.TYPE.CORPORATE;
}

/**
* Whether the provided report is a chat room
* @param {Object} report
Expand Down Expand Up @@ -365,17 +392,6 @@ function isPublicAnnounceRoom(report) {
return visibility === CONST.REPORT.VISIBILITY.PUBLIC_ANNOUNCE;
}

/**
Comment thread
youssef-lr marked this conversation as resolved.
Resolved
* Get the policy type from a given report
* @param {Object} report
* @param {String} report.policyID
* @param {Object} policies must have Onyxkey prefix (i.e 'policy_') for keys
* @returns {String}
*/
function getPolicyType(report, policies) {
return lodashGet(policies, [`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`, 'type'], '');
}

/**
* If the report is a policy expense, the route should be for adding bank account for that policy
* else since the report is a personal IOU, the route should be for personal bank account.
Expand Down Expand Up @@ -1852,6 +1868,7 @@ function getIOUReportActionMessage(type, total, comment, currency, paymentType =
* @param {Boolean} [isSettlingUp] - Whether we are settling up an IOU.
* @param {Boolean} [isSendMoneyFlow] - Whether this is send money flow
* @param {Object} [receipt]
* @param {Boolean} [isOwnPolicyExpenseChat] - Whether this is an expense report create from the current user's policy expense chat

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @param {Boolean} [isOwnPolicyExpenseChat] - Whether this is an expense report create from the current user's policy expense chat
* @param {Boolean} [isOwnPolicyExpenseChat] - Whether this is an expense report created from the current user's policy expense chat

Nitpicking here!

* @returns {Object}
*/
function buildOptimisticIOUReportAction(
Expand All @@ -1866,6 +1883,7 @@ function buildOptimisticIOUReportAction(
isSettlingUp = false,
isSendMoneyFlow = false,
receipt = {},
isOwnPolicyExpenseChat = false,
) {
const IOUReportID = iouReportID || generateReportID();

Expand Down Expand Up @@ -1898,7 +1916,12 @@ function buildOptimisticIOUReportAction(
// IOUs of type split only exist in group DMs and those don't have an iouReport so we need to delete the IOUReportID key
if (type === CONST.IOU.REPORT_ACTION_TYPE.SPLIT) {
delete originalMessage.IOUReportID;
originalMessage.participantAccountIDs = [currentUserAccountID, ..._.pluck(participants, 'accountID')];
// Split bill made from a policy expense chat only have the payee's accountID as the participant because the payer could be any policy admin
if (isOwnPolicyExpenseChat) {
originalMessage.participantAccountIDs = [currentUserAccountID];
} else {
originalMessage.participantAccountIDs = [currentUserAccountID, ..._.pluck(participants, 'accountID')];
}
}

return {
Expand Down Expand Up @@ -2796,7 +2819,7 @@ function getMoneyRequestOptions(report, reportParticipants, betas) {
// unless there are no participants at all (e.g. #admins room for a policy with only 1 admin)
// DM chats will have the Split Bill option only when there are at least 3 people in the chat.
// There is no Split Bill option for Workspace chats
if (isChatRoom(report) || (hasMultipleParticipants && !isPolicyExpenseChat(report))) {
if (isChatRoom(report) || (hasMultipleParticipants && !isPolicyExpenseChat(report)) || isControlPolicyExpenseChat(report)) {
return [CONST.IOU.MONEY_REQUEST_TYPE.SPLIT];
}

Expand Down Expand Up @@ -3171,6 +3194,8 @@ export {
formatReportLastMessageText,
chatIncludesConcierge,
isPolicyExpenseChat,
isControlPolicyExpenseChat,
isControlPolicyExpenseReport,
getIconsForParticipants,
getIcons,
getRoomWelcomeMessage,
Expand Down Expand Up @@ -3208,6 +3233,7 @@ export {
getAllPolicyReports,
getIOUReportActionMessage,
getDisplayNameForParticipant,
getWorkspaceIcon,
isOptimisticPersonalDetail,
shouldDisableDetailPage,
isChatReport,
Expand Down
Loading