Skip to content

Commit

Permalink
refactor: ♻️ unionType -> throw Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
niamu01 committed Feb 20, 2024
1 parent 3bfd9be commit 0c767db
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 76 deletions.
42 changes: 22 additions & 20 deletions app/src/follow/follow.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { UseFilters, UseGuards } from '@nestjs/common';
import { NotFoundException, UseFilters, UseGuards } from '@nestjs/common';
import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
import { PubSub } from 'graphql-subscriptions';
import { MyUserId } from 'src/auth/myContext';
import { StatAuthGuard } from 'src/auth/statAuthGuard';
import { HttpExceptionFilter } from 'src/http-exception.filter';
import { FollowListPaginatedArgs } from './dto/follow.dto.getFollowList';
import { FollowService } from './follow.service';
import { FollowListPaginated, FollowResult } from './model/follow.model';
import { FollowListPaginated, FollowSuccess } from './model/follow.model';

const pubSub = new PubSub();

Expand All @@ -15,44 +15,46 @@ const pubSub = new PubSub();
export class FollowResolver {
constructor(private readonly followService: FollowService) {}

@Subscription((_returns) => FollowResult, {
name: 'followUpdated',
filter: (payload, _variables) => {
return payload.followUpdated.message === 'OK';
},
})
@Subscription((_returns) => FollowSuccess, { name: 'followUpdated' })
followUpdated() {
return pubSub.asyncIterator('followUpdated');
}

@UseGuards(StatAuthGuard)
@Mutation((_returns) => FollowResult)
@Mutation((_returns) => FollowSuccess)
async followUser(
@MyUserId() userId: number,
@Args('target') target: string,
): Promise<typeof FollowResult> {
const followResult = await this.followService.followUser(userId, target);
): Promise<FollowSuccess> {
try {
const followResult = await this.followService.followUser(userId, target);

if (followResult.message === 'OK') {
await pubSub.publish('followUpdated', { followUpdated: followResult });
}

return followResult;
return followResult;
} catch (e) {
throw new NotFoundException();
}
}

@UseGuards(StatAuthGuard)
@Mutation((_returns) => FollowResult)
@Mutation((_returns) => FollowSuccess)
async unfollowUser(
@MyUserId() userId: number,
@Args('target') target: string,
): Promise<typeof FollowResult> {
const followResult = await this.followService.unfollowUser(userId, target);
): Promise<FollowSuccess> {
try {
const followResult = await this.followService.unfollowUser(
userId,
target,
);

if (followResult.message === 'OK') {
await pubSub.publish('followUpdated', { followUpdated: followResult });
}

return followResult;
return followResult;
} catch (e) {
throw new NotFoundException();
}
}

@UseGuards(StatAuthGuard)
Expand Down
24 changes: 6 additions & 18 deletions app/src/follow/follow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import {
findAllAndLean,
findOneAndLean,
} from 'src/database/mongoose/database.mongoose.query';
import {
CursorExtractor,
FieldExtractor,
} from 'src/pagination/cursor/pagination.cursor.service';
import { PaginationIndexService } from 'src/pagination/index/pagination.index.service';
import { CursusUserService } from '../api/cursusUser/cursusUser.service';
import { follow } from './db/follow.database.schema';
Expand All @@ -22,7 +18,7 @@ import {
import type {
FollowList,
FollowListPaginated,
FollowResult,
FollowSuccess,
} from './model/follow.model';

type FollowListCursorField = [number, string];
Expand Down Expand Up @@ -56,10 +52,7 @@ export class FollowService {
return id;
}

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

const alreadyFollow = await this.followModel.findOne(
Expand All @@ -71,7 +64,7 @@ export class FollowService {
);

if (!following || userId === following || alreadyFollow) {
return { message: 'fail' };
throw new NotFoundException();
}

const result = await this.followModel.create({
Expand All @@ -81,20 +74,16 @@ export class FollowService {
});

return {
message: 'OK',
userId: result.userId,
followId: result.followId,
};
}

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

if (!following || userId === following) {
return { message: 'fail' };
throw new NotFoundException();
}

const deletedCount = await this.followModel
Expand All @@ -106,13 +95,12 @@ export class FollowService {

if (deletedCount === 1) {
return {
message: 'OK',
userId: userId,
followId: following,
};
}

return { message: 'fail' };
throw new NotFoundException();
}

async followerList(
Expand Down
24 changes: 1 addition & 23 deletions app/src/follow/model/follow.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Field, ObjectType, createUnionType } from '@nestjs/graphql';
import { Field, ObjectType } from '@nestjs/graphql';
import { UserPreview } from 'src/common/models/common.user.model';
import { IndexPaginated } from 'src/pagination/index/models/pagination.index.model';

Expand All @@ -23,33 +23,11 @@ export class FollowListWithCount {
count: number;
}

@ObjectType()
export class FollowFail {
@Field()
message: 'fail';
}

@ObjectType()
export class FollowSuccess {
@Field()
message: 'OK';

@Field()
userId: number;

@Field()
followId: number;
}

export const FollowResult = createUnionType({
name: 'FollowResult',
types: () => [FollowSuccess, FollowFail] as const,
resolveType(value) {
switch (value.message) {
case 'OK':
return FollowSuccess;
case 'fail':
return FollowFail;
}
},
});
23 changes: 8 additions & 15 deletions app/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ type FollowListPaginated {
pageNumber: Int!
}

type FollowSuccess {
userId: Int!
followId: Int!
}

type LinkableAccount {
platform: String!
id: String!
Expand Down Expand Up @@ -656,8 +661,8 @@ type Mutation {
refreshToken(refreshToken: String!): LoginSuccess!
logout: Int!
deleteAccount: Int!
followUser(target: String!): FollowResult!
unfollowUser(target: String!): FollowResult!
followUser(target: String!): FollowSuccess!
unfollowUser(target: String!): FollowSuccess!
}

union LoginResult = LoginSuccess | LoginNotLinked
Expand All @@ -671,18 +676,6 @@ input GoogleLoginInput {
credential: String!
}

union FollowResult = FollowSuccess | FollowFail

type FollowSuccess {
message: String!
userId: Int!
followId: Int!
}

type FollowFail {
message: String!
}

type Subscription {
followUpdated: FollowResult!
followUpdated: FollowSuccess!
}

0 comments on commit 0c767db

Please sign in to comment.