Skip to content

Admin Approval #22

@amina69

Description

@amina69

Dont add anything new to the schema

📋 Description

Create endpoints for admins to approve or reject adoption requests. When approved, the pet status should update and (optionally) trigger escrow creation. When rejected, the pet returns to available status.

🎯 Context

After users submit adoption requests (#12) and admins can view them (#13), admins need the ability to make decisions. Approval moves the adoption forward (potentially creating blockchain escrow), while rejection frees the pet for other adopters.

📝 What You'll Build

Two Admin Endpoints

1. PATCH /adoptions/:id/approve - Approve a request

  • Changes adoption status: PENDING → APPROVED
  • Changes pet status: PENDING → AVAILABLE (or keeps PENDING for escrow)
  • Optional: Triggers escrow creation (Phase 2)
  • Returns updated adoption

2. PATCH /adoptions/:id/reject - Reject a request

  • Changes adoption status: PENDING → REJECTED
  • Changes pet status: PENDING → AVAILABLE
  • Optional: Accepts rejection reason
  • Returns updated adoption

What Happens on Approval

1. Validate adoption exists and is PENDING
2. Update adoption status to APPROVED
3. Update approvedAt timestamp
4. Future: Trigger escrow creation
5. Future: Send notification to adopter
6. Return updated adoption

What Happens on Rejection

1. Validate adoption exists and is PENDING
2. Update adoption status to REJECTED
3. Update pet status to AVAILABLE (free for others)
4. Optional: Store rejection reason/notes
5. Future: Send notification to adopter
6. Return updated adoption

🔧 Implementation Checklist

Files to Create/Modify

  • src/adoption/adoption.controller.ts - Add approve/reject endpoints
  • src/adoption/adoption.service.ts - Add approve/reject methods
  • src/adoption/dto/reject-adoption.dto.ts - Optional rejection reason DTO
  • src/pets/pets.service.ts - May need to update pet status
  • Update Swagger documentation
  • Add unit tests for both scenarios

What to Build

Approve Method:

  • Accept adoptionId and adminUserId
  • Verify adoption exists and status is PENDING
  • Update adoption status to APPROVED
  • Set approvedAt timestamp to now
  • Return updated adoption with pet and user data

Reject Method:

  • Accept adoptionId, adminUserId, and optional reason
  • Verify adoption exists and status is PENDING
  • Update adoption status to REJECTED
  • Update pet status back to AVAILABLE
  • Store rejection reason in notes field
  • Return updated adoption

Validation:

  • Only PENDING adoptions can be approved/rejected
  • Only ADMIN role can perform these actions
  • Adoption must exist (404 if not found)
  • Pet must exist and be linked to adoption

Authorization:

  • Both endpoints require ADMIN role
  • Apply JwtAuthGuard + RolesGuard
  • Use @roles('ADMIN') decorator

✅ Acceptance Criteria

  • PATCH /adoptions/:id/approve endpoint exists
  • PATCH /adoptions/:id/reject endpoint exists
  • Both endpoints require ADMIN role
  • Only PENDING adoptions can be approved
  • Only PENDING adoptions can be rejected
  • Approval updates adoption status to APPROVED
  • Approval sets approvedAt timestamp
  • Rejection updates adoption status to REJECTED
  • Rejection updates pet status to AVAILABLE
  • Rejection can include optional reason/notes
  • Returns 404 if adoption not found
  • Returns 400 if adoption not PENDING
  • Returns 403 for non-admin users

🧪 How to Test

Setup:

  1. Create ADMIN user
  2. Create SHELTER user
  3. Create regular USER
  4. Shelter creates a pet
  5. User submits adoption request (status: PENDING, pet: PENDING)

Test Approval Flow:

  1. Admin approves adoption:

    • Login as ADMIN
    • PATCH /adoptions/123/approve
    • Response: 200 OK ✅
    • Adoption status: APPROVED
    • approvedAt: Current timestamp
    • Pet status: PENDING (ready for escrow)
  2. Non-admin tries to approve:

    • Login as regular USER or SHELTER
    • PATCH /adoptions/123/approve
    • Response: 403 Forbidden ❌
  3. Approve already approved:

    • PATCH /adoptions/123/approve (already APPROVED)
    • Response: 400 Bad Request ❌
    • Error: "Adoption is not pending"
  4. Approve non-existent:

    • PATCH /adoptions/999/approve
    • Response: 404 Not Found ❌

Test Rejection Flow:

  1. Admin rejects adoption:

    • Create new PENDING adoption
    • Login as ADMIN
    • PATCH /adoptions/456/reject
    • Body: { "reason": "Incomplete documentation" }
    • Response: 200 OK ✅
    • Adoption status: REJECTED
    • Pet status: AVAILABLE (freed for others)
    • notes: "Incomplete documentation"
  2. Reject without reason:

    • PATCH /adoptions/456/reject
    • Body: {} (empty)
    • Should still work ✅
  3. Non-admin tries to reject:

    • Login as regular USER
    • PATCH /adoptions/456/reject
    • Response: 403 Forbidden ❌

Edge Cases:

  • Approve COMPLETED adoption → 400 Bad Request
  • Reject REJECTED adoption → 400 Bad Request
  • Approve CANCELLED adoption → 400 Bad Request

Suggested Approach:

  1. Create approve endpoint in controller (ADMIN only)
  2. Create reject endpoint in controller (ADMIN only)
  3. Implement approve logic in service
  4. Implement reject logic in service
  5. Add status validation for both
  6. Test with Postman/curl
  7. Add unit tests
  8. Update Swagger docs

Transaction Tip:
Use Prisma transactions for reject (updates adoption + pet) to ensure data consistency.

Approve Flow

Adoption: PENDING → APPROVED
Pet: PENDING → PENDING (awaiting escrow)

Reject Flow

Adoption: PENDING → REJECTED
Pet: PENDING → AVAILABLE (freed)

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave program

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions