Skip to content
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
15 changes: 13 additions & 2 deletions src/components/KYCWall/BaseKYCWall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import React, {useCallback, useEffect, useImperativeHandle, useRef, useState} fr
import {Dimensions} from 'react-native';
import type {EmitterSubscription, View} from 'react-native';
import AddPaymentMethodMenu from '@components/AddPaymentMethodMenu';
import {usePersonalDetails} from '@components/OnyxListItemProvider';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useParentReportAction from '@hooks/useParentReportAction';
import {openPersonalBankAccountSetupView} from '@libs/actions/BankAccounts';
import {completePaymentOnboarding, savePreferredPaymentMethod} from '@libs/actions/IOU';
import {navigateToBankAccountRoute} from '@libs/actions/ReimbursementAccount';
Expand Down Expand Up @@ -63,7 +66,11 @@ function KYCWall({
const [reimbursementAccount] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {canBeMissing: true});

const {formatPhoneNumber} = useLocalize();

const currentUserDetails = useCurrentUserPersonalDetails();
const currentUserEmail = currentUserDetails.email ?? '';
const reportPreviewAction = useParentReportAction(iouReport);
const personalDetails = usePersonalDetails();
const employeeEmail = personalDetails?.[iouReport?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID]?.login ?? '';
const anchorRef = useRef<HTMLDivElement | View>(null);
const transferBalanceButtonRef = useRef<HTMLDivElement | View | null>(null);

Expand Down Expand Up @@ -155,7 +162,8 @@ function KYCWall({
return;
}

const {policyID, workspaceChatReportID, reportPreviewReportActionID, adminsChatReportID} = createWorkspaceFromIOUPayment(iouReport) ?? {};
const {policyID, workspaceChatReportID, reportPreviewReportActionID, adminsChatReportID} =
createWorkspaceFromIOUPayment(iouReport, reportPreviewAction, currentUserEmail, employeeEmail) ?? {};
if (policyID && iouReport?.policyID) {
savePreferredPaymentMethod(iouReport.policyID, policyID, CONST.LAST_PAYMENT_METHOD.IOU, lastPaymentMethod?.[iouReport?.policyID]);
}
Expand Down Expand Up @@ -200,6 +208,9 @@ function KYCWall({
introSelected,
formatPhoneNumber,
lastPaymentMethod,
reportPreviewAction,
currentUserEmail,
employeeEmail,
],
);

Expand Down
59 changes: 29 additions & 30 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
PolicyCategories,
PolicyCategory,
Report,
ReportAction,
ReportActions,
Request,
TaxRatesWithDefault,
Expand Down Expand Up @@ -214,7 +215,7 @@
};

const deprecatedAllPolicies: OnyxCollection<Policy> = {};
Onyx.connect({

Check warning on line 218 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
callback: (val, key) => {
if (!key) {
Expand All @@ -230,7 +231,7 @@
});

let deprecatedAllReportActions: OnyxCollection<ReportActions>;
Onyx.connect({

Check warning on line 234 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand All @@ -240,7 +241,7 @@

let deprecatedSessionEmail = '';
let deprecatedSessionAccountID = 0;
Onyx.connect({

Check warning on line 244 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (val) => {
deprecatedSessionEmail = val?.email ?? '';
Expand All @@ -249,25 +250,25 @@
});

let deprecatedAllPersonalDetails: OnyxEntry<PersonalDetailsList>;
Onyx.connect({

Check warning on line 253 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => (deprecatedAllPersonalDetails = val),
});

let deprecatedAllRecentlyUsedCurrencies: string[];
Onyx.connect({

Check warning on line 259 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.RECENTLY_USED_CURRENCIES,
callback: (val) => (deprecatedAllRecentlyUsedCurrencies = val ?? []),
});

let deprecatedActivePolicyID: OnyxEntry<string>;
Onyx.connect({

Check warning on line 265 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
callback: (value) => (deprecatedActivePolicyID = value),
});

let deprecatedIntroSelected: OnyxEntry<IntroSelected>;
Onyx.connect({

Check warning on line 271 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NVP_INTRO_SELECTED,
callback: (value) => (deprecatedIntroSelected = value),
});
Expand Down Expand Up @@ -3483,15 +3484,20 @@
* @returns policyID of the workspace we have created
*/
// eslint-disable-next-line rulesdir/no-call-actions-from-actions
function createWorkspaceFromIOUPayment(iouReport: OnyxEntry<Report>): WorkspaceFromIOUCreationData | undefined {
function createWorkspaceFromIOUPayment(
iouReport: OnyxEntry<Report>,
reportPreviewAction: ReportAction | undefined,
currentUserEmail: string,
iouReportOwnerEmail: string,
): WorkspaceFromIOUCreationData | undefined {
// This flow only works for IOU reports
if (!iouReport || !ReportUtils.isIOUReportUsingReport(iouReport)) {
return;
}

// Generate new variables for the policy
const policyID = generatePolicyID();
const workspaceName = generateDefaultWorkspaceName(deprecatedSessionEmail);
const workspaceName = generateDefaultWorkspaceName(currentUserEmail);
const employeeAccountID = iouReport?.ownerAccountID;
const {customUnits, customUnitID, customUnitRateID} = buildOptimisticDistanceRateCustomUnits(iouReport?.currency);
const oldPersonalPolicyID = iouReport?.policyID;
Expand All @@ -3513,18 +3519,16 @@
return;
}

const employeeEmail = deprecatedAllPersonalDetails?.[employeeAccountID]?.login ?? '';

// Create the expense chat for the employee whose IOU is being paid
const employeeWorkspaceChat = createPolicyExpenseChats(policyID, {[employeeEmail]: employeeAccountID}, true);
const employeeWorkspaceChat = createPolicyExpenseChats(policyID, {[iouReportOwnerEmail]: employeeAccountID}, true);
const newWorkspace = {
id: policyID,

// We are creating a collect policy in this case
type: CONST.POLICY.TYPE.TEAM,
name: workspaceName,
role: CONST.POLICY.ROLE.ADMIN,
owner: deprecatedSessionEmail,
owner: currentUserEmail,
ownerAccountID: deprecatedSessionAccountID,
isPolicyExpenseChatEnabled: true,

Expand All @@ -3534,7 +3538,7 @@
autoReporting: true,
autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.IMMEDIATE,
approvalMode: CONST.POLICY.APPROVAL_MODE.BASIC,
approver: deprecatedSessionEmail,
approver: currentUserEmail,
harvesting: {
enabled: false,
},
Expand All @@ -3548,17 +3552,17 @@
areConnectionsEnabled: false,
areExpensifyCardsEnabled: false,
employeeList: {
[deprecatedSessionEmail]: {
email: deprecatedSessionEmail,
submitsTo: deprecatedSessionEmail,
[currentUserEmail]: {
email: currentUserEmail,
submitsTo: currentUserEmail,
role: CONST.POLICY.ROLE.ADMIN,
errors: {},
},
...(employeeEmail
...(iouReportOwnerEmail
? {
[employeeEmail]: {
email: employeeEmail,
submitsTo: deprecatedSessionEmail,
[iouReportOwnerEmail]: {
email: iouReportOwnerEmail,
submitsTo: currentUserEmail,
role: CONST.POLICY.ROLE.USER,
errors: {},
},
Expand Down Expand Up @@ -3741,9 +3745,9 @@
// optimistically create the expense chat for them.
const memberData = {
accountID: Number(employeeAccountID),
email: employeeEmail,
workspaceChatReportID: employeeWorkspaceChat.reportCreationData[employeeEmail].reportID,
workspaceChatCreatedReportActionID: employeeWorkspaceChat.reportCreationData[employeeEmail].reportActionID,
email: iouReportOwnerEmail,
workspaceChatReportID: employeeWorkspaceChat.reportCreationData[iouReportOwnerEmail].reportID,
workspaceChatCreatedReportActionID: employeeWorkspaceChat.reportCreationData[iouReportOwnerEmail].reportActionID,
};

const oldChatReportID = iouReport?.chatReportID;
Expand Down Expand Up @@ -3800,21 +3804,16 @@
value: transactionFailureData,
});

// We need to move the report preview action from the DM to the expense chat.
const parentReport = deprecatedAllReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.parentReportID}`];
const parentReportActionID = iouReport?.parentReportActionID;
const reportPreview = iouReport?.parentReportID && parentReportActionID ? parentReport?.[parentReportActionID] : undefined;

if (reportPreview?.reportActionID) {
if (reportPreviewAction?.reportActionID) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldChatReportID}`,
value: {[reportPreview.reportActionID]: null},
value: {[reportPreviewAction.reportActionID]: null},
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldChatReportID}`,
value: {[reportPreview.reportActionID]: reportPreview},
value: {[reportPreviewAction.reportActionID]: reportPreviewAction},
});
}

Expand All @@ -3834,14 +3833,14 @@
},
});

if (reportPreview?.reportActionID) {
if (reportPreviewAction?.reportActionID) {
// Update the created timestamp of the report preview action to be after the expense chat created timestamp.
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${memberData.workspaceChatReportID}`,
value: {
[reportPreview.reportActionID]: {
...reportPreview,
[reportPreviewAction.reportActionID]: {
...reportPreviewAction,
message: [
{
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
Expand All @@ -3855,7 +3854,7 @@
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${memberData.workspaceChatReportID}`,
value: {[reportPreview.reportActionID]: null},
value: {[reportPreviewAction.reportActionID]: null},
});
}

Expand Down Expand Up @@ -3949,7 +3948,7 @@

API.write(WRITE_COMMANDS.CREATE_WORKSPACE_FROM_IOU_PAYMENT, params, {optimisticData, successData, failureData});

return {policyID, workspaceChatReportID: memberData.workspaceChatReportID, reportPreviewReportActionID: reportPreview?.reportActionID, adminsChatReportID};
return {policyID, workspaceChatReportID: memberData.workspaceChatReportID, reportPreviewReportActionID: reportPreviewAction?.reportActionID, adminsChatReportID};
}

function enablePolicyConnections(policyID: string, enabled: boolean) {
Expand Down
Loading