diff --git a/api-spanboon/src/api/controllers/EmergencyEventController.ts b/api-spanboon/src/api/controllers/EmergencyEventController.ts index ae2855c68..f16544765 100644 --- a/api-spanboon/src/api/controllers/EmergencyEventController.ts +++ b/api-spanboon/src/api/controllers/EmergencyEventController.ts @@ -36,6 +36,7 @@ import { EmergencyLastestProcessor } from '../processors/emergency/EmergencyLast import { EmergencyShareProcessor } from '../processors/emergency/EmergencyShareProcessor'; import { EmergencyPostLikedProcessor } from '../processors/emergency/EmergencyPostLikedProcessor'; import { DateTimeUtil } from '../../utils/DateTimeUtil'; +import { SearchFilter } from './requests/SearchFilterRequest'; @JsonController('/emergency') export class EmergencyEventController { @@ -115,53 +116,61 @@ export class EmergencyEventController { const filter = search.filter; const hashTag = search.hashTag; - const hashTagIdList = []; - const hashTagMap = {}; - if (hashTag !== null && hashTag !== undefined && hashTag !== '') { - filter.whereConditions = { name: { $regex: '.*' + hashTag + '.*', $options: 'si' } }; - } else { - filter.whereConditions = {}; + // whereConditions will be in object search only + const emergencyEventAggr: any[] = []; + if (filter.whereConditions !== undefined && typeof filter.whereConditions === 'object') { + emergencyEventAggr.push({ $match: filter.whereConditions }); } - const hashTagList: HashTag[] = await this.hashTagService.search(filter); - - if (hashTagList !== null && hashTagList !== undefined && hashTagList.length > 0) { - for (const masterHashTag of hashTagList) { - const id = masterHashTag.id; - hashTagMap[id] = masterHashTag; - hashTagIdList.push(new ObjectID(id)); - } - } else { - const successResponse = ResponseUtil.getSuccessResponse('Hashtag Not Found', []); - return res.status(200).send(successResponse); + if (filter.offset === undefined) { + filter.offset = 0; } - - let emergencyLists: EmergencyEvent[]; - - if (hashTagIdList !== null && hashTagIdList !== undefined && hashTagIdList.length > 0) { - emergencyLists = await this.emergencyEventService.find({ hashTag: { $in: hashTagIdList } }); - } else { - emergencyLists = await this.emergencyEventService.find(); + if (filter.limit === undefined) { + filter.limit = 10; } - if (emergencyLists !== null && emergencyLists !== undefined) { - emergencyLists.map((data) => { - const hashTagKey = data.hashTag; - const emergencyHashTag = hashTagMap[hashTagKey]; - - if (emergencyHashTag) { - const hashTagName = emergencyHashTag.name; + emergencyEventAggr.push({ + $lookup: { + from: 'HashTag', + localField: 'hashTag', + foreignField: '_id', + as: 'hasTagObj' + } + }); + emergencyEventAggr.push({ + $unwind: { + path: '$hasTagObj', + preserveNullAndEmptyArrays: true + } + }); + if (hashTag !== null && hashTag !== undefined && hashTag !== '') { + emergencyEventAggr.push({ + $match: { + 'hasTagObj.name': { $regex: '.*' + hashTag + '.*', $options: 'si' } + } + }); + } + emergencyEventAggr.push({ $skip: filter.offset }); + emergencyEventAggr.push({ $limit: filter.limit }); + emergencyEventAggr.push({ $addFields: { id: '$_id' } }); + emergencyEventAggr.push({ $project: { '_id': 0 } }); + + const emergencyEventAggResult = await this.emergencyEventService.aggregate(emergencyEventAggr); + + if (emergencyEventAggResult !== null && emergencyEventAggResult !== undefined) { + // change hashTag from id to name string + emergencyEventAggResult.map((data) => { + if (data.hasTagObj) { + const hashTagName = data.hasTagObj.name; data.hashTag = hashTagName; } }); - const emergencyResult = ObjectUtil.removeDuplicateJSONValue(emergencyLists, data => data.hashTag); - - const successResponse = ResponseUtil.getSuccessResponse('Successfully Search EmergencyEvent', emergencyResult); + const successResponse = ResponseUtil.getSuccessResponse('Successfully Search EmergencyEvent', emergencyEventAggResult); return res.status(200).send(successResponse); } else { - const errorResponse = ResponseUtil.getSuccessResponse('EmergencyEvent Not Found', undefined); + const errorResponse = ResponseUtil.getSuccessResponse('EmergencyEvent Not Found', []); return res.status(200).send(errorResponse); } }