From 696cf8490b09586270eb8efd6a79c29562a585c0 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 6 Apr 2023 20:24:10 +0600 Subject: [PATCH] refactor: removing unused import, commented codes, improved cursor query (#69) * chore: refactor code in some modules * refactor: getTeamsOfUser functino * chore: remove unused import * chore: revert some changes --- .../src/admin/admin.model.ts | 2 +- .../src/admin/admin.resolver.ts | 7 ++- .../src/admin/input-types.args.ts | 1 + .../src/shortcode/shortcode.service.ts | 1 - .../gql-collection-team-member.guard.ts | 1 - .../team-collection.resolver.ts | 44 ------------------- .../team-collection.service.ts | 2 - .../gql-team-env-team.guard.ts | 1 - .../team-invitation.service.ts | 2 - .../src/team/guards/gql-team-member.guard.ts | 1 - .../src/team/team.service.ts | 2 - .../user-collection.service.ts | 6 ++- .../user-settings.service.spec.ts | 1 - .../user-settings/user-settings.service.ts | 23 +++++----- 14 files changed, 22 insertions(+), 72 deletions(-) diff --git a/packages/hoppscotch-backend/src/admin/admin.model.ts b/packages/hoppscotch-backend/src/admin/admin.model.ts index 2c58d3721..bf4288dd4 100644 --- a/packages/hoppscotch-backend/src/admin/admin.model.ts +++ b/packages/hoppscotch-backend/src/admin/admin.model.ts @@ -1,4 +1,4 @@ -import { ObjectType, ID, Field, ResolveField } from '@nestjs/graphql'; +import { ObjectType } from '@nestjs/graphql'; @ObjectType() export class Admin {} diff --git a/packages/hoppscotch-backend/src/admin/admin.resolver.ts b/packages/hoppscotch-backend/src/admin/admin.resolver.ts index 6b68e616d..12838963a 100644 --- a/packages/hoppscotch-backend/src/admin/admin.resolver.ts +++ b/packages/hoppscotch-backend/src/admin/admin.resolver.ts @@ -38,7 +38,9 @@ export class AdminResolver { private adminService: AdminService, private readonly pubsub: PubSubService, ) {} - // Query + + /* Query */ + @Query(() => Admin, { description: 'Gives details of the admin executing this query', }) @@ -229,7 +231,8 @@ export class AdminResolver { return this.adminService.getTeamRequestsCount(); } - // Mutations + /* Mutations */ + @Mutation(() => InvitedUser, { description: 'Invite a user to the infra using email', }) diff --git a/packages/hoppscotch-backend/src/admin/input-types.args.ts b/packages/hoppscotch-backend/src/admin/input-types.args.ts index ab7b91d68..dadd1fa10 100644 --- a/packages/hoppscotch-backend/src/admin/input-types.args.ts +++ b/packages/hoppscotch-backend/src/admin/input-types.args.ts @@ -20,6 +20,7 @@ export class ChangeUserRoleInTeamArgs { }) newRole: TeamMemberRole; } + @ArgsType() export class AddUserToTeamArgs { @Field(() => ID, { diff --git a/packages/hoppscotch-backend/src/shortcode/shortcode.service.ts b/packages/hoppscotch-backend/src/shortcode/shortcode.service.ts index ecd2ad7b5..acb92e9d5 100644 --- a/packages/hoppscotch-backend/src/shortcode/shortcode.service.ts +++ b/packages/hoppscotch-backend/src/shortcode/shortcode.service.ts @@ -5,7 +5,6 @@ import * as TO from 'fp-ts/TaskOption'; import * as E from 'fp-ts/Either'; import { PrismaService } from 'src/prisma/prisma.service'; import { - SHORTCODE_ALREADY_EXISTS, SHORTCODE_INVALID_JSON, SHORTCODE_NOT_FOUND, } from 'src/errors'; diff --git a/packages/hoppscotch-backend/src/team-collection/guards/gql-collection-team-member.guard.ts b/packages/hoppscotch-backend/src/team-collection/guards/gql-collection-team-member.guard.ts index e70e5892f..cf32a3659 100644 --- a/packages/hoppscotch-backend/src/team-collection/guards/gql-collection-team-member.guard.ts +++ b/packages/hoppscotch-backend/src/team-collection/guards/gql-collection-team-member.guard.ts @@ -1,7 +1,6 @@ import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import { GqlExecutionContext } from '@nestjs/graphql'; -import { User } from '../../user/user.model'; import { TeamCollectionService } from '../team-collection.service'; import { TeamService } from '../../team/team.service'; import { TeamMemberRole } from '../../team/team.model'; diff --git a/packages/hoppscotch-backend/src/team-collection/team-collection.resolver.ts b/packages/hoppscotch-backend/src/team-collection/team-collection.resolver.ts index 6eeabc945..7ba886884 100644 --- a/packages/hoppscotch-backend/src/team-collection/team-collection.resolver.ts +++ b/packages/hoppscotch-backend/src/team-collection/team-collection.resolver.ts @@ -415,48 +415,4 @@ export class TeamCollectionResolver { ) { return this.pubsub.asyncIterator(`team_coll/${teamID}/coll_order_updated`); } - - // @Mutation(() => TeamCollection, { - // description: 'Import collection from user firestore', - // }) - // @UseGuards(GqlAuthGuard, GqlTeamMemberGuard) - // @RequiresTeamRole(TeamMemberRole.OWNER, TeamMemberRole.EDITOR) - // importCollectionFromUserFirestore( - // @Args({ - // name: 'teamID', - // type: () => ID, - // description: 'ID of the team to add to', - // }) - // teamID: string, - // @Args({ - // name: 'fbCollectionPath', - // description: - // 'slash separated array indicies path to the target collection', - // }) - // fbCollectionPath: string, - // @GqlUser() user: User, - // @Args({ - // name: 'parentCollectionID', - // type: () => ID, - // description: - // 'ID to the collection which is going to be parent to the result (null if root)', - // nullable: true, - // }) - // parentCollectionID?: string - // ): Promise { - // if (parentCollectionID) { - // return this.teamCollectionService.importCollectionFromFirestore( - // user.uid, - // fbCollectionPath, - // teamID, - // parentCollectionID, - // ); - // } else { - // return this.teamCollectionService.importCollectionFromFirestore( - // user.uid, - // fbCollectionPath, - // teamID, - // ); - // } - // } } diff --git a/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts b/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts index bd5bb208f..01a2ab78a 100644 --- a/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts +++ b/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts @@ -1,8 +1,6 @@ import { Injectable } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; -import { Team } from '../team/team.model'; import { TeamCollection } from './team-collection.model'; -// import { FirebaseService } from '../firebase/firebase.service'; import { TEAM_COLL_SHORT_TITLE, TEAM_COLL_INVALID_JSON, diff --git a/packages/hoppscotch-backend/src/team-environments/gql-team-env-team.guard.ts b/packages/hoppscotch-backend/src/team-environments/gql-team-env-team.guard.ts index af6c503e9..181fd6c76 100644 --- a/packages/hoppscotch-backend/src/team-environments/gql-team-env-team.guard.ts +++ b/packages/hoppscotch-backend/src/team-environments/gql-team-env-team.guard.ts @@ -8,7 +8,6 @@ import { getAnnotatedRequiredRoles, getGqlArg, getUserFromGQLContext, - namedTrace, throwErr, } from 'src/utils'; import { TeamEnvironmentsService } from './team-environments.service'; diff --git a/packages/hoppscotch-backend/src/team-invitation/team-invitation.service.ts b/packages/hoppscotch-backend/src/team-invitation/team-invitation.service.ts index 2f35b328c..906565206 100644 --- a/packages/hoppscotch-backend/src/team-invitation/team-invitation.service.ts +++ b/packages/hoppscotch-backend/src/team-invitation/team-invitation.service.ts @@ -3,7 +3,6 @@ import * as T from 'fp-ts/Task'; import * as O from 'fp-ts/Option'; import * as TO from 'fp-ts/TaskOption'; import * as TE from 'fp-ts/TaskEither'; -import * as E from 'fp-ts/Either'; import { pipe, flow, constVoid } from 'fp-ts/function'; import { PrismaService } from 'src/prisma/prisma.service'; import { Team, TeamMemberRole } from 'src/team/team.model'; @@ -11,7 +10,6 @@ import { Email } from 'src/types/Email'; import { User } from 'src/user/user.model'; import { TeamService } from 'src/team/team.service'; import { - TEAM_INVITATION_NOT_FOUND, TEAM_INVITE_ALREADY_MEMBER, TEAM_INVITE_EMAIL_DO_NOT_MATCH, TEAM_INVITE_MEMBER_HAS_INVITE, diff --git a/packages/hoppscotch-backend/src/team/guards/gql-team-member.guard.ts b/packages/hoppscotch-backend/src/team/guards/gql-team-member.guard.ts index d4a34ae56..fcd2329d8 100644 --- a/packages/hoppscotch-backend/src/team/guards/gql-team-member.guard.ts +++ b/packages/hoppscotch-backend/src/team/guards/gql-team-member.guard.ts @@ -3,7 +3,6 @@ import { Reflector } from '@nestjs/core'; import { TeamService } from '../team.service'; import { TeamMemberRole } from '../team.model'; import { GqlExecutionContext } from '@nestjs/graphql'; -import { User } from '../../user/user.model'; import { TEAM_NOT_REQUIRED_ROLE, BUG_AUTH_NO_USER_CTX, diff --git a/packages/hoppscotch-backend/src/team/team.service.ts b/packages/hoppscotch-backend/src/team/team.service.ts index 9d2c5c953..f1afcf908 100644 --- a/packages/hoppscotch-backend/src/team/team.service.ts +++ b/packages/hoppscotch-backend/src/team/team.service.ts @@ -12,7 +12,6 @@ import { TEAM_INVALID_ID_OR_USER, TEAM_MEMBER_NOT_FOUND, USER_IS_OWNER, - TEAMS_NOT_FOUND, } from '../errors'; import { PubSubService } from '../pubsub/pubsub.service'; import { flow, pipe } from 'fp-ts/function'; @@ -294,7 +293,6 @@ export class TeamService implements UserDataHandler, OnModuleInit { team: true, }, }); - return entries.map((entry) => entry.team); } } diff --git a/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts b/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts index de8fbf9dc..827dd0d05 100644 --- a/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts +++ b/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts @@ -34,6 +34,8 @@ export class UserCollectionService { private readonly pubsub: PubSubService, ) {} + TITLE_LENGTH = 3; + /** * Typecast a database UserCollection to a UserCollection model * @param userCollection database UserCollection @@ -212,7 +214,7 @@ export class UserCollectionService { parentUserCollectionID: string | null, type: ReqType, ) { - const isTitleValid = isValidLength(title, 3); + const isTitleValid = isValidLength(title, this.TITLE_LENGTH); if (!isTitleValid) return E.left(USER_COLL_SHORT_TITLE); // If creating a child collection @@ -330,7 +332,7 @@ export class UserCollectionService { userCollectionID: string, userID: string, ) { - const isTitleValid = isValidLength(newTitle, 3); + const isTitleValid = isValidLength(newTitle, this.TITLE_LENGTH); if (!isTitleValid) return E.left(USER_COLL_SHORT_TITLE); // Check to see is the collection belongs to the user diff --git a/packages/hoppscotch-backend/src/user-settings/user-settings.service.spec.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.service.spec.ts index 37d0eb781..547489407 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.spec.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.spec.ts @@ -4,7 +4,6 @@ import { PubSubService } from 'src/pubsub/pubsub.service'; import { UserSettingsService } from './user-settings.service'; import { JSON_INVALID, USER_SETTINGS_NULL_SETTINGS } from 'src/errors'; import { UserSettings } from './user-settings.model'; -import { User } from 'src/user/user.model'; import { AuthUser } from 'src/types/AuthUser'; const mockPrisma = mockDeep(); diff --git a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts index ed1be3ceb..c36fc002b 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -4,6 +4,7 @@ import { PubSubService } from 'src/pubsub/pubsub.service'; import { User } from 'src/user/user.model'; import * as E from 'fp-ts/Either'; import { stringToJson } from 'src/utils'; +import { UserSettings as DbUserSettings } from '@prisma/client'; import { UserSettings } from './user-settings.model'; import { USER_SETTINGS_ALREADY_EXISTS, @@ -19,6 +20,13 @@ export class UserSettingsService { private readonly pubsub: PubSubService, ) {} + private castToUserSettings(userSettings: DbUserSettings): UserSettings { + return { + ...userSettings, + properties: JSON.stringify(userSettings.properties), + }; + } + /** * Fetch user settings for a given user * @param user User object @@ -30,10 +38,7 @@ export class UserSettingsService { where: { userUid: user.uid }, }); - const settings: UserSettings = { - ...userSettings, - properties: JSON.stringify(userSettings.properties), - }; + const settings = this.castToUserSettings(userSettings); return E.right(settings); } catch (e) { @@ -61,10 +66,7 @@ export class UserSettingsService { }, }); - const settings: UserSettings = { - ...userSettings, - properties: JSON.stringify(userSettings.properties), - }; + const settings = this.castToUserSettings(userSettings); // Publish subscription for user settings creation await this.pubsub.publish(`user_settings/${user.uid}/created`, settings); @@ -95,10 +97,7 @@ export class UserSettingsService { }, }); - const settings: UserSettings = { - ...updatedUserSettings, - properties: JSON.stringify(updatedUserSettings.properties), - }; + const settings = this.castToUserSettings(updatedUserSettings); // Publish subscription for user settings update await this.pubsub.publish(`user_settings/${user.uid}/updated`, settings);