Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.encoreos.io/llms.txt

Use this file to discover all available pages before exploring further.

Version: 1.1.0
Last Updated: 2026-03-06
Owner: FM (Facilities Management)

Overview

This document defines the integration contracts for the Facilities Management (FM) core module. All FM events follow the standard event contract pattern defined in EVENT_CONTRACTS.md.

Event Categories

1. Work Order Events (FM-01)

work_order_created

Published when: A work order is created (request, scheduled, PM-generated, or inspection-generated).
interface WorkOrderCreatedEvent {
  event_type: 'work_order_created';
  organization_id: string;
  payload: {
    work_order_id: string;
    work_order_number: string;
    work_order_type: 'request' | 'scheduled' | 'preventive_maintenance' | 'inspection' | 'emergency';
    site_id: string | null;
    category: string;
    priority: 'critical' | 'high' | 'medium' | 'low';
    requested_by: string;
    asset_id: string | null;
    pm_schedule_id: string | null;
    inspection_schedule_id: string | null;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Notify facilities manager of new work orders
  • FA (Finance): Track pending maintenance costs

work_order_completed

Published when: A work order status changes to completed.
interface WorkOrderCompletedEvent {
  event_type: 'work_order_completed';
  organization_id: string;
  payload: {
    work_order_id: string;
    work_order_number: string;
    work_order_type: string;
    completed_at: string;
    completed_by: string;
    labor_cost: number;
    material_cost: number;
    total_cost: number;
    asset_id: string | null;
    pm_schedule_id: string | null;
    inspection_schedule_id: string | null;
    satisfaction_rating: number | null;
  };
  timestamp: string;
}
Consumers:
  • FA (Finance): Create cost allocation journal entries
  • FM-04 (Preventive Maintenance): Update PM schedule next due date
  • FM-05 (Asset Management): Update asset maintenance history
  • FM-06 (Compliance): Update inspection compliance status
  • FM-03 (Vendor Management): Update vendor performance metrics

work_order_overdue

Published when: A work order passes its due date without completion.
interface WorkOrderOverdueEvent {
  event_type: 'work_order_overdue';
  organization_id: string;
  payload: {
    work_order_id: string;
    work_order_number: string;
    due_date: string;
    days_overdue: number;
    priority: string;
    assigned_to_employee_id: string | null;
    assigned_to_vendor_id: string | null;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert facilities manager and assigned technician/vendor

2. Inventory Events (FM-02)

inventory_low_stock

Published when: Inventory quantity falls below reorder point.
interface InventoryLowStockEvent {
  event_type: 'inventory_low_stock';
  organization_id: string;
  payload: {
    inventory_item_id: string;
    item_name: string;
    sku: string | null;
    current_quantity: number;
    reorder_point: number;
    reorder_quantity: number;
    preferred_vendor_id: string | null;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert procurement manager
  • FA-04 (Purchase Orders): Auto-create purchase order (if enabled)

inventory_item_used

Published when: Inventory is used on a work order (usage transaction created).
interface InventoryItemUsedEvent {
  event_type: 'inventory_item_used';
  organization_id: string;
  payload: {
    transaction_id: string;
    inventory_item_id: string;
    item_name: string;
    work_order_id: string;
    quantity_used: number;
    unit_cost: number;
    total_cost: number;
    new_quantity_on_hand: number;
  };
  timestamp: string;
}
Consumers:
  • FM-01 (Work Orders): Update work order material cost
  • FA (Finance): Track inventory cost allocation

3. Vendor Events (FM-03)

vendor_certification_expiring

Published when: Vendor certification is within 90/60/30 days of expiration.
interface VendorCertificationExpiringEvent {
  event_type: 'vendor_certification_expiring';
  organization_id: string;
  payload: {
    vendor_id: string;
    vendor_name: string;
    certification_id: string;
    certification_type: 'coi' | 'license' | 'bonding' | 'other';
    expiration_date: string;
    days_until_expiration: number;
    alert_level: '90_day' | '60_day' | '30_day' | 'expired';
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert compliance officer and facilities manager
  • GR (Governance): Track compliance status

vendor_work_order_assigned

Published when: A work order is assigned to an external vendor.
interface VendorWorkOrderAssignedEvent {
  event_type: 'vendor_work_order_assigned';
  organization_id: string;
  payload: {
    work_order_id: string;
    work_order_number: string;
    vendor_id: string;
    vendor_name: string;
    assigned_at: string;
    estimated_cost: number | null;
    due_date: string | null;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Notify vendor of assignment
  • FA-03 (Accounts Payable): Prepare for vendor invoice matching

4. Preventive Maintenance Events (FM-04)

pm_due

Published when: A PM schedule is due (work order auto-generated).
interface PMDueEvent {
  event_type: 'pm_due';
  organization_id: string;
  payload: {
    pm_schedule_id: string;
    pm_template_id: string;
    pm_template_name: string;
    asset_id: string;
    asset_name: string;
    due_date: string;
    work_order_id: string;
    is_compliance_required: boolean;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert facilities manager and assigned technician
  • FM-05 (Asset Management): Track upcoming maintenance

pm_overdue

Published when: A PM schedule passes its due date without completion.
interface PMOverdueEvent {
  event_type: 'pm_overdue';
  organization_id: string;
  payload: {
    pm_schedule_id: string;
    pm_template_name: string;
    asset_id: string;
    asset_name: string;
    due_date: string;
    days_overdue: number;
    is_compliance_required: boolean;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert facilities manager and compliance officer
  • GR (Governance): Track compliance violations

pm_completed

Published when: A PM work order is completed.
interface PMCompletedEvent {
  event_type: 'pm_completed';
  organization_id: string;
  payload: {
    pm_schedule_id: string;
    pm_template_name: string;
    asset_id: string;
    asset_name: string;
    completed_at: string;
    work_order_id: string;
    next_due_date: string;
    labor_cost: number;
    material_cost: number;
  };
  timestamp: string;
}
Consumers:
  • FM-05 (Asset Management): Update asset maintenance history
  • FA (Finance): Track maintenance costs

5. Asset Events (FM-05)

asset_purchased

Published when: A new asset is created/registered.
interface AssetPurchasedEvent {
  event_type: 'asset_purchased';
  organization_id: string;
  payload: {
    asset_id: string;
    asset_tag: string;
    asset_name: string;
    asset_category: string;
    purchase_date: string;
    purchase_cost: number;
    vendor_id: string | null;
    purchase_order_id: string | null;
  };
  timestamp: string;
}
Consumers:
  • FA (Finance): Create capitalization journal entry

asset_disposed

Published when: An asset status changes to disposed.
interface AssetDisposedEvent {
  event_type: 'asset_disposed';
  organization_id: string;
  payload: {
    asset_id: string;
    asset_tag: string;
    asset_name: string;
    disposed_date: string;
    disposal_method: 'sold' | 'scrapped' | 'donated' | 'other';
    disposal_value: number | null;
    book_value_at_disposal: number;
    total_maintenance_cost: number;
  };
  timestamp: string;
}
Consumers:
  • FA (Finance): Create disposal journal entry
  • FM-04 (Preventive Maintenance): Deactivate PM schedules for asset

asset_warranty_expiring

Published when: Asset warranty is within 90/60/30 days of expiration.
interface AssetWarrantyExpiringEvent {
  event_type: 'asset_warranty_expiring';
  organization_id: string;
  payload: {
    asset_id: string;
    asset_tag: string;
    asset_name: string;
    warranty_expiration_date: string;
    days_until_expiration: number;
    alert_level: '90_day' | '60_day' | '30_day' | 'expired';
    warranty_vendor_id: string | null;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert compliance officer and asset manager

6. Inspection & Compliance Events (FM-06)

inspection_due

Published when: An inspection schedule is due (work order auto-generated).
interface InspectionDueEvent {
  event_type: 'inspection_due';
  organization_id: string;
  payload: {
    inspection_schedule_id: string;
    inspection_template_id: string;
    inspection_type: string;
    asset_id: string | null;
    site_id: string | null;
    location_description: string | null;
    due_date: string;
    work_order_id: string;
    is_compliance_required: boolean;
    compliance_type: string | null;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert inspector and compliance officer
  • GR (Governance): Track upcoming compliance inspections

inspection_failed

Published when: An inspection is completed with failed checklist items.
interface InspectionFailedEvent {
  event_type: 'inspection_failed';
  organization_id: string;
  payload: {
    inspection_schedule_id: string;
    inspection_type: string;
    asset_id: string | null;
    site_id: string | null;
    completed_at: string;
    work_order_id: string;
    failed_items_count: number;
    total_items_count: number;
    corrective_action_work_order_id: string;
    is_compliance_required: boolean;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert facilities manager and compliance officer
  • GR (Governance): Track compliance violations
  • FM-01 (Work Orders): Create corrective action work order

inspection_compliant

Published when: An inspection is completed with all checklist items passed.
interface InspectionCompliantEvent {
  event_type: 'inspection_compliant';
  organization_id: string;
  payload: {
    inspection_schedule_id: string;
    inspection_type: string;
    asset_id: string | null;
    site_id: string | null;
    completed_at: string;
    work_order_id: string;
    next_due_date: string;
    is_compliance_required: boolean;
  };
  timestamp: string;
}
Consumers:
  • GR (Governance): Update compliance status
  • FM-05 (Asset Management): Update asset inspection history (if asset-based)

Cross-Module Dependencies

FM-04 ↔ FM-05 Circular Dependency Resolution

Issue: FM-04 (Preventive Maintenance) and FM-05 (Asset Management) have mutual dependencies:
  • FM-04 PM schedules reference assets (FM-05)
  • FM-05 asset maintenance history references PM completions (FM-04)
Resolution Strategy:
  1. Database Level: Use foreign key references with ON DELETE SET NULL where appropriate to allow independent table creation
  2. Event-Based Updates: Use events (pm_completed, asset_disposed) for cross-module state updates rather than direct queries
  3. Implementation Order:
    • Phase 1: Implement FM-05 base tables (fm_assets)
    • Phase 2: Implement FM-04 with asset references
    • Phase 3: Add FM-05 maintenance history integration via events
  4. RLS Policies: Both use fm_has_org_access() SECURITY DEFINER function to avoid RLS recursion

RLS Security Pattern

All FM tables use the fm_has_org_access() SECURITY DEFINER function for RLS policies to avoid infinite recursion:
-- SECURITY DEFINER function for FM organization access
CREATE OR REPLACE FUNCTION fm_has_org_access(org_id UUID)
RETURNS BOOLEAN
LANGUAGE sql
STABLE
SECURITY DEFINER
SET search_path = public
AS $$
  SELECT EXISTS (
    SELECT 1
    FROM pf_user_organizations
    WHERE user_id = auth.uid()
      AND organization_id = org_id
  )
$$;

-- Example RLS policy
CREATE POLICY "Users can view FM records in their organization"
ON fm_work_orders
FOR SELECT
USING (fm_has_org_access(organization_id));

FA Integration Dependencies

The following FA specs are referenced by FM but not yet implemented:
  • FA-03 (Accounts Payable): Vendor invoice matching, payment tracking
  • FA-04 (Purchase Orders): Auto-create POs from low stock alerts
Temporary Resolution:
  • FM publishes events; FA will consume when implemented
  • FM stores purchase_order_id and vendor_invoice_id as nullable foreign keys
  • No blocking dependencies; FM can operate independently

Event Publishing Pattern

// Standard event publishing pattern for FM modules
import { supabase } from '@/integrations/supabase/client';

async function publishFMEvent(event: FMEvent) {
  const { error } = await supabase
    .from('pf_domain_events')
    .insert({
      event_type: event.event_type,
      organization_id: event.organization_id,
      payload: event.payload,
      status: 'pending',
      created_at: new Date().toISOString(),
    });

  if (error) {
    console.error('Failed to publish FM event:', error);
    throw error;
  }
}

7. Fleet Management Events (FM-13)

vehicle_registered

Published when: A new vehicle is registered in the fleet.
interface VehicleRegisteredEvent {
  event_type: 'vehicle_registered';
  organization_id: string;
  payload: {
    vehicle_id: string;
    vin: string;
    make: string;
    model: string;
    year: number;
    site_id: string | null;
    asset_id: string | null;
    registration_expiration: string | null;
    insurance_expiration: string | null;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Notify fleet manager of new vehicle
  • FM-05 (Asset Management): Link to asset record if created

vehicle_status_changed

Published when: Vehicle status changes (active → maintenance → retired → disposed).
interface VehicleStatusChangedEvent {
  event_type: 'vehicle_status_changed';
  organization_id: string;
  payload: {
    vehicle_id: string;
    vin: string;
    make: string;
    model: string;
    previous_status: 'active' | 'maintenance' | 'retired' | 'disposed';
    new_status: 'active' | 'maintenance' | 'retired' | 'disposed';
    actor_id: string | null;      // Standard field for traceability (user who made the change)
    changed_by: string;           // Kept for backward compatibility, maps to actor_id
    reason: string | null;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert fleet manager of status change
  • FM-05 (Asset Management): Update asset status

license_expiring

Published when: Driver license is within 90/60/30 days of expiration.
interface LicenseExpiringEvent {
  event_type: 'license_expiring';
  organization_id: string;
  payload: {
    driver_id: string;
    user_id: string;
    driver_name: string;
    license_number: string;
    license_state: string;
    expiration_date: string;
    days_until_expiration: number;
    alert_level: '90_day' | '60_day' | '30_day' | 'expired';
    has_cdl: boolean;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert driver and fleet manager
  • GR (Governance): Track compliance status

registration_expiring

Published when: Vehicle registration is within 90/60/30 days of expiration.
interface RegistrationExpiringEvent {
  event_type: 'registration_expiring';
  organization_id: string;
  payload: {
    vehicle_id: string;
    vin: string;
    make: string;
    model: string;
    plate_number: string;
    plate_state: string;
    expiration_date: string;
    days_until_expiration: number;
    alert_level: '90_day' | '60_day' | '30_day' | 'expired';
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert fleet manager
  • GR (Governance): Track compliance status

fleet_maintenance_due

Published when: A fleet vehicle maintenance schedule is due.
interface FleetMaintenanceDueEvent {
  event_type: 'fleet_maintenance_due';
  organization_id: string;
  payload: {
    schedule_id: string;
    vehicle_id: string;
    vin: string;
    make: string;
    model: string;
    maintenance_type: string;
    trigger_type: 'mileage' | 'time' | 'both';
    due_date: string | null;
    due_odometer: number | null;
    current_odometer: number;
    work_order_id: string | null;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert fleet manager and assigned technician
  • FM-01 (Work Orders): Track associated work order

fleet_maintenance_overdue

Published when: A fleet vehicle maintenance schedule is overdue.
interface FleetMaintenanceOverdueEvent {
  event_type: 'fleet_maintenance_overdue';
  organization_id: string;
  payload: {
    schedule_id: string;
    vehicle_id: string;
    vin: string;
    make: string;
    model: string;
    maintenance_type: string;
    due_date: string | null;
    due_odometer: number | null;
    current_odometer: number;
    days_overdue: number | null;
    miles_overdue: number | null;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert fleet manager with urgency
  • GR (Governance): Track maintenance compliance violations


8. Budgeting & Planning Events (FM-09)

budget_variance_alert

Published when: A facilities budget line item exceeds its threshold variance (default: >10% over budget for approved budgets).
interface BudgetVarianceAlertEvent {
  event_type: 'budget_variance_alert';
  organization_id: string;
  payload: {
    budget_id: string;
    budget_name: string;
    fiscal_year: number;
    line_item_id: string;
    category: string;
    site_id: string | null;
    budget_amount: number;
    actual_amount: number;
    variance_amount: number;
    variance_pct: number;
    alert_threshold_pct: number;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert facilities manager and finance manager
  • FA-08 (Budgeting & Forecasting): Sync variance to FA budget module (📝 Planned — FA-08 not yet implemented; event replays when available)

9. Capital Planning Events (FM-12)

capital_project_approved

Published when: A capital project status changes to approved.
interface CapitalProjectApprovedEvent {
  event_type: 'capital_project_approved';
  organization_id: string;
  payload: {
    project_id: string;
    project_number: string;
    name: string;
    project_type: string;
    fiscal_year: number;
    total_budget: number;
    site_id: string | null;
    asset_id: string | null;
    approved_by: string;
    approved_at: string;
    start_date: string | null;
    target_completion_date: string | null;
  };
  timestamp: string;
}
Consumers:
  • FA-09 (Capital Projects): Create capital project record in FA (📝 Planned — FA-09 not yet implemented; event replays when available)
  • PF-10 (Notifications): Notify requesting user and finance manager of approval

10. Energy Management Events (FM-10)

energy_anomaly_detected

Published when: Automated anomaly detection identifies abnormal energy usage (spike, off-hours usage, baseline deviation). Requires FM-10-DEFERRED-EXPANSION Phase 2 (smart meter integration) to be implemented.
interface EnergyAnomalyDetectedEvent {
  event_type: 'energy_anomaly_detected';
  organization_id: string;
  payload: {
    meter_id: string;
    meter_number: string;
    energy_type: string;
    site_id: string | null;
    anomaly_type: 'spike' | 'baseline_deviation' | 'off_hours_usage';
    severity: 'low' | 'medium' | 'high' | 'critical';
    detected_at: string;
    baseline_value: number;
    actual_value: number;
    deviation_pct: number;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert energy manager of anomaly
  • GR (Governance): Track compliance if anomaly indicates equipment regulatory issue

11. Space Management Events (FM-11)

space_occupancy_threshold

Published when: A space’s recorded occupancy exceeds its configured capacity threshold.
interface SpaceOccupancyThresholdEvent {
  event_type: 'space_occupancy_threshold';
  organization_id: string;
  payload: {
    space_id: string;
    space_number: string;
    space_name: string | null;
    site_id: string | null;
    capacity: number;
    current_occupancy: number;
    occupancy_pct: number;
    threshold_pct: number;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Alert facilities manager
  • HR (Human Resources): Space occupancy data for headcount planning (📝 Planned)

12. Analytics Events (FM-07)

facilities_report_generated

Published when: A scheduled or on-demand facilities analytics report is generated and available for export.
interface FacilitiesReportGeneratedEvent {
  event_type: 'facilities_report_generated';
  organization_id: string;
  payload: {
    report_type: 'work_order_analytics' | 'maintenance_cost' | 'compliance_summary' | 'asset_health' | 'vendor_performance';
    report_period_start: string;
    report_period_end: string;
    generated_by: string | null;
    report_url: string | null;
    record_count: number;
  };
  timestamp: string;
}
Consumers:
  • PF-10 (Notifications): Notify requestor when report is ready

Consumer Verification Matrix

See: FM-CONSUMER-VERIFICATION.md (to be created as part of FM-14 implementation) for implementation status of each consumer.
EventOwner SpecStatusFA ConsumerGR ConsumerPF-10 ConsumerHR Consumer
work_order_createdFM-01✅ Defined📝 Planned📝 Planned
work_order_completedFM-01✅ Defined📝 Planned📝 Planned
work_order_overdueFM-01✅ Defined📝 Planned
inventory_low_stockFM-02✅ Defined📝 Planned (FA-04)📝 Planned
inventory_item_usedFM-02✅ Defined📝 Planned
vendor_certification_expiringFM-03✅ Defined📝 Planned✅ Verified
vendor_work_order_assignedFM-03✅ Defined📝 Planned (FA-03)📝 Planned
pm_dueFM-04✅ Defined📝 Planned
pm_overdueFM-04✅ Defined📝 Planned📝 Planned
pm_completedFM-04✅ Defined📝 Planned
asset_purchasedFM-05✅ Defined📝 Planned
asset_disposedFM-05✅ Defined📝 Planned
asset_warranty_expiringFM-05✅ Defined📝 Planned
inspection_dueFM-06✅ Defined📝 Planned📝 Planned
inspection_failedFM-06✅ Defined📝 Planned📝 Planned
inspection_compliantFM-06✅ Defined📝 Planned
vehicle_registeredFM-13✅ Defined📝 Planned
vehicle_status_changedFM-13✅ Defined📝 Planned
license_expiringFM-13✅ Defined📝 Planned📝 Planned
registration_expiringFM-13✅ Defined📝 Planned📝 Planned
fleet_maintenance_dueFM-13✅ Defined📝 Planned
fleet_maintenance_overdueFM-13✅ Defined📝 Planned📝 Planned
budget_variance_alertFM-09📝 Planned📝 Planned (FA-08)📝 Planned
capital_project_approvedFM-12📝 Planned📝 Planned (FA-09)📝 Planned
energy_anomaly_detectedFM-10📝 Planned📝 Planned📝 Planned
space_occupancy_thresholdFM-11📝 Planned📝 Planned📝 Planned
facilities_report_generatedFM-07📝 Planned📝 Planned
Legend: ✅ Verified = subscription handler code exists and tested. 📝 Planned = listed as consumer in spec but handler not yet implemented.

FM↔FA Finance Interface Summary

See: FM-16: FM↔FA Finance Interface for full interface contract.
All FM→FA flows are non-blocking:
  • FM publishes events to pf_domain_events; FA subscribes when implemented
  • FM stores nullable FKs (purchase_order_id, vendor_invoice_id) for future FA reconciliation
  • FM-09 and FM-12 use FM-local cost data; FA integration is an enhancement layer
Temporary resolution: FM can operate fully without FA. No blocking dependencies on FA-03, FA-04, FA-08, or FA-09.