Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] - ค้นหาเหตุการณ์ด่วน ผลลัพธ์มาไม่ครบ #351 #357

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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