Skip to content

♻️ Remove BoardsService.updateByUser & BoardsService.deleteByUser method #10

@SebastienCozeDev

Description

@SebastienCozeDev

BoardsService.updateByUser & BoardsService.deleteByUser method aren't necessary :

    async updateByUser(userId: string, updateBoardDto: UpdateBoardDto): Promise<BoardResponseDto> {
        const user = await this.usersService.findById(userId);
        const board = await this.findById(updateBoardDto.id);
        if (!this.hasRightToUpdate(board, userId))
            throw new ForbiddenException("You can only update your boards");
        return this.update(userId, updateBoardDto, user, board);
    }

    async update(userId: string, updateBoardDto: UpdateBoardDto, user?: User, board?: Board): Promise<BoardResponseDto> {
        if (!user)
            user = await this.usersService.findById(userId);
        if (!board)
            board = await this.findById(updateBoardDto.id);
        if (updateBoardDto.ownerId && !updateBoardDto.password)
            throw new ForbiddenException("You can't transfert this board without password");
        if (updateBoardDto.password && !this.usersService.checkPassword(user, updateBoardDto.password))
            throw new ForbiddenException("Invalid password");
        if (updateBoardDto.ownerId) // TODO: Remove ownerId everywhere to replace by role
            board.ownerId = updateBoardDto.ownerId;
        if (updateBoardDto.title)
            board.title = updateBoardDto.title;
        if (updateBoardDto.description)
            board.description = updateBoardDto.description;
        const updatedBoard = await this.boardsRepository.save(board);
        return this.toResponseDto(updatedBoard);
    }

    async deleteByUser(userId: string, deleteBoardDto: DeleteBoardDto): Promise<BoardDeletionResponseDto> {
        const user = await this.usersService.findById(userId);
        const board = await this.findById(deleteBoardDto.id);
        if (this.hasRightToDelete(board, userId))
            throw new ForbiddenException("You can only delete your boards");
        return this.delete(userId, deleteBoardDto, user, board);
    }

    async delete(userId: string, deleteBoardDto: DeleteBoardDto, user?: User, board?: Board): Promise<DeletionResponseDto> {
        if (!user)
            user = await this.usersService.findById(userId);
        if (!board)
            board = await this.findById(deleteBoardDto.id);
        if (!this.usersService.checkPassword(user, deleteBoardDto.password))
            throw new ForbiddenException("Invalid password");
        if (!deleteBoardDto.permanently)
            await this.boardsRepository.softDelete(board.id);
        else
            await this.boardsRepository.delete(board.id);
        return new DeletionResponseDto({
            message: `"${board.title}" (ID: ${board.id}) has been successfully deleted ${deleteBoardDto.permanently ? 'with' : 'without'} permanently method`,
        });
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions