|
| 1 | +import mongoose from "mongoose"; |
| 2 | +import { generateUniqueId } from "@courselit/utils"; |
| 3 | +import { |
| 4 | + Constants, |
| 5 | + Course, |
| 6 | + type ProductAccessType, |
| 7 | + type Group, |
| 8 | +} from "@courselit/common-models"; |
| 9 | +import { MediaSchema } from "./media"; |
| 10 | +import { EmailSchema } from "./email"; |
| 11 | + |
| 12 | +export interface InternalCourse extends Omit<Course, "paymentPlans"> { |
| 13 | + domain: mongoose.Types.ObjectId; |
| 14 | + id: mongoose.Types.ObjectId; |
| 15 | + privacy: ProductAccessType; |
| 16 | + published: boolean; |
| 17 | + isFeatured: boolean; |
| 18 | + tags: string[]; |
| 19 | + lessons: any[]; |
| 20 | + sales: number; |
| 21 | + customers: string[]; |
| 22 | + certificate?: boolean; |
| 23 | +} |
| 24 | + |
| 25 | +export const CourseSchema = new mongoose.Schema<InternalCourse>( |
| 26 | + { |
| 27 | + domain: { type: mongoose.Schema.Types.ObjectId, required: true }, |
| 28 | + courseId: { type: String, required: true, default: generateUniqueId }, |
| 29 | + title: { type: String, required: true }, |
| 30 | + slug: { type: String, required: true }, |
| 31 | + cost: { type: Number, required: true }, |
| 32 | + costType: { |
| 33 | + type: String, |
| 34 | + required: true, |
| 35 | + enum: ["free", "email", "paid"], |
| 36 | + }, |
| 37 | + privacy: { |
| 38 | + type: String, |
| 39 | + required: true, |
| 40 | + enum: Object.values(Constants.ProductAccessType), |
| 41 | + }, |
| 42 | + type: { |
| 43 | + type: String, |
| 44 | + required: true, |
| 45 | + enum: Object.values(Constants.CourseType), |
| 46 | + }, |
| 47 | + creatorId: { type: String, required: true }, |
| 48 | + published: { type: Boolean, required: true, default: false }, |
| 49 | + tags: [{ type: String }], |
| 50 | + lessons: [String], |
| 51 | + description: String, |
| 52 | + featuredImage: MediaSchema, |
| 53 | + groups: [ |
| 54 | + { |
| 55 | + name: { type: String, required: true }, |
| 56 | + _id: { |
| 57 | + type: String, |
| 58 | + required: true, |
| 59 | + default: generateUniqueId, |
| 60 | + }, |
| 61 | + rank: { type: Number, required: true }, |
| 62 | + collapsed: { type: Boolean, required: true, default: true }, |
| 63 | + lessonsOrder: { type: [String] }, |
| 64 | + drip: new mongoose.Schema<Group["drip"]>({ |
| 65 | + type: { |
| 66 | + type: String, |
| 67 | + required: true, |
| 68 | + enum: Constants.dripType, |
| 69 | + }, |
| 70 | + status: { type: Boolean, required: true, default: false }, |
| 71 | + delayInMillis: { type: Number }, |
| 72 | + dateInUTC: { type: Number }, |
| 73 | + email: EmailSchema, |
| 74 | + }), |
| 75 | + }, |
| 76 | + ], |
| 77 | + sales: { type: Number, required: true, default: 0.0 }, |
| 78 | + customers: [String], |
| 79 | + pageId: { type: String }, |
| 80 | + // paymentPlans: [String], |
| 81 | + defaultPaymentPlan: { type: String }, |
| 82 | + leadMagnet: { type: Boolean, required: true, default: false }, |
| 83 | + certificate: Boolean, |
| 84 | + }, |
| 85 | + { |
| 86 | + timestamps: true, |
| 87 | + }, |
| 88 | +); |
| 89 | + |
| 90 | +CourseSchema.index({ |
| 91 | + title: "text", |
| 92 | +}); |
| 93 | + |
| 94 | +CourseSchema.index({ domain: 1, title: 1 }, { unique: true }); |
| 95 | + |
| 96 | +CourseSchema.statics.paginatedFind = async function ( |
| 97 | + filter, |
| 98 | + options: { |
| 99 | + page?: number; |
| 100 | + limit?: number; |
| 101 | + sort?: number; |
| 102 | + }, |
| 103 | +) { |
| 104 | + const page = options.page || 1; |
| 105 | + const limit = options.limit || 10; |
| 106 | + const sort = options.sort || -1; |
| 107 | + const skip = (page - 1) * limit; |
| 108 | + |
| 109 | + const docs = await this.find(filter) |
| 110 | + .sort({ createdAt: sort }) |
| 111 | + .lean() |
| 112 | + .skip(skip) |
| 113 | + .limit(limit) |
| 114 | + .exec(); |
| 115 | + return docs; |
| 116 | +}; |
0 commit comments