Skip to content

Commit

Permalink
#355 คำนวน Like Count ผิดเมื่อมีคนกด Like มากกว่า 1 คน
Browse files Browse the repository at this point in the history
  • Loading branch information
junsudas committed Sep 29, 2021
1 parent c191dd7 commit 80a91f9
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions api-spanboon/src/api/controllers/PostsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,6 @@ export class PostsController {
const contentType = ENGAGEMENT_CONTENT_TYPE.POST;

let userEngagementAction: UserEngagement;
let userLiked: UserLike[];
let result = {};
let userLikeStmt;
let action;
Expand Down Expand Up @@ -879,7 +878,7 @@ export class PostsController {

engagement = await this.userEngagementService.getEngagement(postObjId, userObjId, action, contentType, likeAsPageObjId);

userLikeStmt = { where: { subjectId: postObjId, subjectType: LIKE_TYPE.POST, likeAsPage: likeAsPageObjId } };
userLikeStmt = { where: { subjectId: postObjId, subjectType: LIKE_TYPE.POST } };
} else {
userEngagement.likeAsPage = null;

Expand All @@ -895,8 +894,16 @@ export class PostsController {
}

userEngagementAction = await this.userEngagementService.create(userEngagement);
userLiked = await this.userLikeService.find(userLikeStmt);
likeCount = userLiked.length;
const matchStmt: any = [
{ $match: userLikeStmt.where },
{ $group: { _id: '$subjectId', count: { $sum: 1 } } }
];
const countLikeAggr: any[] = await this.userLikeService.aggregate(matchStmt);
if (countLikeAggr && countLikeAggr.length > 0) {
likeCount = countLikeAggr[0].count;
} else {
likeCount = 0;
}

result['isLike'] = false;
result['likeCount'] = likeCount;
Expand Down Expand Up @@ -962,7 +969,7 @@ export class PostsController {

engagement = await this.userEngagementService.getEngagement(postObjId, userObjId, action, contentType, likeAsPageObjId);

userLikeStmt = { where: { subjectId: postObjId, subjectType: LIKE_TYPE.POST, likeAsPage: likeAsPageObjId } };
userLikeStmt = { where: { subjectId: postObjId, subjectType: LIKE_TYPE.POST } };
} else {
userEngagement.likeAsPage = null;

Expand All @@ -978,8 +985,17 @@ export class PostsController {
}

userEngagementAction = await this.userEngagementService.create(userEngagement);
userLiked = await this.userLikeService.find(userLikeStmt);
likeCount = userLiked.length;

const matchStmt: any = [
{ $match: userLikeStmt.where },
{ $group: { _id: '$subjectId', count: { $sum: 1 } } }
];
const countLikeAggr: any[] = await this.userLikeService.aggregate(matchStmt);
if (countLikeAggr && countLikeAggr.length > 0) {
likeCount = countLikeAggr[0].count;
} else {
likeCount = 0;
}

result['isLike'] = true;
result['likeCount'] = likeCount;
Expand Down

0 comments on commit 80a91f9

Please sign in to comment.