File size: 3,422 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {
  Body,
  Controller,
  Get,
  HttpStatus,
  Post,
  Query,
  Req,
  Res,
  UseGuards,
} from '@nestjs/common';
import { Response } from 'express';
import { CommonServices } from '../shared/common.service';
import { PropertyService } from './property.service';
import { v4 as uuidv4 } from 'uuid';

@Controller('property')
export class PropertyController extends CommonServices {
  constructor(private readonly propertyService: PropertyService) {
    super();
  }

  @Get('')
  async getPropertListings(@Res() res: Response, @Req() req): Promise<any> {
    try {
      const response = await this.propertyService.sharedFind({});
      return this.sendResponse(
        this.messages.Success,
        response ?? 'NOT_FOUND',
        HttpStatus.OK,
        res,
      );
    } catch (error) {
      return this.sendResponse(
        'Internal server Error',
        {},
        HttpStatus.INTERNAL_SERVER_ERROR,
        res,
      );
    }
  }

  /**

   *

   * @param query

   * @param res

   * @param req

   */
  @Get('/listings/dashboard')
  async getDashboardLisings(@Res() res, @Req() req) {
    try {
      const listings = await this.propertyService.propertyRepository
        .find({})
        .sort({ _id: -1 })
        .limit(20);

      this.sendResponse(this.messages.Success, listings, HttpStatus.OK, res);
    } catch (error) {
      console.error('error', error);
      this.sendResponse(
        this.messages.Error,
        null,
        HttpStatus.INTERNAL_SERVER_ERROR,
        res,
      );
    }
  }

  /**

   *

   * @param query

   * @param res

   * @param req

   */
  @Get('/listings')
  async getDashboardActiveUsers(@Query() query, @Res() res, @Req() req) {
    try {

      const page = Number(query.page);
      const resPerPage = query.resPerPage ? Number(query.resPerPage) : 20;
      const search = query.search || ''; // Default to empty search if not provided
      let sessionId = null;

      if (!query.sessionId) sessionId = uuidv4();

      let listings;
      if (!query.sessionId) {
        listings = await this.propertyService.propertyLisitng(
          page,
          resPerPage,
          search,
        );
      } else {
        listings = await this.propertyService.propertyLisitngRecommendedSearch(
          page,
          resPerPage,
          query.recommendations,
        );
      }

      this.sendResponse(
        this.messages.Success,
        { listings, sessionId: sessionId },
        HttpStatus.OK,
        res,
      );
    } catch (error) {
      console.error('error', error);
      this.sendResponse(
        this.messages.Error,
        null,
        HttpStatus.INTERNAL_SERVER_ERROR,
        res,
      );
    }
  }

  /**

   *

   * @param query

   * @param res

   * @param req

   */
  @Get('/detail')
  async getPropertyDetails(@Query() query, @Res() res, @Req() req) {
    try {
      // property id
      const id = query.id;

      const property =
        await this.propertyService.propertyRepository.findById(id);

      this.sendResponse(this.messages.Success, property, HttpStatus.OK, res);
    } catch (error) {
      console.error('error', error);
      this.sendResponse(
        this.messages.Error,
        null,
        HttpStatus.INTERNAL_SERVER_ERROR,
        res,
      );
    }
  }
}