Skip to content

Commit

Permalink
Merge pull request #28570 from fabioh8010/ts/lib/ReportActionsUtils
Browse files Browse the repository at this point in the history
[No QA] [TS migration] Migrate 'ReportActionsUtils.js' lib to TypeScript
  • Loading branch information
neil-marcellini committed Oct 18, 2023
2 parents a03ab51 + fabce75 commit 3b86da6
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 325 deletions.
2 changes: 1 addition & 1 deletion src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ type OnyxValues = {
[ONYXKEYS.COLLECTION.WORKSPACE_INVITE_MEMBERS_DRAFT]: Record<string, number>;
[ONYXKEYS.COLLECTION.REPORT]: OnyxTypes.Report;
[ONYXKEYS.COLLECTION.REPORT_METADATA]: OnyxTypes.ReportMetadata;
[ONYXKEYS.COLLECTION.REPORT_ACTIONS]: OnyxTypes.ReportAction;
[ONYXKEYS.COLLECTION.REPORT_ACTIONS]: OnyxTypes.ReportActions;
[ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS]: string;
[ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS]: OnyxTypes.ReportActionReactions;
[ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT]: string;
Expand Down
7 changes: 4 additions & 3 deletions src/languages/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {ReportAction} from '../types/onyx';
import en from './en';

type AddressLineParams = {
Expand Down Expand Up @@ -42,15 +43,15 @@ type LocalTimeParams = {
};

type EditActionParams = {
action: NonNullable<unknown>;
action: ReportAction | null;
};

type DeleteActionParams = {
action: NonNullable<unknown>;
action: ReportAction | null;
};

type DeleteConfirmationParams = {
action: NonNullable<unknown>;
action: ReportAction | null;
};

type BeginningOfChatHistoryDomainRoomPartOneParams = {
Expand Down
505 changes: 208 additions & 297 deletions src/libs/ReportActionsUtils.js → src/libs/ReportActionsUtils.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -2006,7 +2006,7 @@ function deleteMoneyRequest(transactionID, reportAction, isSingleTransactionView
}

updatedIOUReport.lastMessageText = iouReportLastMessageText;
updatedIOUReport.lastVisibleActionCreated = lastVisibleAction.created;
updatedIOUReport.lastVisibleActionCreated = lodashGet(lastVisibleAction, 'created');

updatedReportPreviewAction = {...reportPreviewAction};
const hasNonReimbursableTransactions = ReportUtils.hasNonReimbursableTransactions(iouReport);
Expand Down Expand Up @@ -2068,7 +2068,7 @@ function deleteMoneyRequest(transactionID, reportAction, isSingleTransactionView
hasOutstandingIOU: false,
iouReportID: null,
lastMessageText: ReportActionsUtils.getLastVisibleMessage(iouReport.chatReportID, {[reportPreviewAction.reportActionID]: null}).lastMessageText,
lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction(iouReport.chatReportID, {[reportPreviewAction.reportActionID]: null}).created,
lastVisibleActionCreated: lodashGet(ReportActionsUtils.getLastVisibleAction(iouReport.chatReportID, {[reportPreviewAction.reportActionID]: null}), 'created'),
},
},
]
Expand Down
10 changes: 5 additions & 5 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ function addActions(reportID, text = '', file) {
const {lastMessageText = '', lastMessageTranslationKey = ''} = ReportActionsUtils.getLastVisibleMessage(reportID);
if (lastMessageText || lastMessageTranslationKey) {
const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(reportID);
const lastVisibleActionCreated = lastVisibleAction.created;
const lastActorAccountID = lastVisibleAction.actorAccountID;
const lastVisibleActionCreated = lodashGet(lastVisibleAction, 'created');
const lastActorAccountID = lodashGet(lastVisibleAction, 'actorAccountID');
failureReport = {
lastMessageTranslationKey,
lastMessageText,
Expand Down Expand Up @@ -1057,8 +1057,8 @@ function deleteReportComment(reportID, reportAction) {
const {lastMessageText = '', lastMessageTranslationKey = ''} = ReportUtils.getLastVisibleMessage(originalReportID, optimisticReportActions);
if (lastMessageText || lastMessageTranslationKey) {
const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(originalReportID, optimisticReportActions);
const lastVisibleActionCreated = lastVisibleAction.created;
const lastActorAccountID = lastVisibleAction.actorAccountID;
const lastVisibleActionCreated = lodashGet(lastVisibleAction, 'created');
const lastActorAccountID = lodashGet(lastVisibleAction, 'actorAccountID');
optimisticReport = {
lastMessageTranslationKey,
lastMessageText,
Expand Down Expand Up @@ -1239,7 +1239,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) {
];

const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(originalReportID, optimisticReportActions);
if (reportActionID === lastVisibleAction.reportActionID) {
if (reportActionID === lodashGet(lastVisibleAction, 'reportActionID')) {
const lastMessageText = ReportUtils.formatReportLastMessageText(reportComment);
const optimisticReport = {
lastMessageTranslationKey: '',
Expand Down
9 changes: 2 additions & 7 deletions src/libs/isReportMessageAttachment.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import CONST from '../CONST';

type IsReportMessageAttachmentParams = {
text: string;
html: string;
translationKey: string;
};
import {Message} from '../types/onyx/ReportAction';

/**
* Check whether a report action is Attachment or not.
* Ignore messages containing [Attachment] as the main content. Attachments are actions with only text as [Attachment].
*
* @param reportActionMessage report action's message as text, html and translationKey
*/
export default function isReportMessageAttachment({text, html, translationKey}: IsReportMessageAttachmentParams): boolean {
export default function isReportMessageAttachment({text, html, translationKey}: Message): boolean {
if (!text || !html) {
return false;
}
Expand Down
54 changes: 48 additions & 6 deletions src/types/onyx/OriginalMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import {ValueOf} from 'type-fest';
import CONST from '../../CONST';
import DeepValueOf from '../utils/DeepValueOf';

type ActionName = DeepValueOf<typeof CONST.REPORT.ACTIONS.TYPE>;

type OriginalMessageApproved = {
actionName: typeof CONST.REPORT.ACTIONS.TYPE.APPROVED;
originalMessage: unknown;
};

type IOUDetails = {
amount: number;
comment?: string;
currency: string;
};

type OriginalMessageIOU = {
actionName: typeof CONST.REPORT.ACTIONS.TYPE.IOU;
Expand All @@ -8,23 +22,38 @@ type OriginalMessageIOU = {
IOUTransactionID?: string;

IOUReportID?: number;

/** Only exists when we are sending money */
IOUDetails?: IOUDetails;

amount: number;
comment?: string;
currency: string;
lastModified?: string;
participantAccountIDs?: number[];
type: string;
type: ValueOf<typeof CONST.IOU.REPORT_ACTION_TYPE>;
};
};

type FlagSeverityName = 'spam' | 'inconsiderate' | 'bullying' | 'intimidation' | 'harassment' | 'assault';
type FlagSeverityName = ValueOf<
Pick<
typeof CONST.MODERATION,
'FLAG_SEVERITY_SPAM' | 'FLAG_SEVERITY_INCONSIDERATE' | 'FLAG_SEVERITY_INTIMIDATION' | 'FLAG_SEVERITY_BULLYING' | 'FLAG_SEVERITY_HARASSMENT' | 'FLAG_SEVERITY_ASSAULT'
>
>;
type FlagSeverity = {
accountID: number;
timestamp: string;
};

type DecisionName = ValueOf<
Pick<
typeof CONST.MODERATION,
'MODERATOR_DECISION_PENDING' | 'MODERATOR_DECISION_PENDING_HIDE' | 'MODERATOR_DECISION_PENDING_REMOVE' | 'MODERATOR_DECISION_APPROVED' | 'MODERATOR_DECISION_HIDDEN'
>
>;
type Decision = {
decision: string;
decision: DecisionName;
timestamp: string;
};

Expand Down Expand Up @@ -62,7 +91,7 @@ type OriginalMessageClosed = {
actionName: typeof CONST.REPORT.ACTIONS.TYPE.CLOSED;
originalMessage: {
policyName: string;
reason: 'default' | 'accountClosed' | 'accountMerged' | 'removedPolicy' | 'policyDeleted';
reason: ValueOf<typeof CONST.REPORT.ARCHIVE_REASON>;
lastModified?: string;
};
};
Expand Down Expand Up @@ -128,7 +157,18 @@ type OriginalMessagePolicyTask = {
originalMessage: unknown;
};

type OriginalMessageModifiedExpense = {
actionName: typeof CONST.REPORT.ACTIONS.TYPE.MODIFIEDEXPENSE;
originalMessage: unknown;
};

type OriginalMessageReimbursementQueued = {
actionName: typeof CONST.REPORT.ACTIONS.TYPE.REIMBURSEMENTQUEUED;
originalMessage: unknown;
};

type OriginalMessage =
| OriginalMessageApproved
| OriginalMessageIOU
| OriginalMessageAddComment
| OriginalMessageSubmitted
Expand All @@ -138,7 +178,9 @@ type OriginalMessage =
| OriginalMessageChronosOOOList
| OriginalMessageReportPreview
| OriginalMessagePolicyChangeLog
| OriginalMessagePolicyTask;
| OriginalMessagePolicyTask
| OriginalMessageModifiedExpense
| OriginalMessageReimbursementQueued;

export default OriginalMessage;
export type {Reaction, ChronosOOOEvent};
export type {ChronosOOOEvent, Decision, Reaction, ActionName};
1 change: 1 addition & 0 deletions src/types/onyx/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Report = {
/** The report type */
type?: string;

lastMessageTranslationKey?: string;
parentReportID?: string;
parentReportActionID?: string;
isOptimisticReport?: boolean;
Expand Down
28 changes: 25 additions & 3 deletions src/types/onyx/ReportAction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import OriginalMessage, {Reaction} from './OriginalMessage';
import * as OnyxCommon from './OnyxCommon';
import OriginalMessage, {Decision, Reaction} from './OriginalMessage';

type Message = {
/** The type of the action item fragment. Used to render a corresponding component */
type: string;

/** The html content of the fragment. */
html: string;

/** The text content of the fragment. */
text: string;

Expand Down Expand Up @@ -37,6 +40,9 @@ type Message = {
isReversedTransaction?: boolean;
whisperedTo: number[];
reactions: Reaction[];

moderationDecision?: Decision;
translationKey?: string;
};

type Person = {
Expand All @@ -47,7 +53,10 @@ type Person = {

type ReportActionBase = {
/** The ID of the reportAction. It is the string representation of the a 64-bit integer. */
reportActionID?: string;
reportActionID: string;

/** @deprecated Used in old report actions before migration. Replaced by reportActionID. */
sequenceNumber?: number;

/** The ID of the previous reportAction on the report. It is a string represenation of a 64-bit integer (or null for CREATED actions). */
previousReportActionID?: string;
Expand All @@ -58,7 +67,7 @@ type ReportActionBase = {
person?: Person[];

/** ISO-formatted datetime */
created?: string;
created: string;

/** report action message */
message?: Message[];
Expand All @@ -83,10 +92,23 @@ type ReportActionBase = {
childCommenterCount?: number;
childLastVisibleActionCreated?: string;
childVisibleActionCount?: number;
childMoneyRequestCount?: number;

/** ISO-formatted datetime */
lastModified?: string;

pendingAction?: OnyxCommon.PendingAction;
delegateAccountID?: string;

/** Server side errors keyed by microtime */
errors?: OnyxCommon.Errors;

isAttachment?: boolean;
};

type ReportAction = ReportActionBase & OriginalMessage;

type ReportActions = Record<string, ReportAction>;

export default ReportAction;
export type {ReportActions, Message};
3 changes: 2 additions & 1 deletion src/types/onyx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import Policy from './Policy';
import PolicyCategory from './PolicyCategory';
import Report from './Report';
import ReportMetadata from './ReportMetadata';
import ReportAction from './ReportAction';
import ReportAction, {ReportActions} from './ReportAction';
import ReportActionReactions from './ReportActionReactions';
import SecurityGroup from './SecurityGroup';
import Transaction from './Transaction';
Expand Down Expand Up @@ -88,6 +88,7 @@ export type {
Report,
ReportMetadata,
ReportAction,
ReportActions,
ReportActionReactions,
SecurityGroup,
Transaction,
Expand Down

0 comments on commit 3b86da6

Please sign in to comment.