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

Chore : ai classification 수정 - response에 metadata , folderId,isRead 추가 #66

Merged
merged 3 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
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
35 changes: 23 additions & 12 deletions src/modules/classification/classification.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Patch,
} from '@nestjs/common';
import { ClassificationService } from './classification.service';
import { GetUser, PaginationQuery } from '@src/common';
import { GetUser, PaginationMetadata, PaginationQuery } from '@src/common';
import {
ClassificationControllerDocs,
GetAIFolderNameListDocs,
Expand Down Expand Up @@ -37,29 +37,40 @@ export class ClassificationController {
@GetAIPostListDocs
async getSuggestedPostList(
@GetUser() userId: string,
@Query() paingQuery: PaginationQuery,
@Query() pagingQuery: PaginationQuery,
) {
const posts = await this.classificationService.getPostList(
userId,
paingQuery,
const { count, classificationPostList } =
await this.classificationService.getPostList(userId, pagingQuery);

const metadata = new PaginationMetadata(
pagingQuery.page,
pagingQuery.limit,
count,
);

return new AIPostListResponse(posts);
return new AIPostListResponse(metadata, classificationPostList);
}
@Get('/posts/:folderId')
@GetAIPostListDocs
async getSuggestedPostListInFolder(
@GetUser() userId: string,
@Param('folderId') folderId: string,
@Query() paingQuery: PaginationQuery,
@Query() pagingQuery: PaginationQuery,
) {
const posts = await this.classificationService.getPostListInFolder(
userId,
folderId,
paingQuery,
const { count, classificationPostList } =
await this.classificationService.getPostListInFolder(
userId,
folderId,
pagingQuery,
);

const metadata = new PaginationMetadata(
pagingQuery.page,
pagingQuery.limit,
count,
);

return new AIPostListResponse(posts);
return new AIPostListResponse(metadata, classificationPostList);
}
@Patch('/posts')
@PatchAIPostDocs
Expand Down
40 changes: 40 additions & 0 deletions src/modules/classification/classification.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,46 @@ export class ClassficiationRepository {
private readonly aiClassificationModel: Model<AIClassification>,
) {}

async getClassificationPostCount(
userId: string,
suggestedFolderId?: string,
): Promise<number> {
const result = await this.aiClassificationModel
.aggregate([
{
$match: {
deletedAt: null,
},
},
{
$lookup: {
from: 'posts',
localField: '_id',
foreignField: 'aiClassificationId',
as: 'post',
},
},
{
$unwind: '$post',
},
{
$match: {
'post.userId': new Types.ObjectId(userId),
...(suggestedFolderId && {
suggestedFolderId: new Types.ObjectId(suggestedFolderId),
}),
},
},
{
$count: 'count',
},
])
.exec();

const count = result[0]?.count || 0;
return count;
}

async createClassification(
url: string,
description: string,
Expand Down
46 changes: 29 additions & 17 deletions src/modules/classification/classification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ClassificationFolderWithCount,
PostListInClassificationFolder,
} from './dto/classification.dto';
import { PaginationQuery } from '@src/common';
import { PaginationQuery, sum } from '@src/common';
import { PostListInFolderResponse } from '../folders/responses';

@Injectable()
Expand All @@ -34,41 +34,53 @@ export class ClassificationService {
}

async getPostList(userId: string, paingQuery: PaginationQuery) {
const orderedFolderIdList = await this.getFolderOrder(userId);
const { count, orderedFolderIdList } =
await this.getFolderCountAndOrder(userId);

const offset = (paingQuery.page - 1) * paingQuery.limit;
return await this.postRepository.findAndSortBySuggestedFolderIds(
new Types.ObjectId(userId),
orderedFolderIdList,
offset,
paingQuery.limit,
);
const classificationPostList =
await this.postRepository.findAndSortBySuggestedFolderIds(
new Types.ObjectId(userId),
orderedFolderIdList,
offset,
paingQuery.limit,
);

return { count, classificationPostList };
}

async getFolderOrder(userId: string) {
async getFolderCountAndOrder(userId: string) {
const orderedFolderList =
await this.classficationRepository.findContainedFolderByUserId(
new Types.ObjectId(userId),
);

return orderedFolderList.map(
const count = sum(orderedFolderList, (folder) => folder.postCount);
const orderedFolderIdList = orderedFolderList.map(
(folder) => new Types.ObjectId(folder.folderId),
);

return { count, orderedFolderIdList };
}

async getPostListInFolder(
userId: string,
folderId: string,
paingQuery: PaginationQuery,
): Promise<PostListInClassificationFolder[]> {
) {
const offset = (paingQuery.page - 1) * paingQuery.limit;

return await this.postRepository.findBySuggestedFolderId(
userId,
new Types.ObjectId(folderId),
offset,
paingQuery.limit,
);
const [count, classificationPostList] = await Promise.all([
this.classficationRepository.getClassificationPostCount(userId, folderId),
this.postRepository.findBySuggestedFolderId(
userId,
new Types.ObjectId(folderId),
offset,
paingQuery.limit,
),
]);

return { count, classificationPostList };
}
async moveAllPostTosuggestionFolder(
userId: string,
Expand Down
2 changes: 2 additions & 0 deletions src/modules/classification/dto/classification.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface ClassificationFolderWithCount {
export interface PostListInClassificationFolder {
postId: string;

folderId: string;

title: string;

url: string;
Expand Down
14 changes: 12 additions & 2 deletions src/modules/classification/response/ai-post-list.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ import {
ClassificationPostList,
PostListInClassificationFolder,
} from '../dto/classification.dto';
import { PaginationMetadata } from '@src/common';

export class AIPostListResponse {
@ApiProperty({
type: PaginationMetadata,
})
metadata: PaginationMetadata;

@ApiProperty()
list: PostListInClassificationFolder[] | ClassificationPostList[];

constructor(
data: PostListInClassificationFolder[] | ClassificationPostList[],
metaData: PaginationMetadata,
classificationPostList:
| PostListInClassificationFolder[]
| ClassificationPostList[],
) {
this.list = data;
this.metadata = metaData;
this.list = classificationPostList;
}
}
21 changes: 17 additions & 4 deletions src/modules/posts/posts.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,20 @@ export class PostsRepository {
{
$project: {
_id: 0,
folderId: { $toString: '$aiClassification.suggestedFolderId' },
postId: { $toString: '$_id' },
title: 1,
url: 1,
description: 1,
createdAt: 1,
isRead: 1,
'aiClassification.keywords': 1,
isRead: {
$cond: {
if: { $eq: ['$readAt', null] },
then: false,
else: true,
},
},
keywords: '$aiClassification.keywords',
},
},
])
Expand Down Expand Up @@ -266,8 +273,14 @@ export class PostsRepository {
url: 1,
description: 1,
createdAt: 1,
isRead: 1,
'aiClassification.keywords': 1,
isRead: {
$cond: {
if: { $eq: ['$readAt', null] },
then: false,
else: true,
},
},
keywords: '$aiClassification.keywords',
},
},
])
Expand Down