Skip to content

Commit

Permalink
Merge pull request Expensify#22401 from allroundexperts/fix-22104
Browse files Browse the repository at this point in the history
feat: fix the logic for generating optimistic personal details
  • Loading branch information
Beamanator authored Jul 11, 2023
2 parents 38890e2 + a4ad0b4 commit e5b7e3a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function buildOnyxDataForMoneyRequest(
},
];

if (isNewChatReport && !_.isEmpty(optimisticPersonalDetailListAction)) {
if (!_.isEmpty(optimisticPersonalDetailListAction)) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
Expand Down Expand Up @@ -371,14 +371,16 @@ function requestMoney(report, amount, currency, payeeEmail, payeeAccountID, part
);

// Add optimistic personal details for participant
const optimisticPersonalDetailListAction = {
[payerAccountID]: {
accountID: payerAccountID,
avatar: UserUtils.getDefaultAvatarURL(payerAccountID),
displayName: participant.displayName || payerEmail,
login: participant.login,
},
};
const optimisticPersonalDetailListAction = isNewChatReport
? {
[payerAccountID]: {
accountID: payerAccountID,
avatar: UserUtils.getDefaultAvatarURL(payerAccountID),
displayName: participant.displayName || payerEmail,
login: participant.login,
},
}
: undefined;

let reportPreviewAction = isNewIOUReport ? null : ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID);
if (reportPreviewAction) {
Expand Down Expand Up @@ -589,11 +591,18 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco
// If we only have one participant and the request was initiated from the global create menu, i.e. !existingGroupChatReportID, the oneOnOneChatReport is the groupChatReport
let oneOnOneChatReport;
let isNewOneOnOneChatReport = false;
oneOnOneChatReport = !hasMultipleParticipants && !existingGroupChatReportID ? groupChatReport : ReportUtils.getChatByParticipants([accountID]);
let shouldCreateOptimisticPersonalDetails = false;

if (!oneOnOneChatReport) {
isNewOneOnOneChatReport = true;
oneOnOneChatReport = ReportUtils.buildOptimisticChatReport([accountID]);
// If this is a split between two people only and the function
// wasn't provided with an existing group chat report id
if (!hasMultipleParticipants && !existingGroupChatReportID) {
oneOnOneChatReport = groupChatReport;
shouldCreateOptimisticPersonalDetails = !existingGroupChatReport;
} else {
const existingChatReport = ReportUtils.getChatByParticipants([accountID]);
isNewOneOnOneChatReport = !existingChatReport;
shouldCreateOptimisticPersonalDetails = isNewOneOnOneChatReport;
oneOnOneChatReport = existingChatReport || ReportUtils.buildOptimisticChatReport([accountID]);
}

// STEP 2: Get existing IOU report and update its total OR build a new optimistic one
Expand Down Expand Up @@ -635,14 +644,16 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco
);

// Add optimistic personal details for new participants
const oneOnOnePersonalDetailListAction = {
[accountID]: {
accountID,
avatar: UserUtils.getDefaultAvatarURL(accountID),
displayName: participant.displayName || email,
login: participant.login,
},
};
const oneOnOnePersonalDetailListAction = shouldCreateOptimisticPersonalDetails
? {
[accountID]: {
accountID,
avatar: UserUtils.getDefaultAvatarURL(accountID),
displayName: participant.displayName || email,
login: participant.login,
},
}
: undefined;

let oneOnOneReportPreviewAction = ReportActionsUtils.getReportPreviewAction(oneOnOneChatReport.reportID, oneOnOneIOUReport.reportID);
if (oneOnOneReportPreviewAction) {
Expand Down
20 changes: 20 additions & 0 deletions tests/actions/IOUTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,26 @@ describe('actions/IOU', () => {
});
}),
)
.then(
() =>
new Promise((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
waitForCollectionCallback: true,
callback: (allPersonalDetails) => {
Onyx.disconnect(connectionID);
expect(allPersonalDetails).toMatchObject({
[VIT_ACCOUNT_ID]: {
accountID: VIT_ACCOUNT_ID,
displayName: VIT_EMAIL,
login: VIT_EMAIL,
},
});
resolve();
},
});
}),
)
.then(fetch.resume)
.then(
() =>
Expand Down

0 comments on commit e5b7e3a

Please sign in to comment.