Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
dbaf171
Add rules engine types and merchant rule AST helpers
Krishna2323 Sep 4, 2026
9d523ff
Fix knip and jest typecheck findings
Krishna2323 Sep 4, 2026
94f6362
Keep expense default rules when approvals are disabled
Krishna2323 Sep 4, 2026
18b1291
Read and write merchant rules through the rules engine
Krishna2323 Sep 4, 2026
e65b234
Copy and import merchant rules through the rules collection
Krishna2323 Sep 4, 2026
0edfd62
Update tests for the rules engine migration
Krishna2323 Sep 4, 2026
135b8e3
Merge upstream/main, keeping the rules engine storage under the new c…
Krishna2323 Sep 6, 2026
87009d5
Fix tests missed by the rules engine migration
Krishna2323 Sep 7, 2026
2acfff5
Assert merchant rule optimistic state with the request paused
Krishna2323 Sep 7, 2026
836b5b6
Match existing patterns for the rules collection reads and fetch trig…
Krishna2323 Sep 7, 2026
6048090
Reduce rules collection reads to what each consumer uses
Krishna2323 Sep 7, 2026
30420f2
Derive merchant rule condition text from the filter tree
Krishna2323 Sep 7, 2026
efb92da
Merge branch 'main' into krishna2323/issue-100300
Krishna2323 Sep 8, 2026
76a721a
fix ESLint and tests.
Krishna2323 Sep 8, 2026
7e1a7a5
Merge main into krishna2323/issue-100300
Krishna2323 Sep 9, 2026
cbf5092
Refuse to open a merchant rule the editor would overwrite
Krishna2323 Sep 9, 2026
4fbbe46
Resolve merchant rule tax defaults against the live rate
Krishna2323 Sep 9, 2026
227835e
Fetch rules where the merchant rule count is consumed, and throttle G…
Krishna2323 Sep 9, 2026
780e516
Use getByTestId for the guard test's presence assertion
Krishna2323 Sep 9, 2026
9cd0ae4
Drop the GetRules freshness window
Krishna2323 Sep 9, 2026
a41f492
Fetch the rules collection through a prefetch hook
Krishna2323 Sep 9, 2026
04cc8f1
Render the merchant rule guard test inside a navigator
Krishna2323 Sep 9, 2026
413e7c1
Add hasRulesDataBeenFetched to the Onyx export safe keys bucket
Krishna2323 Sep 9, 2026
15a9a44
merge main and resolve conflicts.
Krishna2323 Sep 10, 2026
eee1393
Write merchant rules through SetPolicyCodingRule instead of SetRule
Krishna2323 Sep 10, 2026
440446a
Merge main, keep new rules_ collection reads/writes over legacy revam…
Krishna2323 Sep 14, 2026
6c90b49
Fix CI: repair merge fallout in PolicyUtils and a stale test call
Krishna2323 Sep 14, 2026
f24b9f1
Merge upstream/main into krishna2323/issue-100300
Krishna2323 Sep 23, 2026
bd27131
One-index rule triggers and actions in tests and docs
Krishna2323 Sep 23, 2026
a6809e7
Merge upstream/main into krishna2323/issue-100300
Krishna2323 Sep 23, 2026
08b1366
Address review: fix editor deep link, unify rule normalization
Krishna2323 Sep 23, 2026
4b4b7a6
Address review: extract RuleUtils, flatten rule triggers and actions
Krishna2323 Sep 23, 2026
2fca025
Say when the legacy write path goes away
Krishna2323 Sep 23, 2026
a9350e3
Address review: fix deep link race, consolidate rule helpers
Krishna2323 Sep 25, 2026
f32eb5c
Address review: say why the rules refetch bypasses the prefetch guard
Krishna2323 Sep 25, 2026
9ef372d
Fix ExportOnyxState buckets after the rules flag rename
Krishna2323 Sep 25, 2026
21e33c7
Merge upstream/main into krishna2323/issue-100300
Krishna2323 Sep 26, 2026
375b4ca
Merge branch 'main' into krishna2323/issue-100300
Krishna2323 Sep 29, 2026
5475dc2
Order expense default rule summaries by editor field order
Krishna2323 Sep 30, 2026
aca0005
Merge branch 'main' into krishna2323/issue-100300
Krishna2323 Sep 30, 2026
a683cef
fix CI check.
Krishna2323 Sep 30, 2026
51ab5ec
Merge branch 'main' into krishna2323/issue-100300
Krishna2323 Oct 1, 2026
65899a2
Fix the rules fetch lifecycle and let unsupported rules be deleted
Krishna2323 Oct 1, 2026
b0e37ce
Bound the rules in-flight flag and hold the editor while rules load
Krishna2323 Oct 1, 2026
8034769
Refetch rules on the Rules page after a cache clear
Krishna2323 Oct 1, 2026
163064e
Merge branch 'main' into krishna2323/issue-100300
Krishna2323 Oct 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/CONST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ const RESERVATION_TYPE = {
TRAIN: 'train',
} as const;

/** Every event that can fire a rule, across all rule kinds. Hoisted so `RULES` can list subsets of it. */
const RULE_TRIGGERS = {
REPORT_SUBMIT: 'ReportSubmit',
REPORT_APPROVE: 'ReportApprove',
CREATE_TRANSACTION: 'CreateTransaction',
} as const;

const EMAIL = {
ACCOUNTING: 'accounting@expensify.com',
ACCOUNTS_PAYABLE: 'accountspayable@expensify.com',
Expand Down Expand Up @@ -8758,16 +8765,33 @@ const CONST = {
RULES: {
SCOPE: {
POLICY: 'policy',
ACCOUNT: 'account',
},
TRIGGERS: RULE_TRIGGERS,
/** Every action a rule can perform, across all rule kinds. */
ACTIONS: {
FORWARD_TO: 'ForwardTo',
APPROVE_REPORT: 'ApproveReport',
SET: 'Set',
},
APPROVAL_WORKFLOW: {
TRIGGER: {
REPORT_SUBMIT: 'ReportSubmit',
REPORT_APPROVE: 'ReportApprove',
},
ACTION: {
FORWARD_TO: 'ForwardTo',
APPROVE_REPORT: 'ApproveReport',
},
/** A rule firing only on these is an approval workflow rather than an expense default. */
TRIGGERS: [RULE_TRIGGERS.REPORT_SUBMIT, RULE_TRIGGERS.REPORT_APPROVE],
},
EXPENSE_DEFAULT: {
Comment thread
Krishna2323 marked this conversation as resolved.
Resolved
/** Expense fields a `Set` action can write to */
FIELD: {
BILLABLE: 'billable',
CATEGORY: 'category',
COMMENT: 'comment',
MERCHANT: 'merchant',
REIMBURSABLE: 'reimbursable',
TAG: 'tag',
TAX: 'tax',
VENDOR_ID: 'vendorID',
},
/** Every expense default rule is created with the same priority, per the rules engine spec */
PRIORITY: 10000,
},
},

Expand Down
11 changes: 11 additions & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,15 @@ const ONYXKEYS = {
/** Set whether the search filters category data has loaded */
IS_SEARCH_FILTERS_CATEGORY_DATA_LOADED: 'isSearchFiltersCategoryDataLoaded',

/**
* Set once `GetRules` has answered, so screens that only consume the rules collection fetch it once.
* RAM only, so a new session fetches again rather than trusting a collection another device has since changed.
*/
RAM_ONLY_HAS_RULES_DATA_BEEN_FETCHED: 'hasRulesDataBeenFetched',

/** Set while a `GetRules` request is in flight, so screens can wait for it instead of reading an empty collection */
RAM_ONLY_IS_LOADING_RULES: 'isLoadingRules',

/** Set while search filter category data is loading */
RAM_ONLY_IS_LOADING_SEARCH_FILTERS_CATEGORY_DATA: 'isLoadingSearchFiltersCategoryData',

Expand Down Expand Up @@ -1751,6 +1760,8 @@ type OnyxValuesMapping = {
[ONYXKEYS.IS_LOADING_REPORT_DATA]: boolean;
[ONYXKEYS.IS_SEARCH_FILTERS_CARD_DATA_LOADED]: boolean;
[ONYXKEYS.IS_SEARCH_FILTERS_CATEGORY_DATA_LOADED]: boolean;
[ONYXKEYS.RAM_ONLY_HAS_RULES_DATA_BEEN_FETCHED]: boolean;
[ONYXKEYS.RAM_ONLY_IS_LOADING_RULES]: boolean;
[ONYXKEYS.RAM_ONLY_IS_LOADING_SEARCH_FILTERS_CATEGORY_DATA]: boolean;
[ONYXKEYS.IS_LOADING_SUBSCRIPTION_DATA]: boolean;
[ONYXKEYS.IS_PENDING_UPDATE_PERSONAL_KARMA]: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function WorkspaceExpenseDefaultsTableRow({item, rowIndex, shouldUseNarrowTableL
<Table.Row
interactive
rowIndex={rowIndex}
disabled={isDeleting}
disabled={isDeleting || !!item.disabled}
accessibilityLabel={accessibilityLabel}
sentryLabel={SECTION_SENTRY_LABELS[item.section]}
offlineWithFeedback={{
Expand Down
46 changes: 46 additions & 0 deletions src/hooks/useRulesPrefetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {getRules, resetRulesFetchState} from '@libs/actions/Policy/Rules';

import ONYXKEYS from '@src/ONYXKEYS';
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';

import {useEffect} from 'react';

import useNetwork from './useNetwork';
import useOnyx from './useOnyx';

/**
* Fetches the `rules_` collection for a screen that reads it without opening a workspace.
*
* Merchant rules used to arrive on the policy, so anything holding a policy could read them. They now
* live in their own collection that only `GetRules` populates, which leaves screens reached from the
* workspaces list or from Home reading an empty collection. Where that collection decides whether an
* option is offered at all, an empty read silently drops the rules rather than showing a stale count.
*
* `GetRules` takes no parameters and its response SETs the whole collection, so this fetches once per
* session rather than on every mount. The shared guards live here so callers cannot drift apart.
*
* Returns whether the collection is still on its way, which a screen that decides something from a rule's
* absence has to wait for. Onyx hydrates the collection as empty long before the response lands, so without
* this a deep linked screen reads "no such rule" for a rule that exists.
*/
function useRulesPrefetch(enabled = true): {areRulesLoading: boolean} {
// Reconnecting is the only signal that a fetch which never settled is not still on its way.
const {isOffline} = useNetwork({onReconnect: resetRulesFetchState});
const [hasBeenFetched, hasBeenFetchedResult] = useOnyx(ONYXKEYS.RAM_ONLY_HAS_RULES_DATA_BEEN_FETCHED);
const [isLoadingRules] = useOnyx(ONYXKEYS.RAM_ONLY_IS_LOADING_RULES);
const isFlagLoading = isLoadingOnyxValue(hasBeenFetchedResult);
const isFetchNeeded = enabled && !isFlagLoading && !isOffline && !hasBeenFetched && !isLoadingRules;

useEffect(() => {
if (!isFetchNeeded) {
return;
}
getRules();
}, [isFetchNeeded]);

// `isFetchNeeded` and `isFlagLoading` cover the renders before the effect has run, which are the ones a
// guard reading the collection would otherwise resolve against nothing. Offline there is nothing to wait for.
return {areRulesLoading: enabled && !isOffline && (isFlagLoading || isFetchNeeded || !!isLoadingRules)};
}

export default useRulesPrefetch;
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,7 @@ const READ_COMMANDS = {
OPEN_POLICY_REPORT_FIELDS_PAGE: 'OpenPolicyReportFieldsPage',
OPEN_POLICY_INVOICES_PAGE: 'OpenPolicyInvoicesPage',
OPEN_POLICY_RULES_PAGE: 'OpenPolicyRulesPage',
GET_RULES: 'GetRules',
OPEN_POLICY_EXPENSIFY_CARDS_PAGE: 'OpenPolicyExpensifyCardsPage',
OPEN_POLICY_TRAVEL_PAGE: 'OpenPolicyTravelPage',
GET_TRAVEL_BILLING_STATEMENT_PDF: 'GetTravelBillingStatementPDF',
Expand Down Expand Up @@ -1706,6 +1707,7 @@ type ReadCommandParameters = {
[READ_COMMANDS.OPEN_POLICY_REPORT_FIELDS_PAGE]: Parameters.OpenPolicyReportFieldsPageParams;
[READ_COMMANDS.OPEN_POLICY_INVOICES_PAGE]: Parameters.OpenPolicyReportFieldsPageParams;
[READ_COMMANDS.OPEN_POLICY_RULES_PAGE]: Parameters.OpenPolicyRulesPageParams;
[READ_COMMANDS.GET_RULES]: EmptyObject;
[READ_COMMANDS.OPEN_WORKSPACE_INVITE_PAGE]: Parameters.OpenWorkspaceInvitePageParams;
[READ_COMMANDS.OPEN_DRAFT_WORKSPACE_REQUEST]: Parameters.OpenDraftWorkspaceRequestParams;
[READ_COMMANDS.OPEN_DRAFT_PER_DIEM_EXPENSE]: Parameters.OpenDraftPerDiemExpenseParams;
Expand Down
Loading
Loading