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

refactor: extract useCases from services #1447

Merged
merged 5 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import faker from '@faker-js/faker';
import Comment from 'src/modules/comments/schemas/comment.schema';
import Comment from 'src/modules/comments/entities/comment.schema';
import { buildTestFactory } from './generic-factory.mock';
import { UserFactory } from './user-factory';

Expand Down
2 changes: 1 addition & 1 deletion backend/src/modules/boards/utils/clean-board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { boardVotesIdHidden } from 'src/libs/utils/boardVotesIdHidden';
import { hideText } from 'src/libs/utils/hideText';
import CardItem from 'src/modules/cards/entities/card.item.schema';
import Card from 'src/modules/cards/entities/card.schema';
import Comment from 'src/modules/comments/schemas/comment.schema';
import Comment from 'src/modules/comments/entities/comment.schema';
import User from 'src/modules/users/entities/user.schema';
import Board from '../entities/board.schema';

Expand Down
2 changes: 1 addition & 1 deletion backend/src/modules/boards/utils/clean-boards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { UserFactory } from 'src/libs/test-utils/mocks/factories/user-factory';
import { hideText } from 'src/libs/utils/hideText';
import Card from 'src/modules/cards/entities/card.schema';
import Column from 'src/modules/columns/entities/column.schema';
import Comment from 'src/modules/comments/schemas/comment.schema';
import Comment from 'src/modules/comments/entities/comment.schema';
import User from 'src/modules/users/entities/user.schema';
import { cleanBoard, replaceUser } from './clean-board';

Expand Down
2 changes: 1 addition & 1 deletion backend/src/modules/cards/entities/card.item.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Document, ObjectId, SchemaTypes } from 'mongoose';
import * as leanVirtualsPlugin from 'mongoose-lean-virtuals';
import BaseModel from 'src/libs/models/base.model';
import User from 'src/modules/users/entities/user.schema';
import Comment, { CommentSchema } from '../../comments/schemas/comment.schema';
import Comment, { CommentSchema } from '../../comments/entities/comment.schema';

export type CardItemDocument = CardItem & Document;

Expand Down
2 changes: 1 addition & 1 deletion backend/src/modules/cards/entities/card.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Document, ObjectId, SchemaTypes } from 'mongoose';
import * as leanVirtualsPlugin from 'mongoose-lean-virtuals';
import BaseModel from 'src/libs/models/base.model';
import User from 'src/modules/users/entities/user.schema';
import Comment, { CommentSchema } from '../../comments/schemas/comment.schema';
import Comment, { CommentSchema } from '../../comments/entities/comment.schema';
import CardItem, { CardItemSchema } from './card.item.schema';

export type CardDocument = Card & Document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { UpdateResult } from 'mongodb';
import { ObjectId } from 'mongoose';
import { BaseInterfaceRepository } from 'src/libs/repositories/interfaces/base.repository.interface';
import Board from 'src/modules/boards/entities/board.schema';
import Comment from 'src/modules/comments/schemas/comment.schema';
import Comment from 'src/modules/comments/entities/comment.schema';
import User from 'src/modules/users/entities/user.schema';
import CardDto from '../dto/card.dto';
import CardItem from '../entities/card.item.schema';
Expand Down
2 changes: 1 addition & 1 deletion backend/src/modules/cards/repository/card.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Card from '../entities/card.schema';
import { CardRepositoryInterface } from './card.repository.interface';
import { UpdateResult } from 'mongodb';
import CardDto from '../dto/card.dto';
import Comment from 'src/modules/comments/schemas/comment.schema';
import Comment from 'src/modules/comments/entities/comment.schema';
import User from 'src/modules/users/entities/user.schema';

@Injectable()
Expand Down
214 changes: 214 additions & 0 deletions backend/src/modules/comments/applications/create-comment.use-case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import { Inject, Injectable } from '@nestjs/common';
import { UseCase } from 'src/libs/interfaces/use-case.interface';
import { COMMENT_REPOSITORY } from '../constants';
import { CommentRepositoryInterface } from 'src/modules/comments/interfaces/repositories/comment.repository.interface';
import { replaceComments } from 'src/modules/boards/utils/clean-board';
import { hideText } from 'src/libs/utils/hideText';
import CreateCommentDto from 'src/modules/comments/dto/create.comment.dto';
import Comment from 'src/modules/comments/entities/comment.schema';
import CreateCommentUseCaseDto from 'src/modules/comments/dto/useCase/create-comment.use-case.dto';
import User from 'src/modules/users/entities/user.schema';
import { InsertFailedException } from 'src/libs/exceptions/insertFailedBadRequestException';
import Column from 'src/modules/columns/entities/column.schema';

type HideCommentsType = {
hideCards: boolean;
user: User;
comment: Comment;
anonymous: boolean;
boardId: string;
cardId: string;
columnId: string;
isCardGroup: boolean;
socketId: string;
text: string;
completionHandler: (createCommentData: CreateCommentDto) => void;
cardItemId?: string;
};

@Injectable()
export class CreateCommentUseCase implements UseCase<CreateCommentUseCaseDto, Comment> {
constructor(
@Inject(COMMENT_REPOSITORY)
private readonly commentRepository: CommentRepositoryInterface
) {}

async execute({
boardId,
cardId,
cardItemId,
user,
text,
anonymous,
columnId,
completionHandler,
socketId
}: CreateCommentUseCaseDto): Promise<Comment> {
if (cardItemId) {
return await this.createItemComment({
boardId,
cardId,
cardItemId,
user,
text,
anonymous,
columnId,
completionHandler,
socketId
});
} else {
return await this.createCardGroupComment({
boardId,
cardId,
user,
text,
anonymous,
columnId,
completionHandler,
socketId
});
}
}

private async createItemComment({
boardId,
cardId,
cardItemId,
user,
text,
anonymous,
columnId,
completionHandler,
socketId
}: CreateCommentUseCaseDto): Promise<Comment> {
const userId = String(user._id);

const updatedBoard = await this.commentRepository.insertItemComment(
boardId,
cardId,
cardItemId,
userId,
text,
anonymous
);

if (!updatedBoard) throw new InsertFailedException();

const { colIdx, cardIdx } = this.findColumnAndCardIndex(updatedBoard.columns, columnId, cardId);

const cardItemIdx = updatedBoard.columns[colIdx].cards[cardIdx].items.findIndex(
(item) => item._id.toString() === cardItemId
);

const newComment =
updatedBoard.columns[colIdx].cards[cardIdx].items[cardItemIdx].comments[
updatedBoard.columns[colIdx].cards[cardIdx].items[cardItemIdx].comments.length - 1
];

this.replaceCommentsAndCallCompletionHandler({
hideCards: updatedBoard.hideCards,
user,
comment: newComment,
anonymous,
boardId,
cardId,
columnId,
isCardGroup: false,
socketId,
text,
completionHandler,
cardItemId
});

return newComment;
}

private async createCardGroupComment({
boardId,
cardId,
user,
text,
anonymous,
columnId,
completionHandler,
socketId
}: CreateCommentUseCaseDto) {
const userId = String(user._id);

const updatedBoard = await this.commentRepository.insertCardGroupComment(
boardId,
cardId,
userId,
text,
anonymous
);

if (!updatedBoard) throw new InsertFailedException();

const { colIdx, cardIdx } = this.findColumnAndCardIndex(updatedBoard.columns, columnId, cardId);

const newComment =
updatedBoard.columns[colIdx].cards[cardIdx].comments[
updatedBoard.columns[colIdx].cards[cardIdx].comments.length - 1
];

this.replaceCommentsAndCallCompletionHandler({
hideCards: updatedBoard.hideCards,
user,
comment: newComment,
anonymous,
boardId,
cardId,
columnId,
isCardGroup: true,
socketId,
text,
completionHandler
});

return newComment;
}

private replaceCommentsAndCallCompletionHandler({
hideCards,
user,
comment,
anonymous,
boardId,
cardId,
columnId,
isCardGroup,
socketId,
text,
completionHandler,
cardItemId
}: HideCommentsType) {
const userId = String(user._id);

const commentWithHiddenInfo = replaceComments(hideCards, user, [comment], hideText(userId));

const commentData: CreateCommentDto = {
anonymous,
boardId,
cardId,
cardItemId,
columnId,
isCardGroup,
newComment: commentWithHiddenInfo[0],
socketId,
text
};

completionHandler(commentData);
}

private findColumnAndCardIndex(columns: Column[], columnId: string, cardId: string) {
const colIdx = columns.findIndex((col) => col._id.toString() === columnId);
const cardIdx = columns[colIdx].cards.findIndex((card) => card._id.toString() === cardId);

return {
colIdx,
cardIdx
};
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Inject, Injectable } from '@nestjs/common';
import { UseCase } from 'src/libs/interfaces/use-case.interface';
import { COMMENT_REPOSITORY } from '../constants';
import { CommentRepositoryInterface } from 'src/modules/comments/interfaces/repositories/comment.repository.interface';
import Board from 'src/modules/boards/entities/board.schema';
import DeleteCommentUseCaseDto from 'src/modules/comments/dto/useCase/delete-comment.use-case.dto';
import { DeleteFailedException } from 'src/libs/exceptions/deleteFailedBadRequestException';

@Injectable()
export class DeleteCommentUseCase implements UseCase<DeleteCommentUseCaseDto, void> {
constructor(
@Inject(COMMENT_REPOSITORY)
private readonly commentRepository: CommentRepositoryInterface
) {}

async execute(commentData: DeleteCommentUseCaseDto) {
const { boardId, commentId, userId, isCardGroup, completionHandler } = commentData;
let board;

if (isCardGroup) {
board = await this.deleteCardGroupComment(boardId, commentId, userId);
} else {
board = await this.deleteItemComment(boardId, commentId, userId);
}

if (!board) {
throw new DeleteFailedException();
}

completionHandler();

return;
}

deleteItemComment(boardId: string, commentId: string, userId: string): Promise<Board> {
return this.commentRepository.deleteItemComment(boardId, commentId, userId);
}

deleteCardGroupComment(boardId: string, commentId: string, userId: string): Promise<Board> {
return this.commentRepository.deleteCardGroupComment(boardId, commentId, userId);
}
}

This file was deleted.

Loading