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

[Drafts] Show the GBR optimistically to the submitter when Scheduled submit is turned off #33030

Merged
merged 11 commits into from
Jan 2, 2024
14 changes: 11 additions & 3 deletions src/components/MoneyReportHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const propTypes = {

/** The role of the current user in the policy */
role: PropTypes.string,

/** Whether Scheduled Submit is turned on for this policy */
isHarvestingEnabled: PropTypes.bool,
}),

/** The chat report this report is linked to */
Expand All @@ -70,7 +73,9 @@ const defaultProps = {
session: {
email: null,
},
policy: {},
policy: {
isHarvestingEnabled: false,
},
};

function MoneyReportHeader({session, personalDetails, policy, chatReport, nextStep, report: moneyRequestReport, isSmallScreenWidth}) {
Expand Down Expand Up @@ -108,6 +113,9 @@ function MoneyReportHeader({session, personalDetails, policy, chatReport, nextSt
const formattedAmount = CurrencyUtils.convertToDisplayString(reimbursableTotal, moneyRequestReport.currency);
const isMoreContentShown = shouldShowNextStep || (shouldShowAnyButton && isSmallScreenWidth);

// The submit button should be success green colour only if the user is submitter and the policy does not have Scheduled Submit turned on
const isWaitingForSubmissionFromCurrentUser = chatReport.isOwnPolicyExpenseChat && !policy.isHarvestingEnabled;

const threeDotsMenuItems = [HeaderUtils.getPinMenuItem(moneyRequestReport)];
if (!ReportUtils.isArchivedRoom(chatReport)) {
threeDotsMenuItems.push({
Expand Down Expand Up @@ -164,7 +172,7 @@ function MoneyReportHeader({session, personalDetails, policy, chatReport, nextSt
<View style={styles.pv2}>
<Button
medium
success={chatReport.isOwnPolicyExpenseChat}
success={isWaitingForSubmissionFromCurrentUser}
text={translate('common.submit')}
style={[styles.mnw120, styles.pv2, styles.pr0]}
onPress={() => IOU.submitReport(moneyRequestReport)}
Expand Down Expand Up @@ -193,7 +201,7 @@ function MoneyReportHeader({session, personalDetails, policy, chatReport, nextSt
<View style={[styles.ph5, styles.pb2]}>
<Button
medium
success={chatReport.isOwnPolicyExpenseChat}
success={isWaitingForSubmissionFromCurrentUser}
text={translate('common.submit')}
style={[styles.w100, styles.pr0]}
onPress={() => IOU.submitReport(moneyRequestReport)}
Expand Down
12 changes: 10 additions & 2 deletions src/components/ReportActionItem/ReportPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ const propTypes = {

/** The role of the current user in the policy */
role: PropTypes.string,

/** Whether Scheduled Submit is turned on for this policy */
isHarvestingEnabled: PropTypes.bool,
}),

/* Onyx Props */
Expand Down Expand Up @@ -116,7 +119,9 @@ const defaultProps = {
accountID: null,
},
isWhisper: false,
policy: {},
policy: {
isHarvestingEnabled: false,
},
};

function ReportPreview(props) {
Expand Down Expand Up @@ -165,6 +170,9 @@ function ReportPreview(props) {

const shouldShowSubmitButton = isDraftExpenseReport && reimbursableSpend !== 0;

// The submit button should be success green colour only if the user is submitter and the policy does not have Scheduled Submit turned on
const isWaitingForSubmissionFromCurrentUser = props.chatReport.isOwnPolicyExpenseChat && !props.policy.isHarvestingEnabled;

const getDisplayAmount = () => {
if (hasPendingWaypoints) {
return props.translate('common.tbd');
Expand Down Expand Up @@ -327,7 +335,7 @@ function ReportPreview(props) {
{shouldShowSubmitButton && (
<Button
medium
success={props.chatReport.isOwnPolicyExpenseChat}
success={isWaitingForSubmissionFromCurrentUser}
text={translate('common.submit')}
style={styles.mt3}
onPress={() => IOU.submitReport(props.iouReport)}
Expand Down
30 changes: 24 additions & 6 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Navigation from '@libs/Navigation/Navigation';
import * as NumberUtils from '@libs/NumberUtils';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import Permissions from '@libs/Permissions';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
Expand Down Expand Up @@ -304,6 +305,7 @@ function buildOnyxDataForMoneyRequest(
optimisticPolicyRecentlyUsedTags,
isNewChatReport,
isNewIOUReport,
hasOutstandingChildRequest = false,
) {
const optimisticData = [
{
Expand All @@ -315,6 +317,7 @@ function buildOnyxDataForMoneyRequest(
lastReadTime: DateUtils.getDBTime(),
lastMessageTranslationKey: '',
iouReportID: iouReport.reportID,
hasOutstandingChildRequest,
...(isNewChatReport ? {pendingFields: {createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}} : {}),
},
},
Expand Down Expand Up @@ -623,11 +626,18 @@ function getMoneyRequestInformation(
const isNewIOUReport = !chatReport.iouReportID || ReportUtils.hasIOUWaitingOnCurrentUserBankAccount(chatReport);
let iouReport = isNewIOUReport ? null : allReports[`${ONYXKEYS.COLLECTION.REPORT}${chatReport.iouReportID}`];

// If the linked expense report on paid policy is not draft, we need to create a new draft expense report
if (isPolicyExpenseChat && iouReport) {
const policyType = ReportUtils.getPolicy(iouReport.policyID).type || '';
const isFromPaidPolicy = policyType === CONST.POLICY.TYPE.TEAM || policyType === CONST.POLICY.TYPE.CORPORATE;
if (isFromPaidPolicy && !ReportUtils.isDraftExpenseReport(iouReport)) {
// Check if the Scheduled Submit is enabled in case of expense report
let needsToBeManuallySubmitted = false;
let isFromPaidPolicy = false;
if (isPolicyExpenseChat) {
const policy = ReportUtils.getPolicy(chatReport.policyID);
isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy);

// If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN
needsToBeManuallySubmitted = isFromPaidPolicy && !(policy.isHarvestingEnabled || false);

// If the linked expense report on paid policy is not draft, we need to create a new draft expense report
if (iouReport && isFromPaidPolicy && !ReportUtils.isDraftExpenseReport(iouReport)) {
iouReport = null;
}
}
Expand Down Expand Up @@ -736,6 +746,10 @@ function getMoneyRequestInformation(
}
: undefined;

// The policy expense chat should have the GBR only when its a paid policy and the scheduled submit is turned off
// so the employee has to submit to their manager manually.
const hasOutstandingChildRequest = isPolicyExpenseChat && needsToBeManuallySubmitted;

// STEP 5: Build Onyx Data
const [optimisticData, successData, failureData] = buildOnyxDataForMoneyRequest(
chatReport,
Expand All @@ -750,6 +764,7 @@ function getMoneyRequestInformation(
optimisticPolicyRecentlyUsedTags,
isNewChatReport,
isNewIOUReport,
hasOutstandingChildRequest,
);

return {
Expand Down Expand Up @@ -3019,6 +3034,7 @@ function submitReport(expenseReport) {

const optimisticSubmittedReportAction = ReportUtils.buildOptimisticSubmittedReportAction(expenseReport.total, expenseReport.currency, expenseReport.reportID);
const parentReport = ReportUtils.getReport(expenseReport.parentReportID);
const isCurrentUserManager = currentUserPersonalDetails.accountID === expenseReport.managerID;

const optimisticData = [
{
Expand Down Expand Up @@ -3050,7 +3066,9 @@ function submitReport(expenseReport) {
key: `${ONYXKEYS.COLLECTION.REPORT}${parentReport.reportID}`,
value: {
...parentReport,
hasOutstandingChildRequest: false,

// In case its a manager who force submitted the report, they are the next user who needs to take an action
hasOutstandingChildRequest: isCurrentUserManager,
iouReportID: null,
},
},
Expand Down
5 changes: 4 additions & 1 deletion src/types/onyx/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ type Policy = {
/** Whether policy expense chats can be created and used on this policy. Enabled manually by CQ/JS snippet. Always true for free policies. */
isPolicyExpenseChatEnabled: boolean;

/** Whether the scheduled submit is enabled */
/** Whether the auto reporting is enabled */
autoReporting: boolean;

/** The scheduled submit frequency set up on the this policy */
autoReportingFrequency: ValueOf<typeof CONST.POLICY.AUTO_REPORTING_FREQUENCIES>;

/** Whether the scheduled submit is enabled */
isHarvestingEnabled: boolean;

/** The accountID of manager who the employee submits their expenses to on paid policies */
submitsTo?: number;

Expand Down
1 change: 1 addition & 0 deletions tests/utils/LHNTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ function getFakePolicy(id = 1, name = 'Workspace-Test-001') {
lastModified: 1697323926777105,
autoReporting: true,
autoReportingFrequency: 'immediate',
isHarvestingEnabled: true,
submitsTo: 123456,
defaultBillable: false,
disabledFields: {defaultBillable: true, reimbursable: false},
Expand Down
1 change: 1 addition & 0 deletions tests/utils/collections/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function createRandomPolicy(index: number): Policy {
autoReporting: randBoolean(),
isPolicyExpenseChatEnabled: randBoolean(),
autoReportingFrequency: rand(Object.values(CONST.POLICY.AUTO_REPORTING_FREQUENCIES)),
isHarvestingEnabled: randBoolean(),
submitsTo: index,
outputCurrency: randCurrencyCode(),
role: rand(Object.values(CONST.POLICY.ROLE)),
Expand Down
Loading