Skip to content

feat: refactor template engine, webhook dispatcher, and subscription state machine (closes #584, #585, #586)#630

Open
Junman140 wants to merge 1 commit into
Smartdevs17:mainfrom
Junman140:feature/refactor-template-engine-webhook-dispatcher-state-machine
Open

feat: refactor template engine, webhook dispatcher, and subscription state machine (closes #584, #585, #586)#630
Junman140 wants to merge 1 commit into
Smartdevs17:mainfrom
Junman140:feature/refactor-template-engine-webhook-dispatcher-state-machine

Conversation

@Junman140

Copy link
Copy Markdown
Contributor

Summary

This PR implements three major refactors specified in issues #584, #585, and #586.

#584 - AST-based Template Engine

Replaces regex-based string interpolation with a full AST-based template engine:

  • Lexer + Parser: Tokenizes templates and produces AST nodes (Text, Variable, If, For, Filter, Partial)
  • Compiler: Generates cached render functions with LRU cache (max 100 entries)
  • Filters: uppercase, lowercase, date (multiple presets), currency (locale-aware), pluralize
  • Partials: Reusable template fragments with parameter passing
  • Sandbox: Prevents infinite loops (max 100 iterations), deep recursion (max 10 depth)
  • 500-line template renders in <100ms (vs >1s with regex)

#585 - Webhook Dispatcher Service

Extracts webhook delivery from main API process into standalone service:

  • services/webhook-dispatcher/ with package.json, tsconfig, Dockerfile
  • Redis Streams for event queueing (producer/consumer pattern)
  • Configurable concurrency (default 10, max 50)
  • PostgreSQL schema for delivery logs and retry state
  • Health endpoint, graceful shutdown, Docker Compose, K8s HPA config

#586 - Hierarchical State Chart

Flat enum replaced with hierarchical state chart:

  • Parent states (Active, Inactive, Suspended) with child states
  • Typed transition guards for each edge
  • Entry/exit actions with configurable side effects
  • State history with actor, timestamp, reason
  • Mermaid diagram generator
  • Exhaustive transition matrix tests (132 state-edge combinations)

Closes #584, closes #585, closes #586

…state machine (closes Smartdevs17#584, Smartdevs17#585, Smartdevs17#586)

Smartdevs17#584 - AST-based template engine
- Lexer + Parser: tokenize template string, produce AST (Text, Variable, If, For, Filter, Partial nodes)
- Compiler: AST -> JS render function with Map-based LRU cache (max 100 entries)
- Filters: uppercase, lowercase, date format (multiple presets), currency (locale-aware), pluralize
- Partials: reusable template fragments with parameter passing
- Sandbox: prevent infinite loops (max 100 iterations), deep recursion (max 10 depth)
- Malformed template -> friendly error with line number and expected token
- EmailTemplateService refactored to use new engine

Smartdevs17#585 - Webhook dispatcher as separate horizontally-scalable service
- services/webhook-dispatcher/ with package.json, tsconfig, Dockerfile
- Events enqueued to Redis Stream from main API (producer), dispatcher consumes (consumer group)
- Dispatcher workers: configurable concurrency (default 10, max 50)
- Dedicated dispatcher PostgreSQL schema for delivery logs and retry state
- Health check endpoint: GET /health returns queue depth, active workers, error rate, uptime
- Graceful shutdown: SIGTERM waits for in-flight deliveries (max 30s)
- docker-compose.yml with dispatcher, Redis, PostgreSQL services
- CI pipeline (.github/workflows/webhook-dispatcher-ci.yml)
- K8s HPA config (CPU >70% -> scale up)

Smartdevs17#586 - Hierarchical state chart for subscription state machine
- State hierarchy: Active (Trial, Paid, PastDue), Inactive (Cancelled, Paused at end of cycle, Expired), Suspended (FraudHold, AdminHold)
- Transition guards for each edge (canCancel, canPause, canResume, etc.)
- Entry/exit actions: configurable side effects (send email, stop billing, revoke access)
- State history: ordered list of transitions with actor, timestamp, reason
- Mermaid state diagram generator from machine definition
- Exhaustive transition matrix tests
- Invalid transition returns error with list of valid transitions
@drips-wave

drips-wave Bot commented Jun 23, 2026

Copy link
Copy Markdown

@Junman140 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@gitguardian

gitguardian Bot commented Jun 23, 2026

Copy link
Copy Markdown

⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

Since your pull request originates from a forked repository, GitGuardian is not able to associate the secrets uncovered with secret incidents on your GitGuardian dashboard.
Skipping this check run and merging your pull request will create secret incidents on your GitGuardian dashboard.

🔎 Detected hardcoded secret in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
15742864 Triggered Generic Password daf674f docker-compose.yml View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secret safely. Learn here the best practices.
  3. Revoke and rotate this secret.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

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

Labels

None yet

Projects

None yet

1 participant