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

Allow Adding/Viewing tag on a Split Bill #31647

Merged
merged 17 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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 src/components/MoneyRequestConfirmationList.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function MoneyRequestConfirmationList(props) {
const policyTagList = lodashGet(policyTag, 'tags', {});
const policyTagListName = lodashGet(policyTag, 'name', translate('common.tag'));
// A flag for showing the tags field
const shouldShowTags = props.isPolicyExpenseChat && OptionsListUtils.hasEnabledOptions(_.values(policyTagList));
const shouldShowTags = props.isPolicyExpenseChat && (props.iouTag || OptionsListUtils.hasEnabledOptions(_.values(policyTagList)));

// A flag for showing the billable field
const shouldShowBillable = !lodashGet(props.policy, 'disabledFields.defaultBillable', true);
Expand Down
2 changes: 1 addition & 1 deletion src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ function isUserCreatedPolicyRoom(report) {
* @returns {Boolean}
*/
function isPolicyExpenseChat(report) {
return getChatType(report) === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT;
return getChatType(report) === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT || lodashGet(report, 'isPolicyExpenseChat', false);
rezkiy37 marked this conversation as resolved.
Show resolved Hide resolved
}

/** Wether the provided report belongs to a Control policy and is an epxense chat
Expand Down
76 changes: 24 additions & 52 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,6 @@ Onyx.connect({
},
});

let allRecentlyUsedTags = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS,
waitForCollectionCallback: true,
callback: (value) => {
if (!value) {
allRecentlyUsedTags = {};
return;
}

allRecentlyUsedTags = value;
},
});

let allPolicyTags = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY_TAGS,
waitForCollectionCallback: true,
callback: (value) => {
if (!value) {
allPolicyTags = {};
return;
}

allPolicyTags = value;
},
});

let userAccountID = '';
let currentUserEmail = '';
Onyx.connect({
Expand Down Expand Up @@ -516,15 +488,9 @@ function getMoneyRequestInformation(
optimisticPolicyRecentlyUsedCategories = Policy.buildOptimisticPolicyRecentlyUsedCategories(iouReport.policyID, category);
}

const optimisticPolicyRecentlyUsedTags = {};
const policyTags = allPolicyTags[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${iouReport.policyID}`];
const recentlyUsedPolicyTags = allRecentlyUsedTags[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${iouReport.policyID}`];

if (policyTags) {
// For now it only uses the first tag of the policy, since multi-tags are not yet supported
const tagListKey = _.first(_.keys(policyTags));
const uniquePolicyRecentlyUsedTags = recentlyUsedPolicyTags ? _.filter(recentlyUsedPolicyTags[tagListKey], (recentlyUsedPolicyTag) => recentlyUsedPolicyTag !== tag) : [];
optimisticPolicyRecentlyUsedTags[tagListKey] = [tag, ...uniquePolicyRecentlyUsedTags];
let optimisticPolicyRecentlyUsedTags = {};
rezkiy37 marked this conversation as resolved.
Show resolved Hide resolved
if (tag) {
optimisticPolicyRecentlyUsedTags = Policy.buildOptimisticPolicyRecentlyUsedTags(iouReport.policyID, tag);
}

// If there is an existing transaction (which is the case for distance requests), then the data from the existing transaction
Expand Down Expand Up @@ -921,11 +887,12 @@ function requestMoney(
* @param {String} comment
* @param {String} currency
* @param {String} category
* @param {String} tag
* @param {String} existingSplitChatReportID - the report ID where the split bill happens, could be a group chat or a workspace chat
*
* @return {Object}
*/
function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, existingSplitChatReportID = '') {
function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, tag, existingSplitChatReportID = '') {
const currentUserEmailForIOUSplit = OptionsListUtils.addSMSDomainIfPhoneNumber(currentUserLogin);
const participantAccountIDs = _.map(participants, (participant) => Number(participant.accountID));
const existingSplitChatReport =
Expand Down Expand Up @@ -953,6 +920,7 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco
undefined,
undefined,
category,
tag,
);

// Note: The created action must be optimistically generated before the IOU action so there's no chance that the created action appears after the IOU action in the chat
Expand Down Expand Up @@ -1143,6 +1111,7 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco
undefined,
undefined,
category,
tag,
);

// STEP 4: Build optimistic reportActions. We need:
Expand Down Expand Up @@ -1196,6 +1165,12 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco
optimisticPolicyRecentlyUsedCategories = Policy.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category);
}

// Add tag to optimistic policy recently used tags when a participant is a workspace
let optimisticPolicyRecentlyUsedTags = {};
if (isPolicyExpenseChat) {
optimisticPolicyRecentlyUsedTags = Policy.buildOptimisticPolicyRecentlyUsedTags(participant.policyID, tag);
}

// STEP 5: Build Onyx Data
const [oneOnOneOptimisticData, oneOnOneSuccessData, oneOnOneFailureData] = buildOnyxDataForMoneyRequest(
oneOnOneChatReport,
Expand All @@ -1207,7 +1182,7 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco
oneOnOnePersonalDetailListAction,
oneOnOneReportPreviewAction,
optimisticPolicyRecentlyUsedCategories,
{},
optimisticPolicyRecentlyUsedTags,
isNewOneOnOneChatReport,
shouldCreateNewOneOnOneIOUReport,
);
Expand Down Expand Up @@ -1257,10 +1232,11 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco
* @param {String} comment
* @param {String} currency
* @param {String} category
* @param {String} tag
* @param {String} existingSplitChatReportID - Either a group DM or a workspace chat
*/
function splitBill(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, existingSplitChatReportID = '') {
const {splitData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, existingSplitChatReportID);
function splitBill(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, tag, existingSplitChatReportID = '') {
const {splitData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, tag, existingSplitChatReportID);
API.write(
'SplitBill',
{
Expand All @@ -1270,6 +1246,7 @@ function splitBill(participants, currentUserLogin, currentUserAccountID, amount,
currency,
comment,
category,
tag,
transactionID: splitData.transactionID,
reportActionID: splitData.reportActionID,
createdReportActionID: splitData.createdReportActionID,
Expand All @@ -1291,10 +1268,10 @@ function splitBill(participants, currentUserLogin, currentUserAccountID, amount,
* @param {String} comment
* @param {String} currency
* @param {String} category
* @param {String} tag
Copy link
Contributor

Choose a reason for hiding this comment

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

Coming from #34988
When creating the optimistic split transaction, we needed to pass the category & tag to buildOptimisticTransaction

*/
function splitBillAndOpenReport(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category) {
const {splitData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category);

function splitBillAndOpenReport(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, tag) {
const {splitData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, category, tag);
API.write(
'SplitBillAndOpenReport',
{
Expand All @@ -1304,6 +1281,7 @@ function splitBillAndOpenReport(participants, currentUserLogin, currentUserAccou
currency,
comment,
category,
tag,
transactionID: splitData.transactionID,
reportActionID: splitData.reportActionID,
createdReportActionID: splitData.createdReportActionID,
Expand Down Expand Up @@ -1833,15 +1811,9 @@ function editRegularMoneyRequest(transactionID, transactionThreadReportID, trans
updatedChatReport.lastMessageHtml = messageText;
}

const optimisticPolicyRecentlyUsedTags = {};
let optimisticPolicyRecentlyUsedTags = {};
if (_.has(transactionChanges, 'tag')) {
const tagListName = transactionChanges.tagListName;
const recentlyUsedPolicyTags = allRecentlyUsedTags[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${iouReport.policyID}`];

const uniquePolicyRecentlyUsedTags = recentlyUsedPolicyTags
? _.filter(recentlyUsedPolicyTags[tagListName], (recentlyUsedPolicyTag) => recentlyUsedPolicyTag !== transactionChanges.tag)
: [];
optimisticPolicyRecentlyUsedTags[tagListName] = [transactionChanges.tag, ...uniquePolicyRecentlyUsedTags];
optimisticPolicyRecentlyUsedTags = Policy.buildOptimisticPolicyRecentlyUsedTags(iouReport.policyID, transactionChanges.tag);
}

const isScanning = TransactionUtils.hasReceipt(updatedTransaction) && TransactionUtils.isReceiptBeingScanned(updatedTransaction);
Expand Down
43 changes: 43 additions & 0 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ Onyx.connect({
callback: (val) => (allRecentlyUsedCategories = val),
});

let allPolicyTags = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY_TAGS,
waitForCollectionCallback: true,
callback: (value) => {
if (!value) {
rezkiy37 marked this conversation as resolved.
Show resolved Hide resolved
allPolicyTags = {};
return;
}

allPolicyTags = value;
},
});

let allRecentlyUsedTags = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS,
waitForCollectionCallback: true,
callback: (val) => (allRecentlyUsedTags = val),
});

let networkStatus = {};
Onyx.connect({
key: ONYXKEYS.NETWORK,
Expand Down Expand Up @@ -1463,6 +1484,27 @@ function buildOptimisticPolicyRecentlyUsedCategories(policyID, category) {
return lodashUnion([category], policyRecentlyUsedCategories);
}

/**
* @param {String} policyID
* @param {String} tag
* @returns {Object}
*/
function buildOptimisticPolicyRecentlyUsedTags(policyID, tag) {
if (!policyID || !tag) {
return [];
}

const policyTags = lodashGet(allPolicyTags, `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, {});
// For now it only uses the first tag of the policy, since multi-tags are not yet supported
const tagListKey = _.first(_.keys(policyTags));
const policyRecentlyUsedTags = lodashGet(allRecentlyUsedTags, `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${policyID}`, {});

return {
...policyRecentlyUsedTags,
[tagListKey]: lodashUnion([tag], lodashGet(policyRecentlyUsedTags, [tagListKey], [])),
};
}

/**
* This flow is used for bottom up flow converting IOU report to an expense report. When user takes this action,
* we create a Collect type workspace when the person taking the action becomes an owner and an admin, while we
Expand Down Expand Up @@ -1892,5 +1934,6 @@ export {
dismissAddedWithPrimaryLoginMessages,
openDraftWorkspaceRequest,
buildOptimisticPolicyRecentlyUsedCategories,
buildOptimisticPolicyRecentlyUsedTags,
createDraftInitialWorkspace,
};
2 changes: 2 additions & 0 deletions src/pages/iou/SplitBillDetailsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function SplitBillDetailsPage(props) {
merchant: splitMerchant,
created: splitCreated,
category: splitCategory,
tag: splitTag,
} = isEditingSplitBill && props.draftTransaction ? ReportUtils.getTransactionDetails(props.draftTransaction) : ReportUtils.getTransactionDetails(props.transaction);

const onConfirm = useCallback(
Expand Down Expand Up @@ -131,6 +132,7 @@ function SplitBillDetailsPage(props) {
iouCreated={splitCreated}
iouMerchant={splitMerchant}
iouCategory={splitCategory}
iouTag={splitTag}
iouType={CONST.IOU.TYPE.SPLIT}
isReadOnly={!isEditingSplitBill}
shouldShowSmartScanFields
Expand Down
3 changes: 3 additions & 0 deletions src/pages/iou/steps/MoneyRequestConfirmPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ function MoneyRequestConfirmPage(props) {
trimmedComment,
props.iou.currency,
props.iou.category,
props.iou.tag,
reportID,
);
return;
Expand All @@ -254,6 +255,7 @@ function MoneyRequestConfirmPage(props) {
trimmedComment,
props.iou.currency,
props.iou.category,
props.iou.tag,
);
return;
}
Expand All @@ -277,6 +279,7 @@ function MoneyRequestConfirmPage(props) {
props.currentUserPersonalDetails.accountID,
props.iou.currency,
props.iou.category,
props.iou.tag,
props.iou.receiptPath,
props.iou.receiptFilename,
isDistanceRequest,
Expand Down
Loading