Skip to content

Improve Agreement Router reliability, validation consistency, and template handling - #151

Closed
muralimadhava96-ui wants to merge 3 commits into
accordproject:mainfrom
muralimadhava96-ui:patch-1
Closed

Improve Agreement Router reliability, validation consistency, and template handling#151
muralimadhava96-ui wants to merge 3 commits into
accordproject:mainfrom
muralimadhava96-ui:patch-1

Conversation

@muralimadhava96-ui

Copy link
Copy Markdown

Overview
This PR refactors the Agreement router to improve code structure, validation robustness, and execution safety. It also introduces comprehensive test coverage for agreement execution, template handling, and error scenarios.

The goal is to align the implementation with production-grade backend practices while ensuring reliability and maintainability.


Key Improvements

-->Architecture & Maintainability

  • Introduced service-level abstractions (e.g., resolveAgreement, template resolution)
  • Reduced duplication in template lookup logic
  • Simplified control flow and improved readability

--> Validation Enhancements

  • Combined schema validation with domain-level validation (Concerto)
  • Added strict ID parsing to prevent invalid database queries
  • Improved validation for trigger request types

--> Safer Execution

  • Avoided full object mutation during database updates (partial state updates only)
  • Ensured agreement state is initialized safely before trigger execution
  • Improved error handling with consistent response structure

--> Template Handling

  • Simplified template URI normalization
  • Added hash-based template caching to avoid duplicate inserts
  • Improved external template fetching and storage flow

--> Logging

  • Replaced console logs with structured logging for better debugging and observability

Test Coverage

-->Added comprehensive tests covering:

  • ✅ Successful agreement trigger execution
  • ✅ State persistence across multiple triggers
  • ✅ Validation failures (invalid request types and malformed payloads)
  • ✅ Error scenarios (missing agreement, missing template, DB failures)
  • ✅ External template fetching and database insertion
  • ✅ Request parsing edge cases (invalid JSON)

-->Test improvements:

  • Removed duplicate test definitions
  • Introduced structured test organization (Trigger, Errors, Creation)
  • Replaced unsafe mocks with partial mocks
  • Improved type safety for mocked database layer

--> Behavior Changes

  • More consistent HTTP responses (clear distinction between 400 and 500 errors)
  • Safer handling of invalid IDs and malformed inputs
  • More predictable and reliable trigger execution flow

--> Performance

  • Reduced redundant database queries via centralized template resolution
  • Improved caching efficiency using template hash lookup

--> Compatibility

  • No breaking API changes introduced
  • Existing routes and response formats remain unchanged

--> Future Work

  • Add authentication and authorization middleware
  • Introduce rate limiting for trigger endpoints
  • Replace remaining any types with strict typings
  • Expand test coverage (edge cases, performance scenarios)
  • Support additional template retrievers (e.g., IPFS, GitHub)

--> Motivation
This refactor improves reliability, maintainability, and developer experience while preparing the codebase for future scalability and extensibility. The addition of strong test coverage ensures confidence in agreement execution and template processing workflows.

@muralimadhava96-ui

Copy link
Copy Markdown
Author

Hi maintainers 👋

This PR focuses on improving Agreement API reliability by:

  • reducing over-mocking in tests
  • improving error visibility
  • making validation responses consistent

I’d really appreciate your feedback on whether this direction aligns with the project’s testing and API design approach.

Especially curious about:
👉 preferred balance between mocking vs real template execution in tests

Thanks!

…on, and safer state handling

Refactored the Agreement router to improve code quality, maintainability, and production readiness.

Key improvements:
- Introduced service-level abstraction (getTemplate, resolveAgreement) to reduce duplication and improve separation of concerns
- Added strict ID parsing with validation to prevent invalid database queries
- Improved error handling with consistent responses and removed internal error leakage
- Integrated structured logging using pino instead of console logs
- Simplified template retrieval and caching logic
- Ensured safer database updates by avoiding full object mutation (partial state update only)
- Cleaned up validation flow combining Zod schema and Concerto validation
- Reduced branching complexity and improved readability

This refactor aligns the codebase with production-grade backend practices and prepares it for future extensibility (e.g., additional template retrievers, auth middleware, and rate limiting).
Future work:
- Add authentication and authorization middleware
- Implement rate limiting for trigger endpoints
- Replace `any` types with strict Drizzle ORM typings
- Add unit and integration tests

Signed-off-by: muralimadhava96-ui <muralimadhava96@gmail.com>
…proved structure

test(apap): refactor agreement API tests with simplified mocks

Signed-off-by: muralimadhava <muralimadhava96@gmail.com>
Signed-off-by: muralimadhava96-ui <muralimadhava96@gmail.com>
…dling and validation

feat(apap): improve agreement API reliability, validation consistency, and error handling

Signed-off-by: muralimadhava <muralimadhava96@gmail.com>
Signed-off-by: muralimadhava96-ui <muralimadhava96@gmail.com>
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open 15 days with no activity. Remove stale label or comment or this will be closed in 10 days.

@github-actions github-actions Bot added the Stale label Apr 13, 2026
@github-actions github-actions Bot closed this Apr 24, 2026
@mttrbrts mttrbrts removed the Stale label May 28, 2026
@mttrbrts mttrbrts reopened this May 28, 2026
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open with no activity. Remove the stale label or comment to keep it active. Only items with maintainer engagement are auto-closed.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open with no activity. Remove the stale label or comment to keep it active. Only items with maintainer engagement are auto-closed.

@github-actions github-actions Bot added the Stale label Jun 29, 2026
@JayDS22

JayDS22 commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Wanted to flag before you invest in a rebase. server/handlers/agreements.ts has been through a service extraction since this PR opened: the CRUD half moved into services/agreementService.ts (#211), then convertAgreement (#213), then triggerAgreement (#214). Typed errors and centralized Express error handling landed alongside (#184, ab46121). A lot of the reliability and validation improvements this PR targets have since been addressed in that redesign.

A rebase now will surface heavy conflicts, and the diff shape will change substantially. Worth reassessing which of the improvements are still relevant against current main and re-scoping into a smaller focused PR if so.

@github-actions github-actions Bot added maintainer-engaged A maintainer has commented or reviewed this item and removed Stale labels Jul 24, 2026
@JayDS22

JayDS22 commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Thanks @muralimadhava96-ui for the original direction here. Closing this out because the reliability, validation, and template-handling intent has landed on main via a different code path: the router logic was extracted into a transport-agnostic service layer, and each concern from this PR shipped as a focused change on top of that.

Coverage on main:

Closing rather than rebasing because the underlying router shape this PR targets no longer exists on main, so a rebase would be a rewrite rather than a merge. If you see specific gaps the service extraction missed (a validation case, a template edge case, a router response shape), a focused follow-up PR against current main is welcome and will be much easier to review than reconstructing this one.

Credit for surfacing the original set of concerns stays with you.

@JayDS22 JayDS22 closed this Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer-engaged A maintainer has commented or reviewed this item

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants