diff --git a/src/CONST.ts b/src/CONST.ts index 286d3556bd74..49019170da66 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -1526,7 +1526,7 @@ const CONST = { }, COLLECTION_KEYS: { DESCRIPTION: 'description', - REIMBURSER_EMAIL: 'reimburserEmail', + REIMBURSER: 'reimburser', REIMBURSEMENT_CHOICE: 'reimbursementChoice', APPROVAL_MODE: 'approvalMode', AUTOREPORTING: 'autoReporting', diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index c0c90143a67a..98577e73766a 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1336,8 +1336,8 @@ function isPayer(session: OnyxEntry, iouReport: OnyxEntry) { 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); diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index b64b4d8643f5..08c493e0c0aa 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -675,7 +675,7 @@ 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[] = [ @@ -683,10 +683,9 @@ function setWorkspacePayer(policyID: string, reimburserEmail: string, reimburser 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}, }, }, ]; @@ -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}, }, }, ]; @@ -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}, }, }, ]; @@ -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, reimburserAccountID: number, reimburserEmail: string) { +function setWorkspaceReimbursement(policyID: string, reimbursementChoice: ValueOf, reimburserEmail: string) { const policy = getPolicy(policyID); const optimisticData: OnyxUpdate[] = [ @@ -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}, }, @@ -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}, }, diff --git a/src/pages/workspace/WorkspaceInitialPage.tsx b/src/pages/workspace/WorkspaceInitialPage.tsx index 0d3bb027b8e6..512b637f7f46 100644 --- a/src/pages/workspace/WorkspaceInitialPage.tsx +++ b/src/pages/workspace/WorkspaceInitialPage.tsx @@ -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, }); } diff --git a/src/pages/workspace/workflows/WorkspaceWorkflowsPage.tsx b/src/pages/workspace/workflows/WorkspaceWorkflowsPage.tsx index f810020fd733..35412d768bcc 100644 --- a/src/pages/workspace/workflows/WorkspaceWorkflowsPage.tsx +++ b/src/pages/workspace/workflows/WorkspaceWorkflowsPage.tsx @@ -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'; @@ -39,12 +39,10 @@ import type {AutoReportingFrequencyKey} from './WorkspaceAutoReportingFrequencyP type WorkspaceWorkflowsPageOnyxProps = { /** Beta features list */ betas: OnyxEntry; - /** Policy details */ - session: OnyxEntry; }; type WorkspaceWorkflowsPageProps = WithPolicyProps & WorkspaceWorkflowsPageOnyxProps & StackScreenProps; -function WorkspaceWorkflowsPage({policy, betas, route, session}: WorkspaceWorkflowsPageProps) { +function WorkspaceWorkflowsPage({policy, betas, route}: WorkspaceWorkflowsPageProps) { const {translate, preferredLocale} = useLocalize(); const styles = useThemeStyles(); const {isSmallScreenWidth} = useWindowDimensions(); @@ -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]); @@ -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; @@ -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: ( <> @@ -205,9 +199,9 @@ function WorkspaceWorkflowsPage({policy, betas, route, session}: WorkspaceWorkfl /> {hasVBA && policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES && ( 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]} > )} @@ -243,7 +237,6 @@ function WorkspaceWorkflowsPage({policy, betas, route, session}: WorkspaceWorkfl preferredLocale, canUseDelayedSubmission, displayNameForAuthorizedPayer, - session?.accountID, isOffline, isPolicyAdmin, ]); @@ -321,8 +314,5 @@ export default withPolicy( betas: { key: ONYXKEYS.BETAS, }, - session: { - key: ONYXKEYS.SESSION, - }, })(WorkspaceWorkflowsPage), ); diff --git a/src/pages/workspace/workflows/WorkspaceWorkflowsPayerPage.tsx b/src/pages/workspace/workflows/WorkspaceWorkflowsPayerPage.tsx index e96b19ce4442..f2d8cda48ef0 100644 --- a/src/pages/workspace/workflows/WorkspaceWorkflowsPayerPage.tsx +++ b/src/pages/workspace/workflows/WorkspaceWorkflowsPayerPage.tsx @@ -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, @@ -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[] = []; @@ -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(); }; diff --git a/src/types/onyx/Policy.ts b/src/types/onyx/Policy.ts index 247eb64f48e9..a06dd28f85e3 100644 --- a/src/types/onyx/Policy.ts +++ b/src/types/onyx/Policy.ts @@ -190,6 +190,7 @@ type ACHAccount = { routingNumber: string; addressName: string; bankName: string; + reimburser: string; }; type AutoReportingOffset = number | ValueOf; @@ -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; @@ -439,7 +434,7 @@ type Policy = OnyxCommon.OnyxValueWithOfflineFeedback< /** Indicates if the Policy ownership change is failed */ isChangeOwnerFailed?: boolean; } & Partial, - 'generalSettings' | 'addWorkspaceRoom' + 'generalSettings' | 'addWorkspaceRoom' | keyof ACHAccount >; export default Policy;