All files / apps/api-gateway/src/gateway/summary property-summary.service.ts

100% Statements 13/13
100% Branches 12/12
100% Functions 3/3
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 473x 3x 3x                       3x 5x     5x 5x           4x         2x         2x       3x              
import { Injectable, Logger } from '@nestjs/common';
import { PropertiesClient } from '../clients/properties.client';
import { WorkOrdersClient } from '../clients/work-orders.client';
import { PropertyDto, WorkOrderDto } from '../http/api-types';
 
export interface PropertySummary {
  property: PropertyDto;
  workOrders: WorkOrderDto[] | null;
  /** false when the work-orders service could not be reached — partial data,
   * not an error: the property itself is the primary resource here. */
  workOrdersAvailable: boolean;
}
 
@Injectable()
export class PropertySummaryService {
  private readonly logger = new Logger(PropertySummaryService.name);
 
  constructor(
    private readonly properties: PropertiesClient,
    private readonly workOrders: WorkOrdersClient,
  ) {}
 
  async getSummary(propertyId: string): Promise<PropertySummary> {
    // Fan out in parallel: the two calls are independent, so latency is
    // max(a, b) instead of a + b.
    const [property, workOrders] = await Promise.all([
      // Primary resource: failures (404 included) propagate to the caller.
      this.properties.getById(propertyId),
      // Enrichment: degrade to null instead of failing the whole response.
      this.workOrders.list({ propertyId, limit: '50' }).catch((error) => {
        this.logger.warn(
          `work orders unavailable for summary of ${propertyId}: ${
            error instanceof Error ? error.message : String(error)
          }`,
        );
        return null;
      }),
    ]);
 
    return {
      property,
      workOrders: workOrders?.data ?? null,
      workOrdersAvailable: workOrders !== null,
    };
  }
}