Skip to content

⚙️ [Day 1 - Hour 4] Core Services Foundation Implementation #5

@uozopio

Description

@uozopio

📋 Task Overview

Sprint Day: Day 1 - Morning Session
Time Slot: 12:00 - 13:00 (1 hour)
Task Type: Collaborative Service Implementation (All Team Members)
Estimated Duration: 1 hour
Dependencies: Project Structure (Issue #4)

🎯 Objectives

Implement the foundation of core services including basic EscrowService class, PaymentService skeleton, database service methods, and a basic state machine for escrow transaction management. This establishes the business logic foundation before afternoon's parallel development.

📝 Detailed Requirements

1. EscrowService Class Implementation

  • Create services/escrow/EscrowService.ts with:
    • Class structure with dependency injection setup
    • Methods for transaction lifecycle:
      • createTransaction() - Initialize new escrow
      • updateStatus() - State transitions
      • getTransaction() - Retrieve transaction details
      • listTransactions() - Query transactions with filters
      • validateTransition() - Validate state changes
    • Integration with Prisma client
    • Error handling with custom error classes
  • Create services/escrow/types.ts with:
    • CreateEscrowInput interface
    • UpdateEscrowInput interface
    • EscrowStatus enum
    • TransactionResponse type
  • Implement basic validation logic
  • Add activity logging for all operations

2. PaymentService Skeleton

  • Create services/payment/PaymentService.ts with:
    • Jetcheckout API integration structure
    • Payment method interfaces:
      • createPayment() - Initialize payment
      • verifyPayment() - Check payment status
      • processRefund() - Handle refunds
      • handleWebhook() - Process gateway webhooks
    • Hash generation utilities for security
    • Mock responses for development
  • Create services/payment/types.ts with:
    • PaymentRequest interface
    • PaymentResponse interface
    • WebhookPayload type
    • PaymentStatus enum
  • Set up payment gateway configuration
  • Prepare webhook signature verification

3. Database Service Methods

  • Create services/database/DatabaseService.ts with:
    • Prisma client singleton pattern
    • Transaction management utilities
    • Common query builders:
      • findWithPagination()
      • upsertRecord()
      • softDelete()
      • bulkUpdate()
    • Connection health check
    • Query performance logging
  • Implement repository pattern for each model:
    • UserRepository
    • EscrowRepository
    • PaymentRepository
    • DocumentRepository
  • Add database transaction support
  • Create query optimization helpers

4. State Machine Implementation

  • Create services/escrow/StateMachine.ts with:
    • State transition definitions:
      • DRAFTPENDING_PAYMENT
      • PENDING_PAYMENTPAYMENT_RECEIVED
      • PAYMENT_RECEIVEDPENDING_DELIVERY
      • PENDING_DELIVERYDELIVERED
      • DELIVEREDCOMPLETED
      • Any state → DISPUTED
      • Certain states → CANCELLED
    • Transition validation rules
    • Guard conditions for each transition
    • Event emission for state changes
    • Rollback capabilities
  • Create state machine tests structure
  • Document state transition diagram
  • Implement transition history tracking

🔧 Technical Context

Service Architecture Principles

  • Dependency Injection: Services should be loosely coupled
  • Single Responsibility: Each service handles one domain
  • Error Handling: Consistent error types and messages
  • Logging: Comprehensive logging for debugging
  • Testing: Services designed for easy unit testing

State Machine Design

The state machine ensures transaction integrity by:

  • Enforcing valid transitions only
  • Preventing race conditions with locks
  • Maintaining audit trail of all changes
  • Supporting rollback for failed operations
  • Emitting events for external integrations

Database Patterns

  • Repository Pattern: Abstraction over Prisma
  • Unit of Work: Transaction management
  • Query Builder: Flexible query construction
  • Connection Pooling: Optimal performance
  • Read Replicas: Future scalability consideration

Integration Considerations

  • API Contracts: Well-defined interfaces
  • Error Recovery: Graceful failure handling
  • Idempotency: Safe retry mechanisms
  • Monitoring: Health checks and metrics
  • Documentation: Inline JSDoc comments

✅ Acceptance Criteria

  • All services compile without TypeScript errors
  • Basic CRUD operations work for escrow transactions
  • State machine validates transitions correctly
  • Payment service can mock payment creation
  • Database service handles connections properly
  • Services are properly exported and importable
  • Basic unit test structure is in place
  • Services follow SOLID principles

👥 Team Assignments

All Team Members:

  • @uozopio (Umut) - Lead service architecture and state machine
  • @mbyzopio (Burak) - Review service interfaces for frontend needs
  • @teczopio (Enes) - Ensure services support UI requirements

This task is part of the 2-day MVP sprint for the Paylox Escrow System

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions