Skip to content

Commit

Permalink
Merge pull request #22778 from Expensify/jack-allowOptimisticAssignee
Browse files Browse the repository at this point in the history
  • Loading branch information
thienlnam authored Aug 11, 2023
2 parents f0a6977 + 62e2eb1 commit 9418b87
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 156 deletions.
119 changes: 119 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2352,6 +2352,7 @@ function shouldReportBeInOptionList(report, currentReportId, isInGSDMode, iouRep
if (
!report ||
!report.reportID ||
report.isHidden ||
(_.isEmpty(report.participantAccountIDs) && !isChatThread(report) && !isPublicRoom(report) && !isArchivedRoom(report) && !isMoneyRequestReport(report) && !isTaskReport(report))
) {
return false;
Expand Down Expand Up @@ -2903,6 +2904,123 @@ function shouldDisableRename(report, policy) {
return !_.keys(loginList).includes(policy.owner) && policy.role !== CONST.POLICY.ROLE.ADMIN;
}

/**
* Returns the onyx data needed for the task assignee chat
* @param {Number} accountID
* @param {String} assigneeEmail
* @param {Number} assigneeAccountID
* @param {String} taskReportID
* @param {String} assigneeChatReportID
* @param {String} parentReportID
* @param {String} title
* @param {Object} assigneeChatReport
* @returns {Object}
*/
function getTaskAssigneeChatOnyxData(accountID, assigneeEmail, assigneeAccountID, taskReportID, assigneeChatReportID, parentReportID, title, assigneeChatReport) {
// Set if we need to add a comment to the assignee chat notifying them that they have been assigned a task
let optimisticAssigneeAddComment;
// Set if this is a new chat that needs to be created for the assignee
let optimisticChatCreatedReportAction;
const currentTime = DateUtils.getDBTime();
const optimisticData = [];
const successData = [];
const failureData = [];

// You're able to assign a task to someone you haven't chatted with before - so we need to optimistically create the chat and the chat reportActions
// Only add the assignee chat report to onyx if we haven't already set it optimistically
if (assigneeChatReport.isOptimisticReport && lodashGet(assigneeChatReport, 'pendingFields.createChat') !== CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
optimisticChatCreatedReportAction = buildOptimisticCreatedReportAction(assigneeChatReportID);
optimisticData.push(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`,
value: {
pendingFields: {
createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
isHidden: false,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`,
value: {[optimisticChatCreatedReportAction.reportActionID]: optimisticChatCreatedReportAction},
},
);

successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`,
value: {
pendingFields: {
createChat: null,
},
isOptimisticReport: false,
},
});

failureData.push(
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`,
value: null,
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`,
value: {[optimisticChatCreatedReportAction.reportActionID]: {pendingAction: null}},
},
// If we failed, we want to remove the optimistic personal details as it was likely due to an invalid login
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[assigneeAccountID]: null,
},
},
);
}

// If you're choosing to share the task in the same DM as the assignee then we don't need to create another reportAction indicating that you've been assigned
if (assigneeChatReportID !== parentReportID) {
optimisticAssigneeAddComment = buildOptimisticTaskCommentReportAction(taskReportID, title, assigneeEmail, assigneeAccountID, `Assigned a task to you: ${title}`, parentReportID);

const lastAssigneeCommentText = formatReportLastMessageText(optimisticAssigneeAddComment.reportAction.message[0].text);
const optimisticAssigneeReport = {
lastVisibleActionCreated: currentTime,
lastMessageText: lastAssigneeCommentText,
lastActorAccountID: accountID,
lastReadTime: currentTime,
};

optimisticData.push(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`,
value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: optimisticAssigneeAddComment.reportAction},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`,
value: optimisticAssigneeReport,
},
);
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${assigneeChatReportID}`,
value: {[optimisticAssigneeAddComment.reportAction.reportActionID]: {pendingAction: null}},
});
}

return {
optimisticData,
successData,
failureData,
optimisticAssigneeAddComment,
optimisticChatCreatedReportAction,
};
}

export {
getReportParticipantsTitle,
isReportMessageAttachment,
Expand Down Expand Up @@ -3024,4 +3142,5 @@ export {
shouldDisableSettings,
shouldDisableRename,
hasSingleParticipant,
getTaskAssigneeChatOnyxData,
};
Loading

0 comments on commit 9418b87

Please sign in to comment.