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

Replaces usage of reimburserEmail with achAccount.reimburser #39017

Merged
merged 18 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ const CONST = {
},
COLLECTION_KEYS: {
DESCRIPTION: 'description',
REIMBURSER_EMAIL: 'reimburserEmail',
REIMBURSER: 'reimburser',
REIMBURSEMENT_CHOICE: 'reimbursementChoice',
APPROVAL_MODE: 'approvalMode',
AUTOREPORTING: 'autoReporting',
Expand Down
4 changes: 2 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1336,8 +1336,8 @@ function isPayer(session: OnyxEntry<Session>, iouReport: OnyxEntry<Report>) {
const isManager = iouReport?.managerID === session?.accountID;
if (isPaidGroupPolicy(iouReport)) {
if (policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES) {
const isReimburser = session?.email === policy?.reimburserEmail;
return (!policy?.reimburserEmail || isReimburser) && (isApproved || isManager);
const isReimburser = session?.email === policy?.achAccount?.reimburser;
return (!policy?.achAccount?.reimburser || isReimburser) && (isApproved || isManager);
}
if (policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL) {
return isAdmin && (isApproved || isManager);
Expand Down
28 changes: 12 additions & 16 deletions src/libs/actions/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,18 +675,17 @@ function setWorkspaceApprovalMode(policyID: string, approver: string, approvalMo
API.write(WRITE_COMMANDS.SET_WORKSPACE_APPROVAL_MODE, params, {optimisticData, failureData, successData});
}

function setWorkspacePayer(policyID: string, reimburserEmail: string, reimburserAccountID: number) {
function setWorkspacePayer(policyID: string, reimburserEmail: string) {
const policy = getPolicy(policyID);

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
reimburserEmail,
reimburserAccountID,
errorFields: {reimburserEmail: null},
pendingFields: {reimburserEmail: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE},
achAccount: {reimburser: reimburserEmail},
errorFields: {reimburser: null},
pendingFields: {reimburser: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE},
},
},
];
Expand All @@ -696,8 +695,8 @@ function setWorkspacePayer(policyID: string, reimburserEmail: string, reimburser
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
errorFields: {reimburserEmail: null},
pendingFields: {reimburserEmail: null},
errorFields: {reimburser: null},
pendingFields: {reimburser: null},
},
},
];
Expand All @@ -707,10 +706,9 @@ function setWorkspacePayer(policyID: string, reimburserEmail: string, reimburser
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
reimburserEmail: policy.reimburserEmail ?? null,
reimburserAccountID: policy.reimburserAccountID ?? null,
errorFields: {reimburserEmail: ErrorUtils.getMicroSecondOnyxError('workflowsPayerPage.genericErrorMessage')},
pendingFields: {reimburserEmail: null},
achAccount: {reimburser: policy.achAccount?.reimburser ?? null},
errorFields: {reimburser: ErrorUtils.getMicroSecondOnyxError('workflowsPayerPage.genericErrorMessage')},
pendingFields: {reimburser: null},
},
},
];
Expand All @@ -724,7 +722,7 @@ function clearPolicyErrorField(policyID: string, fieldName: string) {
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {errorFields: {[fieldName]: null}});
}

function setWorkspaceReimbursement(policyID: string, reimbursementChoice: ValueOf<typeof CONST.POLICY.REIMBURSEMENT_CHOICES>, reimburserAccountID: number, reimburserEmail: string) {
function setWorkspaceReimbursement(policyID: string, reimbursementChoice: ValueOf<typeof CONST.POLICY.REIMBURSEMENT_CHOICES>, reimburserEmail: string) {
const policy = getPolicy(policyID);

const optimisticData: OnyxUpdate[] = [
Expand All @@ -733,8 +731,7 @@ function setWorkspaceReimbursement(policyID: string, reimbursementChoice: ValueO
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
reimbursementChoice,
reimburserAccountID,
reimburserEmail,
achAccount: {reimburser: reimburserEmail},
errorFields: {reimbursementChoice: null},
pendingFields: {reimbursementChoice: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE},
},
Expand All @@ -758,8 +755,7 @@ function setWorkspaceReimbursement(policyID: string, reimbursementChoice: ValueO
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
reimbursementChoice: policy.reimbursementChoice ?? null,
reimburserAccountID: policy.reimburserAccountID ?? null,
reimburserEmail: policy.reimburserEmail ?? null,
achAccount: {reimburser: policy.achAccount?.reimburser ?? null},
errorFields: {reimbursementChoice: ErrorUtils.getMicroSecondOnyxError('common.genericErrorMessage')},
pendingFields: {reimbursementChoice: null},
},
Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/WorkspaceInitialPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function WorkspaceInitialPage({policyDraft, policy: policyProp, policyMembers, r
icon: Expensicons.Workflows,
action: singleExecution(waitForNavigate(() => Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS.getRoute(policyID)))),
routeName: SCREENS.WORKSPACE.WORKFLOWS,
brickRoadIndicator: !isEmptyObject(policy?.errorFields?.reimburserEmail ?? {}) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined,
brickRoadIndicator: !isEmptyObject(policy?.errorFields?.reimburser ?? {}) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined,
});
}

Expand Down
36 changes: 13 additions & 23 deletions src/pages/workspace/workflows/WorkspaceWorkflowsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {Beta, Session} from '@src/types/onyx';
import type {Beta} from '@src/types/onyx';
import ToggleSettingOptionRow from './ToggleSettingsOptionRow';
import type {ToggleSettingOptionRowProps} from './ToggleSettingsOptionRow';
import {getAutoReportingFrequencyDisplayNames} from './WorkspaceAutoReportingFrequencyPage';
Expand All @@ -39,12 +39,10 @@ import type {AutoReportingFrequencyKey} from './WorkspaceAutoReportingFrequencyP
type WorkspaceWorkflowsPageOnyxProps = {
/** Beta features list */
betas: OnyxEntry<Beta[]>;
/** Policy details */
session: OnyxEntry<Session>;
};
type WorkspaceWorkflowsPageProps = WithPolicyProps & WorkspaceWorkflowsPageOnyxProps & StackScreenProps<WorkspacesCentralPaneNavigatorParamList, typeof SCREENS.WORKSPACE.WORKFLOWS>;

function WorkspaceWorkflowsPage({policy, betas, route, session}: WorkspaceWorkflowsPageProps) {
function WorkspaceWorkflowsPage({policy, betas, route}: WorkspaceWorkflowsPageProps) {
const {translate, preferredLocale} = useLocalize();
const styles = useThemeStyles();
const {isSmallScreenWidth} = useWindowDimensions();
Expand All @@ -55,11 +53,10 @@ function WorkspaceWorkflowsPage({policy, betas, route, session}: WorkspaceWorkfl
const canUseDelayedSubmission = Permissions.canUseWorkflowsDelayedSubmission(betas);
const [isCurrencyModalOpen, setIsCurrencyModalOpen] = useState(false);

const displayNameForAuthorizedPayer = useMemo(() => {
const personalDetails = PersonalDetailsUtils.getPersonalDetailsByIDs([policy?.reimburserAccountID ?? 0], session?.accountID ?? 0);
const displayNameFromReimburserEmail = PersonalDetailsUtils.getPersonalDetailByEmail(policy?.reimburserEmail ?? '')?.displayName ?? policy?.reimburserEmail;
return displayNameFromReimburserEmail ?? personalDetails?.[0]?.displayName;
}, [policy?.reimburserAccountID, policy?.reimburserEmail, session?.accountID]);
const displayNameForAuthorizedPayer = useMemo(
() => PersonalDetailsUtils.getPersonalDetailByEmail(policy?.achAccount?.reimburser ?? '')?.displayName ?? policy?.achAccount?.reimburser,
[policy?.achAccount?.reimburser],
);

const onPressAutoReportingFrequency = useCallback(() => Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS_AUTOREPORTING_FREQUENCY.getRoute(policy?.id ?? '')), [policy?.id]);

Expand Down Expand Up @@ -92,7 +89,7 @@ function WorkspaceWorkflowsPage({policy, betas, route, session}: WorkspaceWorkfl
if (accountNumber && bankDisplayName !== accountNumber) {
bankDisplayName += ` ${accountNumber.slice(-5)}`;
}
const hasReimburserEmailError = !!policy?.errorFields?.reimburserEmail;
const hasReimburserError = !!policy?.errorFields?.reimburser;
const hasApprovalError = !!policy?.errorFields?.approvalMode;
const hasDelayedSubmissionError = !!policy?.errorFields?.autoReporting;

Expand Down Expand Up @@ -174,11 +171,8 @@ function WorkspaceWorkflowsPage({policy, betas, route, session}: WorkspaceWorkfl
newReimbursementChoice = hasVBA ? CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES : CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL;
}

const newReimburserAccountID =
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
PersonalDetailsUtils.getPersonalDetailByEmail(policy?.reimburserEmail ?? '')?.accountID || policy?.reimburserAccountID || policy?.ownerAccountID;
const newReimburserEmail = PersonalDetailsUtils.getPersonalDetailsByIDs([newReimburserAccountID ?? 0], session?.accountID ?? 0)?.[0]?.login;
Policy.setWorkspaceReimbursement(policy?.id ?? '', newReimbursementChoice, newReimburserAccountID ?? 0, newReimburserEmail ?? '');
const newReimburserEmail = policy?.achAccount?.reimburser ?? policy?.owner;
Policy.setWorkspaceReimbursement(policy?.id ?? '', newReimbursementChoice, newReimburserEmail ?? '');
},
subMenuItems: (
<>
Expand All @@ -205,9 +199,9 @@ function WorkspaceWorkflowsPage({policy, betas, route, session}: WorkspaceWorkfl
/>
{hasVBA && policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES && (
<OfflineWithFeedback
pendingAction={policy?.pendingFields?.reimburserEmail}
errors={ErrorUtils.getLatestErrorField(policy ?? {}, CONST.POLICY.COLLECTION_KEYS.REIMBURSER_EMAIL)}
onClose={() => Policy.clearPolicyErrorField(policy?.id ?? '', CONST.POLICY.COLLECTION_KEYS.REIMBURSER_EMAIL)}
pendingAction={policy?.pendingFields?.reimburser}
errors={ErrorUtils.getLatestErrorField(policy ?? {}, CONST.POLICY.COLLECTION_KEYS.REIMBURSER)}
onClose={() => Policy.clearPolicyErrorField(policy?.id ?? '', CONST.POLICY.COLLECTION_KEYS.REIMBURSER)}
errorRowStyles={[styles.ml7]}
>
<MenuItem
Expand All @@ -219,7 +213,7 @@ function WorkspaceWorkflowsPage({policy, betas, route, session}: WorkspaceWorkfl
shouldShowRightIcon
wrapperStyle={containerStyle}
hoverAndPressStyle={[styles.mr0, styles.br2]}
brickRoadIndicator={hasReimburserEmailError ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
brickRoadIndicator={hasReimburserError ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
/>
</OfflineWithFeedback>
)}
Expand All @@ -243,7 +237,6 @@ function WorkspaceWorkflowsPage({policy, betas, route, session}: WorkspaceWorkfl
preferredLocale,
canUseDelayedSubmission,
displayNameForAuthorizedPayer,
session?.accountID,
isOffline,
isPolicyAdmin,
]);
Expand Down Expand Up @@ -321,8 +314,5 @@ export default withPolicy(
betas: {
key: ONYXKEYS.BETAS,
},
session: {
key: ONYXKEYS.SESSION,
},
})(WorkspaceWorkflowsPage),
);
24 changes: 6 additions & 18 deletions src/pages/workspace/workflows/WorkspaceWorkflowsPayerPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function WorkspaceWorkflowsPayerPage({route, policy, policyMembers, personalDeta
/>
);

const isAuthorizedPayer = policy?.reimburserEmail === details?.login ?? policy?.reimburserAccountID === accountID;
const isAuthorizedPayer = policy?.achAccount?.reimburser === details?.login;

const formattedMember = {
keyForList: accountIDKey,
Expand All @@ -106,28 +106,17 @@ function WorkspaceWorkflowsPayerPage({route, policy, policyMembers, personalDeta
},
],
errors: policyMember.errors,
pendingAction: policyMember.pendingAction ?? isAuthorizedPayer ? policy?.pendingFields?.reimburserEmail : null,
pendingAction: policyMember.pendingAction ?? isAuthorizedPayer ? policy?.pendingFields?.reimburser : null,
};

if (policy?.reimburserEmail === details?.login ?? policy?.reimburserAccountID === accountID) {
if (isAuthorizedPayer) {
authorizedPayerDetails.push(formattedMember);
} else {
policyAdminDetails.push(formattedMember);
}
});
return [policyAdminDetails, authorizedPayerDetails];
}, [
personalDetails,
policyMembers,
translate,
policy?.reimburserEmail,
isDeletedPolicyMember,
policy?.owner,
styles,
StyleUtils,
policy?.reimburserAccountID,
policy?.pendingFields?.reimburserEmail,
]);
}, [personalDetails, policyMembers, translate, policy?.achAccount?.reimburser, isDeletedPolicyMember, policy?.owner, styles, StyleUtils, policy?.pendingFields?.reimburser]);

const sections: MembersSection[] = useMemo(() => {
const sectionsArray: MembersSection[] = [];
Expand Down Expand Up @@ -169,13 +158,12 @@ function WorkspaceWorkflowsPayerPage({route, policy, policyMembers, personalDeta
const setPolicyAuthorizedPayer = (member: MemberOption) => {
const authorizedPayerEmail = personalDetails?.[member.accountID]?.login ?? '';

if (policy?.reimburserEmail === authorizedPayerEmail || policy?.reimbursementChoice !== CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES) {
if (policy?.achAccount?.reimburser === authorizedPayerEmail || policy?.reimbursementChoice !== CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES) {
Navigation.goBack();
return;
}

const authorizedPayerAccountID = member.accountID;
Policy.setWorkspacePayer(policy?.id ?? '', authorizedPayerEmail, authorizedPayerAccountID);
Policy.setWorkspacePayer(policy?.id ?? '', authorizedPayerEmail);
Navigation.goBack();
};

Expand Down
9 changes: 2 additions & 7 deletions src/types/onyx/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ type ACHAccount = {
routingNumber: string;
addressName: string;
bankName: string;
reimburser: string;
};

type AutoReportingOffset = number | ValueOf<typeof CONST.POLICY.AUTO_REPORTING_OFFSET>;
Expand Down Expand Up @@ -391,12 +392,6 @@ type Policy = OnyxCommon.OnyxValueWithOfflineFeedback<
/** Collection of tax rates attached to a policy */
taxRates?: TaxRatesWithDefault;

/** Email of the reimburser when reimbursement is set direct */
reimburserEmail?: string;

/** AccountID of the reimburser when reimbursement is set direct */
reimburserAccountID?: number;

/** ReportID of the admins room for this workspace */
chatReportIDAdmins?: number;

Expand Down Expand Up @@ -439,7 +434,7 @@ type Policy = OnyxCommon.OnyxValueWithOfflineFeedback<
/** Indicates if the Policy ownership change is failed */
isChangeOwnerFailed?: boolean;
} & Partial<PendingJoinRequestPolicy>,
'generalSettings' | 'addWorkspaceRoom'
'generalSettings' | 'addWorkspaceRoom' | keyof ACHAccount
>;

export default Policy;
Expand Down
Loading