|
| 1 | +import { generateUniqueId } from "@courselit/utils"; |
| 2 | +import mongoose from "mongoose"; |
| 3 | +import { CommunityMediaSchema } from "./community-media"; |
| 4 | +import { |
| 5 | + CommunityComment, |
| 6 | + CommunityCommentReply, |
| 7 | +} from "@courselit/common-models"; |
| 8 | + |
| 9 | +export interface InternalCommunityComment |
| 10 | + extends Pick< |
| 11 | + CommunityComment, |
| 12 | + "communityId" | "postId" | "commentId" | "content" | "media" |
| 13 | + > { |
| 14 | + domain: mongoose.Types.ObjectId; |
| 15 | + userId: string; |
| 16 | + likes: string[]; |
| 17 | + replies: InternalReply[]; |
| 18 | + deleted: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +export interface InternalReply |
| 22 | + extends Omit<CommunityCommentReply, "likesCount" | "hasLiked"> { |
| 23 | + userId: string; |
| 24 | + likes: string[]; |
| 25 | +} |
| 26 | + |
| 27 | +export const ReplySchema = new mongoose.Schema( |
| 28 | + { |
| 29 | + userId: { type: String, required: true }, |
| 30 | + content: { type: String, required: true }, |
| 31 | + media: [CommunityMediaSchema], |
| 32 | + replyId: { type: String, required: true, default: generateUniqueId }, |
| 33 | + parentReplyId: { type: String, default: null }, |
| 34 | + likes: [String], |
| 35 | + deleted: { type: Boolean, default: false }, |
| 36 | + }, |
| 37 | + { |
| 38 | + timestamps: true, |
| 39 | + }, |
| 40 | +); |
| 41 | + |
| 42 | +export const CommunityCommentSchema = |
| 43 | + new mongoose.Schema<InternalCommunityComment>( |
| 44 | + { |
| 45 | + domain: { type: mongoose.Schema.Types.ObjectId, required: true }, |
| 46 | + userId: { type: String, required: true }, |
| 47 | + communityId: { type: String, required: true }, |
| 48 | + postId: { type: String, required: true }, |
| 49 | + commentId: { |
| 50 | + type: String, |
| 51 | + required: true, |
| 52 | + unique: true, |
| 53 | + default: generateUniqueId, |
| 54 | + }, |
| 55 | + content: { type: String, required: true }, |
| 56 | + media: [CommunityMediaSchema], |
| 57 | + likes: [String], |
| 58 | + replies: [ReplySchema], |
| 59 | + deleted: { type: Boolean, required: true, default: false }, |
| 60 | + }, |
| 61 | + { |
| 62 | + timestamps: true, |
| 63 | + }, |
| 64 | + ); |
| 65 | + |
| 66 | +CommunityCommentSchema.statics.paginatedFind = async function ( |
| 67 | + filter, |
| 68 | + options, |
| 69 | +) { |
| 70 | + const page = options.page || 1; |
| 71 | + const limit = options.limit || 10; |
| 72 | + const skip = (page - 1) * limit; |
| 73 | + |
| 74 | + const docs = await this.find(filter).skip(skip).limit(limit).exec(); |
| 75 | + return docs; |
| 76 | +}; |
0 commit comments