import { ICommandHandler, QueryHandler} from "@nestjs/cqrs";
import {Logger} from "@nestjs/common";
import {GuestsService} from "../guests.service";

export class GetGuestPropertyQuery {
  constructor(public user: number, public property: string) {
  }
}

@QueryHandler(GetGuestPropertyQuery)
export class GetGuestPropertyHandler implements ICommandHandler<GetGuestPropertyQuery,any> {
  private readonly logger = new Logger(GetGuestPropertyHandler.name);
  constructor(private guestService: GuestsService) {
  }
  async execute(command: GetGuestPropertyQuery): Promise<string> {
    this.logger.verbose(`entering`);
    const guest = await this.guestService.findById(command.user);
    this.logger.verbose(command);
    if(!command.property.startsWith('properties.')) {
      command.property = `properties.${command.property}`;
      this.logger.warn(`update prop ${command.property}`);
    }
    this.logger.verbose(`prop value ${command.property} for ${command.user} is ${guest.properties[command.property]} `);
    return guest.properties[command.property];
  }
}

