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
13 changes: 7 additions & 6 deletions src/components/KYCWall/BaseKYCWall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,18 @@ function KYCWall({

const lastWorkspaceNumber = lastWorkspaceNumberSelector(policies, currentUserEmail);
const {policyID, workspaceChatReportID, adminsChatReportID} =
createWorkspaceFromIOUPayment(
createWorkspaceFromIOUPayment({
iouReport,
reportPreviewAction,
currentUserAccountID,
currentUserEmail,
employeeLogin ?? '',
localCurrency,
iouReportOwnerEmail: employeeLogin ?? '',
currentUserLocalCurrency: localCurrency,
lastWorkspaceNumber,
translate,
filteredReportActions,
) ?? {};
localeTranslate: translate,
reportActionsList: filteredReportActions,
doesEmployeePersonalDetailExist: doesSubmitterPersonalDetailExist ?? false,
}) ?? {};
if (policyID && iouReport?.policyID) {
savePreferredPaymentMethod(iouReport.policyID, policyID, CONST.LAST_PAYMENT_METHOD.IOU, lastPaymentMethod?.[iouReport?.policyID]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Policy/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ function buildAddMembersToWorkspaceOnyxData(
const announceRoomChat = optimisticAnnounceChat.announceChatData;

// create onyx data for policy expense chats for each new member
const membersChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, currentUser, reportActionsList, undefined, policyExpenseChatNotificationPreference);
const membersChats = createPolicyExpenseChats({policyID, invitedEmailsToAccountIDs, currentUser, reportActionsList, notificationPreference: policyExpenseChatNotificationPreference});

const optimisticMembersState: OnyxCollectionInputValue<PolicyEmployee> = {};
const successMembersState: OnyxCollectionInputValue<PolicyEmployee> = {};
Expand Down
85 changes: 58 additions & 27 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ type WorkspaceMembersChats = {
reportCreationData: ReportCreationData;
};

type CreatePolicyExpenseChatsParams = {
policyID: string;
invitedEmailsToAccountIDs: InvitedEmailsToAccountIDs;
currentUser: CurrentUser;
// TODO: Remove optional (?) once all is updated (https://github.com/Expensify/App/issues/66578)
reportActionsList?: OnyxCollection<ReportActions>;
hasOutstandingChildRequest?: boolean;
notificationPreference?: NotificationPreference;
// Remove optional (?) and the deprecatedAllPersonalDetails fallback once all callers pass this (https://github.com/Expensify/App/issues/66580)
doesPersonalDetailExistByAccountID?: Record<number, boolean>;
};

type OptimisticCustomUnits = {
customUnits: Record<string, CustomUnit>;
customUnitID: string;
Expand All @@ -195,6 +207,19 @@ type WorkspaceFromIOUCreationData = {
adminsChatReportID: string;
};

type CreateWorkspaceFromIOUPaymentOptions = {
iouReport: OnyxEntry<Report>;
reportPreviewAction: ReportAction | undefined;
currentUserAccountID: number;
currentUserEmail: string;
iouReportOwnerEmail: string;
currentUserLocalCurrency: string;
lastWorkspaceNumber: number | undefined;
localeTranslate: LocalizedTranslate;
reportActionsList: OnyxCollection<ReportActions>;
doesEmployeePersonalDetailExist: boolean;
};

type PolicyCashExpenseMode = ValueOf<typeof CONST.POLICY.CASH_EXPENSE_REIMBURSEMENT_CHOICES>;

type CurrentUser = {
Expand Down Expand Up @@ -1568,16 +1593,15 @@ function verifySetupIntentAndRequestPolicyOwnerChange(policyID: string, currentU
*
* @returns - object with onyxSuccessData, onyxOptimisticData, and optimisticReportIDs (map login to reportID)
*/
function createPolicyExpenseChats(
policyID: string,
invitedEmailsToAccountIDs: InvitedEmailsToAccountIDs,
currentUser: CurrentUser,
// TODO: Remove optional (?) once all is updated (https://github.com/Expensify/App/issues/66578)
reportActionsList?: OnyxCollection<ReportActions>,
function createPolicyExpenseChats({
policyID,
invitedEmailsToAccountIDs,
currentUser,
reportActionsList,
hasOutstandingChildRequest = false,
notificationPreference: NotificationPreference = CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
doesPersonalDetailExistByAccountID?: Record<number, boolean>,
): WorkspaceMembersChats {
notificationPreference = CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
doesPersonalDetailExistByAccountID,
}: CreatePolicyExpenseChatsParams): WorkspaceMembersChats {
const {accountID: currentUserAccountID, displayName: currentUserDisplayName, email: currentUserEmail, avatar: currentUserAvatar} = currentUser;
const workspaceMembersChats: WorkspaceMembersChats = {
onyxSuccessData: [],
Expand Down Expand Up @@ -3102,14 +3126,13 @@ function buildPolicyData(options: BuildPolicyDataOptions): OnyxData<BuildPolicyD
}

if (adminParticipant?.login) {
const employeeWorkspaceChat = createPolicyExpenseChats(
const employeeWorkspaceChat = createPolicyExpenseChats({
policyID,
{[adminParticipant.login]: adminParticipant.accountID ?? CONST.DEFAULT_NUMBER_ID},
{accountID: currentUserAccountIDParam},
// reportActionsList is intentionally undefined. See https://github.com/Expensify/App/pull/88312#issuecomment-4286942084
undefined,
invitedEmailsToAccountIDs: {[adminParticipant.login]: adminParticipant.accountID ?? CONST.DEFAULT_NUMBER_ID},
currentUser: {accountID: currentUserAccountIDParam},
// reportActionsList is intentionally omitted. See https://github.com/Expensify/App/pull/88312#issuecomment-4286942084
hasOutstandingChildRequest,
);
});
params.memberData = JSON.stringify({
accountID: Number(adminParticipant.accountID),
email: adminParticipant.login,
Expand Down Expand Up @@ -4177,17 +4200,18 @@ function dismissAddedWithPrimaryLoginMessages(policyID: string) {
* @returns policyID of the workspace we have created
*/

function createWorkspaceFromIOUPayment(
iouReport: OnyxEntry<Report>,
reportPreviewAction: ReportAction | undefined,
currentUserAccountID: number,
currentUserEmail: string,
iouReportOwnerEmail: string,
currentUserLocalCurrency: string,
lastWorkspaceNumber: number | undefined,
localeTranslate: LocalizedTranslate,
reportActionsList: OnyxCollection<ReportActions>,
): WorkspaceFromIOUCreationData | undefined {
function createWorkspaceFromIOUPayment({
iouReport,
reportPreviewAction,
currentUserAccountID,
currentUserEmail,
iouReportOwnerEmail,
currentUserLocalCurrency,
lastWorkspaceNumber,
localeTranslate,
reportActionsList,
doesEmployeePersonalDetailExist,
}: CreateWorkspaceFromIOUPaymentOptions): WorkspaceFromIOUCreationData | undefined {
// This flow only works for IOU reports
if (!iouReport || !ReportUtils.isIOUReportUsingReport(iouReport)) {
return;
Expand Down Expand Up @@ -4219,7 +4243,14 @@ function createWorkspaceFromIOUPayment(
}

// Create the expense chat for the employee whose IOU is being paid
const employeeWorkspaceChat = createPolicyExpenseChats(policyID, {[iouReportOwnerEmail]: employeeAccountID}, {accountID: currentUserAccountID}, reportActionsList, true);
const employeeWorkspaceChat = createPolicyExpenseChats({
policyID,
invitedEmailsToAccountIDs: {[iouReportOwnerEmail]: employeeAccountID},
currentUser: {accountID: currentUserAccountID},
reportActionsList,
hasOutstandingChildRequest: true,
doesPersonalDetailExistByAccountID: {[employeeAccountID]: doesEmployeePersonalDetailExist},
});
const newWorkspace = {
id: policyID,

Expand Down
8 changes: 6 additions & 2 deletions src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6740,8 +6740,12 @@ function moveIOUReportToPolicyAndInviteSubmitter(
const announceRoomMembers = buildRoomMembersOnyxData(CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE, policyID, [submitterAccountID]);

// Create policy expense chat for the submitter
const policyExpenseChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, {accountID: currentUserAccountID}, reportActions, undefined, undefined, {
[submitterAccountID]: doesSubmitterPersonalDetailExist,
const policyExpenseChats = createPolicyExpenseChats({
policyID,
invitedEmailsToAccountIDs,
currentUser: {accountID: currentUserAccountID},
reportActionsList: reportActions,
doesPersonalDetailExistByAccountID: {[submitterAccountID]: doesSubmitterPersonalDetailExist},
});
const optimisticPolicyExpenseChatReportID = policyExpenseChats.reportCreationData[submitterLogin].reportID;
const optimisticPolicyExpenseChatCreatedReportActionID = policyExpenseChats.reportCreationData[submitterLogin].reportActionID;
Expand Down
104 changes: 80 additions & 24 deletions tests/actions/PolicyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5853,7 +5853,7 @@ describe('actions/Policy', () => {
await waitForBatchedUpdates();

// When creating policy expense chats for a new member
const result = Policy.createPolicyExpenseChats(policyID, {[newMemberEmail]: newMemberAccountID}, {accountID: ESH_ACCOUNT_ID});
const result = Policy.createPolicyExpenseChats({policyID, invitedEmailsToAccountIDs: {[newMemberEmail]: newMemberAccountID}, currentUser: {accountID: ESH_ACCOUNT_ID}});

// Then optimistic data should be generated
expect(result.onyxOptimisticData.length).toBeGreaterThan(0);
Expand Down Expand Up @@ -5911,7 +5911,7 @@ describe('actions/Policy', () => {
await waitForBatchedUpdates();

// When creating policy expense chats for the existing member
const result = Policy.createPolicyExpenseChats(policyID, {[existingMemberEmail]: existingMemberAccountID}, {accountID: ESH_ACCOUNT_ID});
const result = Policy.createPolicyExpenseChats({policyID, invitedEmailsToAccountIDs: {[existingMemberEmail]: existingMemberAccountID}, currentUser: {accountID: ESH_ACCOUNT_ID}});

// Then the existing report should be reused (no new reportActionID)
const reportCreationEntry = result.reportCreationData[existingMemberEmail];
Expand All @@ -5930,7 +5930,7 @@ describe('actions/Policy', () => {
const newMemberAccountID = 200;
const customAccountID = 999;

const result = Policy.createPolicyExpenseChats(policyID, {[newMemberEmail]: newMemberAccountID}, {accountID: customAccountID});
const result = Policy.createPolicyExpenseChats({policyID, invitedEmailsToAccountIDs: {[newMemberEmail]: newMemberAccountID}, currentUser: {accountID: customAccountID}});

// Find the optimistic report data
const reportCreationEntry = result.reportCreationData[newMemberEmail];
Expand Down Expand Up @@ -5962,14 +5962,20 @@ describe('actions/Policy', () => {
const participantPath = ['participants', String(newMemberAccountID)];

// When the explicit map says the member exists, the success-data participant is kept ({}), beating the empty fallback
const existsResult = Policy.createPolicyExpenseChats(policyID, {[newMemberEmail]: newMemberAccountID}, {accountID: ESH_ACCOUNT_ID}, undefined, undefined, undefined, {
[newMemberAccountID]: true,
const existsResult = Policy.createPolicyExpenseChats({
policyID,
invitedEmailsToAccountIDs: {[newMemberEmail]: newMemberAccountID},
currentUser: {accountID: ESH_ACCOUNT_ID},
doesPersonalDetailExistByAccountID: {[newMemberAccountID]: true},
});
expect(getSuccessValue(existsResult)).toHaveProperty(participantPath, {});

// When the explicit map says the member does NOT exist, the success-data participant is removed (null)
const missingResult = Policy.createPolicyExpenseChats(policyID, {[newMemberEmail]: newMemberAccountID}, {accountID: ESH_ACCOUNT_ID}, undefined, undefined, undefined, {
[newMemberAccountID]: false,
const missingResult = Policy.createPolicyExpenseChats({
policyID,
invitedEmailsToAccountIDs: {[newMemberEmail]: newMemberAccountID},
currentUser: {accountID: ESH_ACCOUNT_ID},
doesPersonalDetailExistByAccountID: {[newMemberAccountID]: false},
});
expect(getSuccessValue(missingResult)).toHaveProperty(participantPath, null);
});
Expand Down Expand Up @@ -7167,8 +7173,19 @@ describe('actions/Policy', () => {
const apiWriteSpy = jest.spyOn(require('@libs/API'), 'write').mockImplementation(() => Promise.resolve());
const isIOUReportUsingReportSpy = jest.spyOn(ReportUtils, 'isIOUReportUsingReport').mockReturnValue(true);

const mockTranslate = ((key: string) => key) as unknown as Parameters<typeof Policy.createWorkspaceFromIOUPayment>[7];
Policy.createWorkspaceFromIOUPayment(iouReport, undefined, customAccountID, customEmail, iouReportOwnerEmail, CONST.CURRENCY.USD, undefined, mockTranslate, {});
const mockTranslate = ((key: string) => key) as unknown as Parameters<typeof Policy.createWorkspaceFromIOUPayment>[0]['localeTranslate'];
Policy.createWorkspaceFromIOUPayment({
iouReport,
reportPreviewAction: undefined,
currentUserAccountID: customAccountID,
currentUserEmail: customEmail,
iouReportOwnerEmail,
currentUserLocalCurrency: CONST.CURRENCY.USD,
lastWorkspaceNumber: undefined,
localeTranslate: mockTranslate,
reportActionsList: {},
doesEmployeePersonalDetailExist: false,
});
await waitForBatchedUpdates();

const writeOptions = apiWriteSpy.mock.calls.at(0)?.at(2) as {
Expand Down Expand Up @@ -7198,8 +7215,19 @@ describe('actions/Policy', () => {
type: CONST.REPORT.TYPE.EXPENSE,
};

const mockTranslate = ((key: string) => key) as unknown as Parameters<typeof Policy.createWorkspaceFromIOUPayment>[7];
const result = Policy.createWorkspaceFromIOUPayment(nonIOUReport, undefined, ESH_ACCOUNT_ID, ESH_EMAIL, 'owner@example.com', CONST.CURRENCY.USD, undefined, mockTranslate, {});
const mockTranslate = ((key: string) => key) as unknown as Parameters<typeof Policy.createWorkspaceFromIOUPayment>[0]['localeTranslate'];
const result = Policy.createWorkspaceFromIOUPayment({
iouReport: nonIOUReport,
reportPreviewAction: undefined,
currentUserAccountID: ESH_ACCOUNT_ID,
currentUserEmail: ESH_EMAIL,
iouReportOwnerEmail: 'owner@example.com',
currentUserLocalCurrency: CONST.CURRENCY.USD,
lastWorkspaceNumber: undefined,
localeTranslate: mockTranslate,
reportActionsList: {},
doesEmployeePersonalDetailExist: false,
});
expect(result).toBeUndefined();
});

Expand Down Expand Up @@ -7245,24 +7273,30 @@ describe('actions/Policy', () => {
const isIOUReportUsingReportSpy = jest.spyOn(ReportUtils, 'isIOUReportUsingReport').mockReturnValue(true);
const apiWriteSpy = jest.spyOn(require('@libs/API'), 'write').mockImplementation(() => Promise.resolve());

const mockTranslate = ((key: string) => key) as unknown as Parameters<typeof Policy.createWorkspaceFromIOUPayment>[7];
const result = Policy.createWorkspaceFromIOUPayment(
const mockTranslate = ((key: string) => key) as unknown as Parameters<typeof Policy.createWorkspaceFromIOUPayment>[0]['localeTranslate'];
const result = Policy.createWorkspaceFromIOUPayment({
iouReport,
undefined,
ESH_ACCOUNT_ID,
ESH_EMAIL,
reportPreviewAction: undefined,
currentUserAccountID: ESH_ACCOUNT_ID,
currentUserEmail: ESH_EMAIL,
iouReportOwnerEmail,
CONST.CURRENCY.USD,
undefined,
mockTranslate,
currentUserLocalCurrency: CONST.CURRENCY.USD,
lastWorkspaceNumber: undefined,
localeTranslate: mockTranslate,
reportActionsList,
);
doesEmployeePersonalDetailExist: true,
});

// Verify the function returns a valid result (not undefined)
expect(result).toBeDefined();
expect(result?.policyID).toBeDefined();
expect(result?.workspaceChatReportID).toBeDefined();

// The explicit doesEmployeePersonalDetailExist=true is forwarded to createPolicyExpenseChats, so the
// employee's success-data participant is kept ({}) even though the employee is absent from PERSONAL_DETAILS_LIST.
const writeArgs = apiWriteSpy.mock.calls.at(0)?.at(2);
expect(writeArgs).toHaveProperty('successData', expect.arrayContaining([expect.objectContaining({value: expect.objectContaining({participants: {[employeeAccountID]: {}}})})]));

apiWriteSpy.mockRestore();
isIOUReportUsingReportSpy.mockRestore();
});
Expand Down Expand Up @@ -7293,8 +7327,19 @@ describe('actions/Policy', () => {
const isIOUReportUsingReportSpy = jest.spyOn(ReportUtils, 'isIOUReportUsingReport').mockReturnValue(true);
const apiWriteSpy = jest.spyOn(require('@libs/API'), 'write').mockImplementation(() => Promise.resolve());

const mockTranslate = ((key: string) => key) as unknown as Parameters<typeof Policy.createWorkspaceFromIOUPayment>[7];
Policy.createWorkspaceFromIOUPayment(iouReport, undefined, ESH_ACCOUNT_ID, ESH_EMAIL, iouReportOwnerEmail, CONST.CURRENCY.USD, undefined, mockTranslate, {});
const mockTranslate = ((key: string) => key) as unknown as Parameters<typeof Policy.createWorkspaceFromIOUPayment>[0]['localeTranslate'];
Policy.createWorkspaceFromIOUPayment({
iouReport,
reportPreviewAction: undefined,
currentUserAccountID: ESH_ACCOUNT_ID,
currentUserEmail: ESH_EMAIL,
iouReportOwnerEmail,
currentUserLocalCurrency: CONST.CURRENCY.USD,
lastWorkspaceNumber: undefined,
localeTranslate: mockTranslate,
reportActionsList: {},
doesEmployeePersonalDetailExist: false,
});
await waitForBatchedUpdates();

const writeOptions = apiWriteSpy.mock.calls.at(0)?.at(2) as {
Expand Down Expand Up @@ -7355,8 +7400,19 @@ describe('actions/Policy', () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction);
await waitForBatchedUpdates();

const mockTranslate = ((key: string) => key) as unknown as Parameters<typeof Policy.createWorkspaceFromIOUPayment>[7];
Policy.createWorkspaceFromIOUPayment(iouReport, undefined, ESH_ACCOUNT_ID, ESH_EMAIL, iouReportOwnerEmail, CONST.CURRENCY.USD, undefined, mockTranslate, {});
const mockTranslate = ((key: string) => key) as unknown as Parameters<typeof Policy.createWorkspaceFromIOUPayment>[0]['localeTranslate'];
Policy.createWorkspaceFromIOUPayment({
iouReport,
reportPreviewAction: undefined,
currentUserAccountID: ESH_ACCOUNT_ID,
currentUserEmail: ESH_EMAIL,
iouReportOwnerEmail,
currentUserLocalCurrency: CONST.CURRENCY.USD,
lastWorkspaceNumber: undefined,
localeTranslate: mockTranslate,
reportActionsList: {},
doesEmployeePersonalDetailExist: false,
});
await waitForBatchedUpdates();

// Optimistic merge: read the stored transaction from Onyx
Expand Down
Loading