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";
|
|
|
|
import { hasMigrated } from "~/lib/dbconfig";
|
|
|
|
import { populate, populateSelected } from "~/lib/functions";
|
|
|
|
import { IUser } from "~/models/user";
|
|
|
|
|
|
|
|
const AutoIncrement = SequenceFactory(mongoose);
|
|
|
|
|
|
|
|
export interface IReview {
|
|
|
|
_id: number;
|
|
|
|
text: string;
|
|
|
|
leftOn: number;
|
|
|
|
whichChapter: number;
|
|
|
|
author: PopulatedDoc<IUser & Document>;
|
|
|
|
datePosted: Date;
|
|
|
|
replyingTo: PopulatedDoc<IReview & Document> | null;
|
2023-10-03 01:22:43 -04:00
|
|
|
replies: PopulatedDoc<IReview & Document>[];
|
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-10-03 01:22:43 -04:00
|
|
|
CommentSchema.pre("findOne", populate("replies"))
|
|
|
|
.pre("find", populate("replies"))
|
2023-09-25 19:19:32 -04:00
|
|
|
.pre("findOne", populate("commentAuthor"))
|
|
|
|
.pre("find", populate("commentAuthor"))
|
|
|
|
.pre("findOne", populateSelected("replyingTo", "-replies"))
|
|
|
|
.pre("find", populateSelected("replyingTo", "-replies"));
|
|
|
|
|
2023-10-12 22:28:37 -04: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(
|
|
|
|
"Review",
|
|
|
|
CommentSchema,
|
|
|
|
"reviews",
|
|
|
|
);
|