Skip to content

Commit

Permalink
[API] - ค้นหาเหตุการณ์ด่วน ผลลัพธ์มาไม่ครบ #351
Browse files Browse the repository at this point in the history
  • Loading branch information
junsudas committed Sep 29, 2021
1 parent 80a91f9 commit 2800ecf
Showing 1 changed file with 44 additions and 35 deletions.
79 changes: 44 additions & 35 deletions api-spanboon/src/api/controllers/EmergencyEventController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 2800ecf

Please sign in to comment.