feat: Introducing Admin Module to Backend (HBE-83) (#21)

* feat: introducing admin module, resolvers and service files as a module

* feat: adding admin module in the app module

* feat: introducing admin guard and decorator for allowing admin operations

* feat: invited user model

* chore: added user invitation mail description to mailer service

* chore: added admin and user related error

* feat: added invited users as a new model in prisma

* chore: added admin related topics to pubsub

* chore: added service method to fetch all users from user table

* chore: added user deletion base implementation

* Revert "chore: added user deletion base implementation"

This reverts commit d1615ad83db2bae946e2d366a903d2f95051dabb.

* feat: adding team related operations to admin

* chore: adding admin related service methods to teams module service

* chore: adding admin related service methods to team coll invitations requests envs

* chore: added more module error messages

* chore: added admin check service method

* chore: added find individual user by UID in admin

* HBE-106 feat: introduced code to handle first time admin login setup (#23)

* test: wrote test cases for verifyAdmin route service method

* chore: added comments to verifyAdmin service method

* chore: deleted the prisma migration file

* chore: added find admin users

* feat: added user deletion into admin module

* chore: admin user related errors

* chore: fixed registry pattern in the shortcodes and teams to handle user deletion

* chore: add subscription topic for user deletion

* chore: updated user type in data handler

* feat: implement and fix user deletion

* feat: added make user admin mutation

* chore: added unit tests for admin specific service methods in admin module

* chore: added invitation not found error

* chore: added admin specific operation test cases in specific modules

* chore: added tests related to user deletion and admin related operation in user module

* chore: updated to error constant when invitations not found

* chore: fix rebase overwritten methods

* feat: implement remove user as admin

* chore: add new line

* feat: introducing basic metrics into the self-hosted admin module (HBE-104) (#43)

* feat: introducing admin module, resolvers and service files as a module

* feat: adding admin module in the app module

* feat: introducing admin guard and decorator for allowing admin operations

* feat: invited user model

* chore: added user invitation mail description to mailer service

* chore: added admin and user related error

* feat: added invited users as a new model in prisma

* chore: added admin related topics to pubsub

* chore: added service method to fetch all users from user table

* chore: added user deletion base implementation

* Revert "chore: added user deletion base implementation"

This reverts commit d1615ad83db2bae946e2d366a903d2f95051dabb.

* feat: adding team related operations to admin

* chore: adding admin related service methods to teams module service

* chore: adding admin related service methods to team coll invitations requests envs

* chore: added more module error messages

* chore: added admin check service method

* chore: added find individual user by UID in admin

* HBE-106 feat: introduced code to handle first time admin login setup (#23)

* test: wrote test cases for verifyAdmin route service method

* chore: added comments to verifyAdmin service method

* chore: deleted the prisma migration file

* chore: added find admin users

* feat: added user deletion into admin module

* chore: admin user related errors

* chore: fixed registry pattern in the shortcodes and teams to handle user deletion

* chore: add subscription topic for user deletion

* chore: updated user type in data handler

* feat: implement and fix user deletion

* feat: added make user admin mutation

* chore: added unit tests for admin specific service methods in admin module

* chore: added invitation not found error

* chore: added admin specific operation test cases in specific modules

* chore: added tests related to user deletion and admin related operation in user module

* chore: updated to error constant when invitations not found

* chore: fix rebase overwritten methods

* feat: implement remove user as admin

* chore: add new line

* chore: created new GQL return type for admin module

* chore: created resolver and service method for method to fetch org metrics

* chore: removed all entities relevant to seperate query for fetching admin metrics

* chore: created all resolvers for metrics

* feat: completed adding field resolves to query org metrics

* test: wrote tests for all metrics related methods in admin module

* test: added test cases for get count functions in multiple modules

* chore: removed prisma migration folder

* Delete backend-schema.gql

* chore: resolved merge conflicts in team test file

---------

Co-authored-by: ankitsridhar16 <ankit.sridhar16@gmail.com>

* refactor: update mailer service to stop using postmark (#38)

* refactor: update mailer service to stop using postmark

* chore: remove postmark as a dep and move out postmark code

* chore: remove postmark variables from .env.example

* chore: add formal errors for mailer initialization errors

* chore: add and update jsdoc comments in mailer service methods

* chore: added user invitation mail description to mailer service

* chore: updated with review changes requested for admin module

* feat: adding admin resolver to gql schema

* feat: adding input args for admin resolvers

* chore: invited user renamed

* chore: updated mailer service to be compatible with new mailer

* chore: updated team service with review changes

* chore: updated team collection service with review changes

* chore: updated team environments service with review changes

* chore: updated team requests service with review changes

* chore: updated user service with review changes

* refactor: invited user model

* chore: review changes implemented

* chore: implemented the review changes for admin, user and teams module

* chore: removed error handling and implemented review changes

* refactor: naming change for IsAdmin

---------

Co-authored-by: Balu Babu <balub997@gmail.com>
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
Ankit Sridhar
2023-03-21 16:42:30 +05:30
committed by GitHub
parent 8b1d8e6a90
commit f78354a377
34 changed files with 1986 additions and 35 deletions

View File

@@ -37,6 +37,17 @@ const team: Team = {
id: 'teamID',
name: 'teamName',
};
const teams: Team[] = [
{
id: 'teamID',
name: 'teamName',
},
{
id: 'teamID2',
name: 'teamName2',
},
];
const dbTeamMember: DbTeamMember = {
id: 'teamMemberID',
role: TeamMemberRole.VIEWER,
@@ -51,6 +62,8 @@ const teamMember: TeamMember = {
describe('getCountOfUsersWithRoleInTeam', () => {
test('resolves to the correct count of owners in a team', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
mockPrisma.teamMember.count.mockResolvedValue(2);
await expect(
@@ -234,6 +247,8 @@ describe('addMemberToTeamWithEmail', () => {
describe('deleteTeam', () => {
test('resolves for proper deletion', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
mockPrisma.team.findUnique.mockResolvedValue(team);
mockPrisma.teamMember.deleteMany.mockResolvedValue({
count: 10,
@@ -918,3 +933,56 @@ describe('deleteUserFromAllTeams', () => {
});
});
});
describe('fetchAllTeams', () => {
test('should resolve right and return 20 teams when cursor is null', async () => {
mockPrisma.team.findMany.mockResolvedValueOnce(teams);
const result = await teamService.fetchAllTeams(null, 20);
expect(result).toEqual(teams);
});
test('should resolve right and return next 20 teams when cursor is provided', async () => {
mockPrisma.team.findMany.mockResolvedValueOnce(teams);
const result = await teamService.fetchAllTeams('teamID', 20);
expect(result).toEqual(teams);
});
test('should resolve left and return an error when users not found', async () => {
mockPrisma.team.findMany.mockResolvedValueOnce([]);
const result = await teamService.fetchAllTeams(null, 20);
expect(result).toEqual([]);
});
});
describe('getCountOfMembersInTeam', () => {
test('should resolve right and return a total team member count ', async () => {
mockPrisma.teamMember.count.mockResolvedValueOnce(2);
const result = await teamService.getCountOfMembersInTeam(team.id);
expect(mockPrisma.teamMember.count).toHaveBeenCalledWith({
where: {
teamID: team.id,
},
});
expect(result).toEqual(2);
});
test('should resolve left and return an error when no team members found', async () => {
mockPrisma.teamMember.count.mockResolvedValueOnce(0);
const result = await teamService.getCountOfMembersInTeam(team.id);
expect(mockPrisma.teamMember.count).toHaveBeenCalledWith({
where: {
teamID: team.id,
},
});
expect(result).toEqual(0);
});
describe('getTeamsCount', () => {
test('should return count of all teams in the organization', async () => {
mockPrisma.team.count.mockResolvedValueOnce(10);
const result = await teamService.getTeamsCount();
expect(result).toEqual(10);
});
});
});

View File

@@ -4,7 +4,6 @@ import { PrismaService } from '../prisma/prisma.service';
import { TeamMember as DbTeamMember } from '@prisma/client';
import { UserService } from '../user/user.service';
import { UserDataHandler } from 'src/user/user.data.handler';
import { User } from 'src/user/user.model';
import {
TEAM_NAME_INVALID,
TEAM_ONLY_ONE_OWNER,
@@ -13,6 +12,7 @@ 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';
@@ -23,6 +23,7 @@ import * as E from 'fp-ts/Either';
import * as T from 'fp-ts/Task';
import * as A from 'fp-ts/Array';
import { throwErr } from 'src/utils';
import { AuthUser } from '../types/AuthUser';
@Injectable()
export class TeamService implements UserDataHandler, OnModuleInit {
@@ -36,7 +37,7 @@ export class TeamService implements UserDataHandler, OnModuleInit {
this.userService.registerUserDataHandler(this);
}
canAllowUserDeletion(user: User): TO.TaskOption<string> {
canAllowUserDeletion(user: AuthUser): TO.TaskOption<string> {
return pipe(
this.isUserOwnerRoleInTeams(user.uid),
TO.fromTask,
@@ -44,7 +45,7 @@ export class TeamService implements UserDataHandler, OnModuleInit {
);
}
onUserDelete(user: User): T.Task<void> {
onUserDelete(user: AuthUser): T.Task<void> {
return this.deleteUserFromAllTeams(user.uid);
}
@@ -452,6 +453,21 @@ export class TeamService implements UserDataHandler, OnModuleInit {
return this.filterMismatchedUsers(teamID, members);
}
/**
* Get a count of members in a team
* @param teamID Team ID
* @returns a count of members in a team
*/
async getCountOfMembersInTeam(teamID: string) {
const memberCount = await this.prisma.teamMember.count({
where: {
teamID: teamID,
},
});
return memberCount;
}
async getMembersOfTeam(
teamID: string,
cursor: string | null,
@@ -489,4 +505,31 @@ export class TeamService implements UserDataHandler, OnModuleInit {
return this.filterMismatchedUsers(teamID, members);
}
/**
* Fetch all the teams in the `Team` table based on cursor
* @param cursorID string of teamID or undefined
* @param take number of items to query
* @returns an array of `Team` object
*/
async fetchAllTeams(cursorID: string, take: number) {
const options = {
skip: cursorID ? 1 : 0,
take: take,
cursor: cursorID ? { id: cursorID } : undefined,
};
const fetchedTeams = await this.prisma.team.findMany(options);
return fetchedTeams;
}
/**
* Fetch list of all the Teams in the DB
*
* @returns number of teams in the org
*/
async getTeamsCount() {
const teamsCount = await this.prisma.team.count();
return teamsCount;
}
}