Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: date handlebar functions #3749

Merged
merged 3 commits into from
Oct 28, 2024

Conversation

UnderKoen
Copy link
Member

Fixxes an issue that {{day today}} wasn't working. As reported in #3606 by @IsThisThingStillOn

@actual-github-bot actual-github-bot bot changed the title fix: date handlebar functions [WIP] fix: date handlebar functions Oct 28, 2024
Copy link

netlify bot commented Oct 28, 2024

Deploy Preview for actualbudget ready!

Name Link
🔨 Latest commit 9a76988
🔍 Latest deploy log https://app.netlify.com/sites/actualbudget/deploys/671fc972314e7100088253a9
😎 Deploy Preview https://deploy-preview-3749.demo.actualbudget.org
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

github-actions bot commented Oct 28, 2024

Bundle Stats — desktop-client

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
9 5.32 MB 0%

Changeset

No files were changed

View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

No assets were bigger

Smaller

No assets were smaller

Unchanged

Asset File Size % Changed
static/js/indexeddb-main-thread-worker-e59fee74.js 13.5 kB 0%
static/js/usePreviewTransactions.js 1.64 kB 0%
static/js/resize-observer.js 18.37 kB 0%
static/js/BackgroundImage.js 122.29 kB 0%
static/js/AppliedFilters.js 21.3 kB 0%
static/js/narrow.js 82.57 kB 0%
static/js/wide.js 232.16 kB 0%
static/js/ReportRouter.js 1.5 MB 0%
static/js/index.js 3.33 MB 0%

Copy link
Contributor

github-actions bot commented Oct 28, 2024

Bundle Stats — loot-core

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
1 1.27 MB → 1.27 MB (+15 B) +0.00%
Changeset
File Δ Size
packages/loot-core/src/server/accounts/rules.ts 📈 +37 B (+0.11%) 31.86 kB → 31.9 kB
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

Asset File Size % Changed
kcab.worker.js 1.27 MB → 1.27 MB (+15 B) +0.00%

Smaller

No assets were smaller

Unchanged

No assets were unchanged

@UnderKoen UnderKoen changed the title [WIP] fix: date handlebar functions fix: date handlebar functions Oct 28, 2024
Copy link
Contributor

coderabbitai bot commented Oct 28, 2024

Walkthrough

The changes introduced in this pull request enhance the functionality and testing of several components within the rules engine. In the rules.test.ts file, new tests have been added for the Condition, Action, Rule, and RuleIndexer classes, focusing on handling undefined values in date helper functions such as day, month, year, and format. These functions now return an empty string when provided with undefined inputs. Additionally, the Action class tests have been expanded to check for invalid field names and empty account values, ensuring proper error handling. The Rule class tests have been improved to validate rule execution with various conditions and actions, including the handling of split actions. The RuleIndexer class has also been tested for its indexing capabilities, confirming that it correctly retrieves applicable rules. In rules.ts, the signatures of the date helper functions have been updated to accept optional parameters, allowing for more flexible handling of undefined values without causing errors.

Possibly related PRs

  • feat(rules): templating actions #3305: This PR enhances the Action class with templating functionality, which is directly related to the changes in the main PR that also involve testing and expanding the templating capabilities of actions.
  • Clarify logic to generate splits from rules #3641: This PR adds functionality for handling split actions in rules, which is relevant to the main PR's enhancements to the Rule class tests, ensuring that rules are evaluated correctly based on specified conditions and actions.
  • refactor: fill in basic types in rules.ts #3365: This PR refactors the rules.ts file to improve type safety, which aligns with the main PR's focus on enhancing the robustness of the rules engine and ensuring correct behavior in various conditions.
  • Fix Parse for Date Range #3735: Although primarily focused on date parsing, this PR's changes to date handling could relate to the main PR's enhancements in handling undefined values in date helpers, ensuring consistent behavior across the system.

Suggested labels

sparkles: Merged

Suggested reviewers

  • MikesGlitch
  • youngcw

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
packages/loot-core/src/server/accounts/rules.ts (1)

79-82: LGTM! The changes address the PR objective.

The updated function signatures now properly handle undefined values, fixing the {{day today}} handlebar function issue. The short-circuit evaluation using && ensures that undefined inputs return undefined instead of throwing errors.

Consider adding validation for invalid date formats to provide more helpful error messages. Here's a suggested implementation:

-    day: (date?: string) => date && format(date, 'd'),
-    month: (date?: string) => date && format(date, 'M'),
-    year: (date?: string) => date && format(date, 'yyyy'),
-    format: (date?: string, f?: string) => date && f && format(date, f),
+    day: (date?: string) => {
+      if (!date) return '';
+      try {
+        return format(date, 'd');
+      } catch (e) {
+        return '';
+      }
+    },
+    month: (date?: string) => {
+      if (!date) return '';
+      try {
+        return format(date, 'M');
+      } catch (e) {
+        return '';
+      }
+    },
+    year: (date?: string) => {
+      if (!date) return '';
+      try {
+        return format(date, 'yyyy');
+      } catch (e) {
+        return '';
+      }
+    },
+    format: (date?: string, f?: string) => {
+      if (!date || !f) return '';
+      try {
+        return format(date, f);
+      } catch (e) {
+        return '';
+      }
+    },

This implementation:

  1. Explicitly handles undefined values by returning an empty string
  2. Catches and handles invalid date format errors gracefully
  3. Maintains consistent behavior across all date helper functions
packages/loot-core/src/server/accounts/rules.test.ts (1)

409-412: Consider adding tests for additional edge cases.

While the current tests cover undefined inputs, consider adding test cases for:

  • null inputs
  • Invalid date strings (e.g., "not-a-date")
  • Empty strings

Example:

      testHelper('{{day undefined}}', '');
      testHelper('{{month undefined}}', '');
      testHelper('{{year undefined}}', '');
      testHelper('{{format undefined undefined}}', '');
+     testHelper('{{day null}}', '');
+     testHelper('{{month "not-a-date"}}', '');
+     testHelper('{{year ""}}', '');
+     testHelper('{{format null "yyyy-MM-dd"}}', '');
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b349edd and 9a76988.

⛔ Files ignored due to path filters (1)
  • upcoming-release-notes/3749.md is excluded by !**/*.md
📒 Files selected for processing (2)
  • packages/loot-core/src/server/accounts/rules.test.ts (1 hunks)
  • packages/loot-core/src/server/accounts/rules.ts (1 hunks)
🧰 Additional context used
📓 Learnings (1)
packages/loot-core/src/server/accounts/rules.test.ts (1)
Learnt from: jfdoming
PR: actualbudget/actual#3641
File: packages/loot-core/src/server/accounts/rules.test.ts:524-536
Timestamp: 2024-10-12T19:13:25.005Z
Learning: In `packages/loot-core/src/server/accounts/rules.test.ts`, prefer explicit action definitions over refactoring similar actions into loops or helper functions, even when actions are similar.
🔇 Additional comments (1)
packages/loot-core/src/server/accounts/rules.test.ts (1)

409-412: LGTM! Tests align with PR objectives.

The added tests verify that date helper functions handle undefined inputs gracefully by returning empty strings. This directly addresses the issue with {{day today}} handlebar function mentioned in the PR objectives.

@youngcw youngcw merged commit 59835a3 into actualbudget:master Oct 28, 2024
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants