Skip to content

Add merchant rule - Merchant rule not applied after member creating expense #82890

Closed

Description

@jponikarchuk

If you haven’t already, check out our contributing guidelines for onboarding and email contributors@expensify.com to request to join our Slack channel!


Version Number: v9.3.22-1
Reproducible in staging?: Yes
Reproducible in production?: N/A - new feature, doesn't exist in prod
If this was caught during regression testing, add the test name, ID and link from BrowserStack: #82438
Email or phone of affected tester (no customers): applausetester+piyomn@applause.expensifail.com
Issue reported by: Applause Internal Team
Bug source: Pull Request QA execution
Device used: iPhone 13 / iOS 18.7.1, Samsung Galaxy A52 / Android 14, Windows 11 / Chrome
App Component: Workspace Settings

Action Performed:

Preconditions:

  1. Prepare two accounts (Admin and Member that you have access to the email)
  2. As Admin create a workspace with:
  • At least 3-4 categories enabled (e.g., "Meals & Entertainment", "Travel", "Software", "Advertising")
  • At least 2-3 tags enabled (e.g., "Client A", "Internal", "Project X")
  • Reimbursable and billable settings available
  • Enable rules in more feature

Steps:

  1. (Admin) navigate to workspace -> rules -> add merchant rule
  2. Create a comprehensive rule:
  • Merchant: "TestCorp"
  • Match type: Contains
  • Category: "Software"
  • Tag: "Project X"
  • Reimbursable: No
  • Billable: Yes
  • Description: "TestCorp subscription"
  • Rename merchant to: "TestCorp Inc."
  • Preview matches to verify expected expenses
  1. (Member) create a new expense with merchant: "TestCorp Services"
  2. (Admin) Navigate back to the rules page and refresh the page

Expected Result:

  • in step 3, As member, after creating the new expense, all field of merchant rule that already configured before should be applied correctly
  • in step 4, As admin, after refreshing the rules page, the configured merchant rules should be remains listed

Actual Result:

  • in step 3, as member, after creating the new expense, all field of merchant rule that already configured before is not applied
  • in step 4, as admin, after refreshing the rules page, the configured merchant rules disappear

Workaround:

Unknown

Platforms:

  • Android: App
  • Android: mWeb Chrome
  • iOS: App
  • iOS: mWeb Safari
  • iOS: mWeb Chrome
  • Windows: Chrome
  • MacOS: Chrome / Safari

Screenshots/Videos

1.mp4

View all open jobs on GitHub

Issue OwnerCurrent Issue Owner: @luacmartins

Activity

  1. added
    DeployBlockerCashThis issue or pull request should block deployment
    BugSomething is broken. Auto assigns a BugZero manager.
    on Feb 19, 2026
  2. melvin-bot commented on Feb 19, 2026

    @melvin-bot

    You have been assigned to this deploy blocker because you recently merged this PR: #82438

  3. melvin-bot commented on Feb 19, 2026

    @melvin-bot

    💬 A slack conversation has been started in #expensify-open-source

  4. melvin-bot commented on Feb 19, 2026

    @melvin-bot

    Triggered auto assignment to @laurenreidexpensify (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details. Please add this bug to a GH project, as outlined in the SO.

  5. github-actions commented on Feb 19, 2026

    @github-actions
    Contributor

    👋 Friendly reminder that deploy blockers are time-sensitive ⏱ issues! Check out the open `StagingDeployCash` deploy checklist to see the list of PRs included in this release, then work quickly to do one of the following:

    1. Identify the pull request that introduced this issue and revert it.
    2. Find someone who can quickly fix the issue.
    3. Fix the issue yourself.
  6. OSBotify commented on Feb 19, 2026

    @OSBotify
    Contributor

    Investigation Summary

    Classification: Frontend bug
    Causing PR: #82438 - "Enable merchant rules v2" by @luacmartins (High confidence)
    Related Issues: #82241, #82228, #82245, #82225 (from previous enable attempt)

    Recommendation: REVERT

    PR #82438 should be reverted immediately. This PR simply removed the production gate for merchant rules, but the underlying feature has unresolved bugs that prevent it from working correctly.

    Labels: Keeping DeployBlockerCash (this blocks the App deploy). The DeployBlocker label is not present on this issue.

    Detailed Analysis

    Evidence

    This is a case of an existing bug being exposed by enabling a feature, rather than a new bug being introduced:

    1. First Enable Attempt: PR Enable merchant rules #81033 enabled merchant rules on Feb 10, 2026 by removing the same production gate
    2. Issues Discovered: Multiple bugs were found including:
    3. Revert: PR [CP Staging] Revert "Enable merchant rules" #82248 reverted Enable merchant rules #81033 on Feb 12, 2026 due to these issues
    4. Second Enable Attempt: PR Enable merchant rules v2 #82438 enables merchant rules again on Feb 18, 2026 with identical code change (just removing production gate)
    5. Current Issue: Add merchant rule - Merchant rule not applied after member creating expense #82890 reports the same problems - rules not being applied when members create expenses, and rules disappearing after page refresh

    Verification

    Confirmed that PR #82438 only modifies src/pages/workspace/rules/MerchantRulesSection.tsx:

    • Removes useEnvironment hook import
    • Removes the if (isProduction) return null; gate
    • No other logic changes

    The file shows merchant rules are stored in policy?.rules?.codingRules and managed through src/libs/actions/Policy/Rules.ts, but the underlying functionality for applying rules to new expenses and persisting them correctly was never fixed between the revert and re-enable.

    Root Cause

    The merchant rules feature has fundamental bugs in how rules are:

    1. Applied to newly created expenses (particularly for workspace members)
    2. Persisted and retrieved from the backend/Onyx store

    PR #82438 only removed the visibility gate without addressing these core issues. The feature should not have been re-enabled without fixing the underlying problems that caused the first revert.

  7. MelvinBot commented on Feb 19, 2026

    @MelvinBot
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    Two problems are reported after enabling merchant rules (PR #82438):

    1. Rules not applied to new expenses: When a member creates an expense with a merchant matching a configured rule (e.g., "TestCorp Services" should match "TestCorp" with "Contains" match type), the rule's configured fields (category, tag, reimbursable, billable, description) are NOT applied to the expense.
    2. Rules disappear on refresh: When the admin navigates back to the rules page and refreshes, the configured merchant rules vanish.

    What is the root cause of that problem?

    Both symptoms point to an incomplete backend implementation for the codingRules feature. Specifically:

    Symptom 1 (Rules not applied): There is no client-side logic in the App that applies codingRules to new expenses. The expense creation flow (RequestMoney/TrackExpense in src/libs/actions/IOU/) has zero references to codingRules. The merchant name is sent to the server in the API params, and the server is expected to match it against the policy's coding rules and apply the configured fields to the transaction — but this server-side matching logic either doesn't exist yet or has a bug.

    • Evidence: Grep for codingRule across src/libs/actions/IOU/ returns no matches. IOURequestStepMerchant.tsx simply stores the merchant string via setMoneyRequestMerchant() with no rule-matching logic. The shouldUpdateMatchingTransactions flag (sent at rule-save time) is the only mechanism that triggers server-side rule application, and it only applies to existing transactions, not newly created ones.

    Symptom 2 (Rules vanish on refresh): The openPolicyRulesPage function (src/libs/actions/Policy/Rules.ts:105-113) sends a READ_COMMANDS.OPEN_POLICY_RULES_PAGE request with no client-side onyxData — the backend entirely controls what data is returned. If the OpenPolicyRulesPage backend handler doesn't include codingRules in its response, they won't appear after refresh. Additionally, AccessOrNotFoundWrapper calls openWorkspace() when the policy is empty, and if that response uses Onyx.METHOD.SET (full replace) without codingRules, any previously stored rules would be wiped.

    • Evidence: The frontend write path (setPolicyCodingRule at src/libs/actions/Policy/Rules.ts:170-183) correctly stores rules at policy.rules.codingRules[ruleID] via Onyx.METHOD.MERGE, and the read path (MerchantRulesSection.tsx:85) reads from policy?.rules?.codingRules — the key structure is consistent. The successData pattern at Rules.ts:184-199 only clears pendingAction/errors, leaving the optimistic rule data intact. The frontend code is structurally sound and follows the same pattern used successfully by approvalRules and expenseRules.

    What changes do you think we should make in order to solve the problem?

    Since the root cause is server-side, the fix needs to be in the backend (Auth/Web-Expensify repos):

    1. For rules disappearing on refresh: Ensure the OpenPolicyRulesPage backend handler returns codingRules as part of the policy.rules object in its onyxData response. Verify that SetPolicyCodingRule backend handler actually persists the rule to the database. Also verify that OpenWorkspace/OpenApp responses either use merge instead of set for the policy key, or include codingRules in the response.

    2. For rules not applying to new expenses: Implement server-side logic in the RequestMoney/TrackExpense command handlers to match the expense merchant against the policy's codingRules (using the filter criteria: operator = eq for exact match or contains for partial match against the right field) and apply the rule's fields (category, tag, tax, comment, reimbursable, billable, merchant rename) to the transaction.

    What alternative solutions did you explore? (Optional)

    • Client-side rule application: We could add logic to the App to match merchant names against codingRules and pre-fill expense fields client-side. However, this would be redundant with server-side application (which is needed anyway for the shouldUpdateMatchingTransactions feature that already exists), could lead to data inconsistencies, and wouldn't work for expenses submitted via API or other clients. Server-side is the correct approach, consistent with how the older personal expenseRules (NVP_EXPENSE_RULES) are handled.
    Detailed Investigation

    Evidence Chain

    Frontend write path (confirmed correct):

    • MerchantRulePageBase.tsx:208 → calls setPolicyCodingRule(policyID, form, policy, ruleID, shouldUpdateMatchingTransactions)
    • Rules.ts:123-226 → builds ruleForOnyx (with nulls for cleared fields) and ruleForAPI (without nulls), writes optimistic data to policy.rules.codingRules[ruleID], calls API.write(WRITE_COMMANDS.SET_POLICY_CODING_RULE, {policyID, codingRuleID, codingRuleValue: JSON.stringify(ruleForAPI), shouldUpdateMatchingTransactions})
    • types.ts:265 → command maps to 'SetPolicyCodingRule'

    Frontend read path (confirmed correct):

    • PolicyRulesPage.tsx:26-32 → calls openPolicyRulesPage(policyID) on mount
    • Rules.ts:105-113 → sends API.read(READ_COMMANDS.OPEN_POLICY_RULES_PAGE, {policyID}) with no client-side onyxData
    • MerchantRulesSection.tsx:85 → reads policy?.rules?.codingRules

    Key Onyx pattern:

    • Optimistic: Onyx.METHOD.MERGE with rule data + pendingAction flag (Rules.ts:170-182)
    • Success: Onyx.METHOD.MERGE clearing only pendingAction and errors (Rules.ts:184-199)
    • Failure: Onyx.METHOD.MERGE reverting rule + setting error (Rules.ts:200-216)
    • This is identical to the working pattern used by approvalRules, expenseRules, and other policy settings

    Expense creation flow (no codingRule references):

    • IOURequestStepMerchant.tsx:108-141 → stores merchant via setMoneyRequestMerchant() (no rule lookup)
    • IOU/index.ts:1438-1440 → setMoneyRequestMerchant is a simple Onyx.merge with no rule logic
    • RequestMoneyParams.ts → contains merchant but no codingRuleID or coding rule fields

    Relevant Code

    • src/libs/actions/Policy/Rules.ts:105-226 — Core CRUD logic for coding rules
    • src/pages/workspace/rules/MerchantRulesSection.tsx:85-102 — UI reads policy?.rules?.codingRules
    • src/pages/workspace/rules/PolicyRulesPage.tsx:26-32 — Calls openPolicyRulesPage on mount
    • src/pages/workspace/AccessOrNotFoundWrapper.tsx:160-168 — Calls openWorkspace which may overwrite policy
    • src/types/onyx/Policy.ts:1958-1968 — Type definition for rules.codingRules
    • src/libs/actions/OnyxUpdates.ts:32-42 — Backend response handler (method determined by server)

    Next Steps for Contributor+ team: Reply with @MelvinBot implement this to create a draft PR, or @MelvinBot <your feedback> to refine this analysis.

  8. added and removed
    DeployBlockerCashThis issue or pull request should block deployment
    on Feb 19, 2026
  9. luacmartins commented on Feb 19, 2026

    @luacmartins
    Contributor

    I'm unable to reproduce this issue. The fact that the rule disappears means that it wasn't created and explains why it wasn't applied. That being said, I couldn't find logs for the account mentioned in the OP creating the rule.

    @jponikarchuk can you please check if you can still reproduce the OP and if so, share the email of the account trying to create the rule?

  10. jponikarchuk commented on Feb 20, 2026

    @jponikarchuk
    Author

    @luacmartins This issue is not reproduced using the latest staging build v9.3.23-3

  11. luacmartins commented on Feb 20, 2026

    @luacmartins
    Contributor

    Thanks! We can close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugSomething is broken. Auto assigns a BugZero manager.DailyKSv2Engineering

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions