Skip to content

Commit

Permalink
feat: trade post comment transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Seung-o committed Jan 21, 2024
1 parent dfe9587 commit 3e26bae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/trade-posts/responses/trade-post-comment.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ export class TradePostCommentResponse extends BaseResponse implements ITradePost
@Field((_) => UserResponse, { description: 'The author of the trade post comment' })
author: UserResponse;

postId: string;

constructor(partial: Partial<TradePostCommentDocument>) {
super(partial);
Object.assign(this, {
authorId: partial?.authorId,
content: partial?.content,
postId: partial?.postId,
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/trade-posts/schemas/trade-post-comment.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export type TradePostCommentDocument = HydratedDocument<TradePostComment>;
export class TradePostComment {
id: string;

@Prop({ required: true, index: true })
postId: string;

@Prop({ required: true, index: true })
content: string;

Expand Down
34 changes: 19 additions & 15 deletions src/trade-posts/services/trade-post-comment.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, PaginateModel } from 'mongoose';
import { TradePostNotFound } from '../exceptions/trade-post.exception';
import {
CreateTradePostCommentInput,
GetTradePostCommentListInput,
Expand All @@ -24,7 +23,7 @@ export class TradePostCommentService {
const session = await this.tradePostCommentModel.startSession();

const result = await session.withTransaction(async () => {
const comment = await this.tradePostCommentModel.create({ ...input, authorId });
const comment = await this.tradePostCommentModel.create({ ...input, authorId, postId: tradePostId });
await this.tradePostModel.findByIdAndUpdate(tradePostId, { $push: { comments: comment.id } });
return new TradePostCommentResponse(comment);
});
Expand All @@ -34,29 +33,34 @@ export class TradePostCommentService {
}

async getTradePostCommentList({ tradePostId, pagination }: GetTradePostCommentListInput) {
const tradePost = await this.tradePostModel.findById(tradePostId);

if (!tradePost) {
throw new TradePostNotFound(tradePostId);
}

const comments = await this.tradePostCommentModel.paginate({ _id: tradePost.comments }, pagination.options);
const paginatedResult = await this.tradePostCommentModel.paginate({ postId: tradePostId }, pagination.options);

return new TradePostCommentList({
...comments,
docs: comments.docs.map((doc) => new TradePostCommentResponse(doc)),
...paginatedResult,
docs: paginatedResult.docs.map((doc) => new TradePostCommentResponse(doc)),
});
}

async deleteTradePostComment(tradePostCommentId: string) {
const tradePostComment = await this.getTradePostComment(tradePostCommentId);
await this.tradePostCommentModel.findByIdAndDelete(tradePostCommentId);
return tradePostComment;
if (!tradePostComment) return null;

const session = await this.tradePostCommentModel.startSession();
const result = await session.withTransaction(async () => {
await this.tradePostCommentModel.findByIdAndDelete(tradePostCommentId);
await this.tradePostModel.findOneAndUpdate(
{ _id: tradePostComment.postId },
{ $pull: { comments: tradePostCommentId } },
);
return tradePostComment;
});

return result;
}

async deleteTradePostComments(tradePostCommentIds: string[] | TradePostComment[]) {
async deleteTradePostComments(tradePostId: string) {
try {
await this.tradePostCommentModel.deleteMany({ _id: tradePostCommentIds });
await this.tradePostCommentModel.deleteMany({ postId: tradePostId });
} catch (err) {
this.logger.error('Fail to delete trade post comments', err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/trade-posts/trade-post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class TradePostService implements ITradePostService {
const session = await this.tradePostModel.startSession();
await session.withTransaction(async () => {
await this.bookmarkService.deleteBookmarks(tradePostId);
this.tradePostCommentService.deleteTradePostComments(tradePost.comments);
this.tradePostCommentService.deleteTradePostComments(tradePost.id);
this.tradePostStorageService.deleteImage(tradePostId);
await this.tradePostModel.findByIdAndDelete(tradePostId);
});
Expand Down

0 comments on commit 3e26bae

Please sign in to comment.