import {IQueryHandler, QueryHandler} from "@nestjs/cqrs";
import {GuestsService} from "../../guests.service";
import {Promise} from "mongoose";
import {Logger} from "@nestjs/common";
import {FilterGuestsWithPropertyQuery} from "../filterguestswithproperty.query";



@QueryHandler(FilterGuestsWithPropertyQuery)
export class FilterGuestsWithPropertyQueryHandler implements IQueryHandler<FilterGuestsWithPropertyQuery> {
  private readonly logger = new Logger(FilterGuestsWithPropertyQueryHandler.name)
  constructor(private guestService: GuestsService) {
  }

  async execute(query: FilterGuestsWithPropertyQuery) {
    this.logger.verbose(`enter ${query.property}`)
    this.logger.verbose(`expr ${query.filterExpression}`);
    this.logger.verbose(`term: ${query.filterTerm}`);
    const result = await this.guestService.filter({ ['properties.' +query.property] : { [query.filterExpression]: query.filterTerm.toString()}  });
    this.logger.verbose(`Found: ${result.length}`);
    if(result) {
     return result;
    }
    return Promise.resolve([])
  }
}