All files / src/modules/filters filters.service.ts

83.33% Statements 15/18
80% Branches 4/5
66.66% Functions 4/6
81.25% Lines 13/16

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 47 48 49 50 51 52 53 54 5513x 13x 13x         13x     13x 13x             1x 1x 1x           1x                 1x           1x   1x                      
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '@app/modules/prisma/prisma.service';
import {
  HeatingType,
  ListingType,
  TransactionType,
} from '@prisma/client';
import { filterSchemaConfig } from '@app/modules/filters/config/filter-schema.config';
 
@Injectable()
export class FiltersService {
  constructor(private readonly prisma: PrismaService) {}
 
  getListingTypes() {
    return Object.values(ListingType);
  }
 
  getFilterSchemaForType(type: ListingType) {
    const schema = filterSchemaConfig[type];
    if (!schema) {
      throw new NotFoundException(`No filter schema found for type: ${type}`);
    }
    return schema;
  }
 
  async getDynamicValues() {
    const distinctAddresses = await this.prisma.client.address.findMany({
      select: {
        country: true,
        state: true,
        city: true,
      },
      distinct: ['country', 'state', 'city'],
    });
 
    const buildingTypes = await this.prisma.client.listing.findMany({
      select: { buildingType: true },
      distinct: ['buildingType'],
      where: { buildingType: { not: null } },
    });
 
    return {
      locations: distinctAddresses,
      buildingTypes: buildingTypes.map((b: { buildingType: string | null }) => b.buildingType),
    };
  }
 
  getStaticOptions() {
    return {
      transactionType: Object.values(TransactionType),
      heatingType: Object.values(HeatingType),
      // Add other static enums here as needed
    };
  }
}