Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CP Staging] Fix Split Scan flow for Group Chats #39276

Merged
merged 14 commits into from
Apr 1, 2024
7 changes: 6 additions & 1 deletion src/components/RoomHeaderAvatars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ import Text from './Text';
type RoomHeaderAvatarsProps = {
icons: Icon[];
reportID: string;
isGroupChat?: boolean;
};

function RoomHeaderAvatars({icons, reportID}: RoomHeaderAvatarsProps) {
function RoomHeaderAvatars({icons, reportID, isGroupChat}: RoomHeaderAvatarsProps) {
const navigateToAvatarPage = (icon: Icon) => {
if (isGroupChat) {
return;
}

if (icon.type === CONST.ICON_TYPE_WORKSPACE) {
Navigation.navigate(ROUTES.REPORT_AVATAR.getRoute(reportID));
return;
Expand Down
1 change: 1 addition & 0 deletions src/libs/API/parameters/StartSplitBillParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type StartSplitBillParams = {
isFromGroupDM: boolean;
createdReportActionID?: string;
billable: boolean;
chatType?: string;
};

export default StartSplitBillParams;
62 changes: 32 additions & 30 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2248,6 +2248,35 @@ function trackExpense(
Report.notifyNewAction(activeReportID, payeeAccountID);
}

function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, participants: Participant[], participantAccountIDs: number[], currentUserAccountID: number) {
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
const existingChatReportID = existingSplitChatReportID || participants[0].reportID;
let existingSplitChatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingChatReportID}`];
if (!existingSplitChatReport) {
existingSplitChatReport = participants.length < 2 ? ReportUtils.getChatByParticipants(participantAccountIDs) : null;
}
let newChat: ReportUtils.OptimisticChatReport | EmptyObject = {};
const allParticipantsAccountIDs = [...participantAccountIDs, currentUserAccountID];
if (!existingSplitChatReport && participants.length > 1) {
newChat = ReportUtils.buildOptimisticChatReport(
allParticipantsAccountIDs,
'',
CONST.REPORT.CHAT_TYPE.GROUP,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
);
}
if (isEmptyObject(newChat)) {
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
newChat = ReportUtils.buildOptimisticChatReport(allParticipantsAccountIDs);
}
const splitChatReport = existingSplitChatReport ?? newChat;
return {splitChatReport, existingSplitChatReport};
}

/**
* Build the Onyx data and IOU split necessary for splitting a bill with 3+ users.
* 1. Build the optimistic Onyx data for the group chat, i.e. chatReport and iouReportAction creating the former if it doesn't yet exist.
Expand Down Expand Up @@ -2280,31 +2309,7 @@ function createSplitsAndOnyxData(
const currentUserEmailForIOUSplit = PhoneNumber.addSMSDomainIfPhoneNumber(currentUserLogin);
const participantAccountIDs = participants.map((participant) => Number(participant.accountID));

const existingChatReportID = existingSplitChatReportID || participants[0].reportID;
let existingSplitChatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingChatReportID}`];
if (!existingSplitChatReport) {
existingSplitChatReport = participants.length < 2 ? ReportUtils.getChatByParticipants(participantAccountIDs) : null;
}
let newChat: ReportUtils.OptimisticChatReport | EmptyObject = {};
const allParticipantsAccountIDs = [...participantAccountIDs, currentUserAccountID];
if (!existingSplitChatReport && participants.length > 1) {
newChat = ReportUtils.buildOptimisticChatReport(
allParticipantsAccountIDs,
'',
CONST.REPORT.CHAT_TYPE.GROUP,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
);
}
if (isEmptyObject(newChat)) {
newChat = ReportUtils.buildOptimisticChatReport(allParticipantsAccountIDs);
}
const splitChatReport = existingSplitChatReport ?? newChat;
const {splitChatReport, existingSplitChatReport} = getOrCreateOptimisticSplitChatReport(existingSplitChatReportID, participants, participantAccountIDs, currentUserAccountID);
const isOwnPolicyExpenseChat = !!splitChatReport.isOwnPolicyExpenseChat;

const splitTransaction = TransactionUtils.buildOptimisticTransaction(
Expand Down Expand Up @@ -2785,11 +2790,7 @@ function startSplitBill(
) {
const currentUserEmailForIOUSplit = PhoneNumber.addSMSDomainIfPhoneNumber(currentUserLogin);
const participantAccountIDs = participants.map((participant) => Number(participant.accountID));
const existingSplitChatReport =
existingSplitChatReportID || participants[0].reportID
? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingSplitChatReportID || participants[0].reportID}`]
: ReportUtils.getChatByParticipants(participantAccountIDs);
const splitChatReport = existingSplitChatReport ?? ReportUtils.buildOptimisticChatReport(participantAccountIDs);
const {splitChatReport, existingSplitChatReport} = getOrCreateOptimisticSplitChatReport(existingSplitChatReportID, participants, participantAccountIDs, currentUserAccountID);
const isOwnPolicyExpenseChat = !!splitChatReport.isOwnPolicyExpenseChat;

const {name: filename, source, state = CONST.IOU.RECEIPT_STATE.SCANREADY} = receipt;
Expand Down Expand Up @@ -3047,6 +3048,7 @@ function startSplitBill(
isFromGroupDM: !existingSplitChatReport,
billable,
...(existingSplitChatReport ? {} : {createdReportActionID: splitChatCreatedReportAction.reportActionID}),
chatType: splitChatReport?.chatType,
};

API.write(WRITE_COMMANDS.START_SPLIT_BILL, parameters, {optimisticData, successData, failureData});
Expand Down
1 change: 1 addition & 0 deletions src/pages/ReportDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ function ReportDetailsPage({policies, report, session, personalDetails}: ReportD
<RoomHeaderAvatars
icons={icons}
reportID={report?.reportID}
isGroupChat={ReportUtils.isGroupChat(report)}
/>
)}
</View>
Expand Down
Loading