Skip to content

Commit

Permalink
Merge pull request #33691 from software-mansion-labs/ts-migration/pol…
Browse files Browse the repository at this point in the history
…icy-action-lib

[TS migration] Migrate 'Policy.js' lib to TypeScript
  • Loading branch information
roryabraham authored Jan 17, 2024
2 parents 818567a + b9f4551 commit 7dfbddc
Show file tree
Hide file tree
Showing 7 changed files with 893 additions and 761 deletions.
4 changes: 2 additions & 2 deletions src/libs/DistanceRequestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import * as CurrencyUtils from './CurrencyUtils';
import * as PolicyUtils from './PolicyUtils';

type DefaultMileageRate = {
rate: number;
currency: string;
rate?: number;
currency?: string;
unit: Unit;
};

Expand Down
53 changes: 25 additions & 28 deletions src/libs/PersonalDetailsUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type {OnyxEntry} from 'react-native-onyx';
import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetails, PersonalDetailsList, PrivatePersonalDetails} from '@src/types/onyx';
import type {OnyxData} from '@src/types/onyx/Request';
import * as LocalePhoneNumber from './LocalePhoneNumber';
import * as Localize from './Localize';
import * as UserUtils from './UserUtils';
Expand Down Expand Up @@ -91,16 +92,15 @@ function getLoginsByAccountIDs(accountIDs: number[]): string[] {
* @param accountIDs Array of user accountIDs
* @returns Object with optimisticData, successData and failureData (object of personal details objects)
*/
function getNewPersonalDetailsOnyxData(logins: string[], accountIDs: number[]) {
const optimisticData: PersonalDetailsList = {};
const successData: PersonalDetailsList = {};
const failureData: PersonalDetailsList = {};
function getNewPersonalDetailsOnyxData(logins: string[], accountIDs: number[]): Required<Pick<OnyxData, 'optimisticData' | 'finallyData'>> {
const personalDetailsNew: PersonalDetailsList = {};
const personalDetailsCleanup: PersonalDetailsList = {};

logins.forEach((login, index) => {
const accountID = accountIDs[index];

if (allPersonalDetails && Object.keys(allPersonalDetails?.[accountID] ?? {}).length === 0) {
optimisticData[accountID] = {
personalDetailsNew[accountID] = {
login,
accountID,
avatar: UserUtils.getDefaultAvatarURL(accountID),
Expand All @@ -111,32 +111,29 @@ function getNewPersonalDetailsOnyxData(logins: string[], accountIDs: number[]) {
* Cleanup the optimistic user to ensure it does not permanently persist.
* This is done to prevent duplicate entries (upon success) since the BE will return other personal details with the correct account IDs.
*/
successData[accountID] = null;
personalDetailsCleanup[accountID] = null;
}
});

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: personalDetailsNew,
},
];

const finallyData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: personalDetailsCleanup,
},
];

return {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: optimisticData,
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: successData,
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: failureData,
},
],
optimisticData,
finallyData,
};
}

Expand Down
56 changes: 28 additions & 28 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {Message, ReportActionBase, ReportActions} from '@src/types/onyx/Rep
import type {Receipt, WaypointCollection} from '@src/types/onyx/Transaction';
import type DeepValueOf from '@src/types/utils/DeepValueOf';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject, isNotEmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type IconAsset from '@src/types/utils/IconAsset';
import * as CurrencyUtils from './CurrencyUtils';
import DateUtils from './DateUtils';
Expand Down Expand Up @@ -482,7 +482,7 @@ function getRootParentReport(report: OnyxEntry<Report> | undefined | EmptyObject
const parentReport = getReport(report?.parentReportID);

// Runs recursion to iterate a parent report
return getRootParentReport(isNotEmptyObject(parentReport) ? parentReport : null);
return getRootParentReport(!isEmptyObject(parentReport) ? parentReport : null);
}

function getPolicy(policyID: string): Policy | EmptyObject {
Expand Down Expand Up @@ -569,11 +569,11 @@ function isTaskReport(report: OnyxEntry<Report>): boolean {
* In this case, we have added the key to the report itself
*/
function isCanceledTaskReport(report: OnyxEntry<Report> | EmptyObject = {}, parentReportAction: OnyxEntry<ReportAction> | EmptyObject = {}): boolean {
if (isNotEmptyObject(parentReportAction) && (parentReportAction?.message?.[0]?.isDeletedParentAction ?? false)) {
if (!isEmptyObject(parentReportAction) && (parentReportAction?.message?.[0]?.isDeletedParentAction ?? false)) {
return true;
}

if (isNotEmptyObject(report) && report?.isDeletedParentAction) {
if (!isEmptyObject(report) && report?.isDeletedParentAction) {
return true;
}

Expand Down Expand Up @@ -1039,7 +1039,7 @@ function isExpenseRequest(report: OnyxEntry<Report>): boolean {
if (isThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`] ?? null;
return isExpenseReport(parentReport) && isNotEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
return isExpenseReport(parentReport) && !isEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
}
return false;
}
Expand All @@ -1052,7 +1052,7 @@ function isIOURequest(report: OnyxEntry<Report>): boolean {
if (isThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`] ?? null;
return isIOUReport(parentReport) && isNotEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
return isIOUReport(parentReport) && !isEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
}
return false;
}
Expand Down Expand Up @@ -1132,7 +1132,7 @@ function canDeleteReportAction(reportAction: OnyxEntry<ReportAction>, reportID:
// For now, users cannot delete split actions
const isSplitAction = reportAction?.originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.SPLIT;

if (isSplitAction || isSettled(String(reportAction?.originalMessage?.IOUReportID)) || (isNotEmptyObject(report) && isReportApproved(report))) {
if (isSplitAction || isSettled(String(reportAction?.originalMessage?.IOUReportID)) || (!isEmptyObject(report) && isReportApproved(report))) {
return false;
}

Expand All @@ -1151,7 +1151,7 @@ function canDeleteReportAction(reportAction: OnyxEntry<ReportAction>, reportID:
}

const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`];
const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN && isNotEmptyObject(report) && !isDM(report);
const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN && !isEmptyObject(report) && !isDM(report);

return isActionOwner || isAdmin;
}
Expand Down Expand Up @@ -1632,7 +1632,7 @@ function getLastVisibleMessage(reportID: string | undefined, actionsToMerge: Rep
const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(reportID ?? '', actionsToMerge);

// For Chat Report with deleted parent actions, let us fetch the correct message
if (ReportActionsUtils.isDeletedParentAction(lastVisibleAction) && isNotEmptyObject(report) && isChatReport(report)) {
if (ReportActionsUtils.isDeletedParentAction(lastVisibleAction) && !isEmptyObject(report) && isChatReport(report)) {
const lastMessageText = getDeletedParentActionMessageForChatReport(lastVisibleAction);
return {
lastMessageText,
Expand Down Expand Up @@ -1664,9 +1664,9 @@ function isUnreadWithMention(reportOrOption: OnyxEntry<Report> | OptionData): bo

/**
* Determines if the option requires action from the current user. This can happen when it:
- is unread and the user was mentioned in one of the unread comments
- is for an outstanding task waiting on the user
- has an outstanding child money request that is waiting for an action from the current user (e.g. pay, approve, add bank account)
- is unread and the user was mentioned in one of the unread comments
- is for an outstanding task waiting on the user
- has an outstanding child money request that is waiting for an action from the current user (e.g. pay, approve, add bank account)
*
* @param option (report or optionItem)
* @param parentReportAction (the report action the current report is a thread of)
Expand Down Expand Up @@ -1853,7 +1853,7 @@ function getTransactionDetails(transaction: OnyxEntry<Transaction>, createdDateF
const report = getReport(transaction?.reportID);
return {
created: TransactionUtils.getCreated(transaction, createdDateFormat),
amount: TransactionUtils.getAmount(transaction, isNotEmptyObject(report) && isExpenseReport(report)),
amount: TransactionUtils.getAmount(transaction, !isEmptyObject(report) && isExpenseReport(report)),
currency: TransactionUtils.getCurrency(transaction),
comment: TransactionUtils.getDescription(transaction),
merchant: TransactionUtils.getMerchant(transaction),
Expand Down Expand Up @@ -2044,7 +2044,7 @@ function getTransactionReportName(reportAction: OnyxEntry<ReportAction>): string
}

const transaction = TransactionUtils.getLinkedTransaction(reportAction);
if (!isNotEmptyObject(transaction)) {
if (isEmptyObject(transaction)) {
// Transaction data might be empty on app's first load, if so we fallback to Request
return Localize.translateLocal('iou.request');
}
Expand Down Expand Up @@ -2085,14 +2085,14 @@ function getReportPreviewMessage(
return reportActionMessage;
}

if (isNotEmptyObject(reportAction) && !isIOUReport(report) && reportAction && ReportActionsUtils.isSplitBillAction(reportAction)) {
if (!isEmptyObject(reportAction) && !isIOUReport(report) && reportAction && ReportActionsUtils.isSplitBillAction(reportAction)) {
// This covers group chats where the last action is a split bill action
const linkedTransaction = TransactionUtils.getLinkedTransaction(reportAction);
if (isEmptyObject(linkedTransaction)) {
return reportActionMessage;
}

if (isNotEmptyObject(linkedTransaction)) {
if (!isEmptyObject(linkedTransaction)) {
if (TransactionUtils.isReceiptBeingScanned(linkedTransaction)) {
return Localize.translateLocal('iou.receiptScanning');
}
Expand Down Expand Up @@ -2120,10 +2120,10 @@ function getReportPreviewMessage(
});
}

if (isNotEmptyObject(reportAction) && shouldConsiderReceiptBeingScanned && reportAction && ReportActionsUtils.isMoneyRequestAction(reportAction)) {
if (!isEmptyObject(reportAction) && shouldConsiderReceiptBeingScanned && reportAction && ReportActionsUtils.isMoneyRequestAction(reportAction)) {
const linkedTransaction = TransactionUtils.getLinkedTransaction(reportAction);

if (isNotEmptyObject(linkedTransaction) && TransactionUtils.hasReceipt(linkedTransaction) && TransactionUtils.isReceiptBeingScanned(linkedTransaction)) {
if (!isEmptyObject(linkedTransaction) && TransactionUtils.hasReceipt(linkedTransaction) && TransactionUtils.isReceiptBeingScanned(linkedTransaction)) {
return Localize.translateLocal('iou.receiptScanning');
}
}
Expand Down Expand Up @@ -2229,11 +2229,11 @@ function getReportName(report: OnyxEntry<Report>, policy: OnyxEntry<Policy> = nu
let formattedName: string | undefined;
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
if (isChatThread(report)) {
if (isNotEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction)) {
if (!isEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction)) {
return getTransactionReportName(parentReportAction);
}

const isAttachment = ReportActionsUtils.isReportActionAttachment(isNotEmptyObject(parentReportAction) ? parentReportAction : null);
const isAttachment = ReportActionsUtils.isReportActionAttachment(!isEmptyObject(parentReportAction) ? parentReportAction : null);
const parentReportActionMessage = (parentReportAction?.message?.[0]?.text ?? '').replace(/(\r\n|\n|\r)/gm, ' ');
if (isAttachment && parentReportActionMessage) {
return `[${Localize.translateLocal('common.attachment')}]`;
Expand Down Expand Up @@ -2498,11 +2498,11 @@ function updateOptimisticParentReportAction(parentReportAction: OnyxEntry<Report
*/
function getOptimisticDataForParentReportAction(reportID: string, lastVisibleActionCreated: string, type: string, parentReportID = '', parentReportActionID = ''): OnyxUpdate | EmptyObject {
const report = getReport(reportID);
if (!report || !isNotEmptyObject(report)) {
if (!report || !!isEmptyObject(report)) {
return {};
}
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
if (!parentReportAction || !isNotEmptyObject(parentReportAction)) {
if (!parentReportAction || !!isEmptyObject(parentReportAction)) {
return {};
}

Expand Down Expand Up @@ -2643,7 +2643,7 @@ function getIOUReportActionMessage(iouReportID: string, type: string, total: num
const report = getReport(iouReportID);
const amount =
type === CONST.IOU.REPORT_ACTION_TYPE.PAY
? CurrencyUtils.convertToDisplayString(getMoneyRequestReimbursableTotal(isNotEmptyObject(report) ? report : null), currency)
? CurrencyUtils.convertToDisplayString(getMoneyRequestReimbursableTotal(!isEmptyObject(report) ? report : null), currency)
: CurrencyUtils.convertToDisplayString(total, currency);

let paymentMethodMessage;
Expand Down Expand Up @@ -2971,7 +2971,7 @@ function buildOptimisticReportPreview(
childReportID: childReportID ?? iouReport?.reportID,
childMoneyRequestCount: 1,
childLastMoneyRequestComment: comment,
childRecentReceiptTransactionIDs: hasReceipt && isNotEmptyObject(transaction) ? {[transaction?.transactionID ?? '']: created} : undefined,
childRecentReceiptTransactionIDs: hasReceipt && !isEmptyObject(transaction) ? {[transaction?.transactionID ?? '']: created} : undefined,
whisperedToAccountIDs: isReceiptBeingScanned ? [currentUserAccountID ?? -1] : [],
};
}
Expand Down Expand Up @@ -3470,7 +3470,7 @@ function canAccessReport(report: OnyxEntry<Report>, policies: OnyxCollection<Pol
*/
function shouldHideReport(report: OnyxEntry<Report>, currentReportId: string): boolean {
const currentReport = getReport(currentReportId);
const parentReport = getParentReport(isNotEmptyObject(currentReport) ? currentReport : null);
const parentReport = getParentReport(!isEmptyObject(currentReport) ? currentReport : null);
const reportActions = ReportActionsUtils.getAllReportActions(report?.reportID ?? '');
const isChildReportHasComment = Object.values(reportActions ?? {})?.some((reportAction) => (reportAction?.childVisibleActionCount ?? 0) > 0);
return parentReport?.reportID !== report?.reportID && !isChildReportHasComment;
Expand Down Expand Up @@ -3644,7 +3644,7 @@ function canFlagReportAction(reportAction: OnyxEntry<ReportAction>, reportID: st
reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT &&
!ReportActionsUtils.isDeletedAction(reportAction) &&
!ReportActionsUtils.isCreatedTaskReportAction(reportAction) &&
isNotEmptyObject(report) &&
!isEmptyObject(report) &&
report &&
isAllowedToComment(report),
);
Expand Down Expand Up @@ -4283,7 +4283,7 @@ function getIOUReportActionDisplayMessage(reportAction: OnyxEntry<ReportAction>)
}

const transaction = TransactionUtils.getTransaction(originalMessage.IOUTransactionID ?? '');
const transactionDetails = getTransactionDetails(isNotEmptyObject(transaction) ? transaction : null);
const transactionDetails = getTransactionDetails(!isEmptyObject(transaction) ? transaction : null);
const formattedAmount = CurrencyUtils.convertToDisplayString(transactionDetails?.amount ?? 0, transactionDetails?.currency);
const isRequestSettled = isSettled(originalMessage.IOUReportID);
const isApproved = isReportApproved(iouReport);
Expand Down Expand Up @@ -4627,4 +4627,4 @@ export {
getChildReportNotificationPreference,
};

export type {ExpenseOriginalMessage, OptionData, OptimisticChatReport, OptimisticCreatedReportAction};
export type {ExpenseOriginalMessage, OptionData, OptimisticChatReport, OptimisticClosedReportAction, OptimisticCreatedReportAction};
Loading

0 comments on commit 7dfbddc

Please sign in to comment.