Abdul Rehman
commited on
Commit
·
17b82c4
1
Parent(s):
a569cab
recommendations with API
Browse files
src/modules/property/property.controller.ts
CHANGED
|
@@ -75,6 +75,7 @@ export class PropertyController extends CommonServices {
|
|
| 75 |
@Get('/listings')
|
| 76 |
async getDashboardActiveUsers(@Query() query, @Res() res, @Req() req) {
|
| 77 |
try {
|
|
|
|
| 78 |
const page = Number(query.page);
|
| 79 |
const resPerPage = query.resPerPage ? Number(query.resPerPage) : 20;
|
| 80 |
const search = query.search || ''; // Default to empty search if not provided
|
|
@@ -82,11 +83,20 @@ export class PropertyController extends CommonServices {
|
|
| 82 |
|
| 83 |
if (!query.sessionId) sessionId = uuidv4();
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
this.sendResponse(
|
| 92 |
this.messages.Success,
|
|
|
|
| 75 |
@Get('/listings')
|
| 76 |
async getDashboardActiveUsers(@Query() query, @Res() res, @Req() req) {
|
| 77 |
try {
|
| 78 |
+
|
| 79 |
const page = Number(query.page);
|
| 80 |
const resPerPage = query.resPerPage ? Number(query.resPerPage) : 20;
|
| 81 |
const search = query.search || ''; // Default to empty search if not provided
|
|
|
|
| 83 |
|
| 84 |
if (!query.sessionId) sessionId = uuidv4();
|
| 85 |
|
| 86 |
+
let listings;
|
| 87 |
+
if (!query.sessionId) {
|
| 88 |
+
listings = await this.propertyService.propertyLisitng(
|
| 89 |
+
page,
|
| 90 |
+
resPerPage,
|
| 91 |
+
search,
|
| 92 |
+
);
|
| 93 |
+
} else {
|
| 94 |
+
listings = await this.propertyService.propertyLisitngRecommendedSearch(
|
| 95 |
+
page,
|
| 96 |
+
resPerPage,
|
| 97 |
+
query.recommendations,
|
| 98 |
+
);
|
| 99 |
+
}
|
| 100 |
|
| 101 |
this.sendResponse(
|
| 102 |
this.messages.Success,
|
src/modules/property/property.service.ts
CHANGED
|
@@ -41,4 +41,50 @@ export class PropertyService extends sharedCrudService {
|
|
| 41 |
per_page: resPerPage,
|
| 42 |
};
|
| 43 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
|
|
|
| 41 |
per_page: resPerPage,
|
| 42 |
};
|
| 43 |
}
|
| 44 |
+
|
| 45 |
+
/**
|
| 46 |
+
* It will recieve array of IDs to get the recommended listing
|
| 47 |
+
* @param page
|
| 48 |
+
* @param resPerPage
|
| 49 |
+
* @param search
|
| 50 |
+
* @returns
|
| 51 |
+
*/
|
| 52 |
+
async propertyLisitngRecommendedSearch(
|
| 53 |
+
page: number,
|
| 54 |
+
resPerPage: number,
|
| 55 |
+
recommendations: string[],
|
| 56 |
+
): Promise<any> {
|
| 57 |
+
const query = [];
|
| 58 |
+
|
| 59 |
+
// Check if recommendations is a string and split it into an array
|
| 60 |
+
if (typeof recommendations === 'string') {
|
| 61 |
+
//@ts-ignore
|
| 62 |
+
recommendations = recommendations.split(',');
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
query.push({ id: { $exists: true } });
|
| 66 |
+
|
| 67 |
+
// Modify the query to handle an array of IDs
|
| 68 |
+
if (recommendations && recommendations.length > 0) {
|
| 69 |
+
query.push({ id: { $in: recommendations } });
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
const [listings, tLisitngsCount] = await Promise.all([
|
| 73 |
+
this.propertyRepository
|
| 74 |
+
.find({ $and: [...query] })
|
| 75 |
+
.sort({ createdAt: -1 })
|
| 76 |
+
.skip(resPerPage * (page - 1))
|
| 77 |
+
.limit(resPerPage)
|
| 78 |
+
.exec(),
|
| 79 |
+
this.propertyRepository.countDocuments({ $and: [...query] }).exec(),
|
| 80 |
+
]);
|
| 81 |
+
|
| 82 |
+
return {
|
| 83 |
+
listings,
|
| 84 |
+
current_page: page,
|
| 85 |
+
pages: Math.ceil(tLisitngsCount / resPerPage),
|
| 86 |
+
total_listings: tLisitngsCount,
|
| 87 |
+
per_page: resPerPage,
|
| 88 |
+
};
|
| 89 |
+
}
|
| 90 |
}
|