2023-10-03 01:22:43 -04:00
|
|
|
import mongoose, { Schema, PopulatedDoc, Document, Model } from "mongoose";
|
2023-09-25 19:19:32 -04:00
|
|
|
import SequenceFactory from "mongoose-sequence";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { hasMigrated } from "@dbconfig";
|
|
|
|
import { populate, populateSelected } from "@functions";
|
|
|
|
import { IUser } from "@models/user";
|
2023-09-25 19:19:32 -04:00
|
|
|
|
|
|
|
const AutoIncrement = SequenceFactory(mongoose);
|
|
|
|
|
|
|
|
export interface IReview {
|
|
|
|
_id: number;
|
|
|
|
text: string;
|
|
|
|
leftOn: number;
|
|
|
|
whichChapter: number;
|
2023-12-29 19:06:55 -05:00
|
|
|
author: PopulatedDoc<IUser>;
|
2023-09-25 19:19:32 -04:00
|
|
|
datePosted: Date;
|
2023-12-29 19:06:55 -05:00
|
|
|
replyingTo: PopulatedDoc<IReview> | null;
|
|
|
|
replies: PopulatedDoc<IReview>[];
|
2023-09-25 19:19:32 -04:00
|
|
|
}
|
|
|
|
const CommentSchema = new mongoose.Schema<IReview>({
|
|
|
|
_id: {
|
2023-10-03 01:22:43 -04:00
|
|
|
type: Number,
|
2023-09-25 19:19:32 -04:00
|
|
|
},
|
|
|
|
text: {
|
2023-10-03 01:22:43 -04:00
|
|
|
type: String,
|
2023-09-25 19:19:32 -04:00
|
|
|
},
|
|
|
|
leftOn: {
|
2023-10-03 01:22:43 -04:00
|
|
|
type: Number,
|
2023-09-25 19:19:32 -04:00
|
|
|
},
|
|
|
|
whichChapter: {
|
2023-10-03 01:22:43 -04:00
|
|
|
type: Number,
|
2023-09-25 19:19:32 -04:00
|
|
|
},
|
|
|
|
author: {
|
|
|
|
type: Number,
|
2023-10-03 01:22:43 -04:00
|
|
|
ref: "User",
|
2023-09-25 19:19:32 -04:00
|
|
|
},
|
|
|
|
datePosted: {
|
|
|
|
type: Date,
|
2023-10-03 01:22:43 -04:00
|
|
|
default: new Date(),
|
2023-09-25 19:19:32 -04:00
|
|
|
},
|
|
|
|
replyingTo: {
|
|
|
|
default: null,
|
|
|
|
type: Number,
|
2023-10-03 01:22:43 -04:00
|
|
|
ref: "Review",
|
2023-09-25 19:19:32 -04:00
|
|
|
},
|
2023-10-03 01:22:43 -04:00
|
|
|
replies: [
|
|
|
|
{
|
|
|
|
default: null,
|
|
|
|
type: Number,
|
|
|
|
ref: "Review",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2023-09-25 19:19:32 -04:00
|
|
|
|
2023-10-03 01:22:43 -04:00
|
|
|
CommentSchema.virtual("story", {
|
|
|
|
ref: "Story",
|
|
|
|
localField: "leftOn",
|
|
|
|
foreignField: "_id",
|
2023-09-25 19:19:32 -04:00
|
|
|
justOne: true,
|
|
|
|
});
|
2023-12-06 21:39:59 -05:00
|
|
|
const modelName = "Review";
|
|
|
|
CommentSchema
|
|
|
|
// .pre("findOne", populateSelected("replyingTo", modelName, "-replies -author"))
|
|
|
|
// .pre("find", populateSelected("replyingTo", modelName, "-replies -author"))
|
|
|
|
.pre("findOne", populate("replies", modelName))
|
|
|
|
.pre("find", populate("replies", modelName))
|
|
|
|
.pre(
|
|
|
|
"findOne",
|
|
|
|
populateSelected("author", "User", "profile username _id blocked"),
|
|
|
|
)
|
|
|
|
.pre(
|
|
|
|
"find",
|
|
|
|
populateSelected("author", "User", "profile username _id blocked"),
|
|
|
|
);
|
2023-09-25 19:19:32 -04:00
|
|
|
|
2023-12-29 18:05:18 -05:00
|
|
|
hasMigrated &&
|
|
|
|
!mongoose.models.Review &&
|
|
|
|
CommentSchema.plugin(AutoIncrement, { id: "reviews" });
|
2023-09-25 19:19:32 -04:00
|
|
|
|
2023-10-03 01:22:43 -04:00
|
|
|
export const Review: Model<IReview> =
|
|
|
|
/* mongoose.models.Review || */ mongoose.model(
|
2023-12-06 21:39:59 -05:00
|
|
|
modelName,
|
2023-10-03 01:22:43 -04:00
|
|
|
CommentSchema,
|
|
|
|
"reviews",
|
|
|
|
);
|