Skip to content

Commit

Permalink
Merge pull request Expensify#41698 from Expensify/yuwen-dismissAction
Browse files Browse the repository at this point in the history
[No QA] Add markAsCash action for dismissing the rter violation
  • Loading branch information
yuwenmemon authored May 10, 2024
2 parents b1356de + fd662c2 commit 59b4f1a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/libs/API/parameters/MarkAsCashParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type MarkAsCashParams = {
transactionID: string;
reportActionID: string;
};

export default MarkAsCashParams;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,4 @@ export type {default as LeavePolicyParams} from './LeavePolicyParams';
export type {default as OpenPolicyAccountingPageParams} from './OpenPolicyAccountingPageParams';
export type {default as SearchParams} from './Search';
export type {default as SendInvoiceParams} from './SendInvoiceParams';
export type {default as MarkAsCashParams} from './MarkAsCashParams';
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ const WRITE_COMMANDS = {
LEAVE_POLICY: 'LeavePolicy',
ACCEPT_SPOTNANA_TERMS: 'AcceptSpotnanaTerms',
SEND_INVOICE: 'SendInvoice',
MARK_AS_CASH: 'MarkAsCash',
} as const;

type WriteCommand = ValueOf<typeof WRITE_COMMANDS>;
Expand Down Expand Up @@ -432,6 +433,7 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.LEAVE_POLICY]: Parameters.LeavePolicyParams;
[WRITE_COMMANDS.ACCEPT_SPOTNANA_TERMS]: EmptyObject;
[WRITE_COMMANDS.SEND_INVOICE]: Parameters.SendInvoiceParams;
[WRITE_COMMANDS.MARK_AS_CASH]: Parameters.MarkAsCashParams;
};

const READ_COMMANDS = {
Expand Down
38 changes: 38 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type {
IOUMessage,
OriginalMessageActionName,
OriginalMessageCreated,
OriginalMessageDismissedViolation,
OriginalMessageReimbursementDequeued,
OriginalMessageRenamed,
OriginalMessageRoomChangeLog,
Expand Down Expand Up @@ -253,6 +254,11 @@ type OptimisticClosedReportAction = Pick<
'actionName' | 'actorAccountID' | 'automatic' | 'avatar' | 'created' | 'message' | 'originalMessage' | 'pendingAction' | 'person' | 'reportActionID' | 'shouldShow'
>;

type OptimisticDismissedViolationReportAction = Pick<
ReportAction,
'actionName' | 'actorAccountID' | 'avatar' | 'created' | 'message' | 'originalMessage' | 'person' | 'reportActionID' | 'shouldShow' | 'pendingAction'
>;

type OptimisticCreatedReportAction = OriginalMessageCreated &
Pick<ReportActionBase, 'actorAccountID' | 'automatic' | 'avatar' | 'created' | 'message' | 'person' | 'reportActionID' | 'shouldShow' | 'pendingAction'>;

Expand Down Expand Up @@ -4663,6 +4669,37 @@ function buildOptimisticClosedReportAction(emailClosingReport: string, policyNam
};
}

/**
* Returns an optimistic Dismissed Violation Report Action. Use the originalMessage customize this to the type of
* violation being dismissed.
*/
function buildOptimisticDismissedViolationReportAction(originalMessage: OriginalMessageDismissedViolation['originalMessage']): OptimisticDismissedViolationReportAction {
return {
actionName: CONST.REPORT.ACTIONS.TYPE.DISMISSED_VIOLATION,
actorAccountID: currentUserAccountID,
avatar: getCurrentUserAvatarOrDefault(),
created: DateUtils.getDBTime(),
message: [
{
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
style: 'normal',
text: ReportActionsUtils.getDismissedViolationMessageText(originalMessage),
},
],
originalMessage,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
person: [
{
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
style: 'strong',
text: getCurrentUserDisplayNameOrEmail(),
},
],
reportActionID: NumberUtils.rand64(),
shouldShow: true,
};
}

function buildOptimisticWorkspaceChats(policyID: string, policyName: string, expenseReportId?: string): OptimisticWorkspaceChats {
const announceChatData = buildOptimisticChatReport(
currentUserAccountID ? [currentUserAccountID] : [],
Expand Down Expand Up @@ -6541,6 +6578,7 @@ export {
buildOptimisticChatReport,
buildOptimisticClosedReportAction,
buildOptimisticCreatedReportAction,
buildOptimisticDismissedViolationReportAction,
buildOptimisticEditedTaskFieldReportAction,
buildOptimisticExpenseReport,
buildOptimisticGroupChatReport,
Expand Down
57 changes: 53 additions & 4 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import lodashHas from 'lodash/has';
import type {OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import * as API from '@libs/API';
import type {GetRouteParams} from '@libs/API/parameters';
import {READ_COMMANDS} from '@libs/API/types';
import type {GetRouteParams, MarkAsCashParams} from '@libs/API/parameters';
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as CollectionUtils from '@libs/CollectionUtils';
import {buildOptimisticDismissedViolationReportAction} from '@libs/ReportUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {RecentWaypoint, Transaction} from '@src/types/onyx';
import type {RecentWaypoint, ReportActions, Transaction, TransactionViolation} from '@src/types/onyx';
import type {OnyxData} from '@src/types/onyx/Request';
import type {WaypointCollection} from '@src/types/onyx/Transaction';

Expand Down Expand Up @@ -264,4 +265,52 @@ function clearError(transactionID: string) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {errors: null});
}

export {addStop, createInitialWaypoints, saveWaypoint, removeWaypoint, getRoute, updateWaypoints, clearError};
function markAsCash(transactionID: string, transactionThreadReportID: string, existingViolations: TransactionViolation[]) {
const optimisticReportAction = buildOptimisticDismissedViolationReportAction({
reason: 'manual',
violationName: CONST.VIOLATIONS.RTER,
});
const optimisticReportActions = {
[optimisticReportAction.reportActionID]: optimisticReportAction,
};
const onyxData: OnyxData = {
optimisticData: [
// Optimistically dismissing the violation, removing it from the list of violations
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`,
value: existingViolations.filter((violation: TransactionViolation) => violation.name !== CONST.VIOLATIONS.RTER),
},
// Optimistically adding the system message indicating we dismissed the violation
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: optimisticReportActions as ReportActions,
},
],
failureData: [
// Rolling back the dismissal of the violation
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`,
value: existingViolations,
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {
[optimisticReportAction.reportActionID]: null,
},
},
],
};

const parameters: MarkAsCashParams = {
transactionID,
reportActionID: optimisticReportAction.reportActionID,
};

return API.write(WRITE_COMMANDS.MARK_AS_CASH, parameters, onyxData);
}

export {addStop, createInitialWaypoints, saveWaypoint, removeWaypoint, getRoute, updateWaypoints, clearError, markAsCash};

0 comments on commit 59b4f1a

Please sign in to comment.