refactor: refactoring Team-Collections with reordering in self-host (HBE-150) (#34)

* chore: removed firebase module as a dependency from team-collection module

* chore: modified team-collection resolver file to use input-args types

* chore: modified getTeamOfCollection service method and resolver

* chore: modified getParentOfCollection service method in team-collection module

* chore: modified getChildrenOfCollection service method in team-collection module

* chore: added new fields to TeamCollection model in prisma schema file

* chore: modified getCollection service method and resolver in team-collection module

* chore: modified createCollection service method and resolver in team-collection module

* chore: created cast helper function to resolve issue with creation mutation in team-collection

* chore: modified teamCollectionRemoved subscription return types

* chore: removed return types from subscriptions in team-collection module

* chore: removed all instances of getTeamCollections service method in team-collection module

* feat: added mutation to handle moving collections and supporting subscriptions

* feat: added mutation to re-ordering team-collection order

* chore: added teacher comments to both collection modules

* test: added test cases for getTeamOfCollection service method

* test: added test cases for getParentOfCollection service method

* test: added test cases for getChildrenOfCollection service method

* test: added test cases for getTeamRootCollections service method

* test: added test cases for getCollection service method

* test: added test cases for createCollection service method

* chore: renamed renameCollection to renameUserCollection in UserCollection module

* test: added test cases for renameCollection service method

* test: added test cases for deleteCollection service method

* test: added test cases for moveCollection service method

* test: added test cases for updateCollectionOrder service method

* chore: added import and export to JSON mutations to team-collection module

* chore: created replaceCollectionsWithJSON mutation in team-collection module

* chore: moved the mutation and service method of importCollectionFromFirestore to the end of file

* chore: added helper comments to all import,export functions

* chore: exportCollectionsToJSON service method orders collections and requests in ascending order

* chore: added test cases for importCollectionsFromJSON service method

* chore: added ToDo to write test cases for exportCollectionsToJSON

* chore: removed prisma migration folder

* chore: completed all changes requested in inital PR review

* chore: completed all changes requested in second  PR review

* chore: completed all changes requested in third PR review
This commit is contained in:
Balu Babu
2023-03-09 19:37:40 +05:30
committed by GitHub
parent 9b76d62753
commit 2a715d5348
19 changed files with 3079 additions and 2694 deletions

View File

@@ -43,13 +43,13 @@ export class TeamRequestResolver {
return this.teamRequestService.getTeamOfRequest(req);
}
@ResolveField(() => TeamCollection, {
description: 'Collection the request belongs to',
complexity: 3,
})
collection(@Parent() req: TeamRequest): Promise<TeamCollection> {
return this.teamRequestService.getCollectionOfRequest(req);
}
// @ResolveField(() => TeamCollection, {
// description: 'Collection the request belongs to',
// complexity: 3,
// })
// collection(@Parent() req: TeamRequest): Promise<TeamCollection> {
// return this.teamRequestService.getCollectionOfRequest(req);
// }
// Query
@Query(() => [TeamRequest], {
@@ -126,29 +126,29 @@ export class TeamRequestResolver {
);
}
// Mutation
@Mutation(() => TeamRequest, {
description: 'Create a request in the given collection.',
})
@UseGuards(GqlAuthGuard, GqlCollectionTeamMemberGuard)
@RequiresTeamRole(TeamMemberRole.EDITOR, TeamMemberRole.OWNER)
createRequestInCollection(
@Args({
name: 'collectionID',
description: 'ID of the collection',
type: () => ID,
})
collectionID: string,
@Args({
name: 'data',
type: () => CreateTeamRequestInput,
description:
'The request data (stringified JSON of Hoppscotch request object)',
})
data: CreateTeamRequestInput,
): Promise<TeamRequest> {
return this.teamRequestService.createTeamRequest(collectionID, data);
}
// // Mutation
// @Mutation(() => TeamRequest, {
// description: 'Create a request in the given collection.',
// })
// @UseGuards(GqlAuthGuard, GqlCollectionTeamMemberGuard)
// @RequiresTeamRole(TeamMemberRole.EDITOR, TeamMemberRole.OWNER)
// createRequestInCollection(
// @Args({
// name: 'collectionID',
// description: 'ID of the collection',
// type: () => ID,
// })
// collectionID: string,
// @Args({
// name: 'data',
// type: () => CreateTeamRequestInput,
// description:
// 'The request data (stringified JSON of Hoppscotch request object)',
// })
// data: CreateTeamRequestInput,
// ): Promise<TeamRequest> {
// return this.teamRequestService.createTeamRequest(collectionID, data);
// }
@Mutation(() => TeamRequest, {
description: 'Update a request with the given ID',
@@ -190,30 +190,30 @@ export class TeamRequestResolver {
return true;
}
@Mutation(() => TeamRequest, {
description: 'Move a request to the given collection',
})
@UseGuards(GqlAuthGuard, GqlRequestTeamMemberGuard)
@RequiresTeamRole(TeamMemberRole.EDITOR, TeamMemberRole.OWNER)
moveRequest(
@Args({
name: 'requestID',
description: 'ID of the request to move',
type: () => ID,
})
requestID: string,
@Args({
name: 'destCollID',
description: 'ID of the collection to move the request to',
type: () => ID,
})
destCollID: string,
): Promise<TeamRequest> {
return pipe(
this.teamRequestService.moveRequest(requestID, destCollID),
TE.getOrElse((e) => throwErr(e)),
)();
}
// @Mutation(() => TeamRequest, {
// description: 'Move a request to the given collection',
// })
// @UseGuards(GqlAuthGuard, GqlRequestTeamMemberGuard)
// @RequiresTeamRole(TeamMemberRole.EDITOR, TeamMemberRole.OWNER)
// moveRequest(
// @Args({
// name: 'requestID',
// description: 'ID of the request to move',
// type: () => ID,
// })
// requestID: string,
// @Args({
// name: 'destCollID',
// description: 'ID of the collection to move the request to',
// type: () => ID,
// })
// destCollID: string,
// ): Promise<TeamRequest> {
// return pipe(
// this.teamRequestService.moveRequest(requestID, destCollID),
// TE.getOrElse((e) => throwErr(e)),
// )();
// }
// Subscriptions
@Subscription(() => TeamRequest, {

View File

@@ -19,7 +19,7 @@ import { PubSubService } from 'src/pubsub/pubsub.service';
import { throwErr } from 'src/utils';
import { pipe } from 'fp-ts/function';
import * as TO from 'fp-ts/TaskOption';
import * as TE from 'fp-ts/TaskEither';
import * as E from 'fp-ts/Either';
import { Prisma } from '@prisma/client';
@Injectable()
@@ -127,43 +127,41 @@ export class TeamRequestService {
this.pubsub.publish(`team_req/${req.teamID}/req_deleted`, requestID);
}
async createTeamRequest(
collectionID: string,
input: CreateTeamRequestInput,
): Promise<TeamRequest> {
const team = await this.teamCollectionService.getTeamOfCollection(
collectionID,
);
// async createTeamRequest(collectionID: string, input: CreateTeamRequestInput) {
// const team = await this.teamCollectionService.getTeamOfCollection(
// collectionID,
// );
// if (E.isLeft(team)) return [];
const data = await this.prisma.teamRequest.create({
data: {
team: {
connect: {
id: team.id,
},
},
request: JSON.parse(input.request),
title: input.title,
collection: {
connect: {
id: collectionID,
},
},
},
});
// const data = await this.prisma.teamRequest.create({
// data: {
// team: {
// connect: {
// id: team.right.id,
// },
// },
// request: JSON.parse(input.request),
// title: input.title,
// collection: {
// connect: {
// id: collectionID,
// },
// },
// },
// });
const result = {
id: data.id,
collectionID: data.collectionID,
title: data.title,
request: JSON.stringify(data.request),
teamID: data.teamID,
};
// const result = {
// id: data.id,
// collectionID: data.collectionID,
// title: data.title,
// request: JSON.stringify(data.request),
// teamID: data.teamID,
// };
this.pubsub.publish(`team_req/${result.teamID}/req_created`, result);
// this.pubsub.publish(`team_req/${result.teamID}/req_created`, result);
return result;
}
// return result;
// }
async getRequestsInCollection(
collectionID: string,
@@ -242,12 +240,12 @@ export class TeamRequestService {
);
}
async getCollectionOfRequest(req: TeamRequest): Promise<TeamCollection> {
return (
(await this.teamCollectionService.getCollection(req.collectionID)) ??
throwErr(TEAM_INVALID_COLL_ID)
);
}
// async getCollectionOfRequest(req: TeamRequest): Promise<TeamCollection> {
// return (
// (await this.teamCollectionService.getCollection(req.collectionID)) ??
// throwErr(TEAM_INVALID_COLL_ID)
// );
// }
async getTeamOfRequestFromID(reqID: string): Promise<Team> {
const req =
@@ -263,69 +261,69 @@ export class TeamRequestService {
return req.team;
}
moveRequest(reqID: string, destinationCollID: string) {
return pipe(
TE.Do,
// moveRequest(reqID: string, destinationCollID: string) {
// return pipe(
// TE.Do,
// Check if the request exists
TE.bind('request', () =>
pipe(
this.getRequestTO(reqID),
TE.fromTaskOption(() => TEAM_REQ_NOT_FOUND),
),
),
// // Check if the request exists
// TE.bind('request', () =>
// pipe(
// this.getRequestTO(reqID),
// TE.fromTaskOption(() => TEAM_REQ_NOT_FOUND),
// ),
// ),
// Check if the destination collection exists (or null)
TE.bindW('targetCollection', () =>
pipe(
this.teamCollectionService.getCollectionTO(destinationCollID),
TE.fromTaskOption(() => TEAM_REQ_INVALID_TARGET_COLL_ID),
),
),
// // Check if the destination collection exists (or null)
// TE.bindW('targetCollection', () =>
// pipe(
// this.teamCollectionService.getCollectionTO(destinationCollID),
// TE.fromTaskOption(() => TEAM_REQ_INVALID_TARGET_COLL_ID),
// ),
// ),
// Block operation if target collection is not part of the same team
// as the request
TE.chainW(
TE.fromPredicate(
({ request, targetCollection }) =>
request.teamID === targetCollection.teamID,
() => TEAM_REQ_INVALID_TARGET_COLL_ID,
),
),
// // Block operation if target collection is not part of the same team
// // as the request
// TE.chainW(
// TE.fromPredicate(
// ({ request, targetCollection }) =>
// request.teamID === targetCollection.teamID,
// () => TEAM_REQ_INVALID_TARGET_COLL_ID,
// ),
// ),
// Update the collection
TE.chain(({ request, targetCollection }) =>
TE.fromTask(() =>
this.prisma.teamRequest.update({
where: {
id: request.id,
},
data: {
collectionID: targetCollection.id,
},
}),
),
),
// // Update the collection
// TE.chain(({ request, targetCollection }) =>
// TE.fromTask(() =>
// this.prisma.teamRequest.update({
// where: {
// id: request.id,
// },
// data: {
// collectionID: targetCollection.id,
// },
// }),
// ),
// ),
// Generate TeamRequest model object
TE.map(
(request) =>
<TeamRequest>{
id: request.id,
collectionID: request.collectionID,
request: JSON.stringify(request.request),
teamID: request.teamID,
title: request.title,
},
),
// // Generate TeamRequest model object
// TE.map(
// (request) =>
// <TeamRequest>{
// id: request.id,
// collectionID: request.collectionID,
// request: JSON.stringify(request.request),
// teamID: request.teamID,
// title: request.title,
// },
// ),
// Update on PubSub
TE.chainFirst((req) => {
this.pubsub.publish(`team_req/${req.teamID}/req_deleted`, req.id);
this.pubsub.publish(`team_req/${req.teamID}/req_created`, req);
// // Update on PubSub
// TE.chainFirst((req) => {
// this.pubsub.publish(`team_req/${req.teamID}/req_deleted`, req.id);
// this.pubsub.publish(`team_req/${req.teamID}/req_created`, req);
return TE.of({}); // We don't care about the return type
}),
);
}
// return TE.of({}); // We don't care about the return type
// }),
// );
// }
}