Commit 81627054 authored by Diego Iván's avatar Diego Iván
Browse files

agregando servicio que recomienda una ruta

parent 9f0ed4ed
Loading
Loading
Loading
Loading
+41 −6
Original line number Diff line number Diff line
@@ -9,6 +9,12 @@ import { Town } from 'src/town/entities/town.entity';
import { TravelPlaceService } from 'src/travel-place/travel-place.service';
import { TravelPlace } from 'src/travel-place/entities/travel-place.entity';
import { RecommendationsSystem } from './utils/recommendations';
import { PlaceService } from 'src/place/place.service';
import { LANGUAGES } from 'src/shared/enum/languages.enum';
import { VisitedService } from 'src/visited/visited.service';
import { GetPlaceDto } from 'src/place/dto/get-place.dto';
import { Visited } from 'src/visited/entities/visited.entity';
import { RecommendPlace } from './dto/recommend-route.dto';

@Injectable()
export class RouteService {
@@ -18,10 +24,12 @@ export class RouteService {
    @InjectRepository(Town) private townRepository: Repository<Town>,
    private readonly travelPlaceService: TravelPlaceService,
    @InjectDataSource() private dataSource: DataSource,
    private readonly placeService: PlaceService,
    private readonly visitedService: VisitedService,
  ) {}
  async create() {
    const user: User = await this.userRepository.findOneBy({ userId: 1 });
    const town: Town = await this.townRepository.findOneBy({ townId: 1 });
  private async createRoute(idUser: number, idTown: number) {
    const user: User = await this.userRepository.findOneBy({ userId: idUser });
    const town: Town = await this.townRepository.findOneBy({ townId: idTown });
    await this.routeRepository.save({ user, town, startDate: new Date(), endDate: new Date() });
    await this.travelPlaceService.create({
      idRoute: 1,
@@ -54,10 +62,37 @@ export class RouteService {
    return `This action removes a #${id} route`;
  }

  recommend() {
  async recommend(idTown: number, email: string, language: LANGUAGES) {
    // Obtener los visitados y los candidatos
    const places: GetPlaceDto[] = await this.placeService.findAllByTown(idTown, language);
    const visited: Visited[] = await this.visitedService.getVisitedByUser(email);
    const placesMapped: RecommendPlace[] = places.map((place) => {
      return {
        idPlace: place.idPlace,
        openAt: place.openAt,
        closeAt: place.closeAt,
        categories: place.categories.map((category) => category.idCategory),
        rating: 0,
      };
    });

    const visitedMapped: RecommendPlace[] = visited.map((visit) => {
      return {
        idPlace: visit.place.idPlace,
        openAt: visit.place.openAt,
        closeAt: visit.place.closeAt,
        categories: visit.place.categories.map((category) => category.idCategory),
        rating: visit.rating,
      };
    });

    const system = new RecommendationsSystem();
    // system.recommend(visited, candidates);
    const chosen: [number, number][] = system.recommend(visitedMapped, placesMapped);
    const placesChooen: GetPlaceDto[] = [];
    for (const [index] of chosen) {
      placesChooen.push(await this.placeService.findOneAndTradAndAvailable(index, LANGUAGES.EN));
    }

    return placesChooen;
  }
}