Skip to content

Commit

Permalink
feat: Add post keywords by ai classification
Browse files Browse the repository at this point in the history
  • Loading branch information
Marades committed Jul 17, 2024
1 parent 6ca291e commit 979b5bb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
44 changes: 41 additions & 3 deletions src/ai_handler.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
import { Handler } from 'aws-lambda';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { MongooseModule } from '@nestjs/mongoose';
import { AiService } from '@src/infrastructure/ai/ai.service';
import { AppModule } from '@src/app.module';
import { Handler } from 'aws-lambda';
import {
DatabaseModule,
Folder,
FolderSchema,
Post,
PostSchema,
} from './infrastructure';
import { AiModule } from './infrastructure/ai/ai.module';
import { LambdaEventPayload } from './infrastructure/aws-lambda/type';
import { ClassficiationRepository } from './modules/classification/classification.repository';
import { PostKeywordsRepository } from './modules/posts/postKeywords.repository';
import { PostsRepository } from './modules/posts/posts.repository';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
cache: true,
envFilePath: `.env.${process.env.NODE_ENV || 'local'}`,
}),
DatabaseModule,
MongooseModule.forFeature([
{ name: Post.name, schema: PostSchema },
{ name: Folder.name, schema: FolderSchema },
]),
AiModule,
],
providers: [
ClassficiationRepository,
PostsRepository,
PostKeywordsRepository,
],
})
class WorkerModule {}

export const handler: Handler = async (event: LambdaEventPayload) => {
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(WorkerModule);
const aiService = app.get(AiService);
const classificationRepository = app.get(ClassficiationRepository);
const postRepository = app.get(PostsRepository);
const postKeywordsRepository = app.get(PostKeywordsRepository);

// Map - (Folder Name):(Folder ID)
const folderMapper = {};
Expand Down Expand Up @@ -41,6 +75,10 @@ export const handler: Handler = async (event: LambdaEventPayload) => {
classification._id.toString(),
summarizeUrlContent.response.summary,
);
await postKeywordsRepository.createPostKeywords(
postId,
summarizeUrlContent.response.keywords,
);
}

// NOTE: cloud-watch 로그 확인용
Expand Down
12 changes: 6 additions & 6 deletions src/modules/folders/folders.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { CreateFolderDto, UpdateFolderDto } from './dto/mutate-folder.dto';
import { sum } from '@src/common';
import { FolderType } from '@src/infrastructure/database/types/folder-type.enum';
import { Schema as MongooseSchema } from 'mongoose';
import { FolderRepository } from './folders.repository';
import { PostsRepository } from '../posts/posts.repository';
import { FolderType } from '@src/infrastructure/database/types/folder-type.enum';
import { sum } from '@src/common';
import { FolderListServiceDto } from './dto/folder-with-count.dto';
import { CreateFolderDto, UpdateFolderDto } from './dto/mutate-folder.dto';
import { FolderRepository } from './folders.repository';

@Injectable()
export class FoldersService {
Expand Down Expand Up @@ -67,15 +67,15 @@ export class FoldersService {
userId: new MongooseSchema.Types.ObjectId(userId),
postCount: favoritePostCount,
};
const defaultFolderTmp = {
const readLater = {
id: defaultFolder.id,
name: defaultFolder.name,
type: FolderType.DEFAULT,
userId: new MongooseSchema.Types.ObjectId(userId),
postCount: allPostCount - customFoldersPostCount,
};

const defaultFolders = [all, favorite, defaultFolderTmp].filter(
const defaultFolders = [all, favorite, readLater].filter(
(folder) => !!folder,
);
return { defaultFolders, customFolders };
Expand Down
21 changes: 21 additions & 0 deletions src/modules/posts/postKeywords.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { PostKeyword } from '@src/infrastructure/database/schema/postKeyword.schema';
import { Model } from 'mongoose';

@Injectable()
export class PostKeywordsRepository {
constructor(
@InjectModel(PostKeyword.name)
private readonly postKeywordModel: Model<PostKeyword>,
) {}

async createPostKeywords(postId: string, keywords: string[]) {
const postKeywords = keywords.map((keyword) => ({
postId,
keyword,
}));

await this.postKeywordModel.insertMany(postKeywords);
}
}

0 comments on commit 979b5bb

Please sign in to comment.