Compare commits

...

6 Commits

Author SHA1 Message Date
mirarifhasan
ca6522f370 test: fix test cases 2024-04-29 23:00:57 +06:00
mirarifhasan
872623231f refactor: remove unused imports 2024-04-29 22:37:07 +06:00
mirarifhasan
20ac35becb refactor: getTeamsOfUser function 2024-04-29 22:35:40 +06:00
mirarifhasan
5eed5ce176 refactor: getTeamMemberTE function removed 2024-04-29 22:33:11 +06:00
mirarifhasan
9694c0d373 refactor: getMembersOfTeam function improved 2024-04-29 22:29:39 +06:00
mirarifhasan
293c93cc79 chore: getTeamWithIDTE function removed 2024-04-29 22:26:00 +06:00
4 changed files with 45 additions and 88 deletions

View File

@@ -11,6 +11,7 @@ import {
EMAIL_FAILED,
INVALID_EMAIL,
ONLY_ONE_ADMIN_ACCOUNT,
TEAM_INVALID_ID,
TEAM_INVITE_ALREADY_MEMBER,
TEAM_INVITE_NO_INVITE_FOUND,
USERS_NOT_FOUND,
@@ -318,11 +319,11 @@ export class AdminService {
const user = await this.userService.findUserByEmail(userEmail);
if (O.isNone(user)) return E.left(USER_NOT_FOUND);
const teamMember = await this.teamService.getTeamMemberTE(
const teamMember = await this.teamService.getTeamMember(
teamID,
user.value.uid,
)();
if (E.isLeft(teamMember)) {
);
if (!teamMember) {
const addedUser = await this.teamService.addMemberToTeamWithEmail(
teamID,
userEmail,
@@ -590,9 +591,9 @@ export class AdminService {
* @returns an Either of `Team` or error
*/
async getTeamInfo(teamID: string) {
const team = await this.teamService.getTeamWithIDTE(teamID)();
if (E.isLeft(team)) return E.left(team.left);
return E.right(team.right);
const team = await this.teamService.getTeamWithID(teamID);
if (!team) return E.left(TEAM_INVALID_ID);
return E.right(team);
}
/**

View File

@@ -15,7 +15,11 @@ import * as TE from 'fp-ts/TaskEither';
import * as E from 'fp-ts/Either';
import * as O from 'fp-ts/Option';
import { Team, TeamMember, TeamMemberRole } from 'src/team/team.model';
import { TEAM_INVITE_NO_INVITE_FOUND, USER_NOT_FOUND } from 'src/errors';
import {
TEAM_INVALID_ID,
TEAM_INVITE_NO_INVITE_FOUND,
USER_NOT_FOUND,
} from 'src/errors';
import { GqlUser } from 'src/decorators/gql-user.decorator';
import { User } from 'src/user/user.model';
import { UseGuards } from '@nestjs/common';
@@ -50,10 +54,9 @@ export class TeamInvitationResolver {
description: 'Get the team associated to the invite',
})
async team(@Parent() teamInvitation: TeamInvitation): Promise<Team> {
return pipe(
this.teamService.getTeamWithIDTE(teamInvitation.teamID),
TE.getOrElse(throwErr),
)();
const team = await this.teamService.getTeamWithID(teamInvitation.teamID);
if (!team) throwErr(TEAM_INVALID_ID);
return team;
}
@ResolveField(() => User, {

View File

@@ -11,6 +11,7 @@ import {
} from '../errors';
import { mockDeep, mockReset } from 'jest-mock-extended';
import * as O from 'fp-ts/Option';
import { skip } from 'rxjs';
const mockPrisma = mockDeep<PrismaService>();
@@ -755,6 +756,8 @@ describe('getMembersOfTeam', () => {
expect(mockPrisma.teamMember.findMany).toHaveBeenCalledWith({
take: 10,
skip: 0,
cursor: undefined,
where: {
teamID: team.id,
},
@@ -806,6 +809,8 @@ describe('getTeamsOfUser', () => {
expect(mockPrisma.teamMember.findMany).toHaveBeenCalledWith({
take: 10,
skip: 0,
cursor: undefined,
where: {
userUid: dbTeamMember.userUid,
},

View File

@@ -15,7 +15,6 @@ import {
} from '../errors';
import { PubSubService } from '../pubsub/pubsub.service';
import { flow, pipe } from 'fp-ts/function';
import * as TE from 'fp-ts/TaskEither';
import * as TO from 'fp-ts/TaskOption';
import * as O from 'fp-ts/Option';
import * as E from 'fp-ts/Either';
@@ -264,37 +263,25 @@ export class TeamService implements UserDataHandler, OnModuleInit {
}
async getTeamsOfUser(uid: string, cursor: string | null): Promise<Team[]> {
if (!cursor) {
const entries = await this.prisma.teamMember.findMany({
take: 10,
where: {
userUid: uid,
},
include: {
team: true,
},
});
return entries.map((entry) => entry.team);
} else {
const entries = await this.prisma.teamMember.findMany({
take: 10,
skip: 1,
cursor: {
teamID_userUid: {
teamID: cursor,
userUid: uid,
},
},
where: {
userUid: uid,
},
include: {
team: true,
},
});
return entries.map((entry) => entry.team);
}
const entries = await this.prisma.teamMember.findMany({
take: 10,
skip: cursor ? 1 : 0,
cursor: cursor
? {
teamID_userUid: {
teamID: cursor,
userUid: uid,
},
}
: undefined,
where: {
userUid: uid,
},
include: {
team: true,
},
});
return entries.map((entry) => entry.team);
}
async getTeamWithID(teamID: string): Promise<Team | null> {
@@ -311,19 +298,6 @@ export class TeamService implements UserDataHandler, OnModuleInit {
}
}
getTeamWithIDTE(teamID: string): TE.TaskEither<'team/invalid_id', Team> {
return pipe(
() => this.getTeamWithID(teamID),
TE.fromTask,
TE.chain(
TE.fromPredicate(
(x): x is Team => !!x,
() => TEAM_INVALID_ID,
),
),
);
}
/**
* Filters out team members that we weren't able to match
* (also deletes the membership)
@@ -375,19 +349,6 @@ export class TeamService implements UserDataHandler, OnModuleInit {
}
}
getTeamMemberTE(teamID: string, userUid: string) {
return pipe(
() => this.getTeamMember(teamID, userUid),
TE.fromTask,
TE.chain(
TE.fromPredicate(
(x): x is TeamMember => !!x,
() => TEAM_MEMBER_NOT_FOUND,
),
),
);
}
async getRoleOfUserInTeam(
teamID: string,
userUid: string,
@@ -472,25 +433,12 @@ export class TeamService implements UserDataHandler, OnModuleInit {
): Promise<TeamMember[]> {
let teamMembers: DbTeamMember[];
if (!cursor) {
teamMembers = await this.prisma.teamMember.findMany({
take: 10,
where: {
teamID,
},
});
} else {
teamMembers = await this.prisma.teamMember.findMany({
take: 10,
skip: 1,
cursor: {
id: cursor,
},
where: {
teamID,
},
});
}
teamMembers = await this.prisma.teamMember.findMany({
take: 10,
skip: cursor ? 1 : 0,
cursor: cursor ? { id: cursor } : undefined,
where: { teamID },
});
const members = teamMembers.map(
(entry) =>