feat(tenancies-p4): public-map status flip via active permanent tenancy

- derivePublicStatus gains optional hasActivePermanentTenancy flag;
  precedence updated to "sold > under_offer > available" where
  Sold can come from EITHER berths.status='sold' (admin set) OR an
  active permanent-class tenancy (only when module enabled).
- Permanent-class tenure types defined in one place
  (isPermanentTenureType): permanent | fee_simple | strata_lot.
  Seasonal / fixed_term tenancies do NOT flip — they fall through to
  the existing under_offer / available precedence.
- /api/public/berths (list) + /api/public/berths/[mooringNumber]
  (single) both gate the lookup on isTenanciesModuleEnabled(portId).
  Disabled module = lookup skipped entirely, preserving pre-module
  behaviour for ports that haven't opted in.
- 8 new unit tests covering: flip from available, flip from under_offer,
  explicit sold idempotency, false-flag fallthrough, default-omit pre-
  module behaviour, permanent-class membership for each tenure type,
  and null/undefined/unknown rejection.

Verified: tsc clean, 1493/1493 vitest.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-25 15:17:06 +02:00
parent 20549fb22e
commit bfb29ab619
4 changed files with 137 additions and 14 deletions

View File

@@ -78,20 +78,41 @@ function toString(value: unknown): string | null {
}
/**
* Derive the public-facing status string from the CRM's internal status
* + the per-berth `is_specific_interest` flag. Per plan §1:
* Tenure types that are treated as "permanent-class" by the public-map
* status resolver — an active tenancy with one of these tenure types
* pushes a berth into "Sold" even when `berths.status` is something
* weaker (`available`, `under_offer`). Per docs/tenancies-design.md §
* "Public map status flip". Seasonal / fixed-term tenancies do NOT flip
* the public status (they fall through to the existing precedence).
*/
const PERMANENT_TENURE_TYPES = new Set(['permanent', 'fee_simple', 'strata_lot']);
export function isPermanentTenureType(tenureType: string | null | undefined): boolean {
return typeof tenureType === 'string' && PERMANENT_TENURE_TYPES.has(tenureType);
}
/**
* Derive the public-facing status string. Precedence:
*
* Public "Under Offer" = berth.status = 'sold'? No - that's "Sold".
* berth.status = 'under_offer' OR
* at least one interest_berths row marked
* is_specific_interest = true ON an active
* (non-archived) interest.
* Sold > Under Offer > Available
*
* Sold can come from:
* 1. berths.status = 'sold' (explicit admin set), OR
* 2. an active tenancy with tenure_type in PERMANENT_TENURE_TYPES
* exists for this berth (only when the tenancies module is enabled).
*
* Under Offer fires when berth.status='under_offer' OR at least one
* interest_berths row marked is_specific_interest=true exists on an
* active (non-archived, non-closed) interest. Seasonal / fixed-term
* active tenancies do not flip the public status — they fall through
* to the existing under-offer / available precedence.
*/
export function derivePublicStatus(
internalStatus: string,
hasSpecificInterest: boolean,
hasActivePermanentTenancy = false,
): PublicStatus {
if (internalStatus === 'sold') return 'Sold';
if (internalStatus === 'sold' || hasActivePermanentTenancy) return 'Sold';
if (internalStatus === 'under_offer' || hasSpecificInterest) return 'Under Offer';
return 'Available';
}
@@ -117,6 +138,7 @@ export function toPublicBerth(
berth: Berth,
mapData: BerthMapData | null | undefined,
hasSpecificInterest: boolean,
hasActivePermanentTenancy = false,
): PublicBerth {
return {
Id: berth.id,
@@ -127,7 +149,7 @@ export function toPublicBerth(
'Side Pontoon': toString(berth.sidePontoon),
'Power Capacity': toNumber(berth.powerCapacity),
Voltage: toNumber(berth.voltage),
Status: derivePublicStatus(berth.status, hasSpecificInterest),
Status: derivePublicStatus(berth.status, hasSpecificInterest, hasActivePermanentTenancy),
Area: toString(berth.area),
'Mooring Type': toString(berth.mooringType),
'Bow Facing': toString(berth.bowFacing),