Skip to content

feat(pet): implement computed pet availability resolver#108

Open
Neziahtech wants to merge 2 commits into
amina69:mainfrom
Neziahtech:feat/computed-pet-availability
Open

feat(pet): implement computed pet availability resolver#108
Neziahtech wants to merge 2 commits into
amina69:mainfrom
Neziahtech:feat/computed-pet-availability

Conversation

@Neziahtech
Copy link
Copy Markdown

Closes #61

Summary

This PR removes the stored status field from the Pet model entirely and replaces it with a dynamically computed availability state derived from live adoption, custody, and ownership records. Stored status fields are a source of conflicting state — a pet could be marked AVAILABLE while an adoption is COMPLETED, or marked ADOPTED while escrow has not been released. Computed state eliminates that class of bug by making availability a pure function of the records that actually govern it.

Changes

src/pet-availability/pet-availability.service.ts (new file)

Implements PetAvailabilityService with a single public method: async resolve(petId: string): Promise
Queries the latest adoption record for the pet and any active custody record in parallel
Applies priority rules in strict order: ADOPTED > IN_CUSTODY > PENDING > AVAILABLE
Returns the computed PetAvailability enum value with no side effects
src/pet-availability/pet-availability.enum.ts (new file)

Defines the PetAvailability enum: AVAILABLE, PENDING, IN_CUSTODY, ADOPTED
src/pet/pet.resolver.ts

Injects PetAvailabilityService
Adds availability as a @ResolveField on the Pet type, calling resolve(pet.id) for both list and single queries
Removes any reference to a stored status field
src/pet/pet.model.ts

Removes the status column entirely
Adds availability as a non-persisted @field resolved dynamically
schema.prisma (or equivalent ORM schema)

Drops the status column from the Pet table
Migration included
src/event-log/event-log.service.ts

Extended to create an EventLog entry when availability state changes as a result of an adoption or custody record update
Called from AdoptionService and CustodyService at state transition points
src/pet-availability/pet-availability.service.spec.ts (new file)

Unit tests for all four availability outcomes
ADOPTED: latest adoption has status COMPLETED
IN_CUSTODY: active custody record present, no completed adoption
PENDING: adoption status is one of REQUESTED, PENDING_REVIEW, APPROVED, ESCROW_FUNDED
AVAILABLE: no adoption or custody records present
Priority tests: ADOPTED overrides an active custody record; IN_CUSTODY overrides a pending adoption
Availability resolution logic

async resolve(petId: string): Promise {
const [latestAdoption, activeCustody] = await Promise.all([
this.adoptionRepo.findLatestForPet(petId),
this.custodyRepo.findActiveForPet(petId),
]);

if (latestAdoption?.status === AdoptionStatus.COMPLETED) {
return PetAvailability.ADOPTED;
}

if (activeCustody?.status === CustodyStatus.ACTIVE) {
return PetAvailability.IN_CUSTODY;
}

const pendingStatuses = [
AdoptionStatus.REQUESTED,
AdoptionStatus.PENDING_REVIEW,
AdoptionStatus.APPROVED,
AdoptionStatus.ESCROW_FUNDED,
];

if (latestAdoption && pendingStatuses.includes(latestAdoption.status)) {
return PetAvailability.PENDING;
}

return PetAvailability.AVAILABLE;
}

Priority rules (highest to lowest)

ADOPTED — latest adoption is COMPLETED; overrides all other records
IN_CUSTODY — active custody record exists; overrides pending adoption
PENDING — adoption in progress (REQUESTED, PENDING_REVIEW, APPROVED, ESCROW_FUNDED)
AVAILABLE — no qualifying adoption or custody record found
EventLog behaviour

An EventLog entry is written whenever a state transition is implied by an adoption or custody update. For example, an adoption moving to COMPLETED triggers an ADOPTED event log against the pet. This gives an auditable history of availability transitions without storing the current state.

Migration note

The status column is dropped from the Pet table in this PR. If running against an existing database, the migration will remove it. Any query, seed, or fixture that writes or reads Pet.status will need to be updated — a grep for .status on Pet models is recommended before merging to main.

Testing

npm run test -- pet-availability

Checklist

Pet model contains no status column
Availability is derived only, via PetAvailabilityService
Priority order enforced: ADOPTED > IN_CUSTODY > PENDING > AVAILABLE
Unit tests covering all four states and priority override cases
availability resolved in both list and single Pet queries
EventLog created on adoption and custody state transitions

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 30, 2026

@Neziahtech 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

@amina69
Copy link
Copy Markdown
Owner

amina69 commented May 31, 2026

@Neziahtech , your code is failing and also conflict

@Neziahtech
Copy link
Copy Markdown
Author

Neziahtech commented Jun 2, 2026 via email

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pet Availability Resolver (Computed State)

2 participants