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
14 changes: 13 additions & 1 deletion src/libs/actions/Policy/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,20 @@ function buildAddMembersToWorkspaceOnyxData(
const optimisticAnnounceChat = ReportUtils.buildOptimisticAnnounceChat(policyID, [...policyMemberAccountIDs, ...accountIDs], currentUser.accountID);
const announceRoomChat = optimisticAnnounceChat.announceChatData;

const doesPersonalDetailExistByAccountID: Record<number, boolean> = {};
for (const accountID of accountIDs) {
doesPersonalDetailExistByAccountID[accountID] = !!personalDetailsList?.[accountID];
}

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

const optimisticMembersState: OnyxCollectionInputValue<PolicyEmployee> = {};
const successMembersState: OnyxCollectionInputValue<PolicyEmployee> = {};
Expand Down
39 changes: 17 additions & 22 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ import type {
InvitedEmailsToAccountIDs,
LastPaymentMethod,
LastPaymentMethodType,
PersonalDetailsList,
Policy,
PolicyCategories,
PolicyCategory,
Expand Down Expand Up @@ -194,12 +193,10 @@ 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>;
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>;
doesPersonalDetailExistByAccountID: Record<number, boolean>;
};

type OptimisticCustomUnits = {
Expand Down Expand Up @@ -254,7 +251,9 @@ type BuildPolicyDataOptions = {
isAnnualSubscription?: boolean;
featuresMap?: Array<Pick<Feature, 'id' | 'enabled' | 'enabledByDefault' | 'requiresUpdate'>>;
lastUsedPaymentMethod?: LastPaymentMethodType;
adminParticipant?: Participant;
// `doesPersonalDetailExist` is threaded from the caller's useOnyx(PERSONAL_DETAILS_LIST) so createPolicyExpenseChats
// doesn't read the deprecated module-level copy. It's paired with the participant so it's required whenever an admin is added.
adminParticipant?: {participant: Participant; doesPersonalDetailExist: boolean};
hasOutstandingChildRequest?: boolean;
introSelected: OnyxEntry<IntroSelected>;
activePolicy: OnyxEntry<Policy>;
Expand Down Expand Up @@ -312,12 +311,6 @@ type SetWorkspaceApprovalModeAdditionalData = {
betas?: Beta[];
};

let deprecatedAllPersonalDetails: OnyxEntry<PersonalDetailsList>;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => (deprecatedAllPersonalDetails = val),
});

/**
* Stores in Onyx the policy ID of the last workspace that was accessed by the user
*/
Expand Down Expand Up @@ -1739,7 +1732,7 @@ function createPolicyExpenseChats({
createChat: null,
},
participants: {
[accountID]: (doesPersonalDetailExistByAccountID?.[accountID] ?? !!deprecatedAllPersonalDetails?.[accountID]) ? {} : null,
[accountID]: doesPersonalDetailExistByAccountID[accountID] ? {} : null,
},
},
},
Expand Down Expand Up @@ -2759,11 +2752,11 @@ function buildPolicyData(options: BuildPolicyDataOptions): OnyxData<BuildPolicyD
},
}
: {}),
...(adminParticipant?.login
...(adminParticipant?.participant.login
? {
[adminParticipant.login]: {
[adminParticipant.participant.login]: {
submitsTo: policyOwnerEmail || currentUserEmailParam,
email: adminParticipant.login,
email: adminParticipant.participant.login,
role: CONST.POLICY.ROLE.ADMIN,
errors: {},
},
Expand Down Expand Up @@ -3131,19 +3124,21 @@ function buildPolicyData(options: BuildPolicyDataOptions): OnyxData<BuildPolicyD
failureData.push(...failureCreateWorkspaceTaskData);
}

if (adminParticipant?.login) {
if (adminParticipant?.participant.login) {
const employeeWorkspaceChat = createPolicyExpenseChats({
policyID,
invitedEmailsToAccountIDs: {[adminParticipant.login]: adminParticipant.accountID ?? CONST.DEFAULT_NUMBER_ID},
invitedEmailsToAccountIDs: {[adminParticipant.participant.login]: adminParticipant.participant.accountID ?? CONST.DEFAULT_NUMBER_ID},
currentUser: {accountID: currentUserAccountIDParam},
// reportActionsList is intentionally omitted. See https://github.com/Expensify/App/pull/88312#issuecomment-4286942084
reportActionsList: undefined,
Comment thread
DylanDylann marked this conversation as resolved.
Resolved
hasOutstandingChildRequest,
doesPersonalDetailExistByAccountID: {[adminParticipant.participant.accountID ?? CONST.DEFAULT_NUMBER_ID]: adminParticipant.doesPersonalDetailExist},
});
params.memberData = JSON.stringify({
accountID: Number(adminParticipant.accountID),
email: adminParticipant.login,
workspaceChatReportID: employeeWorkspaceChat.reportCreationData[adminParticipant.login].reportID,
workspaceChatCreatedReportActionID: employeeWorkspaceChat.reportCreationData[adminParticipant.login].reportActionID,
accountID: Number(adminParticipant.participant.accountID),
email: adminParticipant.participant.login,
workspaceChatReportID: employeeWorkspaceChat.reportCreationData[adminParticipant.participant.login].reportID,
workspaceChatCreatedReportActionID: employeeWorkspaceChat.reportCreationData[adminParticipant.participant.login].reportActionID,
role: CONST.POLICY.ROLE.ADMIN,
});
optimisticData.push(...employeeWorkspaceChat.onyxOptimisticData);
Expand Down
5 changes: 4 additions & 1 deletion src/pages/iou/request/step/IOURequestStepUpgrade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ function IOURequestStepUpgrade({
]);

const participant = transaction?.participants?.[0];
const adminParticipant = isDistanceRateUpgrade && participant?.accountID ? getParticipantsOption(participant, personalDetails, translate) : undefined;
const adminParticipant =
isDistanceRateUpgrade && participant?.accountID
? {participant: getParticipantsOption(participant, personalDetails, translate), doesPersonalDetailExist: !!personalDetails?.[participant.accountID]}
: undefined;

const onUpgrade = () => {
if (isRestrictedPolicyCreation) {
Expand Down
33 changes: 33 additions & 0 deletions tests/actions/PolicyMemberTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,39 @@ describe('actions/PolicyMember', () => {
await mockFetch?.resume?.();
});

it('uses the explicit personal details list to decide new-member success-data participants instead of the deprecated copy', () => {
const policyID = '1';
const policy = {...createRandomPolicy(Number(policyID))};

const presentMemberEmail = 'present@example.com';
const presentMemberAccountID = 4321;
const absentMemberEmail = 'absent@example.com';
const absentMemberAccountID = 8765;

// The personal details list knows the "present" member but not the "absent" one.
const personalDetailsList = {[presentMemberAccountID]: {accountID: presentMemberAccountID, login: presentMemberEmail}};

const {membersChats} = Member.buildAddMembersToWorkspaceOnyxData(
{[presentMemberEmail]: presentMemberAccountID, [absentMemberEmail]: absentMemberAccountID},
policy,
[],
CONST.POLICY.ROLE.USER,
TestHelper.formatPhoneNumber,
personalDetailsList,
currentUser,
undefined,
);

const findSuccessReportUpdate = (email: string) => {
const reportID = membersChats.reportCreationData[email]?.reportID;
return membersChats.onyxSuccessData.find((data) => data.key === `${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
};

// The known member keeps their optimistic participant ({}); the unknown member's participant is removed (null).
expect(findSuccessReportUpdate(presentMemberEmail)).toHaveProperty(['value', 'participants', `${presentMemberAccountID}`], {});
expect(findSuccessReportUpdate(absentMemberEmail)).toHaveProperty(['value', 'participants', `${absentMemberAccountID}`], null);
});

it('Add new members with admin/scoped admin role to the #admins room', async () => {
// Given a policy and an #admins room
const policyID = '1';
Expand Down
13 changes: 9 additions & 4 deletions tests/actions/PolicyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ describe('actions/Policy', () => {
betas: undefined,
hasActiveAdminPolicies: false,
activePolicy: undefined,
adminParticipant: {login: adminEmail, accountID: adminAccountID},
adminParticipant: {participant: {login: adminEmail, accountID: adminAccountID}, doesPersonalDetailExist: true},
});
await waitForBatchedUpdates();

Expand Down Expand Up @@ -1876,7 +1876,7 @@ describe('actions/Policy', () => {
isSelfTourViewed: false,
betas: undefined,
hasActiveAdminPolicies: false,
adminParticipant: {login: adminEmail, accountID: adminAccountID},
adminParticipant: {participant: {login: adminEmail, accountID: adminAccountID}, doesPersonalDetailExist: true},
activePolicy: undefined,
});
await waitForBatchedUpdates();
Expand Down Expand Up @@ -1916,7 +1916,7 @@ describe('actions/Policy', () => {
isSelfTourViewed: false,
betas: undefined,
hasActiveAdminPolicies: false,
adminParticipant: {login: adminEmail, accountID: adminAccountID},
adminParticipant: {participant: {login: adminEmail, accountID: adminAccountID}, doesPersonalDetailExist: true},
activePolicy: undefined,
});
await waitForBatchedUpdates();
Expand All @@ -1932,7 +1932,7 @@ describe('actions/Policy', () => {
isSelfTourViewed: false,
betas: undefined,
hasActiveAdminPolicies: false,
adminParticipant: {login: adminEmail, accountID: adminAccountID},
adminParticipant: {participant: {login: adminEmail, accountID: adminAccountID}, doesPersonalDetailExist: true},
activePolicy: undefined,
});
await waitForBatchedUpdates();
Expand Down Expand Up @@ -5930,6 +5930,7 @@ describe('actions/Policy', () => {
invitedEmailsToAccountIDs: {[newMemberEmail]: newMemberAccountID},
currentUser: {accountID: ESH_ACCOUNT_ID},
reportActionsList: {},
doesPersonalDetailExistByAccountID: {},
});

// Then optimistic data should be generated
Expand Down Expand Up @@ -5993,6 +5994,7 @@ describe('actions/Policy', () => {
invitedEmailsToAccountIDs: {[existingMemberEmail]: existingMemberAccountID},
currentUser: {accountID: ESH_ACCOUNT_ID},
reportActionsList: {},
doesPersonalDetailExistByAccountID: {},
});

// Then the existing report should be reused (no new reportActionID)
Expand All @@ -6017,6 +6019,7 @@ describe('actions/Policy', () => {
invitedEmailsToAccountIDs: {[newMemberEmail]: newMemberAccountID},
currentUser: {accountID: customAccountID},
reportActionsList: {},
doesPersonalDetailExistByAccountID: {},
});

// Find the optimistic report data
Expand Down Expand Up @@ -6053,6 +6056,7 @@ describe('actions/Policy', () => {
policyID,
invitedEmailsToAccountIDs: {[newMemberEmail]: newMemberAccountID},
currentUser: {accountID: ESH_ACCOUNT_ID},
reportActionsList: {},
doesPersonalDetailExistByAccountID: {[newMemberAccountID]: true},
});
expect(getSuccessValue(existsResult)).toHaveProperty(participantPath, {});
Expand All @@ -6062,6 +6066,7 @@ describe('actions/Policy', () => {
policyID,
invitedEmailsToAccountIDs: {[newMemberEmail]: newMemberAccountID},
currentUser: {accountID: ESH_ACCOUNT_ID},
reportActionsList: {},
doesPersonalDetailExistByAccountID: {[newMemberAccountID]: false},
});
expect(getSuccessValue(missingResult)).toHaveProperty(participantPath, null);
Expand Down
Loading