Skip to content

Commit

Permalink
refactor: ♻️ targetLogin -> targetId 로 변경
Browse files Browse the repository at this point in the history
- #406

refactor: ♻️ targetLogin -> targetId 로 변경

- #406

fix: 🐛 변수명 오류 수정
  • Loading branch information
niamu01 committed Feb 20, 2024
1 parent 0c767db commit 5ed5e2d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 96 deletions.
31 changes: 0 additions & 31 deletions app/src/api/cursusUser/cursusUser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,37 +130,6 @@ export class CursusUserService {
}));
}

async findOneUserPreviewAndLean(
queryArgs?: Omit<QueryArgs<cursus_user>, 'select'>,
): Promise<UserPreview | null> {
const cursusUsers: {
user: {
id: number;
login: string;
image: {
link?: string;
};
};
} | null = await this.findOneAndLean({
...queryArgs,
select: {
'user.id': 1,
'user.login': 1,
'user.image.link': 1,
},
});

if (!cursusUsers) {
return null;
}

return {
id: cursusUsers.user.id,
login: cursusUsers.user.login,
imgUrl: cursusUsers.user.image.link,
};
}

async userFullProfile(
filter?: FilterQuery<cursus_user>,
): Promise<UserFullProfile[]> {
Expand Down
2 changes: 1 addition & 1 deletion app/src/follow/dto/follow.dto.getFollowList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ registerEnumType(FollowSortOrder, { name: 'FollowSortOrder' });
@ArgsType()
export class FollowListPaginatedArgs extends PaginationIndexArgs {
@Field()
target: string;
targetId: number;

@Field((_type) => FollowSortOrder, {
defaultValue: FollowSortOrder.FOLLOW_AT_DESC,
Expand Down
11 changes: 7 additions & 4 deletions app/src/follow/follow.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ export class FollowResolver {
@Mutation((_returns) => FollowSuccess)
async followUser(
@MyUserId() userId: number,
@Args('target') target: string,
@Args('targetId') targetId: number,
): Promise<FollowSuccess> {
try {
const followResult = await this.followService.followUser(userId, target);
const followResult = await this.followService.followUser(
userId,
targetId,
);

await pubSub.publish('followUpdated', { followUpdated: followResult });

Expand All @@ -41,12 +44,12 @@ export class FollowResolver {
@Mutation((_returns) => FollowSuccess)
async unfollowUser(
@MyUserId() userId: number,
@Args('target') target: string,
@Args('targetId') targetId: number,
): Promise<FollowSuccess> {
try {
const followResult = await this.followService.unfollowUser(
userId,
target,
targetId,
);

await pubSub.publish('followUpdated', { followUpdated: followResult });
Expand Down
104 changes: 48 additions & 56 deletions app/src/follow/follow.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { FilterQuery, Model, SortOrder } from 'mongoose';
import { CursusUserCacheService } from 'src/api/cursusUser/cursusUser.cache.service';
import { UserPreview } from 'src/common/models/common.user.model';
import {
QueryArgs,
Expand All @@ -9,7 +10,6 @@ import {
findOneAndLean,
} from 'src/database/mongoose/database.mongoose.query';
import { PaginationIndexService } from 'src/pagination/index/pagination.index.service';
import { CursusUserService } from '../api/cursusUser/cursusUser.service';
import { follow } from './db/follow.database.schema';
import {
FollowSortOrder,
Expand All @@ -21,14 +21,12 @@ import type {
FollowSuccess,
} from './model/follow.model';

type FollowListCursorField = [number, string];

@Injectable()
export class FollowService {
constructor(
@InjectModel(follow.name)
private readonly followModel: Model<follow>,
private readonly cursusUserService: CursusUserService,
private readonly cursusUserCacheService: CursusUserCacheService,
private readonly paginationIndexService: PaginationIndexService,
) {}

Expand All @@ -42,61 +40,41 @@ export class FollowService {
return await findAllAndLean(this.followModel, queryArgs);
}

async userIdByLogin(login: string): Promise<number> {
const id = await this.cursusUserService.getuserIdByLogin(login);

if (!id) {
throw new NotFoundException();
}

return id;
}

async followUser(userId: number, target: string): Promise<FollowSuccess> {
const following = await this.cursusUserService.getuserIdByLogin(target);

async followUser(userId: number, targetId: number): Promise<FollowSuccess> {
const alreadyFollow = await this.followModel.findOne(
{
userId: userId,
followId: following,
userId,
followId: targetId,
},
{ _id: 1 },
);

if (!following || userId === following || alreadyFollow) {
if (userId === targetId || alreadyFollow) {
throw new NotFoundException();
}

const result = await this.followModel.create({
userId: userId,
followId: following,
followAt: new Date(),
});

return {
userId: result.userId,
followId: result.followId,
userId,
followId: targetId,
};
}

async unfollowUser(userId: number, target: string): Promise<FollowSuccess> {
const following = await this.cursusUserService.getuserIdByLogin(target);

if (!following || userId === following) {
async unfollowUser(userId: number, targetId: number): Promise<FollowSuccess> {
if (!targetId || userId === targetId) {
throw new NotFoundException();
}

const deletedCount = await this.followModel
.deleteOne({
userId: userId,
followId: following,
userId,
followId: targetId,
})
.then((result) => result.deletedCount);

if (deletedCount === 1) {
return {
userId: userId,
followId: following,
userId,
followId: targetId,
};
}

Expand All @@ -105,12 +83,10 @@ export class FollowService {

async followerList(
userId: number,
target: string,
targetId: number,
sortOrder: FollowSortOrder,
filter?: FilterQuery<follow>,
): Promise<FollowList[]> {
const targetId = await this.userIdByLogin(target);

const aggregate = this.followModel.aggregate<follow>();

if (filter) {
Expand All @@ -125,10 +101,19 @@ export class FollowService {

const followerUserPreview = await Promise.all(
follower.map(async (follower) => {
const userPreview =
await this.cursusUserService.findOneUserPreviewAndLean({
filter: { 'user.id': follower.userId },
});
const userFullProfile = await this.cursusUserCacheService
.getUserFullProfile(follower.userId)
.then((user) => user?.cursusUser.user);

if (!userFullProfile) {
throw new NotFoundException();
}

const userPreview = {
id: userFullProfile.id,
login: userFullProfile.login,
imgUrl: userFullProfile.image.link,
};

if (!userPreview) {
throw new NotFoundException();
Expand All @@ -143,9 +128,9 @@ export class FollowService {

async followerPaginated(
userId: number,
{ pageNumber, pageSize, target, sortOrder }: FollowListPaginatedArgs,
{ pageNumber, pageSize, targetId, sortOrder }: FollowListPaginatedArgs,
): Promise<FollowListPaginated> {
const followList = await this.followerList(userId, target, sortOrder);
const followList = await this.followerList(userId, targetId, sortOrder);

return this.paginationIndexService.toPaginated<FollowList>(followList, {
pageNumber,
Expand All @@ -155,12 +140,10 @@ export class FollowService {

async followingList(
userId: number,
target: string,
targetId: number,
sortOrder: FollowSortOrder,
filter?: FilterQuery<follow>,
): Promise<FollowList[]> {
const targetId = await this.userIdByLogin(target);

const aggregate = this.followModel.aggregate<follow>();

if (filter) {
Expand All @@ -175,10 +158,19 @@ export class FollowService {

const followingUserPreview = await Promise.all(
following.map(async (following) => {
const userPreview =
await this.cursusUserService.findOneUserPreviewAndLean({
filter: { 'user.id': following.followId },
});
const userFullProfile = await this.cursusUserCacheService
.getUserFullProfile(following.followId)
.then((user) => user?.cursusUser.user);

if (!userFullProfile) {
throw new NotFoundException();
}

const userPreview = {
id: userFullProfile.id,
login: userFullProfile.login,
imgUrl: userFullProfile.image.link,
};

if (!userPreview) {
throw new NotFoundException();
Expand All @@ -193,9 +185,9 @@ export class FollowService {

async followingPaginated(
userId: number,
{ pageNumber, pageSize, target, sortOrder }: FollowListPaginatedArgs,
{ pageNumber, pageSize, targetId, sortOrder }: FollowListPaginatedArgs,
): Promise<FollowListPaginated> {
const followList = await this.followingList(userId, target, sortOrder);
const followList = await this.followingList(userId, targetId, sortOrder);

return this.paginationIndexService.toPaginated<FollowList>(followList, {
pageSize,
Expand All @@ -217,10 +209,10 @@ export class FollowService {
return await this.followModel.countDocuments({ userId, filter });
}

async isFollowing(userId: number, targetId: number): Promise<boolean> {
async isFollowing(userId: number, followId: number): Promise<boolean> {
return !!(await this.followModel.findOne({
userId,
targetId,
followId,
}));
}

Expand Down
8 changes: 4 additions & 4 deletions app/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,8 @@ type Query {
getSetting: Setting!
getExpTable: [ExpTable!]!
getIsFollowing(targetId: Int!): Boolean!
getFollowerPaginated(pageSize: Int! = 10, pageNumber: Int! = 1, target: String!, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): FollowListPaginated!
getFollowingPaginated(pageSize: Int! = 10, pageNumber: Int! = 1, target: String!, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): FollowListPaginated!
getFollowerPaginated(pageSize: Int! = 10, pageNumber: Int! = 1, targetId: Int!, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): FollowListPaginated!
getFollowingPaginated(pageSize: Int! = 10, pageNumber: Int! = 1, targetId: Int!, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): FollowListPaginated!
}

enum EvalLogSortOrder {
Expand All @@ -661,8 +661,8 @@ type Mutation {
refreshToken(refreshToken: String!): LoginSuccess!
logout: Int!
deleteAccount: Int!
followUser(target: String!): FollowSuccess!
unfollowUser(target: String!): FollowSuccess!
followUser(targetId: Int!): FollowSuccess!
unfollowUser(targetId: Int!): FollowSuccess!
}

union LoginResult = LoginSuccess | LoginNotLinked
Expand Down

0 comments on commit 5ed5e2d

Please sign in to comment.