feat(pet): implement computed pet availability resolver#108
Open
Neziahtech wants to merge 2 commits into
Open
Conversation
|
@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! 🚀 |
Owner
|
@Neziahtech , your code is failing and also conflict |
Author
|
Conflict has been resolved
…On Sun, 31 May 2026, 7:18 pm Amina Sheriff, ***@***.***> wrote:
*amina69* left a comment (amina69/PetAd-backend#108)
<#108 (comment)>
@Neziahtech <https://github.com/Neziahtech> , your code is failing and
also conflict
—
Reply to this email directly, view it on GitHub
<#108?email_source=notifications&email_token=BBDVZOROK655IFTGXKAJUCD45RZQPA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTINJYG43DENZVGY4KM4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-4587627568>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BBDVZORV6TVOHE4VQAQ2SSD45RZQPAVCNFSM6AAAAACZTOV6AWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DKOBXGYZDONJWHA>
.
Triage notifications, keep track of coding agent tasks and review pull
requests on the go with GitHub Mobile for iOS
<https://github.com/notifications/mobile/ios/BBDVZOWB3JRFCQPC7BFJLTL45RZQPA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTINJYG43DENZVGY4KM4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJKTGN5XXIZLSL5UW64Y>
and Android
<https://github.com/notifications/mobile/android/BBDVZORFVYJFBXPYCJRQP4T45RZQPA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTINJYG43DENZVGY4KM4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJLTGN5XXIZLSL5QW4ZDSN5UWI>.
Download it today!
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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