File size: 2,565 Bytes
7b850b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { Inject, Injectable } from '@nestjs/common';
import { Model, Types } from 'mongoose';
import { sharedCrudService } from '../shared/sharedCrud.services';
import { PROPERTY_REPOSITORY } from 'src/constants';
import { IPropertyDocument } from './property.schema';

@Injectable()
export class PropertyService extends sharedCrudService {
  constructor(

    @Inject(PROPERTY_REPOSITORY)

    readonly propertyRepository: Model<IPropertyDocument>,

  ) {
    super(propertyRepository);
  }

  async propertyLisitng(
    page: number,
    resPerPage: number,
    search: string,
  ): Promise<any> {
    const query = [];
    query.push({ id: { $exists: true } });

    if (search) query.push({ title: { $regex: search, $options: 'i' } });

    const [listings, tLisitngsCount] = await Promise.all([
      this.propertyRepository
        .find({ $and: [...query] })
        .sort({ createdAt: -1 })
        .skip(resPerPage * (page - 1))
        .limit(resPerPage)
        .exec(),
      this.propertyRepository.countDocuments({ $and: [...query] }).exec(),
    ]);

    return {
      listings,
      current_page: page,
      pages: Math.ceil(tLisitngsCount / resPerPage),
      total_listings: tLisitngsCount,
      per_page: resPerPage,
    };
  }

  /**

   * It will recieve array of IDs to get the recommended listing

   * @param page

   * @param resPerPage

   * @param search

   * @returns

   */
  async propertyLisitngRecommendedSearch(
    page: number,
    resPerPage: number,
    recommendations: string[],
  ): Promise<any> {
    const query = [];

    // Check if recommendations is a string and split it into an array
    if (typeof recommendations === 'string') {
      //@ts-ignore
      recommendations = recommendations.split(',');
    }

    query.push({ id: { $exists: true } });

    // Modify the query to handle an array of IDs
    if (recommendations && recommendations.length > 0) {
      query.push({ id: { $in: recommendations } });
    }

    const [listings, tLisitngsCount] = await Promise.all([
      this.propertyRepository
        .find({ $and: [...query] })
        .sort({ createdAt: -1 })
        .skip(resPerPage * (page - 1))
        .limit(resPerPage)
        .exec(),
      this.propertyRepository.countDocuments({ $and: [...query] }).exec(),
    ]);

    return {
      listings,
      current_page: page,
      pages: Math.ceil(tLisitngsCount / resPerPage),
      total_listings: tLisitngsCount,
      per_page: resPerPage,
    };
  }
}