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

추천 밈 보기 로직 개선, MemeRecommendWatchModel schema 변경 #35

Merged
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
16 changes: 5 additions & 11 deletions src/model/memeRecommendWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,20 @@ import mongoose, { Schema, Document, Types } from 'mongoose';
export interface IMemeRecommendWatchCreatePayload {
deviceId: string;
startDate: Date;
memeIds: Types.ObjectId[];
}

export interface IMemeRecommendWatchUpdatePayload {
deviceId?: string;
startDate?: Date;
memeIds?: Types.ObjectId[];
memeId: Types.ObjectId;
}

export interface IMemeRecommendWatch {
deviceId: string;
startDate: Date;
memeIds: Types.ObjectId[];
memeId: Types.ObjectId;
}

export interface IMemeRecommendWatchDocument extends Document {
_id: Types.ObjectId;
deviceId: string;
startDate: Date;
memeIds: Types.ObjectId[];
memeId: Types.ObjectId;
createdAt: Date;
updatedAt: Date;
}
Expand All @@ -32,7 +26,7 @@ const MemeRecommendWatchSchema: Schema = new Schema(
deviceId: { type: String, required: true },
startDate: { type: Date, required: true },
isDeleted: { type: Boolean, required: true, default: false },
memeIds: [{ type: Types.ObjectId, ref: 'Meme', required: true }],
memeId: { type: Types.ObjectId, ref: 'Meme', required: true },
},
{
timestamps: true,
Expand All @@ -42,6 +36,6 @@ const MemeRecommendWatchSchema: Schema = new Schema(
);

export const MemeRecommendWatchModel = mongoose.model<IMemeRecommendWatchDocument>(
'MemeRecommendWatch',
'memeRecommendWatch',
MemeRecommendWatchSchema,
);
45 changes: 22 additions & 23 deletions src/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { IMemeDocument, IMemeGetResponse, MemeModel } from '../model/meme';
import { InteractionType, MemeInteractionModel } from '../model/memeInteraction';
import {
MemeRecommendWatchModel,
IMemeRecommendWatchUpdatePayload,
IMemeRecommendWatchCreatePayload,
} from '../model/memeRecommendWatch';
import { IUser, IUserDocument, IUserInfos, UserModel } from '../model/user';
Expand Down Expand Up @@ -202,40 +201,40 @@ async function getSavedMemeList(
async function createMemeRecommendWatch(user: IUserDocument, meme: IMemeDocument): Promise<number> {
try {
const todayWeekStart = startOfWeek(new Date(), { weekStartsOn: 1 });

const memeRecommendWatch = await MemeRecommendWatchModel.findOne({
memeIds: meme._id,
memeId: meme._id,
startDate: todayWeekStart,
deviceId: user.deviceId,
isDeleted: false,
});

if (!_.isNull(memeRecommendWatch)) {
logger.info(`Already watched recommend meme - deviceId(${user.deviceId})`);

const updatedMemeList = Array.from(
new Set([...memeRecommendWatch.memeIds, meme._id].map((id) => id.toString())),
).map((id) => new Types.ObjectId(id));

const updatePayload: IMemeRecommendWatchUpdatePayload = {
memeIds: updatedMemeList,
// 해당 추천 밈을 처음 보는 경우
if (_.isNull(memeRecommendWatch)) {
// 추천 밈 본 기록 남기기
const createPayload: IMemeRecommendWatchCreatePayload = {
deviceId: user.deviceId,
startDate: startOfWeek(new Date(), { weekStartsOn: 1 }),
memeId: meme._id,
};

await MemeRecommendWatchModel.findOneAndUpdate(
{ _id: memeRecommendWatch._id },
{ $set: updatePayload },
);
return updatePayload.memeIds.length;
// MemeRecommendWatch에 memeId 추가
// MemeInteraction에 memeId의 watch 타입 추가
await Promise.all([
MemeRecommendWatchModel.create(createPayload),
MemeService.createMemeInteraction(user, meme, InteractionType.WATCH),
]);
} else {
logger.info(`Already watched recommend meme - deviceId(${user.deviceId})`);
}

const createPayload: IMemeRecommendWatchCreatePayload = {
const memeRecommendWatchCount = await MemeRecommendWatchModel.countDocuments({
startDate: todayWeekStart,
deviceId: user.deviceId,
startDate: startOfWeek(new Date(), { weekStartsOn: 1 }),
memeIds: [meme._id],
};

await MemeRecommendWatchModel.create(createPayload);
isDeleted: false,
});

return 1;
return memeRecommendWatchCount;
} catch (err) {
logger.error(`Failed create memeRecommendWatch`, err.message);
throw new CustomError(
Expand Down
2 changes: 1 addition & 1 deletion test/meme/get-meme-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("[GET] '/api/meme/list' ", () => {
expect(response.body.data.pagination.page).toBe(1);
expect(response.body.data.pagination.totalPages).toBe(2);
expect(response.body.data.memeList.length).toBe(10);
expect(response.body.data.memeList[0]).toHaveProperty('keywordIds');
expect(response.body.data.memeList[0]).toHaveProperty('keywords');
expect(response.body.data.memeList[0]).toHaveProperty('image');
expect(response.body.data.memeList[0]).toHaveProperty('source');
expect(response.body.data.memeList[0]).toHaveProperty('isTodayMeme');
Expand Down