Skip to content

Commit

Permalink
Merge pull request #21 from mash-up-kr/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pjw5521 authored Jul 4, 2024
2 parents b66e70e + 817d5c6 commit fbbb1fa
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
21 changes: 17 additions & 4 deletions src/controller/keyword.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import _ from 'lodash';
import CustomError from '../errors/CustomError';
import { HttpCode } from '../errors/HttpCode';
import { CustomRequest } from '../middleware/requestedInfo';
import { IKeywordUpdatePayload } from '../model/keyword';
import { IKeywordDocument, IKeywordUpdatePayload, IKeywordWithImage } from '../model/keyword';
import * as KeywordService from '../service/keyword.service';
import * as MemeService from '../service/meme.service';
import { logger } from '../util/logger';
Expand Down Expand Up @@ -39,9 +39,22 @@ const deleteKeyword = async (req: CustomRequest, res: Response, next: NextFuncti
const getTopKeywords = async (req: Request, res: Response, next: NextFunction) => {
const limit = 6;
try {
const topKeywords = await KeywordService.getTopKeywords(limit);
logger.info(`Get top ${limit} keywords: ${JSON.stringify(topKeywords)}`);
return res.json(createSuccessResponse(HttpCode.OK, 'Get Top Keywords', topKeywords));
const topKeywords: IKeywordDocument[] = await KeywordService.getTopKeywords(limit);
// 키워드에 해당하는 밈 이미지 가져오기
const promises: Promise<IKeywordWithImage>[] = topKeywords.map(async (keyword: IKeywordDocument) => {
try {
const topReactionImage: string = await MemeService.getTopReactionImage(keyword);
return { ...keyword, topReactionImage } as IKeywordWithImage;
} catch (error) {
logger.error(`Error retrieving top reaction image for keyword: ${keyword}`, error);
throw new CustomError(`Failed to get top reaction image`, HttpCode.INTERNAL_SERVER_ERROR);
}
});

const keywordWithImages: IKeywordWithImage[] = await Promise.all(promises);

logger.info(`Get top ${limit} keywords: ${JSON.stringify(keywordWithImages)}`);
return res.json(createSuccessResponse(HttpCode.OK, 'Get Top Keywords', keywordWithImages));
} catch (err) {
return next(new CustomError(err.message, err.status || HttpCode.INTERNAL_SERVER_ERROR));
}
Expand Down
7 changes: 5 additions & 2 deletions src/controller/meme.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,13 @@ const createMemeWatch = async (req: CustomRequest, res: Response, next: NextFunc

return res.json(createSuccessResponse(HttpCode.CREATED, 'Crate Meme Watch', result));
} else if (type == MemeWatchType.RECOMMEND) {
const memeRecommendWatch = await UserService.createMemeRecommendWatch(user, meme);
const [recommendMemeWatchCount, _]: [number, any] = await Promise.all([
UserService.createMemeRecommendWatch(user, meme),
UserService.updateLastSeenMeme(user, meme),
]);

return res.json(
createSuccessResponse(HttpCode.CREATED, `${type} Meme Watch`, memeRecommendWatch),
createSuccessResponse(HttpCode.CREATED, `${type} Meme Watch`, recommendMemeWatchCount),
);
} else {
return next(new CustomError(`Invalid 'type' parameter.`, HttpCode.BAD_REQUEST));
Expand Down
4 changes: 4 additions & 0 deletions src/model/keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export interface IKeywordUpdatePayload {
category?: string;
}

export interface IKeywordWithImage extends IKeyword {
topReactionImage: string;
}

export interface IKeyword {
name: string;
category: string;
Expand Down
1 change: 1 addition & 0 deletions src/model/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface IUserInfos extends IUser {
reaction: number;
save: number;
level: number;
memeRecommendWatchCount: number;
}

export interface IUserDocument extends Document {
Expand Down
14 changes: 14 additions & 0 deletions src/service/meme.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ async function deleteMemeSave(user: IUserDocument, meme: IMemeDocument): Promise
throw new CustomError(`Failed delete memeSave(${err.message})`, HttpCode.INTERNAL_SERVER_ERROR);
}
}
async function getTopReactionImage(keyword: IKeywordDocument): Promise<string> {
try {
const topReactionMeme = await MemeModel.findOne({ keywordIds: keyword._id }).sort({ reaction: -1 });

logger.info(`Get top reaction meme - keyword(${keyword.name}), meme(${topReactionMeme._id})`);
return topReactionMeme.image;
}catch (err) {
logger.error(`Failed get top reaction meme`, err.message);
throw new CustomError(`Failed get top reaction meme(${err.message})`, HttpCode.INTERNAL_SERVER_ERROR);
}
}


export {
getMeme,
Expand All @@ -263,4 +275,6 @@ export {
deleteKeywordOfMeme,
getMemeWithKeywords,
searchMemeByKeyword,
getTopReactionImage,
};

20 changes: 13 additions & 7 deletions src/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
IMemeRecommendWatchCreatePayload,
} from '../model/memeRecommendWatch';
import { logger } from '../util/logger';
import { startOfWeek, format } from 'date-fns';
import { startOfWeek } from 'date-fns';

async function getUser(deviceId: string): Promise<IUserDocument | null> {
try {
Expand Down Expand Up @@ -47,12 +47,20 @@ async function createUser(deviceId: string): Promise<IUserInfos> {
countInteractionType(InteractionType.SAVE),
]);

const todayWeekStart = startOfWeek(new Date(), { weekStartsOn: 1 });
const memeRecommendWatchCount = await MemeRecommendWatchModel.countDocuments({
startDate: todayWeekStart,
deviceId: foundUser.deviceId,
isDeleted: false,
});

return {
...foundUser.toObject(),
watch,
reaction,
save,
share,
memeRecommendWatchCount: memeRecommendWatchCount,
level: 1,
};
}
Expand Down Expand Up @@ -153,13 +161,11 @@ async function getSavedMeme(user: IUserDocument): Promise<IMemeDocument[]> {
}
}

async function createMemeRecommendWatch(
user: IUserDocument,
meme: IMemeDocument,
): Promise<boolean> {
async function createMemeRecommendWatch(user: IUserDocument, meme: IMemeDocument): Promise<number> {
try {
const todayWeekStart = startOfWeek(new Date(), { weekStartsOn: 1 });
const memeRecommendWatch = await MemeRecommendWatchModel.findOne({
memeId: meme._id,
startDate: todayWeekStart,
deviceId: user.deviceId,
isDeleted: false,
Expand All @@ -176,7 +182,7 @@ async function createMemeRecommendWatch(
{ _id: memeRecommendWatch._id },
{ $set: updatePayload },
);
return true;
return updatePayload.memeIds.length;
}

const createPayload: IMemeRecommendWatchCreatePayload = {
Expand All @@ -187,7 +193,7 @@ async function createMemeRecommendWatch(

await MemeRecommendWatchModel.create(createPayload);

return true;
return 1;
} catch (err) {
logger.error(`Failed create memeRecommendWatch`, err.message);
throw new CustomError(
Expand Down

0 comments on commit fbbb1fa

Please sign in to comment.