AcadeDoc/apps/server/src/core/comment/comment.service.ts
Philipinho c18c9ae02b Refactoring
* replace TypeORM with Kysely query builder
* refactor migrations
* other changes and fixes
2024-03-29 01:46:11 +00:00

97 lines
2.9 KiB
TypeScript

import { BadRequestException, Injectable } from '@nestjs/common';
import { CreateCommentDto } from './dto/create-comment.dto';
import { UpdateCommentDto } from './dto/update-comment.dto';
import { PageService } from '../page/services/page.service';
import { CommentRepo } from '@docmost/db/repos/comment/comment.repo';
import { Comment } from '@docmost/db/types/entity.types';
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
import { PaginatedResult } from 'src/helpers/pagination/paginated-result';
import { PaginationMetaDto } from 'src/helpers/pagination/pagination-meta-dto';
@Injectable()
export class CommentService {
constructor(
private commentRepo: CommentRepo,
private pageService: PageService,
) {}
async findWithCreator(commentId: string) {
// todo: find comment with creator object
}
async create(
userId: string,
workspaceId: string,
createCommentDto: CreateCommentDto,
) {
const commentContent = JSON.parse(createCommentDto.content);
const page = await this.pageService.findById(createCommentDto.pageId);
// const spaceId = null; // todo, get from page
if (!page) {
throw new BadRequestException('Page not found');
}
if (createCommentDto.parentCommentId) {
const parentComment = await this.commentRepo.findById(
createCommentDto.parentCommentId,
);
if (!parentComment) {
throw new BadRequestException('Parent comment not found');
}
if (parentComment.parentCommentId !== null) {
throw new BadRequestException('You cannot reply to a reply');
}
}
const createdComment = await this.commentRepo.insertComment({
pageId: createCommentDto.pageId,
content: commentContent,
selection: createCommentDto?.selection.substring(0, 250),
type: 'inline', // for now
parentCommentId: createCommentDto?.parentCommentId,
creatorId: userId,
workspaceId: workspaceId,
});
// todo return created comment and creator relation
return createdComment;
}
async findByPageId(
pageId: string,
paginationOptions: PaginationOptions,
): Promise<PaginatedResult<Comment>> {
const { comments, count } = await this.commentRepo.findPageComments(
pageId,
paginationOptions,
);
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
return new PaginatedResult(comments, paginationMeta);
}
async update(
commentId: string,
updateCommentDto: UpdateCommentDto,
): Promise<Comment> {
const commentContent = JSON.parse(updateCommentDto.content);
await this.commentRepo.updateComment(
{
content: commentContent,
editedAt: new Date(),
},
commentId,
);
return this.commentRepo.findById(commentId);
}
async remove(id: string): Promise<void> {
await this.commentRepo.deleteComment(id);
}
}