Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add delete team endpoint #633

Merged
merged 11 commits into from
Nov 28, 2022
16 changes: 16 additions & 0 deletions backend/src/modules/teams/applications/delete.team.application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Inject, Injectable } from '@nestjs/common';

import { DeleteTeamApplicationInterface } from '../interfaces/applications/delete.team.application.interface';
import { TYPES } from '../interfaces/types';

@Injectable()
export class DeleteTeamApplication implements DeleteTeamApplicationInterface {
constructor(
@Inject(TYPES.services.DeleteTeamService)
private deleteTeamServices: DeleteTeamApplicationInterface
) {}

delete(teamId: string) {
return this.deleteTeamServices.delete(teamId);
}
}
38 changes: 36 additions & 2 deletions backend/src/modules/teams/controller/team.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BadRequestException,
Body,
Controller,
Delete,
Get,
Inject,
Param,
Expand Down Expand Up @@ -32,7 +33,7 @@ import {
import { TeamParams } from 'src/libs/dto/param/team.params';
import { TeamQueryParams } from 'src/libs/dto/param/team.query.params';
import { TeamRoles } from 'src/libs/enum/team.roles';
import { INSERT_FAILED, UPDATE_FAILED } from 'src/libs/exceptions/messages';
import { DELETE_FAILED, INSERT_FAILED, UPDATE_FAILED } from 'src/libs/exceptions/messages';
import JwtAuthenticationGuard from 'src/libs/guards/jwtAuth.guard';
import RequestWithUser from 'src/libs/interfaces/requestWithUser.interface';
import { BadRequestResponse } from 'src/libs/swagger/errors/bad-request.swagger';
Expand All @@ -41,6 +42,7 @@ import { UnauthorizedResponse } from 'src/libs/swagger/errors/unauthorized.swagg
import { TeamUserGuard } from '../../../libs/guards/teamRoles.guard';
import { ForbiddenResponse } from '../../../libs/swagger/errors/forbidden.swagger';
import { NotFoundResponse } from '../../../libs/swagger/errors/not-found.swagger';
import { DeleteTeamApplication } from '../applications/delete.team.application';
import { UpdateTeamApplication } from '../applications/update.team.application';
import { CreateTeamDto } from '../dto/crate-team.dto';
import TeamDto from '../dto/team.dto';
Expand All @@ -62,7 +64,9 @@ export default class TeamsController {
@Inject(TYPES.applications.GetTeamApplication)
private getTeamApp: GetTeamApplicationInterface,
@Inject(TYPES.applications.UpdateTeamApplication)
private updateTeamApp: UpdateTeamApplication
private updateTeamApp: UpdateTeamApplication,
@Inject(TYPES.applications.DeleteTeamApplication)
private deleteTeamApp: DeleteTeamApplication
) {}

@ApiOperation({ summary: 'Create a new team' })
Expand Down Expand Up @@ -220,4 +224,34 @@ export default class TeamsController {

return teamUser;
}

@ApiOperation({ summary: 'Delete a specific team' })
@ApiParam({ type: String, name: 'teamId', required: true })
@ApiOkResponse({ type: Boolean, description: 'Team successfully deleted!' })
@ApiBadRequestResponse({
description: 'Bad Request',
type: BadRequestResponse
})
@ApiUnauthorizedResponse({
description: 'Unauthorized',
type: UnauthorizedResponse
})
@ApiInternalServerErrorResponse({
description: 'Internal Server Error',
type: InternalServerErrorResponse
})
@ApiForbiddenResponse({
description: 'Forbidden',
type: ForbiddenResponse
})
@TeamUser(TeamRoles.ADMIN)
@UseGuards(TeamUserGuard)
@Delete(':teamId')
async deleteTeam(@Param() { teamId }: TeamParams) {
patricia-mdias marked this conversation as resolved.
Show resolved Hide resolved
const result = await this.deleteTeamApp.delete(teamId);

if (!result) throw new BadRequestException(DELETE_FAILED);

return result;
patricia-mdias marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface DeleteTeamApplicationInterface {
delete(teamId: string): Promise<boolean>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface DeleteTeamServiceInterface {
// delete doesn't return an object
delete(teamId: string, userId: string): Promise<boolean>;
}
8 changes: 4 additions & 4 deletions backend/src/modules/teams/interfaces/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ export const TYPES = {
services: {
CreateTeamService: 'CreateTeamService',
GetTeamService: 'GetTeamService',
UpdateTeamService: 'UpdateTeamService'
// DeleteTeamService: 'DeleteTeamService',
UpdateTeamService: 'UpdateTeamService',
DeleteTeamService: 'DeleteTeamService'
},
applications: {
CreateTeamApplication: 'CreateTeamApplication',
GetTeamApplication: 'GetTeamApplication',
UpdateTeamApplication: 'UpdateTeamApplication'
// DeleteBoardApplication: 'DeleteBoardApplication',
UpdateTeamApplication: 'UpdateTeamApplication',
DeleteTeamApplication: 'DeleteTeamApplication'
// UpdateBoardApplication: 'UpdateBoardApplication',
}
};
12 changes: 12 additions & 0 deletions backend/src/modules/teams/providers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { CreateTeamApplication } from './applications/create.team.application';
import { DeleteTeamApplication } from './applications/delete.team.application';
import { GetTeamApplication } from './applications/get.team.application';
import { UpdateTeamApplication } from './applications/update.team.application';
import { TYPES } from './interfaces/types';
import CreateTeamService from './services/create.team.service';
import DeleteTeamService from './services/delete.team.service';
import GetTeamService from './services/get.team.service';
import UpdateTeamService from './services/update.team.service';

Expand Down Expand Up @@ -35,3 +37,13 @@ export const updateTeamApplication = {
provide: TYPES.applications.UpdateTeamApplication,
useClass: UpdateTeamApplication
};

export const deleteTeamService = {
provide: TYPES.services.DeleteTeamService,
useClass: DeleteTeamService
};

export const deleteTeamApplication = {
provide: TYPES.applications.DeleteTeamApplication,
useClass: DeleteTeamApplication
};
41 changes: 41 additions & 0 deletions backend/src/modules/teams/services/delete.team.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { ClientSession, Model } from 'mongoose';
import { DELETE_FAILED } from 'src/libs/exceptions/messages';
import { DeleteTeamServiceInterface } from '../interfaces/services/delete.team.service.interface';
import Team, { TeamDocument } from '../schemas/teams.schema';

@Injectable()
export default class DeleteTeamService implements DeleteTeamServiceInterface {
constructor(@InjectModel(Team.name) private teamModel: Model<TeamDocument>) {}

async delete(teamId: string): Promise<boolean> {
const teamSession = await this.teamModel.db.startSession();
teamSession.startTransaction();
try {
await this.deleteTeam(teamId, teamSession);
await teamSession.commitTransaction();

return true;
} catch (e) {
await teamSession.abortTransaction();
} finally {
await teamSession.endSession();
}

return false;
}

private async deleteTeam(teamId: string, teamSession: ClientSession) {
const result = await this.teamModel.findOneAndRemove(
{
_id: teamId
},
{ session: teamSession }
);

if (!result) throw new NotFoundException(DELETE_FAILED);

return { _id: result._id };
}
patricia-mdias marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 5 additions & 1 deletion backend/src/modules/teams/teams.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import TeamsController from './controller/team.controller';
import {
createTeamApplication,
createTeamService,
deleteTeamApplication,
deleteTeamService,
getTeamApplication,
getTeamService,
updateTeamApplication,
Expand All @@ -22,7 +24,9 @@ import {
getTeamService,
getTeamApplication,
updateTeamService,
updateTeamApplication
updateTeamApplication,
deleteTeamApplication,
deleteTeamService
],
controllers: [TeamsController],
exports: [getTeamApplication, getTeamService, createTeamService, updateTeamService]
Expand Down